-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #822 from imsosleepy/main
[ackku] Week 4
- Loading branch information
Showing
5 changed files
with
146 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// ๋น์ทํ ๋ฌธ์ ๋ฅผ ํผ ์ ์ด ์์ด์ ์ฝ๊ฒ ํด๊ฒฐ | ||
// https://www.acmicpc.net/problem/2294 | ||
// O(N * amount) ์๊ฐ๋ณต์ก๋๊ฐ ๋ฐฐ์ด ํฌ๊ธฐ์ amount์ ์ข ์๋๋ค. | ||
// dp[N]๋ง ์ฌ์ฉํ๋ฏ๋ก ๊ณต๊ฐ๋ณต์ก๋๋ O(N) | ||
class Solution { | ||
public int coinChange(int[] coins, int amount) { | ||
int[] dp = new int[amount + 1]; | ||
Arrays.fill(dp, amount + 1); ๋ถ๊ฐ๋ฅํ ํฐ ๊ฐ | ||
dp[0] = 0; | ||
|
||
for (int i = 1; i <= amount; i++) { | ||
for (int coin : coins) { | ||
if (i >= coin) { | ||
dp[i] = Math.min(dp[i], dp[i - coin] + 1); | ||
} | ||
} | ||
} | ||
|
||
return dp[amount] > amount ? -1 : dp[amount]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// ์ฒ์์ ์ฌ๋ฌ ์กฐ๊ฑด์ ๋ถ์์ผ๋ ๊ธฐ๋ณธ์ ์ธ ์กฐ๊ฑด๋ง ํ์ํ๋ค. | ||
// ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ๊ฐ๋ ์ ๋ค์์ ์ด๋ํ ๊ณณ์ด ์ด๋์ง๋ง ์๋ ค์ฃผ๋ฉด ๋จ | ||
class Solution { | ||
public ListNode mergeTwoLists(ListNode list1, ListNode list2) { | ||
if (list1 == null) return list2; | ||
if (list2 == null) return list1; | ||
|
||
if (list1.val <= list2.val) { | ||
list1.next = mergeTwoLists(list1.next, list2); | ||
return list1; | ||
} else { | ||
list2.next = mergeTwoLists(list1, list2.next); | ||
return list2; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// GPT์ ๋์์ ๋ฐ์ ๊ฒฐ๊ณผ, ์ํ์ ์ผ๋ก ์ ๊ทผํ๋ฉด ๋๋ค. | ||
// ๋ชจ๋ ๊ฐ์ ์ ๋ํฌํ๊ณ nums ๋ฐฐ์ด ์ฌ์ด์ฆ์ธ n์ ์ง์ผ์ฃผ๊ธฐ ๋๋ฌธ์ ๊ฐ๋ฅํ ๊ฒฐ๊ณผ | ||
class Solution { | ||
public int missingNumber(int[] nums) { | ||
int n = nums.length; | ||
int expected = n * (n + 1) / 2; | ||
int actual = 0; | ||
for (int num : nums) { | ||
actual += num; | ||
} | ||
return expected - actual; | ||
} | ||
} | ||
|
||
// ์๊ฐ๋ณต์ก๋๋ O(N)์ผ๋ก ๋จ์ด์ง๋ค. | ||
// ๊ณต๊ฐ๋ณต์ก๋๊ฐ nums ๋ฐฐ์ด ์ฌ์ด์ฆ์ ์ข ์๋์ O(N)์ด๋ค. | ||
// Accepted๊ฐ ๋์ง๋ง, ๋ค๋ฅธ ๋ฐฉ๋ฒ์ ์ฐพ์๋ด์ผํจ | ||
class Solution { | ||
public int missingNumber(int[] nums) { | ||
boolean[] existCheck = new boolean[nums.length + 1]; | ||
|
||
for (int num : nums) { | ||
existCheck[num] = true; | ||
} | ||
|
||
for (int i = 0; i < existCheck.length; i++) { | ||
if (!existCheck[i]) { | ||
return i; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// O(N^2) ์ด ๋์ฌ ์๋ฐ์ ์๋ ๋ฌธ์ . ์ด๋ฐ ๋ฌธ์ ์ ํน์ง์ N์ ํฌ๊ธฐ๊ฐ ์๋ค. | ||
// ์ด๋ฒ๋ฌธ์ ๋ N์ ํฌ๊ธฐ๊ฐ 1000์ผ๋ก ์ฃผ์ด์ก์๋, ์ด์ฐจ์ for๋ฌธ์ด ํ์ฉ๋๋ค๋๊ฑธ ๊ฐ์ ์ ์ผ๋ก ์์์ฑ ์ ์๋ค. | ||
// ์ด์ฐจ์ ๋ฐฐ์ด ์ด๋ฏ๋ก ๊ณต๊ฐ๋ณต์ก๋๋ O(N^2) | ||
class Solution { | ||
public int countSubstrings(String s) { | ||
int n = s.length(); | ||
boolean[][] dp = new boolean[n][n]; | ||
int count = 0; | ||
|
||
for (int i = 0; i < n; i++) { | ||
dp[i][i] = true; | ||
count++; | ||
} | ||
|
||
for (int len = 2; len <= n; len++) { | ||
for (int i = 0; i <= n - len; i++) { // ์์ ์์น | ||
int j = i + len - 1; // ๋ ์์น | ||
|
||
// ์ ๋ ๋ฌธ์๊ฐ ๊ฐ๊ณ , ๋ด๋ถ๊ฐ ํ๋ฌธ์ด๊ฑฐ๋ ๊ธธ์ด๊ฐ 2์ธ ๊ฒฝ์ฐ | ||
if (s.charAt(i) == s.charAt(j)) { | ||
if (len == 2 || dp[i + 1][j - 1]) { | ||
dp[i][j] = true; | ||
count++; | ||
} | ||
} | ||
} | ||
} | ||
|
||
return count; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// 189ms๊ฐ ๋์์ ๋ค์ ์๋ํด๋ณผ ์์ | ||
class Solution { | ||
private int[] rowMove = {1, -1, 0, 0}; | ||
private int[] colMove = {0, 0, 1, -1}; | ||
|
||
public boolean exist(char[][] board, String word) { | ||
int rows = board.length; | ||
int cols = board[0].length; | ||
|
||
for (int i = 0; i < rows; i++) { | ||
for (int j = 0; j < cols; j++) { | ||
if (dfs(board, word, i, j, 0)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
|
||
private boolean dfs(char[][] board, String word, int row, int col, int index) { | ||
if (index == word.length()) { | ||
return true; | ||
} | ||
|
||
if (row < 0 || col < 0 || row >= board.length || col >= board[0].length || board[row][col] != word.charAt(index)) { | ||
return false; | ||
} | ||
|
||
char temp = board[row][col]; | ||
board[row][col] = '#'; | ||
|
||
for (int i = 0; i < 4; i++) { | ||
int newRow = row + rowMove[i]; | ||
int newCol = col + colMove[i]; | ||
if (dfs(board, word, newRow, newCol, index + 1)) { | ||
return true; | ||
} | ||
} | ||
|
||
board[row][col] = temp; | ||
|
||
return false; | ||
} | ||
} |