forked from fishercoder1534/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_699.java
48 lines (43 loc) · 1.53 KB
/
_699.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package com.fishercoder.solutions;
import java.util.ArrayList;
import java.util.List;
public class _699 {
public static class Solution1 {
/**
* credit: https://discuss.leetcode.com/topic/107107/easy-understood-o-n-2-solution-with-explanation
*/
public List<Integer> fallingSquares(int[][] positions) {
List<Interval> intervals = new ArrayList<>();
List<Integer> result = new ArrayList<>();
int height = 0;
for (int[] position : positions) {
Interval curr = new Interval(position[0], position[0] + position[1] - 1, position[1]);
height = Math.max(height, getHeight(intervals, curr));
result.add(height);
}
return result;
}
private int getHeight(List<Interval> intervals, Interval curr) {
int preMaxHeight = 0;
for (Interval interval : intervals) {
if (interval.end < curr.start || interval.start > curr.end) {
continue;
}
preMaxHeight = Math.max(preMaxHeight, interval.height);
}
curr.height += preMaxHeight;
intervals.add(curr);
return curr.height;
}
class Interval {
int start;
int end;
int height;
public Interval(int start, int end, int height) {
this.start = start;
this.end = end;
this.height = height;
}
}
}
}