Skip to content

Commit 7e6bad2

Browse files
committed
Solutions added for LeetCode #289, #524
1 parent d8581a2 commit 7e6bad2

File tree

4 files changed

+222
-0
lines changed

4 files changed

+222
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package org.sean.array;
2+
3+
/***
4+
* 289. Game of Life
5+
*/
6+
public class LifeGame {
7+
// follow up : in-place update
8+
public void gameOfLife(int[][] board) {
9+
update(board, 0);
10+
}
11+
12+
// recursive
13+
private void update(int[][] grid, int pos) {
14+
int rCnt = grid.length;
15+
int cCnt = grid[0].length;
16+
if (pos >= rCnt * cCnt)
17+
return;
18+
19+
int r = pos / cCnt;
20+
int c = pos % cCnt;
21+
int nVal = calc(grid, r, c);
22+
23+
update(grid, pos + 1);
24+
25+
grid[r][c] = nVal;
26+
}
27+
28+
// basic idea
29+
public void gameOfLife0(int[][] board) {
30+
int rCnt = board.length;
31+
int cCnt = board[0].length;
32+
33+
int[][] copy = new int[rCnt][cCnt];
34+
for (int i = 0; i < rCnt; i++) {
35+
for (int j = 0; j < cCnt; j++) {
36+
copy[i][j] = calc(board, i, j);
37+
}
38+
}
39+
for (int i = 0; i < board.length; i++) {
40+
System.arraycopy(copy[i], 0, board[i], 0, cCnt);
41+
}
42+
}
43+
44+
private int calc(int[][] grid, int r, int c) {
45+
int[][] dir = new int[][]{
46+
{-1, -1}, {-1, 0}, {-1, 1},
47+
{0, -1}, {0, 1},
48+
{1, -1}, {1, 0}, {1, 1}
49+
};
50+
int cnt = 0;
51+
int val = grid[r][c];
52+
for (int[] offset : dir) {
53+
int nr = r + offset[0];
54+
int nc = c + offset[1];
55+
if (nr >= 0 && nr < grid.length && nc >= 0 && nc < grid[0].length) {
56+
if (grid[nr][nc] == 1) {
57+
cnt++;
58+
}
59+
}
60+
}
61+
62+
int next;
63+
if (val == 1)
64+
next = cnt == 2 || cnt == 3 ? 1 : 0;
65+
else
66+
next = cnt == 3 ? 1 : 0;
67+
68+
return next;
69+
}
70+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package org.sean.array;
2+
3+
import java.util.List;
4+
5+
/***
6+
* 524. Longest Word in Dictionary through Deleting
7+
*/
8+
public class LongestWordFinder {
9+
// two pointers : O(m*n)
10+
private boolean match(String text, String pat) {
11+
if (text.length() < pat.length())
12+
return false;
13+
14+
int i = 0;
15+
int cnt = 0;
16+
int startPos = 0;
17+
int len = text.length();
18+
while (i < pat.length()){
19+
char ch = pat.charAt(i);
20+
while (startPos < len) {
21+
if(text.charAt(startPos) == ch) {
22+
break;
23+
}
24+
startPos++;
25+
}
26+
27+
if(startPos == len) {
28+
// not found
29+
break;
30+
}
31+
startPos++;
32+
cnt++;
33+
i++;
34+
}
35+
return cnt == pat.length();
36+
}
37+
38+
public String findLongestWord(String s, List<String> dictionary) {
39+
dictionary.sort((o1, o2) -> {
40+
if (o1.length() == o2.length())
41+
return o1.compareTo(o2);
42+
else
43+
return Integer.compare(o2.length(), o1.length());
44+
});
45+
46+
for (String word : dictionary) {
47+
if(match(s, word))
48+
return word;
49+
}
50+
return "";
51+
}
52+
53+
// LCS O(m*k*n) TLE
54+
private int lcs(String text, String pat) {
55+
int rowLen = text.length();
56+
int colLen = pat.length();
57+
int[][] dp = new int[rowLen+1][colLen + 1];
58+
59+
for (int i = 1; i <= rowLen; i++) {
60+
for (int j = 1; j <= colLen; j++) {
61+
int adjacentMax = Math.max(dp[i][j - 1], dp[i - 1][j]);
62+
if (text.charAt(i-1) == pat.charAt(j-1)) {
63+
dp[i][j] = Math.max(adjacentMax, dp[i - 1][j - 1] + 1);
64+
}else {
65+
dp[i][j] = adjacentMax;
66+
}
67+
}
68+
}
69+
return dp[rowLen][colLen];
70+
}
71+
72+
public String findLongestWord0(String s, List<String> dictionary) {
73+
dictionary.sort((o1, o2) -> {
74+
if (o1.length() == o2.length())
75+
return o1.compareTo(o2);
76+
else
77+
return Integer.compare(o2.length(), o1.length());
78+
});
79+
80+
for (String word : dictionary) {
81+
int lcsLen = lcs(s, word);
82+
if(lcsLen == word.length())
83+
return word;
84+
}
85+
return "";
86+
}
87+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.sean.array;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import static org.junit.Assert.*;
7+
8+
public class LifeGameTest {
9+
10+
private LifeGame game;
11+
12+
@Before
13+
public void setUp() throws Exception {
14+
game = new LifeGame();
15+
}
16+
17+
@Test
18+
public void gameOfLife() {
19+
int[][] matrix = {
20+
{0, 1, 0},
21+
{0, 0, 1},
22+
{1, 1, 1},
23+
{0, 0, 0}
24+
};
25+
int[][] next = {
26+
{0, 0, 0},
27+
{1, 0, 1},
28+
{0, 1, 1},
29+
{0, 1, 0}
30+
};
31+
32+
game.gameOfLife(matrix);
33+
assertEquals(next, matrix);
34+
}
35+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package org.sean.array;
2+
3+
import org.junit.Before;
4+
import org.junit.Test;
5+
6+
import java.util.Arrays;
7+
8+
import static org.junit.Assert.*;
9+
10+
public class LongestWordFinderTest {
11+
12+
private LongestWordFinder finder;
13+
14+
@Before
15+
public void setUp() throws Exception {
16+
finder = new LongestWordFinder();
17+
}
18+
19+
@Test
20+
public void findLongestWord() {
21+
String longestWord = finder.findLongestWord("abpcplea", Arrays.asList("ale", "apple", "monkey", "plea"));
22+
assertEquals("apple", longestWord);
23+
}
24+
25+
@Test
26+
public void testSingle() {
27+
String longestWord = finder.findLongestWord("abpcplea", Arrays.asList("a", "b", "c"));
28+
assertEquals("a", longestWord);
29+
}
30+
}

0 commit comments

Comments
 (0)