Skip to content

Commit 0728da4

Browse files
committed
623-640-870-1238-1326 (5)
1 parent 931f55c commit 0728da4

File tree

20 files changed

+459
-105
lines changed

20 files changed

+459
-105
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,5 +625,6 @@ Hierholzer 算法
625625
- [clist.by](https://clist.by/account/zhang-yi-yang/resource/leetcode.com/)
626626
- [visualgo](https://visualgo.net/zh)
627627
- [宫水三叶の刷题日记](https://www.acoier.com/tags/)
628+
- [灵茶の试炼](https://docs.qq.com/sheet/DWGFoRGVZRmxNaXFz?tab=BB08J2)
628629

629630
(全文完)

leetcode-01/src/main/java/Solution45.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
public class Solution45 {
22
public int jump(int[] nums) {
3-
int len = nums.length;
4-
int max = 0;
5-
// 贪心
6-
int end = 0;
7-
int step = 0;
8-
for (int i = 0; i < len - 1; i++) {
9-
max = Math.max(max, i + nums[i]);
10-
if (i == end) {
11-
end = max;
3+
int n = nums.length;
4+
5+
// 能到达的最远距离下标,当前下标,步数
6+
int maxR = 0, curR = 0, step = 0;
7+
// [1, n-1) 到达 nums[n - 1] 的最小跳跃次数
8+
for (int i = 0; i < n - 1; i++) {
9+
maxR = Math.max(maxR, i + nums[i]);
10+
if (i == curR) {
11+
curR = maxR;
1212
step++;
1313
}
1414
}
@@ -28,4 +28,8 @@ public int jump(int[] nums) {
2828
0 <= nums[i] <= 1000
2929
3030
贪心。
31+
相似题目: 1024. 视频拼接
32+
https://leetcode.cn/problems/video-stitching/
33+
1326. 灌溉花园的最少水龙头数目
34+
https://leetcode.cn/problems/minimum-number-of-taps-to-open-to-water-a-garden/
3135
*/

leetcode-01/src/main/java/Solution89.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,4 +29,7 @@ private int g(int n) {
2929
1 <= n <= 16
3030
3131
位运算
32+
时间复杂度 O(2^n)
33+
相似题目: 1238. 循环码排列
34+
https://leetcode.cn/problems/circular-permutation-in-binary-representation/
3235
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
public class Solution623 {
2+
public TreeNode addOneRow(TreeNode root, int val, int depth) {
3+
if (root == null) {
4+
return null;
5+
}
6+
if (depth == 1) {
7+
return new TreeNode(val, root, null);
8+
}
9+
if (depth == 2) {
10+
root.left = new TreeNode(val, root.left, null);
11+
root.right = new TreeNode(val, null, root.right);
12+
} else {
13+
root.left = addOneRow(root.left, val, depth - 1);
14+
root.right = addOneRow(root.right, val, depth - 1);
15+
}
16+
return root;
17+
}
18+
}
19+
/*
20+
623. 在二叉树中增加一行
21+
https://leetcode.cn/problems/add-one-row-to-tree/
22+
23+
给定一个二叉树的根 root 和两个整数 val 和 depth ,在给定的深度 depth 处添加一个值为 val 的节点行。
24+
注意,根节点 root 位于深度 1 。
25+
加法规则如下:
26+
- 给定整数 depth,对于深度为 depth - 1 的每个非空树节点 cur ,创建两个值为 val 的树节点作为 cur 的左子树根和右子树根。
27+
- cur 原来的左子树应该是新的左子树根的左子树。
28+
- cur 原来的右子树应该是新的右子树根的右子树。
29+
- 如果 depth == 1 意味着 depth - 1 根本没有深度,那么创建一个树节点,值 val 作为整个原始树的新根,而原始树就是新根的左子树。
30+
提示:
31+
节点数在 [1, 10^4] 范围内
32+
树的深度在 [1, 10^4]范围内
33+
-100 <= Node.val <= 100
34+
-10^5 <= val <= 10^5
35+
1 <= depth <= the depth of tree + 1
36+
37+
递归
38+
时间复杂度 O(n)
39+
空间复杂度 O(n)
40+
*/
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
public class Solution640 {
2+
public String solveEquation(String equation) {
3+
int n = equation.length();
4+
char[] s = equation.toCharArray();
5+
int x = 0;
6+
int num = 0;
7+
8+
int i = 0;
9+
int op = 1;
10+
while (i < n) {
11+
if (s[i] == '+') {
12+
op = 1;
13+
i++;
14+
} else if (s[i] == '-') {
15+
op = -1;
16+
i++;
17+
} else if (s[i] == '=') {
18+
op = 1;
19+
x *= -1;
20+
num *= -1;
21+
i++;
22+
} else {
23+
int j = i;
24+
while (j < n && s[j] != '+' && s[j] != '-' && s[j] != '=') {
25+
j++;
26+
}
27+
if (s[j - 1] == 'x') {
28+
x += op * (i < j - 1 ? Integer.parseInt(equation.substring(i, j - 1)) : 1);
29+
} else {
30+
num += op * Integer.parseInt(equation.substring(i, j));
31+
}
32+
i = j;
33+
}
34+
}
35+
if (x == 0) {
36+
return num == 0 ? "Infinite solutions" : "No solution";
37+
}
38+
return "x=" + -num / x;
39+
}
40+
}
41+
/*
42+
640. 求解方程
43+
https://leetcode.cn/problems/solve-the-equation/
44+
45+
求解一个给定的方程,将x以字符串 "x=#value" 的形式返回。该方程仅包含 '+' , '-' 操作,变量 x 和其对应系数。
46+
如果方程没有解或存在的解不为整数,请返回 "No solution" 。如果方程有无限解,则返回 “Infinite solutions” 。
47+
题目保证,如果方程中只有一个解,则 'x' 的值是一个整数。
48+
提示:
49+
3 <= equation.length <= 1000
50+
equation 只有一个 '='.
51+
方程由绝对值在 [0, 100] 范围内且无任何前导零的整数和变量 'x' 组成。
52+
53+
模拟。表达式求值
54+
*/
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution623Tests {
5+
private final Solution623 solution623 = new Solution623();
6+
7+
@Test
8+
public void example1() {
9+
TreeNode root = TreeNode.buildTreeNode("[4,2,6,3,1,5]");
10+
int val = 1;
11+
int depth = 2;
12+
TreeNode expected = TreeNode.buildTreeNode("[4,1,1,2,null,null,6,3,1,5]");
13+
Assertions.assertTrue(TreeNode.assertTreeNodeEquals(expected, solution623.addOneRow(root, val, depth)));
14+
}
15+
16+
@Test
17+
public void example2() {
18+
TreeNode root = TreeNode.buildTreeNode("[4,2,null,3,1]");
19+
int val = 1;
20+
int depth = 3;
21+
TreeNode expected = TreeNode.buildTreeNode("[4,2,null,1,1,3,null,null,1]");
22+
Assertions.assertTrue(TreeNode.assertTreeNodeEquals(expected, solution623.addOneRow(root, val, depth)));
23+
}
24+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution640Tests {
5+
private final Solution640 solution640 = new Solution640();
6+
7+
@Test
8+
public void example1() {
9+
String equation = "x+5-3+x=6+x-2";
10+
String expected = "x=2";
11+
Assertions.assertEquals(expected, solution640.solveEquation(equation));
12+
}
13+
14+
@Test
15+
public void example2() {
16+
String equation = "x=x";
17+
String expected = "Infinite solutions";
18+
Assertions.assertEquals(expected, solution640.solveEquation(equation));
19+
}
20+
21+
@Test
22+
public void example3() {
23+
String equation = "2x=x";
24+
String expected = "x=0";
25+
Assertions.assertEquals(expected, solution640.solveEquation(equation));
26+
}
27+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import java.util.Arrays;
2+
import java.util.Comparator;
3+
import java.util.stream.IntStream;
4+
5+
public class Solution870 {
6+
public int[] advantageCount(int[] nums1, int[] nums2) {
7+
int n = nums1.length;
8+
Arrays.sort(nums1);
9+
Integer[] id2 = IntStream.range(0, n).boxed().toArray(Integer[]::new);
10+
Arrays.sort(id2, Comparator.comparingInt(o -> nums2[o]));
11+
12+
int[] res = new int[n];
13+
int l = 0, r = n - 1;
14+
for (int n1 : nums1) {
15+
if (n1 > nums2[id2[l]]) {
16+
res[id2[l]] = n1;
17+
l++;
18+
} else {
19+
res[id2[r]] = n1;
20+
r--;
21+
}
22+
}
23+
return res;
24+
}
25+
}
26+
/*
27+
870. 优势洗牌
28+
https://leetcode.cn/problems/advantage-shuffle/
29+
30+
给定两个大小相等的数组 nums1 和 nums2,nums1 相对于 nums2 的优势可以用满足 nums1[i] > nums2[i] 的索引 i 的数目来描述。
31+
返回 nums1 的任意排列,使其相对于 nums2 的优势最大化。
32+
提示:
33+
1 <= nums1.length <= 10^5
34+
nums2.length == nums1.length
35+
0 <= nums1[i], nums2[i] <= 10^9
36+
37+
贪心。又名 “田忌赛马”
38+
时间复杂度 O(nlogn)
39+
空间复杂度 O(n)
40+
*/
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import org.junit.jupiter.api.Assertions;
2+
import org.junit.jupiter.api.Test;
3+
4+
public class Solution870Tests {
5+
private final Solution870 solution870 = new Solution870();
6+
7+
@Test
8+
public void example1() {
9+
int[] nums1 = {2, 7, 11, 15};
10+
int[] nums2 = {1, 10, 4, 11};
11+
int[] expected = {2, 11, 7, 15};
12+
Assertions.assertArrayEquals(expected, solution870.advantageCount(nums1, nums2));
13+
}
14+
15+
@Test
16+
public void example2() {
17+
int[] nums1 = {12, 24, 8, 32};
18+
int[] nums2 = {13, 25, 32, 11};
19+
int[] expected = {24, 32, 8, 12};
20+
Assertions.assertArrayEquals(expected, solution870.advantageCount(nums1, nums2));
21+
}
22+
}
Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,29 @@
11
public class Solution1024 {
22
public int videoStitching(int[][] clips, int time) {
3-
// 预处理每个 start 的最长范围
4-
int[] maxRange = new int[time];
3+
// f[i] 表示下标 starti 对应的最长 endi
4+
int[] f = new int[time];
55
for (int[] clip : clips) {
6-
int start = clip[0];
7-
if (start < time) {
8-
maxRange[start] = Math.max(maxRange[start], clip[1]);
6+
int l = clip[0];
7+
// 防止越界
8+
if (l < time) {
9+
f[l] = Math.max(f[l], clip[1]);
910
}
1011
}
1112

12-
// 贪心
13-
int cnt = 0;
14-
int pre = 0;
15-
int last = 0;
13+
// 能到达的最远距离下标,当前下标,步数
14+
int maxR = 0, curR = 0, step = 0;
15+
// [1, time) 返回所需片段的最小数目
1616
for (int i = 0; i < time; i++) {
17-
last = Math.max(last, maxRange[i]);
18-
if (i == last) {
19-
return -1;
20-
}
21-
if (i == pre) {
22-
pre = last;
23-
cnt++;
17+
maxR = Math.max(maxR, f[i]);
18+
if (i == curR) {
19+
if (i == maxR) {
20+
return -1;
21+
}
22+
curR = maxR;
23+
step++;
2424
}
2525
}
26-
return cnt;
26+
return step;
2727
}
2828
}
2929
/*
@@ -44,4 +44,8 @@ public int videoStitching(int[][] clips, int time) {
4444
每次尽可能找最长的区间延申,以达到所需片段的最小
4545
时间复杂度 O(clips.length + time)
4646
空间复杂度 O(time)
47+
相似题目: 45. 跳跃游戏 II
48+
https://leetcode.cn/problems/jump-game-ii/
49+
1326. 灌溉花园的最少水龙头数目
50+
https://leetcode.cn/problems/minimum-number-of-taps-to-open-to-water-a-garden/
4751
*/

0 commit comments

Comments
 (0)