1025. Divisor Game - Easy
1026. Maximum Difference Between Node and Ancestor - Medium
1027. Longest Arithmetic Sequence - Medium
1028. Recover a Tree From Preorder Traversal - Hard
1025. Divisor Game
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:
1、Choosing any x with 0 < x < N and N % x == 0.
2、Replacing the number N on the chalkboard with N - 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.
Solution:
从 Alice 开始,如果 N 是 2,Alice 选 x 为 1,N 变成 1,到了 Bob 没有可以选的了,所以 Alice 赢了,N 是 3 的话,Alice 就输了,N 是 4 的话,Alice可以选 x 为 2,但是这样就输了,所以 Alice 选 x 为1,让 N 变成3 到 B,以此类推,N 的初始值是偶数的话,Alice 肯定赢,因为她可以选 x 为 1,把 N - 1 给 Bob,因为 N - 1 是奇数,所以 B 选的 x 一定也是奇数,A 的值就一定还是偶数,那 A 还选 x 为 1 即可,直到最后,一定是 A 是 2,A 选 x 为 1,B 没得选,A 赢。 同理,初始值为奇数的话,A 一定输。
bool divisorGame(int N) {
return N % 2 == 0;
}
1026. Maximum Difference Between Node and Ancestor
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:
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.
Solution:
其实就是 dfs 就行,每次记录这一层的最大最小值就行。
void dfs(TreeNode* nod, int cur_max, int cur_min, int& max_diff)
{
if(!nod) return;
if(nod->val > cur_max)
{
max_diff = max(max_diff, nod->val - cur_min);
cur_max = nod->val;
}
else if(nod->val < cur_min)
{
max_diff = max(max_diff, cur_max - nod->val);
cur_min = nod->val;
}
dfs(nod->left, cur_max, cur_min, max_diff);
dfs(nod->right, cur_max, cur_min, max_diff);
}
int maxAncestorDiff(TreeNode* root) {
int max_diff = 0;
dfs(root, root->val, root->val, max_diff);
return max_diff;
}
1027. Longest Arithmetic Sequence
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].
Solution:
正常的dp题,用一个 vector<unordered_map<int, int>> a; a[i][j] 表示从第 i 位往前,有 j 这么大的差的等差数列有多长,因为 1 2,这样的等差数列有 1 个空,所以等差数列的长度还要加1。
int longestArithSeqLength(vector<int>& A) {
int res = 0;
const int len = A.size();
if (len <= 1) return len;
vector<unordered_map<int, int>> vec(len);
for(int i = 1; i < len; ++i)
for(int j = 0; j < i; ++j)
{
const int diff = A[i] - A[j];
vec[i][diff] = vec[j][diff] + 1;
res = max(res, vec[i][diff]);
}
return res + 1;
}
1028. Recover a Tree From Preorder Traversal
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.
Solution:
其实就是用一个string 重建二叉树,0 个 dash 后边的是根节点,1个的是第一层,两个的是第二层。我的想法就是直接递归,每次就建出根节点,在对左右节点递归,这之间只要写一个函数对string进行操作,每次把连续的dash删掉一个就行。
string remove_dashes(const string& s)
{
// 这个函数删除每个连续'-'中的一个
string res;
bool to_delete = true;
for(auto c : s) {
if(c == '-' && to_delete){
to_delete = false;
continue;
}
res += c;
if(c != '-')
to_delete = true;
}
return res;
}
TreeNode* recoverFromPreorder(string S) {
// 找到左子树、右子树的根节点,也就是单独的两个'-'的位置
int left = -1, right = -1;
for (int i = 1; i < S.length() - 1; ++i)
if (S[i] == '-' && S[i - 1] != '-' && S[i + 1] != '-')
{
if (left == -1) left = i;
else right = i;
}
// 计算根节点的值
int root_val = 0;
for(auto s : S)
{
if (s == '-') break;
root_val = root_val * 10 + s - '0';
}
auto root = new TreeNode(root_val);
if (left != -1) root->left = recoverFromPreorder(remove_dashes(S.substr(left + 1, right - left - 1)));
if (right != -1) root->right = recoverFromPreorder(remove_dashes(S.substr(right + 1)));
return root;
}