Weekly Contest 113
949. Largest Time for Given Digits (string::compare)
Given an array of 4 digits, return the largest 24 hour time that can be made.
The smallest 24 hour time is 00:00, and the largest is 23:59. Starting from 00:00, a time is larger if more time has elapsed since midnight.
Return the answer as a string of length 5. If no valid time can be made, return an empty string.
Example 1:
Input: [1,2,3,4]
Output: "23:41"
Example 2:
Input: [5,5,5,5]
Output: ""
Note:
A.length == 4
0 <= A[i] <= 9
class Solution {
public:
string largestTimeFromDigits(vector<int>& A) {
check(A[0], A[1], A[2], A[3]);
check(A[0], A[2], A[1], A[3]);
check(A[0], A[3], A[1], A[2]);
check(A[1], A[2], A[0], A[3]);
check(A[1], A[3], A[0], A[2]);
check(A[2], A[3], A[0], A[1]);
return ans;
} void check(int h1, int h2, int m1, int m2) {
string hour = best(h1, h2, 24);
string minute = best(m1, m2, 60);
if (hour == "" || minute == "") return ; string cand = hour + ":" + minute;
if (ans.compare(cand) < 0) ans = cand;
} string best(int d1, int d2, int limit) {
int ans = max(d1*10 + d2 < limit ? d1*10 + d2 : -1,
d2*10 + d1 < limit ? d2*10 + d1 : -1);
string res = "";
if (ans < 0) return res;
else {
if (ans < 10) {
res += "0";
res += to_string(ans);
} else {
res += to_string(ans);
}
}
return res;
} private:
string ans = ""; };
In this problem, we can use difference functions to solve the sub questions, At the first time I try to use if statement to solve difference case, finally, I failed. It's too complicate to deal with all cases.
And In C we can use strcmp to compare two string (char* str[]), but in C++ we have to use string::compare. if str1.compare(str2) < 0, it represent str1 isn't match with str2, and lower in the compare string.
951. Flip Equivalent Binary Trees
For a binary tree T, we can define a flip operation as follows: choose any node, and swap the left and right child subtrees.
A binary tree X is flip equivalent to a binary tree Y if and only if we can make X equal to Y after some number of flip operations.
Write a function that determines whether two binary trees are flip equivalent. The trees are given by root nodes root1
and root2
.
Example 1:
Input: root1 = [1,2,3,4,5,6,null,null,null,7,8], root2 = [1,3,2,null,6,4,5,null,null,null,null,8,7]
Output: true
Explanation: We flipped at nodes with values 1, 3, and 5.
Note:
- Each tree will have at most
100
nodes. - Each value in each tree will be a unique integer in the range
[0, 99]
.
/**
* Definition for a binary tree node.
* struct TreeNode {
* int val;
* TreeNode *left;
* TreeNode *right;
* TreeNode(int x) : val(x), left(NULL), right(NULL) {}
* };
*/
class Solution {
public:
bool flipEquiv(TreeNode* root1, TreeNode* root2) {
if (root1 == root2)
return true;
if (root1 == nullptr || root2 == nullptr || root1->val != root2->val)
return false;
return (flipEquiv(root1->left, root2->left) && flipEquiv(root1->right, root2->right) ||
flipEquiv(root1->left, root2->right) && flipEquiv(root1->right, root2->left));
}
};
950. Reveal Cards In Increasing Order
In a deck of cards, every card has a unique integer. You can order the deck in any order you want.
Initially, all the cards start face down (unrevealed) in one deck.
Now, you do the following steps repeatedly, until all cards are revealed:
- Take the top card of the deck, reveal it, and take it out of the deck.
- If there are still cards in the deck, put the next top card of the deck at the bottom of the deck.
- If there are still unrevealed cards, go back to step 1. Otherwise, stop.
Return an ordering of the deck that would reveal the cards in increasing order.
The first entry in the answer is considered to be the top of the deck.
Example 1:
Input: [17,13,11,2,3,5,7]
Output: [2,13,3,11,5,17,7]
Explanation:
We get the deck in the order [17,13,11,2,3,5,7] (this order doesn't matter), and reorder it.
After reordering, the deck starts as [2,13,3,11,5,17,7], where 2 is the top of the deck.
We reveal 2, and move 13 to the bottom. The deck is now [3,11,5,17,7,13].
We reveal 3, and move 11 to the bottom. The deck is now [5,17,7,13,11].
We reveal 5, and move 17 to the bottom. The deck is now [7,13,11,17].
We reveal 7, and move 13 to the bottom. The deck is now [11,17,13].
We reveal 11, and move 17 to the bottom. The deck is now [13,17].
We reveal 13, and move 17 to the bottom. The deck is now [17].
We reveal 17.
Since all the cards revealed are in increasing order, the answer is correct.
Note:
1 <= A.length <= 1000
1 <= A[i] <= 10^6
A[i] != A[j]
for alli != j
class Solution {
public:
vector<int> deckRevealedIncreasing(vector<int>& deck) {
int N = deck.size();
queue<int> q;
for (int i = 0; i < N; ++i) {
q.push(i);
}
vector<int> ans(N);
sort(deck.begin(), deck.end());
for (int card : deck) {
ans[q.front()] = card;
if (!q.empty()) {
q.pop();
q.push(q.front());
q.pop();
}
}
return ans;
}
};
It's very clever to use a queue to simulation the process.
952. Largest Component Size by Common Factor
Given a non-empty array of unique positive integers A
, consider the following graph:
- There are
A.length
nodes, labelledA[0]
toA[A.length - 1];
- There is an edge between
A[i]
andA[j]
if and only ifA[i]
andA[j]
share a common factor greater than 1.
Return the size of the largest connected component in the graph.
Example 1:
Input: [4,6,15,35]
Output: 4
Example 2:
Input: [20,50,9,63]
Output: 2
Example 3:
Input: [2,3,6,7,4,12,21,39]
Output: 8
Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000
class Solution {
public int largestComponentSize(int[] A) {
int N = A.length; ArrayList<Integer>[] factored = new ArrayList[N];
for (int i = 0; i < N; ++i) {
factored[i] = new ArrayList<Integer>();
int d = 2, x = A[i];
while (d * d <= x) {
if (x % d == 0) {
while (x % d == 0)
x /= d;
factored[i].add(d);
}
d++;
}
if (x > 1 || factored[i].isEmpty())
factored[i].add(x);
} Set<Integer> primes = new HashSet();
for (List<Integer> facs : factored)
for (int x : facs)
primes.add(x); int[] primesL = new int[primes.size()];
int t = 0;
for (int x : primes)
primesL[t++] = x; Map<Integer, Integer> primeToIndex = new HashMap();
for (int i = 0; i < primesL.length; ++i) {
primeToIndex.put(primesL[i], i);
} DSU dsu = new DSU(primesL.length);
for (List<Integer> facs : factored)
for (int x : facs)
dsu.union(primeToIndex.get(facs.get(0)), primeToIndex.get(x)); int[] count = new int[primesL.length];
for (List<Integer> facs : factored)
count[dsu.find(primeToIndex.get(facs.get(0)))]++; int ans = 0;
for (int x : count)
if (x > ans)
ans = x;
return ans;
} } class DSU {
int[] parent;
public DSU(int N) {
parent = new int[N];
for (int i = 0; i < N; ++i) {
parent[i] = i;
}
} public int find(int x) {
if (parent[x] != x)
parent[x] = find(parent[x]);
return parent[x];
} public void union(int x, int y) {
parent[find(x)] = find(y);
}
}
To be honset, I can't understand it.
Weekly Contest 113的更多相关文章
- LeetCode Weekly Contest 8
LeetCode Weekly Contest 8 415. Add Strings User Accepted: 765 User Tried: 822 Total Accepted: 789 To ...
- Leetcode Weekly Contest 86
Weekly Contest 86 A:840. 矩阵中的幻方 3 x 3 的幻方是一个填充有从 1 到 9 的不同数字的 3 x 3 矩阵,其中每行,每列以及两条对角线上的各数之和都相等. 给定一个 ...
- leetcode weekly contest 43
leetcode weekly contest 43 leetcode649. Dota2 Senate leetcode649.Dota2 Senate 思路: 模拟规则round by round ...
- LeetCode Weekly Contest 23
LeetCode Weekly Contest 23 1. Reverse String II Given a string and an integer k, you need to reverse ...
- LeetCode之Weekly Contest 91
第一题:柠檬水找零 问题: 在柠檬水摊上,每一杯柠檬水的售价为 5 美元. 顾客排队购买你的产品,(按账单 bills 支付的顺序)一次购买一杯. 每位顾客只买一杯柠檬水,然后向你付 5 美元.10 ...
- LeetCode Weekly Contest
链接:https://leetcode.com/contest/leetcode-weekly-contest-33/ A.Longest Harmonious Subsequence 思路:hash ...
- LeetCode Weekly Contest 47
闲着无聊参加了这个比赛,我刚加入战场的时候时间已经过了三分多钟,这个时候已经有20多个大佬做出了4分题,我一脸懵逼地打开第一道题 665. Non-decreasing Array My Submis ...
- 75th LeetCode Weekly Contest Champagne Tower
We stack glasses in a pyramid, where the first row has 1 glass, the second row has 2 glasses, and so ...
- LeetCode之Weekly Contest 102
第一题:905. 按奇偶校验排序数组 问题: 给定一个非负整数数组 A,返回一个由 A 的所有偶数元素组成的数组,后面跟 A 的所有奇数元素. 你可以返回满足此条件的任何数组作为答案. 示例: 输入: ...
随机推荐
- Cannot run program “git.exe”: createprocess error=2,系统找不到指定的文件
Android Studio提供VCS(Version Control System)版本控制系统,默认情况使用Git.GitHub工具需要配置git.exe路径,否则提示“cannot run pr ...
- before-request , after-request
1 . flask的中间件 1)@app.before_request # 请求进入视图函数之前,类似于django中间件的request_process 2)@app.after_reque ...
- LibreOJ 数列分块入门
题目链接:https://loj.ac/problem/6277 题目描述 给出一个长为 nnn 的数列,以及 nnn 个操作,操作涉及区间加法,单点查值. 输入格式 第一行输入一个数字 nnn. 第 ...
- Spring Boot2.0之@Async实现异步调用
补充一个知识点: lombok底层原理使用的是: 字节码技术ASM修改字节码文件,生成比如类似于get() set( )方法 一定要在开发工具安装 在编译时候修改字节码文件(底层使用字节码技术),线上 ...
- ASP.NET 4.0 页面 ValidateRequest="false" 失效不起作用
当ASP.NET 2.0升级到 ASP.NET 4.0后,页面的 ValidateRequest="false" 不起作用. 因为 ASP.NET 4.0 请求验证被提前到IHtt ...
- Unix环境编程之文件IO
1.文件IO 2.文件与目录 3.进程 4.多线程编程 5.信号 6.进程间通信 学习linux编程,首先要学会使用shell,这里一些基础命令就不介绍了.这里唯一要提的一个shell命令就是man. ...
- CodeForces -163E :e-Government (AC自动机+DFS序+树状数组)
The best programmers of Embezzland compete to develop a part of the project called "e-Governmen ...
- java面试题06
题目: 数据库 1. 表名:g_cardapply 字段(字段名/类型/长度): g_applyno varchar 8://申请单号(关键字) g_applydate bigint 8://申请日期 ...
- Scrapy,终端startproject,显示错误TimeoutError: [WinError 10060] 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
F:\python_project\test>scrapy startproject spz Traceback (most recent call last): File "d:\p ...
- MySQL_杭州拱墅区、西湖区近9-11月销售过的产品_20161125
需求:杭州拱墅区.西湖区近9-11月销售过的产品 这个SQL如果对数据表熟悉的话可以一步到位,为了看的更清楚,时间间隔在3个月产品数量也不是很多,采取先找明细再进行汇总. 一.第一种写法 分步骤的写S ...