Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading