Skip to content

Commit 4acfa7b

Browse files
authored
Merge pull request #805 from forest000014/main
[forest000014] Week 4
2 parents 8d48b33 + 336cce3 commit 4acfa7b

File tree

5 files changed

+201
-0
lines changed

5 files changed

+201
-0
lines changed

coin-change/forest000014.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
Time Complexity: O(coins.length * amount)
3+
Space Complexity: O(amount)
4+
5+
1 ~ i-1원까지의 최적해를 알고 있다면, i원의 최적해를 구할 때 coins 배열 iteration으로 구할 수 있음
6+
*/
7+
class Solution {
8+
public int coinChange(int[] coins, int amount) {
9+
int c = coins.length;
10+
11+
int[] dp = new int[amount + 1];
12+
Arrays.fill(dp, 99999);
13+
dp[0] = 0;
14+
15+
for (int i = 1; i <= amount; i++) {
16+
for (int j = 0; j < c; j++) {
17+
if (i - coins[j] < 0) {
18+
continue;
19+
}
20+
if (dp[i - coins[j]] >= 0 && dp[i - coins[j]] + 1 < dp[i]) {
21+
dp[i] = dp[i - coins[j]] + 1;
22+
}
23+
}
24+
}
25+
26+
return dp[amount] == 99999 ? -1 : dp[amount];
27+
}
28+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/**
2+
3+
(n = list1.length + list2.length)
4+
시간 복잡도: O(n)
5+
- 최악의 경우 전체 노드 순회
6+
공간 복잡도: O(n)
7+
- 최악의 경우 전체 노드만큼 새 노드 생성
8+
9+
*/
10+
class Solution {
11+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
12+
ListNode head = new ListNode();
13+
ListNode curr = head;
14+
15+
while (list1 != null && list2 != null) {
16+
if (list1.val <= list2.val) {
17+
curr.next = new ListNode(list1.val);
18+
curr = curr.next;
19+
list1 = list1.next;
20+
} else {
21+
curr.next = new ListNode(list2.val);
22+
curr = curr.next;
23+
list2 = list2.next;
24+
}
25+
}
26+
27+
if (list1 == null) {
28+
curr.next = list2;
29+
} else if (list2 == null) {
30+
curr.next = list1;
31+
}
32+
33+
return head.next;
34+
}
35+
}

missing-number/forest000014.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
시간 복잡도: O(n)
3+
공간 복잡도: O(1)
4+
5+
1 ~ n의 합이 n * (n + 1) / 2 라는 점을 활용
6+
*/
7+
class Solution {
8+
public int missingNumber(int[] nums) {
9+
int n = nums.length;
10+
int ans = n * (n + 1) / 2;
11+
for (int i = 0; i < n; i++) {
12+
ans -= nums[i];
13+
}
14+
return ans;
15+
}
16+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
time complexity: O(n^2)
3+
space complexity: O(1)
4+
5+
모든 가능한 조합(O(n^2))에 대해 palindrome 여부를 검사(O(n))하는 brute force는 O(n^3).
6+
i번째 문자에서 시작하여, 앞/뒤로 한 글자씩 늘려가면서 탐색하되, 한번이라도 앞/뒤 글자가 서로 다르다면 그 이후는 탐색하지 않을 수 있음. 이 경우는 O(n^2).
7+
*/
8+
class Solution {
9+
public int countSubstrings(String s) {
10+
int ans = 0;
11+
for (int i = 0; i < s.length(); i++) {
12+
int head = i, tail = i;
13+
while (head >= 0 && tail < s.length()) {
14+
if (s.charAt(head) == s.charAt(tail)) {
15+
ans++;
16+
} else {
17+
break;
18+
}
19+
head--;
20+
tail++;
21+
}
22+
23+
head = i;
24+
tail = i + 1;
25+
while (head >= 0 && tail < s.length()) {
26+
if (s.charAt(head) == s.charAt(tail)) {
27+
ans++;
28+
} else {
29+
break;
30+
}
31+
head--;
32+
tail++;
33+
}
34+
}
35+
36+
return ans;
37+
}
38+
}

word-search/forest000014.java

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
Time Complexity: O(m * n * 4^(word.length))
3+
Space Complexity: O(m * n)
4+
*/
5+
6+
class Solution {
7+
boolean[][] visited;
8+
int m, n;
9+
int len;
10+
int[] dr = {-1, 0, 1, 0}; // clockwise traversal
11+
int[] dc = {0, 1, 0, -1};
12+
char board2[][];
13+
String word2;
14+
15+
public boolean exist(char[][] board, String word) {
16+
int[] cnt = new int[52];
17+
board2 = board;
18+
word2 = word;
19+
m = board.length;
20+
n = board[0].length;
21+
visited = new boolean[m][n];
22+
len = word.length();
23+
24+
// 1. for pruning, count characters in board and word respectively
25+
for (int i = 0; i < m; i++) {
26+
for (int j = 0; j < n; j++) {
27+
cnt[charToInt(board[i][j])]++;
28+
}
29+
}
30+
for (int i = 0; i < len; i++) {
31+
int idx = charToInt(word.charAt(i));
32+
if (--cnt[idx] < 0) {
33+
return false;
34+
}
35+
}
36+
37+
// 2. DFS
38+
for (int i = 0; i < m; i++) {
39+
for (int j = 0; j < n; j++) {
40+
if (board2[i][j] != word.charAt(0)) {
41+
continue;
42+
}
43+
if (dfs(i, j, 1)) {
44+
return true;
45+
}
46+
}
47+
}
48+
return false;
49+
}
50+
51+
private boolean dfs(int row, int col, int idx) {
52+
if (idx == len) { // end of word
53+
return true;
54+
}
55+
visited[row][col] = true;
56+
57+
for (int i = 0; i < 4; i++) {
58+
int nr = row + dr[i];
59+
int nc = col + dc[i];
60+
if (nr < 0 || nr >= m || nc < 0 || nc >= n) { // check boundary of the board
61+
continue;
62+
}
63+
if (visited[nr][nc]) { // check visited
64+
continue;
65+
}
66+
if (board2[nr][nc] == word2.charAt(idx)) {
67+
if (dfs(nr, nc, idx + 1)) {
68+
return true;
69+
}
70+
}
71+
}
72+
73+
visited[row][col] = false;
74+
return false;
75+
}
76+
77+
private int charToInt(char ch) {
78+
if (ch <= 'Z') {
79+
return ch - 'A';
80+
} else {
81+
return ch - 'a' + 26;
82+
}
83+
}
84+
}

0 commit comments

Comments
 (0)