1025. Divisor Game
- User Accepted:2139
- User Tried:2433
- Total Accepted:2208
- Total Submissions:4268
- Difficulty:Easy
Alice and Bob take turns playing a game, with Alice starting first.
Initially, there is a number N
on the chalkboard. On each player's turn, that player makes a move consisting of:
- Choosing any
x
with0 < x < N
andN % x == 0
. - Replacing the number
N
on the chalkboard withN - x
.
Also, if a player cannot make a move, they lose the game.
Return True
if and only if Alice wins the game, assuming both players play optimally.
Example 1:
Input: 2 Output: true Explanation: Alice chooses 1, and Bob has no more moves.
Example 2:
Input: 3 Output: false Explanation: Alice chooses 1, Bob chooses 1, and Alice has no more moves.
Note:
1 <= N <= 1000
解答:
class Solution {
public:
bool divisorGame(int N) {
int dp[1005];
dp[1]=0;
for(int i=2;i<=N;i++)
{
dp[i]=0;
for(int j=1;j<i;j++)
{
if(i%j) continue;
if(!dp[i-j]) dp[i]=1;
}
}
return dp[N];
}
};
1026. Maximum Difference Between Node and Ancestor
- User Accepted:1954
- User Tried:2313
- Total Accepted:1993
- Total Submissions:3969
- Difficulty:Medium
Given the root
of a binary tree, find the maximum value V
for which there exists different nodes A
and B
where V = |A.val - B.val|
and A
is an ancestor of B
.
(A node A is an ancestor of B if either: any child of A is equal to B, or any child of A is an ancestor of B.)
Example 1:
Input: [8,3,10,1,6,null,14,null,null,4,7,13]
Output: 7
Explanation:
We have various ancestor-node differences, some of which are given below :
|8 - 3| = 5
|3 - 7| = 4
|8 - 1| = 7
|10 - 13| = 3
Among all possible differences, the maximum value of 7 is obtained by |8 - 1| = 7.
Note:
- The number of nodes in the tree is between
2
and5000
. - Each node will have value between
0
and100000
.
解答:
/**
* 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:
int v_max = 0;
int maxAncestorDiff(TreeNode* root, int imax = 0, int imin = 100005) {
if(nullptr == root)
return 0;
imax = max(imax, root -> val);
imin = min(imin, root -> val);
v_max = max(v_max, abs(imax - imin));
maxAncestorDiff(root -> left, imax, imin);//go to left child
maxAncestorDiff(root -> right, imax, imin);//go to right child
return v_max;
}
};
1027
1027. Longest Arithmetic Sequence
- User Accepted:961
- User Tried:1589
- Total Accepted:991
- Total Submissions:3310
- Difficulty:Medium
Given an array A
of integers, return the length of the longest arithmetic subsequence in A
.
Recall that a subsequence of A
is a list A[i_1], A[i_2], ..., A[i_k]
with 0 <= i_1 < i_2 < ... < i_k <= A.length - 1
, and that a sequence B
is arithmetic if B[i+1] - B[i]
are all the same value (for 0 <= i < B.length - 1
).
Example 1:
Input: [3,6,9,12]
Output: 4
Explanation:
The whole array is an arithmetic sequence with steps of length = 3.
Example 2:
Input: [9,4,7,2,10]
Output: 3
Explanation:
The longest arithmetic subsequence is [4,7,10].
Example 3:
Input: [20,1,15,3,10,5,8]
Output: 4
Explanation:
The longest arithmetic subsequence is [20,15,10,5].
Note:
2 <= A.length <= 2000
0 <= A[i] <= 10000
解答:
class Solution {
public:
int longestArithSeqLength(vector<int>& A) {
int len=A.size();
int ans=0;
//map<int,int>dp[2005];
map<int,map<int,int>>dp;
for(int i=0;i<A.size();i++)
{
for(int j=0;j<i;j++)
{
int cha=A[j]-A[i];
dp[i][cha]=max(dp[i][cha],dp[j][cha]+1);
ans=max(ans,dp[i][cha]+1);
}
}
return ans;
}
};
1028. Recover a Tree From Preorder Traversal
- User Accepted:841
- User Tried:915
- Total Accepted:857
- Total Submissions:1193
- Difficulty:Hard
We run a preorder depth first search on the root
of a binary tree.
At each node in this traversal, we output D
dashes (where D
is the depth of this node), then we output the value of this node. (If the depth of a node is D
, the depth of its immediate child is D+1
. The depth of the root node is 0
.)
If a node has only one child, that child is guaranteed to be the left child.
Given the output S
of this traversal, recover the tree and return its root
.
Example 1:
Input: "1-2--3--4-5--6--7"
Output: [1,2,5,3,4,6,7]
Example 2:
Input: "1-2--3---4-5--6---7"
Output: [1,2,5,3,null,6,null,4,null,7]
Example 3:
Input: "1-401--349---90--88"
Output: [1,401,null,349,88,90]
Note:
- The number of nodes in the original tree is between
1
and1000
. - Each node will have a value between
1
and10^9
.
解答:
/**
* 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:
TreeNode* recoverFromPreorder(string S,int l=0,int r=0,int step=0) {
if(step==0)
{
int len=S.length();
r=len-1;
}
TreeNode* t;
int tz=0;
for(int i=l;i<=r;i++)
{
if(S[i]=='-')
{
tz=1;
break;
}
}
if(tz==0)
{
int tem=0;
for(int i=l;i<=r;i++) tem=tem*10+S[i]-'0';
t=(TreeNode* )malloc(sizeof(TreeNode));
t->val=tem;
t->left=NULL;
t->right=NULL;
return t;
}
t=(TreeNode* )malloc(sizeof(TreeNode));
int lp,rp,rv,fz,tem,cnt,mini;
cnt=fz=tem=0;
int flag,flag2,flag3,flag4;
flag=flag2=flag3=flag4=0;
for(int i=l;i<=r;i++)
{
if(flag==1)
{
if(S[i]=='-'&&S[i-1]!='-')
{
cnt=1;
}
if(S[i]=='-'&&S[i-1]=='-')
{
cnt++;
}
if(S[i]!='-'&&S[i-1]=='-')
{
if(cnt==mini)
{
rp=i-cnt-1;
rv=i;
fz=1;
break;
}
cnt=0;
}
}
else
{
if(flag2==0&&S[i]!='-')
{
tem=tem*10+S[i]-'0';
flag3=1;
}
else if(flag3==1&&S[i]=='-')
{
cnt++;
flag2=1;
}
else if(flag2==1&&S[i]!='-')
{
mini=cnt;
flag=1;
lp=i;
cnt=0;
}
}
}
t->val=tem;
if(fz==0)
rp=r;
t->left=recoverFromPreorder(S,lp,rp,step+1);
if(fz)
t->right=recoverFromPreorder(S,rv,r,step+1);
else
t->right=NULL;
return t;
}
};