Skip to content

Commit

Permalink
feat : week 4
Browse files Browse the repository at this point in the history
  • Loading branch information
imsosleepy committed Dec 31, 2024
1 parent 08a41dd commit 5b4af6b
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 0 deletions.
19 changes: 19 additions & 0 deletions coin-change/imsosleepy.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// ๋น„์Šทํ•œ ๋ฌธ์ œ๋ฅผ ํ‘ผ ์ ์ด ์žˆ์–ด์„œ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐ
// https://www.acmicpc.net/problem/2294
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];
}
}
16 changes: 16 additions & 0 deletions merge-two-sorted-lists/imsosleepy.java
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;
}
}
}
34 changes: 34 additions & 0 deletions missing-number/imsosleepy.java
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;
}
}
44 changes: 44 additions & 0 deletions word-search/imsosleepy.java
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;
}
}

0 comments on commit 5b4af6b

Please sign in to comment.