Skip to content

Commit 5b4af6b

Browse files
committed
feat : week 4
1 parent 08a41dd commit 5b4af6b

File tree

4 files changed

+113
-0
lines changed

4 files changed

+113
-0
lines changed

coin-change/imsosleepy.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// 비슷한 문제를 푼 적이 있어서 쉽게 해결
2+
// https://www.acmicpc.net/problem/2294
3+
class Solution {
4+
public int coinChange(int[] coins, int amount) {
5+
int[] dp = new int[amount + 1];
6+
Arrays.fill(dp, amount + 1); 불가능한
7+
dp[0] = 0;
8+
9+
for (int i = 1; i <= amount; i++) {
10+
for (int coin : coins) {
11+
if (i >= coin) {
12+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
13+
}
14+
}
15+
}
16+
17+
return dp[amount] > amount ? -1 : dp[amount];
18+
}
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// 처음에 여러 조건을 붙였으나 기본적인 조건만 필요하다.
2+
// 가장 기본적인 개념은 다음에 이동할곳이 어딘지만 알려주면 됨
3+
class Solution {
4+
public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
5+
if (list1 == null) return list2;
6+
if (list2 == null) return list1;
7+
8+
if (list1.val <= list2.val) {
9+
list1.next = mergeTwoLists(list1.next, list2);
10+
return list1;
11+
} else {
12+
list2.next = mergeTwoLists(list1, list2.next);
13+
return list2;
14+
}
15+
}
16+
}

missing-number/imsosleepy.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// GPT의 도움을 받은 결과, 수학적으로 접근하면 된다.
2+
// 모든 값은 유니크하고 nums 배열 사이즈인 n을 지켜주기 때문에 가능한 결과
3+
class Solution {
4+
public int missingNumber(int[] nums) {
5+
int n = nums.length;
6+
int expected = n * (n + 1) / 2;
7+
int actual = 0;
8+
for (int num : nums) {
9+
actual += num;
10+
}
11+
return expected - actual;
12+
}
13+
}
14+
15+
// 시간복잡도는 O(N)으로 떨어진다.
16+
// 공간복잡도가 nums 배열 사이즈에 종속되서 O(N)이다.
17+
// Accepted가 되지만, 다른 방법을 찾아봐야함
18+
class Solution {
19+
public int missingNumber(int[] nums) {
20+
boolean[] existCheck = new boolean[nums.length + 1];
21+
22+
for (int num : nums) {
23+
existCheck[num] = true;
24+
}
25+
26+
for (int i = 0; i < existCheck.length; i++) {
27+
if (!existCheck[i]) {
28+
return i;
29+
}
30+
}
31+
32+
return 0;
33+
}
34+
}

word-search/imsosleepy.java

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// 189ms가 나와서 다시 시도해볼 예정
2+
class Solution {
3+
private int[] rowMove = {1, -1, 0, 0};
4+
private int[] colMove = {0, 0, 1, -1};
5+
6+
public boolean exist(char[][] board, String word) {
7+
int rows = board.length;
8+
int cols = board[0].length;
9+
10+
for (int i = 0; i < rows; i++) {
11+
for (int j = 0; j < cols; j++) {
12+
if (dfs(board, word, i, j, 0)) {
13+
return true;
14+
}
15+
}
16+
}
17+
return false;
18+
}
19+
20+
private boolean dfs(char[][] board, String word, int row, int col, int index) {
21+
if (index == word.length()) {
22+
return true;
23+
}
24+
25+
if (row < 0 || col < 0 || row >= board.length || col >= board[0].length || board[row][col] != word.charAt(index)) {
26+
return false;
27+
}
28+
29+
char temp = board[row][col];
30+
board[row][col] = '#';
31+
32+
for (int i = 0; i < 4; i++) {
33+
int newRow = row + rowMove[i];
34+
int newCol = col + colMove[i];
35+
if (dfs(board, word, newRow, newCol, index + 1)) {
36+
return true;
37+
}
38+
}
39+
40+
board[row][col] = temp;
41+
42+
return false;
43+
}
44+
}

0 commit comments

Comments
 (0)