Skip to content

Commit 8887013

Browse files
authored
Merge pull request #2233 from chjung99/main
[chjung99] WEEK 07 solutions
2 parents 99f1d53 + 057a0d1 commit 8887013

File tree

5 files changed

+216
-0
lines changed

5 files changed

+216
-0
lines changed

decode-ways/chjung99.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
class Solution {
2+
int[] numOfWays = new int[101];
3+
4+
public int numDecodings(String s) {
5+
return dfs(s, 0);
6+
}
7+
8+
public int dfs(String s, int cur) {
9+
if (cur == s.length()) {
10+
return 1;
11+
}
12+
13+
int cnt = 0;
14+
15+
if (cur + 1 <= s.length() && isDecodable(s.substring(cur, cur + 1))){
16+
if (numOfWays[cur + 1] == 0){
17+
numOfWays[cur + 1] = dfs(s, cur + 1);
18+
}
19+
cnt += numOfWays[cur + 1];
20+
}
21+
22+
if (cur + 2 <= s.length()&& isDecodable(s.substring(cur, cur + 2))) {
23+
if (numOfWays[cur + 2] == 0){
24+
numOfWays[cur + 2] = dfs(s, cur + 2);
25+
}
26+
cnt += numOfWays[cur + 2];
27+
}
28+
29+
return cnt;
30+
}
31+
32+
public boolean isDecodable(String s) {
33+
if (s.startsWith("0")){
34+
return false;
35+
}
36+
int digit = Integer.valueOf(s);
37+
return (digit >= 1 && digit <= 26);
38+
}
39+
}
40+
41+

insert-interval/chjung99.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
class Solution {
2+
public int[][] insert(int[][] intervals, int[] newInterval) {
3+
Deque<Interval> deque = new ArrayDeque<>();
4+
List<Interval> ordered = new ArrayList<>();
5+
List<Interval> merged = new ArrayList<>();
6+
7+
for (int i = 0; i < intervals.length; i++){
8+
ordered.add(new Interval(intervals[i]));
9+
}
10+
11+
ordered.add(new Interval(newInterval));
12+
Collections.sort(ordered);
13+
14+
Interval cur;
15+
Interval next;
16+
17+
int curIdx = 0;
18+
deque.add(ordered.get(0));
19+
20+
while (curIdx < ordered.size()){
21+
cur = deque.removeLast();
22+
23+
if (curIdx + 1 < ordered.size()){
24+
next = ordered.get(curIdx + 1);
25+
if (next.start <= cur.end){ // merge
26+
Interval mergedInterval = new Interval( new int[]{
27+
Math.min(cur.start, next.start), Math.max(cur.end, next.end)
28+
});
29+
deque.addLast(mergedInterval);
30+
} else {
31+
deque.addLast(cur);
32+
deque.addLast(next);
33+
}
34+
35+
} else {
36+
deque.addLast(cur);
37+
}
38+
curIdx ++;
39+
}
40+
int[][] answer = new int[deque.size()][2];
41+
int idx = 0;
42+
while (!deque.isEmpty()){
43+
answer[idx++] = deque.removeFirst().raw;
44+
}
45+
return answer;
46+
}
47+
class Interval implements Comparable<Interval>{
48+
int[] raw;
49+
int start;
50+
int end;
51+
52+
public Interval(int[] raw){
53+
this.raw = raw;
54+
this.start = raw[0];
55+
this.end = raw[1];
56+
}
57+
58+
@Override
59+
public int compareTo(Interval o){
60+
return Integer.compare(this.start, o.start);
61+
}
62+
}
63+
}
64+
65+

set-matrix-zeroes/chjung99.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
class Solution {
2+
int m;
3+
int n;
4+
public void setZeroes(int[][] matrix) {
5+
m = matrix.length;
6+
n = matrix[0].length;
7+
8+
Set<Integer> rows = new HashSet<>();
9+
Set<Integer> cols = new HashSet<>();
10+
11+
for (int i = 0; i < m; i++){
12+
for (int j = 0; j < n; j++){
13+
if (matrix[i][j] == 0){
14+
rows.add(i);
15+
cols.add(j);
16+
}
17+
}
18+
}
19+
20+
for (int row : rows) {
21+
setZeroRow(row, matrix);
22+
}
23+
24+
for (int col : cols) {
25+
setZeroCol(col, matrix);
26+
}
27+
}
28+
29+
public void setZeroCol(int col, int[][] matrix){
30+
for (int i = 0; i < m; i++) {
31+
matrix[i][col] = 0;
32+
}
33+
}
34+
35+
public void setZeroRow(int row, int[][] matrix){
36+
for (int i = 0; i < n; i++) {
37+
matrix[row][i] = 0;
38+
}
39+
}
40+
}
41+
42+

unique-paths/chjung99.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
3+
public int uniquePaths(int m, int n) {
4+
int[][] dp = new int[m+1][n+1]; // (1,1) ~ (m, n)
5+
6+
for (int i = 1; i <= n; i++){
7+
dp[1][i] = 1;
8+
}
9+
for (int i = 2; i <= m; i++){
10+
for (int j = 1; j <= n; j++){
11+
dp[i][j] = dp[i][j-1] + dp[i-1][j];
12+
}
13+
}
14+
return dp[m][n];
15+
}
16+
}
17+
18+

word-search/chjung99.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
class Solution {
2+
int m, n;
3+
int[] dx = new int[]{0, 1, 0, -1};
4+
int[] dy = new int[]{1, 0, -1, 0};
5+
boolean answer = false;
6+
boolean[][] visit;
7+
8+
public boolean exist(char[][] board, String word) {
9+
m = board.length;
10+
n = board[0].length;
11+
visit = new boolean[m][n];
12+
13+
for (int i = 0; i < m; i++){
14+
for (int j = 0; j < n; j++){
15+
if (board[i][j] == word.charAt(0)){
16+
visit[i][j] = true;
17+
dfs(i, j, board, word, 0);
18+
visit[i][j] = false;
19+
}
20+
}
21+
}
22+
return answer;
23+
}
24+
25+
public void dfs(int cx, int cy, char[][] board, String word, int depth){
26+
if (depth == word.length()-1){
27+
answer = true;
28+
return;
29+
}
30+
31+
for (int i = 0; i < 4; i++){
32+
int nx = cx + dx[i];
33+
int ny = cy + dy[i];
34+
int nextDepth = depth + 1;
35+
36+
if (outOfRange(nx, ny)||visit[nx][ny]) continue;
37+
if (board[nx][ny] == word.charAt(nextDepth)){
38+
visit[nx][ny] = true;
39+
dfs(nx, ny, board, word, nextDepth);
40+
visit[nx][ny] = false;
41+
}
42+
}
43+
}
44+
45+
public boolean outOfRange(int x, int y){
46+
return (x < 0 || x >= m) || (y < 0 || y >= n);
47+
}
48+
}
49+
50+

0 commit comments

Comments
 (0)