Skip to content

Commit 9645da9

Browse files
committed
Merge remote-tracking branch 'origin/main' into main
2 parents 5d66332 + 5c11a69 commit 9645da9

File tree

4 files changed

+131
-0
lines changed

4 files changed

+131
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.gatsby;
2+
3+
4+
5+
/**
6+
* @ClassName: _1302DeepestLeavesSum
7+
* @Description:
8+
* @author: Gatsby
9+
* @date: 2022/7/21 13:35
10+
*/
11+
12+
public class _1302DeepestLeavesSum {
13+
private int maxDepth = 0;
14+
private int sum;
15+
16+
private void dfs(TreeNode root, int depth) {
17+
if (root == null) {
18+
return;
19+
}
20+
++depth;
21+
if (depth > maxDepth) {
22+
maxDepth = depth;
23+
sum = root.val;
24+
} else if (depth == maxDepth) {
25+
sum += root.val;
26+
}
27+
dfs(root.left, depth);
28+
dfs(root.right, depth);
29+
}
30+
31+
public int deepestLeavesSum(TreeNode root) {
32+
dfs(root, 0);
33+
return sum;
34+
}
35+
}
36+
37+
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.gatsby;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @ClassName: _452MinimumNumberOfArrowsToBurstBalloons
7+
* @Description: 452. 用最少数量的箭引爆气球
8+
* @author: Gatsby
9+
* @date: 2022/7/25 14:10
10+
*/
11+
12+
public class _452MinimumNumberOfArrowsToBurstBalloons {
13+
public int findMinArrowShots(int[][] points) {
14+
Arrays.sort(points, (int[] point1, int[] point2) -> {
15+
// case [[-2147483646,-2147483645],[2147483646,2147483647]]
16+
// 如果使用a-b的话会导致溢出
17+
return Integer.compare(point1[1], point2[1]);
18+
});
19+
int res = 1; // 至少要有一只箭
20+
int maxCover = points[0][1]; // 以最右端为箭的最远端,判断下一个气球的最左边能不能被cover
21+
22+
for (int[] point : points) {
23+
if (point[0] <= maxCover) {
24+
continue;
25+
} else {
26+
res++;
27+
maxCover = point[1];
28+
}
29+
}
30+
return res;
31+
}
32+
}
33+
34+

src/com/gatsby/_455AssignCookies.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.gatsby;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @ClassName: _455AssignCookies
7+
* @Description:
8+
* @author: Gatsby
9+
* @date: 2022/7/25 13:34
10+
*/
11+
12+
public class _455AssignCookies {
13+
public int findContentChildren(int[] appetite, int[] biscuits) {
14+
Arrays.sort(appetite);
15+
Arrays.sort(appetite);
16+
Arrays.sort(biscuits);
17+
int children = 0;
18+
int a = appetite.length;
19+
int b = biscuits.length;
20+
for (int i = 0, j = 0; i < a && j < b; ++i, ++j) {
21+
while (j < b && biscuits[j] < appetite[i]) ++j;
22+
if (j < b) children++;
23+
}
24+
return children;
25+
}
26+
}
27+
28+
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.gatsby;
2+
3+
/**
4+
* @ClassName: _81SearchInRotatedSortedArrayII
5+
* @Description:
6+
* @author: Gatsby
7+
* @date: 2022/7/25 14:26
8+
*/
9+
10+
public class _81SearchInRotatedSortedArrayII {
11+
public boolean search(int[] nums, int target) {
12+
int left = 0;
13+
int right = nums.length;
14+
while (left < right) {
15+
int mid = (right - left) / 2;
16+
if (nums[mid] == target)
17+
return true;
18+
else if (nums[left] == nums[mid]) {
19+
++left;
20+
} else if (nums[left] < nums[mid]) {
21+
if (nums[left] <= target && target < nums[mid]) right = mid - 1;
22+
else left = mid + 1;
23+
} else {
24+
if (nums[mid] < target && target <= nums[right]) left = mid + 1;
25+
else right = mid - 1;
26+
}
27+
}
28+
return false;
29+
}
30+
}
31+
32+

0 commit comments

Comments
 (0)