Skip to content

Commit fc1c43f

Browse files
committed
Upsolved Weekly Contest 132
1 parent 94a3e96 commit fc1c43f

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class Solution {
2+
public boolean divisorGame(int N) {
3+
return N % 2 == 0;
4+
}
5+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Solution {
2+
public int longestArithSeqLength(int[] A) {
3+
if (A.length <= 1) {
4+
return A.length;
5+
}
6+
7+
int res = 0;
8+
HashMap<Integer, Integer>[] dp = new HashMap[A.length];
9+
10+
for (int i = 0; i < A.length; i++) {
11+
dp[i] = new HashMap<>();
12+
}
13+
14+
for (int i = 1; i < A.length; i++) {
15+
int curr = A[i];
16+
for (int j = 0; j < i; j++) {
17+
int prev = A[j];
18+
int diff = curr - prev;
19+
20+
int len = 2;
21+
22+
if (dp[j].containsKey(diff)) {
23+
len = dp[j].get(diff) + 1;
24+
}
25+
26+
int currLen = dp[i].getOrDefault(diff, 0);
27+
dp[i].put(diff, Math.max(currLen, len));
28+
29+
res = Math.max(res, dp[i].get(diff));
30+
}
31+
}
32+
33+
return res;
34+
}
35+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* public class TreeNode {
4+
* int val;
5+
* TreeNode left;
6+
* TreeNode right;
7+
* TreeNode(int x) { val = x; }
8+
* }
9+
*/
10+
class Solution {
11+
public int maxAncestorDiff(TreeNode root) {
12+
return dfs(root, root.val, root.val);
13+
}
14+
15+
public int dfs(TreeNode root, int min, int max) {
16+
if (root == null) {
17+
return 0;
18+
}
19+
20+
int res = Math.max(max - root.val, root.val - min);
21+
max = Math.max(root.val, max);
22+
min = Math.min(root.val, min);
23+
24+
res = Math.max(res, dfs(root.left, min, max));
25+
res = Math.max(res, dfs(root.right, min, max));
26+
27+
return res;
28+
}
29+
}

0 commit comments

Comments
 (0)