Largest Time for Given Digits
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
题意:给出4个数字,输出所有排列中构成的合法的最大的时间。
思路:枚举所有情况,判断是否合法,记录最大值。
char* largestTimeFromDigits(int* A, int ASize) {
int i,j,k,p;
int hh,mm,maxhh,maxmm;
maxhh = -1,maxmm = -1;
for(i = 0; i < ASize; i++) {
for(j = 0; j < ASize; j++) {
if(j == i) continue;
for(k = 0; k < ASize; k++) {
if(k == j || k == i) continue;
for(p = 0; p < ASize; p++) {
if(p == i || p == j || p == k) continue;
hh = A[i] * 10 + A[j];
mm = A[k] * 10 + A[p];
if(hh >= 0 && hh <= 23 && mm >= 0 && mm <= 59) {
if(hh > maxhh) {
maxhh = hh;
maxmm = mm;
}
else if(hh == maxhh && mm > maxmm) {
maxmm = mm;
}
}
}
}
}
}
//printf("%d %d\n",maxhh,maxmm);
if(maxhh == -1) return "";
else {
char* ans = (char*)malloc(sizeof(char) * 5);
if(maxhh >= 10) {
ans[0] = '0' + maxhh / 10;
ans[1] = '0' + maxhh % 10;
}
else {
ans[0] = '0';
ans[1] = '0' + maxhh % 10;
}
ans[2] = ':';
if(maxmm >= 10) {
ans[3] = '0' + maxmm / 10;
ans[4] = '0' + maxmm % 10;
}
else {
ans[3] = '0';
ans[4] = '0' + maxmm % 10;
}
return ans;
}
}
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
题意:给出n个数,构造个排列,按照题目要求的操作,让重新生成的这个排列是递增的。
思路:逆向思维,倒着想,因为最后出来的那个数肯定是最大的,我们在构造新排列时先让最大的数加进去,然后每次进行题目要求操作的逆操作,即把最后一个数放到最前面,然后在新加入一个数放在最前面。重复此操作n-1次。
class Solution {
public:
vector<int> deckRevealedIncreasing(vector<int>& deck) {
vector<int> res;
sort(deck.begin(),deck.end());
deque<int> dq;
dq.push_back(deck.back());
for(int i = deck.size() - 2; i >= 0; i--) {
dq.push_front(dq.back());
dq.pop_back();
dq.push_front(deck[i]);
}
while(!dq.empty()) {
res.push_back(dq.front());
dq.pop_front();
}
return res;
}
};
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
androot2
.
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]
.
题意:给出两个二叉树,每个节点的左右子树可以旋转,判断这两个二叉树是否相等。
思路:递归。
class Solution {
public:
bool flipEquiv(TreeNode* root1, TreeNode* root2) {
if(root1 == NULL && root2 == NULL) return true;
else if(root1 == NULL || root2 == NULL) return false;
else {
return (root1 -> val == root2 -> val) && ( (flipEquiv(root1 -> left,root2 -> left) && flipEquiv(root1 -> right,root2 -> right)) ||
(flipEquiv(root1 -> left,root2 -> right) && flipEquiv(root1 -> right,root2 -> left)) );
}
}
};
Largest Component Size by Common Factor
Hard
4413
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: 4Example 2:
Input: [20,50,9,63] Output: 2Example 3:
Input: [2,3,6,7,4,12,21,39] Output: 8Note:
1 <= A.length <= 20000
1 <= A[i] <= 100000
题意:给出n个数,任意两个数如果相同的因子大于1,则连接一条边。最终形成一张图,求这张图的最大连通块。
思路:每一个数都与它的所有因子连接一条边,然后dfs
using namespace std;
class Solution {
public:vector<int> G[200005];
public:bool vis[200005];
public:
int dfs(int v) {
int cnt = 0;
for(int i = 0; i < G[v].size(); i++) {
if(!vis[G[v][i]]) {
vis[G[v][i]] = 1;
if(G[v][i] <= 100000) cnt++;
cnt += dfs(G[v][i]);
}
}
return cnt;
}
public:
int largestComponentSize(vector<int>& A) {
for(int i = 0; i < A.size(); i++) {
G[A[i]].push_back(A[i] + 100000);
G[A[i] + 100000].push_back(A[i]);
for(int j = 2; j <= sqrt(A[i]); j++) {
if(A[i] % j == 0) {
G[A[i]].push_back(j + 100000);
G[j + 100000].push_back(A[i]);
if(j * j != A[i]) {
G[A[i]].push_back(A[i] / j + 100000);
G[A[i] / j + 100000].push_back(A[i]);
}
}
}
}
int ans = 0;
int t;
for(int i = 0; i < A.size(); i++) {
if(vis[A[i]] == 0) {
vis[A[i]] = 1;
t = 1;
ans = max(t + dfs(A[i]),ans);
}
}
return ans;
}
};