Skip to content

Commit dff5b44

Browse files
committed
Add the solutions for #118, #120, #239, #981
1 parent 091d40a commit dff5b44

File tree

5 files changed

+245
-0
lines changed

5 files changed

+245
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package org.sean.array;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.LinkedList;
6+
import java.util.List;
7+
8+
// 118. Pascal's Triangle
9+
public class PascalTriangle {
10+
public static void main(String[] args) {
11+
PascalTriangle solution = new PascalTriangle();
12+
System.out.println(Arrays.toString(solution.generate(5).toArray()));
13+
}
14+
15+
public List<List<Integer>> generate(int numRows) {
16+
List<List<Integer>> lists = new LinkedList<>();
17+
if (numRows == 0) return lists;
18+
19+
List<Integer> firstList = new ArrayList<>();
20+
firstList.add(1);
21+
lists.add(firstList);
22+
if (numRows == 1) {
23+
return lists;
24+
}
25+
26+
List<Integer> secondList = new ArrayList<>();
27+
secondList.add(1);
28+
secondList.add(1);
29+
lists.add(secondList);
30+
31+
if (numRows == 2) {
32+
return lists;
33+
}
34+
35+
for (int i = 2; i < numRows; i++) {
36+
ArrayList<Integer> list = new ArrayList<>(i + 1);
37+
for (int k = 0; k < (i + 1); k++) {
38+
list.add(1);
39+
}
40+
41+
List<Integer> preList = lists.get(i - 1);
42+
for (int j = 1; j < i; j++) {
43+
list.set(j, preList.get(j - 1) + preList.get(j));
44+
}
45+
46+
lists.add(list);
47+
}
48+
49+
return lists;
50+
}
51+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.sean.array;
2+
3+
import java.util.LinkedList;
4+
5+
// 239. Sliding Window Maximum
6+
public class SlidingWindowMax {
7+
public int[] maxSlidingWindow(int[] nums, int k) {
8+
if(nums == null || nums.length == 0) {
9+
return new int[0];
10+
}
11+
12+
if (k == 1) {
13+
return nums;
14+
}
15+
16+
LinkedList<Integer> deque = new LinkedList<>();
17+
int len = nums.length;
18+
19+
// replace List with raw Array
20+
int[] res = new int[len - k + 1];
21+
for (int i = 0; i < len ; i++) {
22+
int elem = nums[i];
23+
// delete the left-out element
24+
if(i >= k && deque.peek() < i - k + 1) {
25+
deque.pop();
26+
}
27+
28+
// remove the left smaller elements
29+
while (!deque.isEmpty() && nums[deque.peekLast()] <= elem) {
30+
deque.removeLast();
31+
}
32+
33+
deque.add(i); // index
34+
35+
if(i >= k - 1 && !deque.isEmpty()) {
36+
res[i - (k - 1)] = nums[deque.peek()];
37+
}
38+
}
39+
40+
return res;
41+
}
42+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.sean.array;
2+
3+
import java.util.*;
4+
5+
// 981. Time Based Key-Value Store
6+
public class TimeMap {
7+
private static final String EMPTY = "";
8+
private HashMap<String, SortedMap<Integer, String>> map;
9+
10+
/** Initialize your data structure here. */
11+
public TimeMap() {
12+
map = new HashMap<>();
13+
}
14+
15+
// [foo, bar 1]
16+
// [foo, bar 3]
17+
// [foo, bar2, 4]
18+
// -> (foo, 1) // (foo, 3) // (foo, 4) // (foo, 5)
19+
public void set(String key, String value, int timestamp) {
20+
if (map.containsKey(key)) {
21+
SortedMap<Integer, String> valMap = map.get(key);
22+
valMap.put(timestamp, value); // with timestamps increasing strictly
23+
} else {
24+
TreeMap<Integer, String> subMap = new TreeMap<>();
25+
subMap.put(timestamp, value);
26+
map.put(key, subMap);
27+
}
28+
}
29+
30+
public String get(String key, int timestamp) {
31+
if (!map.containsKey(key)) return EMPTY;
32+
33+
TreeMap<Integer, String> sortedMap = (TreeMap<Integer, String>) map.get(key);
34+
if (sortedMap.containsKey(timestamp)) {
35+
return sortedMap.get(timestamp);
36+
} else {
37+
Integer lowerKey = sortedMap.lowerKey(timestamp);
38+
if (lowerKey == null) {
39+
return EMPTY;
40+
} else {
41+
return sortedMap.get(lowerKey);
42+
}
43+
}
44+
}
45+
}
46+
47+
/**
48+
* Your TimeMap object will be instantiated and called as such:
49+
* TimeMap obj = new TimeMap();
50+
* obj.set(key,value,timestamp);
51+
* String param_2 = obj.get(key,timestamp);
52+
*/
53+
54+
/*Note:
55+
All key/value strings are lowercase.
56+
All key/value strings have length in the range [1, 100]
57+
The timestamps for all TimeMap.set operations are strictly increasing.
58+
1 <= timestamp <= 10^7
59+
TimeMap.set and TimeMap.get functions will be called a total of 120000 times (combined) per test case.*/
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package org.sean.array;
2+
3+
import java.util.*;
4+
5+
// 120. Triangle
6+
public class Triangle {
7+
private int minSum = Integer.MAX_VALUE;
8+
9+
public int minimumTotal(List<List<Integer>> triangle) {
10+
if (triangle == null || triangle.size() == 0) {
11+
return 0;
12+
}
13+
14+
// dfs(triangle, 0, 0, "", 0);
15+
int row = triangle.size();
16+
for (int i = row - 2; i >= 0; i--) {
17+
for (int j = 0; j < triangle.get(i).size(); j++) {
18+
triangle.get(i)
19+
.set(
20+
j,
21+
triangle.get(i).get(j)
22+
+ Math.min(
23+
triangle.get(i + 1).get(j),
24+
triangle.get(i + 1).get(j + 1)));
25+
}
26+
}
27+
28+
System.out.println(String.format(">>> minSum is %d", triangle.get(0).get(0)));
29+
30+
List<Integer> integers = new ArrayList<Integer>(5);
31+
Collections.fill(integers, 1);
32+
33+
return 0;
34+
}
35+
36+
// brute force : time out
37+
private void dfs(List<List<Integer>> triangle, int row, int col, String path, int sum) {
38+
if (row == triangle.size()) {
39+
if (minSum > sum) {
40+
minSum = sum;
41+
}
42+
43+
System.out.println(path);
44+
return;
45+
}
46+
47+
path += " -> ";
48+
49+
path += triangle.get(row).get(col);
50+
sum += triangle.get(row).get(col);
51+
52+
dfs(triangle, row + 1, col, path, sum);
53+
dfs(triangle, row + 1, col + 1, path, sum);
54+
}
55+
56+
public static <T> List<T> asList(T... items) {
57+
List<T> list = new ArrayList<T>();
58+
for (T item : items) {
59+
list.add(item);
60+
}
61+
62+
return list;
63+
}
64+
65+
public static void main(String[] args) {
66+
Triangle solution = new Triangle();
67+
List<List<Integer>> triangle =
68+
asList(asList(2), asList(3, 4), asList(6, 5, 7), asList(4, 1, 8, 3));
69+
70+
solution.minimumTotal(triangle);
71+
}
72+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package org.sean.array;
2+
3+
import org.junit.Assert;
4+
import org.junit.Before;
5+
import org.junit.Test;
6+
7+
public class SlidingWindowMaxTest {
8+
9+
private SlidingWindowMax slidingWindowMax;
10+
11+
@Before
12+
public void setUp() throws Exception {
13+
slidingWindowMax = new SlidingWindowMax();
14+
}
15+
16+
@Test
17+
public void maxSlidingWindow() {
18+
int[] ints = slidingWindowMax.maxSlidingWindow(new int[] {1, 3, -1, -3, 5, 3, 6, 7}, 3);
19+
Assert.assertArrayEquals(new int[] {3, 3, 5, 5, 6, 7}, ints);
20+
}
21+
}

0 commit comments

Comments
 (0)