Skip to content

Commit f0d39ed

Browse files
[N-0] add 723
1 parent f30de28 commit f0d39ed

File tree

3 files changed

+94
-1
lines changed

3 files changed

+94
-1
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Your ideas/fixes/algorithms are more than welcome!
2222

2323
| # | Title | Solutions | Time | Space | Difficulty | Tag | Notes
2424
|-----|----------------|---------------|---------------|---------------|-------------|--------------|-----
25+
|723|[Candy Crush](https://leetcode.com/problems/candy-crush/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_723.java) | O((r*c)^2) | O((r*c)) | Medium | Array, Two Pointers
2526
|721|[Accounts Merge](https://leetcode.com/problems/accounts-merge/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_721.java) | | | Medium | DFS, Union Find
2627
|720|[Longest Word in Dictionary](https://leetcode.com/problems/longest-word-in-dictionary/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_720.java) | O(∑wi) where wi is the length of words[i] | O(∑wi) where wi is the length of words[i] | Easy | Trie
2728
|719|[Find K-th Smallest Pair Distance](https://leetcode.com/problems/find-k-th-smallest-pair-distance/)|[Solution](../master/src/main/java/com/fishercoder/solutions/_719.java) | O(nlogw + nlogn) | O(1) | Hard | Binary Search

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

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,45 @@
3838
*/
3939
public class _723 {
4040
public static class Solution1 {
41+
/**credit: https://leetcode.com/articles/candy-crush/*/
4142
public int[][] candyCrush(int[][] board) {
42-
return null;
43+
int row = board.length;
44+
int col = board[0].length;
45+
boolean todo = false;
46+
for (int i = 0; i < row; i++) {
47+
for (int j = 0; j < col - 2; j++) {
48+
int v = Math.abs(board[i][j]);
49+
if (v != 0 && v == Math.abs(board[i][j + 1]) && v == Math.abs(board[i][j + 2])) {
50+
//mark all of them to be negative
51+
board[i][j] = board[i][j + 1] = board[i][j + 2] = -v;
52+
todo = true;
53+
}
54+
}
55+
}
56+
57+
for (int i = 0; i < row - 2; i++) {
58+
for (int j = 0; j < col; j++) {
59+
int v = Math.abs(board[i][j]);
60+
if (v != 0 && v == Math.abs(board[i + 1][j]) && v == Math.abs(board[i + 2][j])) {
61+
board[i + 1][j] = board[i + 2][j] = board[i][j] = -v;
62+
todo = true;
63+
}
64+
}
65+
}
66+
67+
for (int j = 0; j < col; j++) {
68+
int wr = row - 1;
69+
for (int i = row - 1; i >= 0; i--) {
70+
if (board[i][j] > 0) {
71+
board[wr--][j] = board[i][j];
72+
}
73+
}
74+
while (wr >= 0) {
75+
board[wr--][j] = 0;
76+
}
77+
}
78+
79+
return todo ? candyCrush(board) : board;
4380
}
4481
}
4582
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package com.fishercoder;
2+
3+
import com.fishercoder.solutions._723;
4+
import org.junit.BeforeClass;
5+
import org.junit.Test;
6+
7+
import static org.junit.Assert.assertArrayEquals;
8+
9+
public class _723Test {
10+
private static _723.Solution1 solution1;
11+
private static int[][] board;
12+
private static int[][] expected;
13+
14+
@BeforeClass
15+
public static void setup() {
16+
solution1 = new _723.Solution1();
17+
}
18+
19+
@Test
20+
public void test1() {
21+
board = new int[][]{
22+
{110, 5, 112, 113, 114},
23+
{210, 211, 5, 213, 214},
24+
{310, 311, 3, 313, 314},
25+
{410, 411, 412, 5, 414},
26+
{5, 1, 512, 3, 3},
27+
{610, 4, 1, 613, 614},
28+
{710, 1, 2, 713, 714},
29+
{810, 1, 2, 1, 1},
30+
{1, 1, 2, 2, 2},
31+
{4, 1, 4, 4, 1014},
32+
};
33+
34+
expected = new int[][]{
35+
{0, 0, 0, 0, 0},
36+
{0, 0, 0, 0, 0},
37+
{0, 0, 0, 0, 0},
38+
{110, 0, 0, 0, 114},
39+
{210, 0, 0, 0, 214},
40+
{310, 0, 0, 113, 314},
41+
{410, 0, 0, 213, 414},
42+
{610, 211, 112, 313, 614},
43+
{710, 311, 412, 613, 714},
44+
{810, 411, 512, 713, 1014}
45+
};
46+
assert2dArrayEquals(expected, solution1.candyCrush(board));
47+
}
48+
49+
private void assert2dArrayEquals(int[][] expected, int[][] actual) {
50+
for (int i = 0; i < expected.length; i++) {
51+
assertArrayEquals(expected[i], actual[i]);
52+
}
53+
}
54+
55+
}

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