Skip to content

Commit e073526

Browse files
Merge pull request algorithm001#602 from artisticman/master
第三周操作作业,学号140
2 parents dc5ec5e + 273d04b commit e073526

2 files changed

Lines changed: 97 additions & 0 deletions

File tree

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Given a binary tree, find its maximum depth.
3+
*
4+
* The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node.
5+
*
6+
* Note: A leaf is a node with no children.
7+
*
8+
* Example:
9+
*
10+
* Given binary tree [3,9,20,null,null,15,7],
11+
*
12+
* 3
13+
* / \
14+
* 9 20
15+
* / \
16+
* 15 7
17+
* return its depth = 3.
18+
*/
19+
public class Leetcode_104_140 {
20+
21+
public int maxDepth(TreeNode root) {
22+
return(depth(root, 0));
23+
}
24+
25+
int depth(TreeNode node, int depth) {
26+
if(node == null) return depth;
27+
return Math.max(depth(node.left, depth+1), depth(node.right, depth+1));
28+
}
29+
30+
public class TreeNode {
31+
int val;
32+
TreeNode left;
33+
TreeNode right;
34+
TreeNode(int x) { val = x; }
35+
}
36+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import java.util.*;
2+
3+
/**
4+
* You are given two integer arrays nums1 and nums2 sorted in ascending order and an integer k.
5+
*
6+
* Define a pair (u,v) which consists of one element from the first array and one element from the second array.
7+
*
8+
* Find the k pairs (u1,v1),(u2,v2) ...(uk,vk) with the smallest sums.
9+
*
10+
* Example 1:
11+
*
12+
* Input: nums1 = [1,7,11], nums2 = [2,4,6], k = 3
13+
* Output: [[1,2],[1,4],[1,6]]
14+
* Explanation: The first 3 pairs are returned from the sequence:
15+
* [1,2],[1,4],[1,6],[7,2],[7,4],[11,2],[7,6],[11,4],[11,6]
16+
* Example 2:
17+
*
18+
* Input: nums1 = [1,1,2], nums2 = [1,2,3], k = 2
19+
* Output: [1,1],[1,1]
20+
* Explanation: The first 2 pairs are returned from the sequence:
21+
* [1,1],[1,1],[1,2],[2,1],[1,2],[2,2],[1,3],[1,3],[2,3]
22+
* Example 3:
23+
*
24+
* Input: nums1 = [1,2], nums2 = [3], k = 3
25+
* Output: [1,3],[2,3]
26+
* Explanation: All possible pairs are returned from the sequence: [1,3],[2,3]
27+
*/
28+
public class Leetcode_373_140 {
29+
30+
public List<int[]> kSmallestPairs(int[] nums1, int[] nums2, int k) {
31+
Queue<int []> maxHeap = new PriorityQueue<>(k, (a, b) -> (b[0] + b[1] - a[0] - a[1]));
32+
33+
for(int num1 : nums1) {
34+
for(int num2 : nums2) {
35+
int [] a = new int[] {num1, num2};
36+
37+
int sum = num1 + num2;
38+
39+
if(maxHeap.size() >= k) {
40+
int [] max = maxHeap.peek();
41+
if(sum < max[0] + max[1]) {
42+
maxHeap.poll();
43+
maxHeap.offer(a);
44+
}
45+
} else {
46+
maxHeap.offer(a);
47+
}
48+
49+
}
50+
}
51+
52+
List<int []> ans = new LinkedList<>();
53+
while(!maxHeap.isEmpty()) {
54+
ans.add(maxHeap.poll());
55+
}
56+
57+
Collections.reverse(ans);
58+
59+
return ans;
60+
}
61+
}

0 commit comments

Comments
 (0)