Skip to content

Commit

Permalink
Merge pull request #822 from imsosleepy/main
Browse files Browse the repository at this point in the history
[ackku] Week 4
  • Loading branch information
imsosleepy authored Jan 4, 2025
2 parents 85b82a7 + 9bfe4f6 commit 451c8c5
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 0 deletions.
21 changes: 21 additions & 0 deletions coin-change/imsosleepy.java
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];
}
}
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;
}
}
31 changes: 31 additions & 0 deletions palindromic-substrings/imsosleepy.java
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;
}
}
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 451c8c5

Please sign in to comment.