Skip to content

Commit e6cd11d

Browse files
[N-0] refactor 699
1 parent a7c1b09 commit e6cd11d

File tree

3 files changed

+66
-1
lines changed

3 files changed

+66
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ Your ideas/fixes/algorithms are more than welcome!
2828
|714|[Best Time to Buy and Sell Stock with Transaction Fee](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-with-transaction-fee/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_714.java) | O(n) | O(1) | Medium | DP
2929
|713|[Subarray Product Less Than K](https://leetcode.com/problems/subarray-product-less-than-k/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_713.java) | O(n) | O(1) | Medium |
3030
|712|[Minimum ASCII Delete Sum for Two Strings](https://leetcode.com/problems/minimum-ascii-delete-sum-for-two-strings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_712.java) | O(m*n) | O(m*n) | Medium | DP
31+
|699|[Falling Squares](https://leetcode.com/problems/falling-squares/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_699.java) | O(n^2) | O(n) | Hard | Segment Tree
3132
|698|[Partition to K Equal Sum Subsets](https://leetcode.com/problems/partition-to-k-equal-sum-subsets/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_698.java) | O(n*(2^n)) | O(2^n) | Medium | Backtracking
3233
|697|[Degree of an Array](https://leetcode.com/problems/degree-of-an-array/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_697.java) | O(n) | O(n) | Easy |
3334
|696|[Count Binary Substrings](https://leetcode.com/problems/count-binary-substrings/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_696.java) | O(n) | O(n) | Easy |

src/main/java/com/fishercoder/solutions/_699.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.fishercoder.solutions;
22

3+
import java.util.ArrayList;
34
import java.util.List;
45

56
/**
@@ -69,8 +70,42 @@
6970

7071
public class _699 {
7172
public static class Solution1 {
73+
/**credit: https://discuss.leetcode.com/topic/107107/easy-understood-o-n-2-solution-with-explanation*/
7274
public List<Integer> fallingSquares(int[][] positions) {
73-
return null;
75+
List<Interval> intervals = new ArrayList<>();
76+
List<Integer> result = new ArrayList<>();
77+
int height = 0;
78+
for (int[] position : positions) {
79+
Interval curr = new Interval(position[0], position[0] + position[1] - 1, position[1]);
80+
height = Math.max(height, getHeight(intervals, curr));
81+
result.add(height);
82+
}
83+
return result;
84+
}
85+
86+
private int getHeight(List<Interval> intervals, Interval curr) {
87+
int preMaxHeight = 0;
88+
for (Interval interval : intervals) {
89+
if (interval.end < curr.start || interval.start > curr.end) {
90+
continue;
91+
}
92+
preMaxHeight = Math.max(preMaxHeight, interval.height);
93+
}
94+
curr.height += preMaxHeight;
95+
intervals.add(curr);
96+
return curr.height;
97+
}
98+
99+
class Interval {
100+
int start;
101+
int end;
102+
int height;
103+
104+
public Interval(int start, int end, int height) {
105+
this.start = start;
106+
this.end = end;
107+
this.height = height;
108+
}
74109
}
75110
}
76111
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._699;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import java.util.Arrays;
8+
9+
import static junit.framework.TestCase.assertEquals;
10+
11+
public class _699Test {
12+
private static _699.Solution1 solution1;
13+
private static int[][] positions;
14+
15+
@BeforeClass
16+
public static void setup() {
17+
solution1 = new _699.Solution1();
18+
}
19+
20+
@Test
21+
public void test1() {
22+
positions = new int[][]{
23+
{1, 2},
24+
{2, 3},
25+
{6, 1}
26+
};
27+
assertEquals(Arrays.asList(2, 5, 5), solution1.fallingSquares(positions));
28+
}
29+
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy