Skip to content

Commit 451c8c5

Browse files
authored
Merge pull request #822 from imsosleepy/main
[ackku] Week 4
2 parents 85b82a7 + 9bfe4f6 commit 451c8c5

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

coin-change/imsosleepy.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// 비슷한 문제를 푼 적이 있어서 쉽게 해결
2+
// https://www.acmicpc.net/problem/2294
3+
// O(N * amount) 시간복잡도가 배열 크기와 amount에 종속된다.
4+
// dp[N]만 사용하므로 공간복잡도는 O(N)
5+
class Solution {
6+
public int coinChange(int[] coins, int amount) {
7+
int[] dp = new int[amount + 1];
8+
Arrays.fill(dp, amount + 1); 불가능한
9+
dp[0] = 0;
10+
11+
for (int i = 1; i <= amount; i++) {
12+
for (int coin : coins) {
13+
if (i >= coin) {
14+
dp[i] = Math.min(dp[i], dp[i - coin] + 1);
15+
}
16+
}
17+
}
18+
19+
return dp[amount] > amount ? -1 : dp[amount];
20+
}
21+
}
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+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// O(N^2) 이 나올 수밖에 없는 문제. 이런 문제의 특징은 N의 크기가 작다.
2+
// 이번문제도 N의 크기가 1000으로 주어졌을때, 이차원 for문이 허용된다는걸 간접적으로 알아챌 수 있다.
3+
// 이차원 배열 이므로 공간복잡도도 O(N^2)
4+
class Solution {
5+
public int countSubstrings(String s) {
6+
int n = s.length();
7+
boolean[][] dp = new boolean[n][n];
8+
int count = 0;
9+
10+
for (int i = 0; i < n; i++) {
11+
dp[i][i] = true;
12+
count++;
13+
}
14+
15+
for (int len = 2; len <= n; len++) {
16+
for (int i = 0; i <= n - len; i++) { // 시작 위치
17+
int j = i + len - 1; // 끝 위치
18+
19+
// 양 끝 문자가 같고, 내부가 회문이거나 길이가 2인 경우
20+
if (s.charAt(i) == s.charAt(j)) {
21+
if (len == 2 || dp[i + 1][j - 1]) {
22+
dp[i][j] = true;
23+
count++;
24+
}
25+
}
26+
}
27+
}
28+
29+
return count;
30+
}
31+
}

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)