Skip to content

Commit 19edbde

Browse files
committed
190812
1 parent 75f9cb4 commit 19edbde

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// 1.0 greedy, O(n^2)/O(n)
2+
class Solution {
3+
public int[][] reconstructQueue(int[][] people) {
4+
if (people == null || people.length == 0 || people[0].length == 0) {
5+
return new int[0][0];
6+
}
7+
Arrays.sort(people, (a, b) -> (a[0] == b[0] ? a[1] - b[1] : b[0] - a[0]));
8+
List<int[]> res = new LinkedList<>();
9+
for (int[] p : people) {
10+
res.add(p[1], p);
11+
}
12+
return res.toArray(new int[res.size()][]);
13+
}
14+
}
15+
16+
// 2.0 merge sort, O(nlogn)
17+
// https://leetcode.com/problems/queue-reconstruction-by-height/discuss/143403/O(nlogn)-modified-merge-sort-solution-with-detailed-explanation
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//same as 435, difference : overlaps include [1,2] & [2,3], O(nlogn)/O(n)
2+
class Solution {
3+
public int findMinArrowShots(int[][] points) {
4+
if (points.length == 0) return 0;
5+
Arrays.sort(points, (a, b) -> (a[1] - b[1]));
6+
int cnt = 1, end = points[0][1];
7+
for (int i = 1; i < points.length; i++) {
8+
if (points[i][0] <= end) continue;
9+
end = points[i][1];
10+
cnt++;
11+
}
12+
return cnt;
13+
}
14+
}

0 commit comments

Comments
 (0)