Skip to content

Commit d24d3a1

Browse files
committed
Added 2 solutions
1 parent 319cab9 commit d24d3a1

File tree

2 files changed

+98
-0
lines changed

2 files changed

+98
-0
lines changed

Medium/Candy Crush.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
public int[][] candyCrush(int[][] board) {
3+
int rows = board.length;
4+
int cols = board[0].length;
5+
boolean flag = false;
6+
7+
for (int i = 0; i < rows; i++) {
8+
for (int j = 0; j + 2 < cols; j++) {
9+
int val = Math.abs(board[i][j]);
10+
if (val != 0 && val == Math.abs(board[i][j + 1]) && val == Math.abs(board[i][j + 2])) {
11+
board[i][j] = board[i][j + 1] = board[i][j + 2] = -val;
12+
flag = true;
13+
}
14+
}
15+
}
16+
17+
for (int i = 0; i + 2 < rows; i++) {
18+
for (int j = 0; j < cols; j++) {
19+
int val = Math.abs(board[i][j]);
20+
if (val != 0 && val == Math.abs(board[i + 1][j]) && val == Math.abs(board[i + 2][j])) {
21+
board[i][j] = board[i + 1][j] = board[i + 2][j] = -val;
22+
flag = true;
23+
}
24+
}
25+
}
26+
27+
for (int i = 0; i < cols; ++i) {
28+
int rightRow = rows - 1;
29+
for (int j = rows - 1; j >= 0; --j) {
30+
if (board[j][i] > 0) {
31+
board[rightRow--][i] = board[j][i];
32+
}
33+
}
34+
35+
while (rightRow >= 0) {
36+
board[rightRow--][i] = 0;
37+
}
38+
}
39+
40+
return flag ? candyCrush(board) : board;
41+
}
42+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class TimeMap {
2+
3+
/** Initialize your data structure here. */
4+
Map<String, List<Entry>> map;
5+
public TimeMap() {
6+
map = new HashMap<>();
7+
}
8+
9+
public void set(String key, String value, int timestamp) {
10+
map.computeIfAbsent(key, k -> new ArrayList<>()).add(new Entry(value, timestamp));
11+
}
12+
13+
public String get(String key, int timestamp) {
14+
if (!map.containsKey(key)) {
15+
return "";
16+
}
17+
18+
return binarySearch(map.get(key), 0, map.get(key).size() - 1, timestamp);
19+
}
20+
21+
private String binarySearch(List<Entry> entries, int left, int right, int timestamp) {
22+
int idx = Integer.MIN_VALUE;
23+
while (left <= right) {
24+
int mid = (left + right) / 2;
25+
if (entries.get(mid).timestamp == timestamp) {
26+
return entries.get(mid).value;
27+
}
28+
else if (entries.get(mid).timestamp > timestamp) {
29+
right = mid - 1;
30+
}
31+
else {
32+
idx = Math.max(idx, mid);
33+
left = mid + 1;
34+
}
35+
}
36+
37+
return idx == Integer.MIN_VALUE ? "" : entries.get(idx).value;
38+
}
39+
}
40+
41+
class Entry {
42+
String value;
43+
int timestamp;
44+
45+
public Entry(String value, int timestamp) {
46+
this.value = value;
47+
this.timestamp = timestamp;
48+
}
49+
}
50+
51+
/**
52+
* Your TimeMap object will be instantiated and called as such:
53+
* TimeMap obj = new TimeMap();
54+
* obj.set(key,value,timestamp);
55+
* String param_2 = obj.get(key,timestamp);
56+
*/

0 commit comments

Comments
 (0)