From 5c009e64008b9048739bb781e7d89a0e695b20bb Mon Sep 17 00:00:00 2001 From: sora0319 Date: Wed, 23 Apr 2025 00:01:10 +0900 Subject: [PATCH 1/7] merged two sorted list solved --- merge-two-sorted-lists/sora0319.java | 39 ++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 merge-two-sorted-lists/sora0319.java diff --git a/merge-two-sorted-lists/sora0319.java b/merge-two-sorted-lists/sora0319.java new file mode 100644 index 000000000..a8a84fd1c --- /dev/null +++ b/merge-two-sorted-lists/sora0319.java @@ -0,0 +1,39 @@ +/** + * Definition for singly-linked list. + * public class ListNode { + * int val; + * ListNode next; + * ListNode() {} + * ListNode(int val) { this.val = val; } + * ListNode(int val, ListNode next) { this.val = val; this.next = next; } + * } + */ +class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + ListNode answer = new ListNode(0, null); + ListNode merged = answer; + + while(list1 != null || list2 != null){ + if(list1 == null){ + merged.next = list2; + break; + } + if(list2 == null){ + merged.next = list1; + break; + } + if(list1.val <= list2.val){ + merged.next = list1; + list1 = list1.next; + } + else{ + merged.next = list2; + list2 = list2.next; + } + + merged = merged.next; + } + + return answer.next; + } +} \ No newline at end of file From 9cbb1eb721f161d67ca19a4a31c81524233a9de8 Mon Sep 17 00:00:00 2001 From: sora0319 Date: Thu, 24 Apr 2025 23:46:38 +0900 Subject: [PATCH 2/7] maximum depth of binary tree solved --- maximum-depth-of-binary-tree/sora0319.java | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 maximum-depth-of-binary-tree/sora0319.java diff --git a/maximum-depth-of-binary-tree/sora0319.java b/maximum-depth-of-binary-tree/sora0319.java new file mode 100644 index 000000000..16d9dc055 --- /dev/null +++ b/maximum-depth-of-binary-tree/sora0319.java @@ -0,0 +1,9 @@ +class Solution { + public int maxDepth(TreeNode root) { + if(root == null){ + return 0; + } + + return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; + } +} \ No newline at end of file From 40a1ed0966d5b7d8d0b9925188c227d40f752728 Mon Sep 17 00:00:00 2001 From: sora0319 Date: Sat, 26 Apr 2025 21:10:42 +0900 Subject: [PATCH 3/7] add newline --- merge-two-sorted-lists/sora0319.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/merge-two-sorted-lists/sora0319.java b/merge-two-sorted-lists/sora0319.java index a8a84fd1c..7366c2042 100644 --- a/merge-two-sorted-lists/sora0319.java +++ b/merge-two-sorted-lists/sora0319.java @@ -36,4 +36,5 @@ public ListNode mergeTwoLists(ListNode list1, ListNode list2) { return answer.next; } -} \ No newline at end of file +} + From 5d096bdc7479f38ac9d289d8a72eccb1ece1eb64 Mon Sep 17 00:00:00 2001 From: sora0319 Date: Sat, 26 Apr 2025 21:11:35 +0900 Subject: [PATCH 4/7] add newline --- maximum-depth-of-binary-tree/sora0319.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/maximum-depth-of-binary-tree/sora0319.java b/maximum-depth-of-binary-tree/sora0319.java index 16d9dc055..ea1419c8b 100644 --- a/maximum-depth-of-binary-tree/sora0319.java +++ b/maximum-depth-of-binary-tree/sora0319.java @@ -6,4 +6,5 @@ public int maxDepth(TreeNode root) { return Math.max(maxDepth(root.left), maxDepth(root.right)) + 1; } -} \ No newline at end of file +} + From 23d8c7adce6a7f8c97a83afad32628003e302ea3 Mon Sep 17 00:00:00 2001 From: sora0319 Date: Sat, 26 Apr 2025 21:13:12 +0900 Subject: [PATCH 5/7] find minimun in rotated sorted array solved --- .../sora0319.java | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 find-minimum-in-rotated-sorted-array/sora0319.java diff --git a/find-minimum-in-rotated-sorted-array/sora0319.java b/find-minimum-in-rotated-sorted-array/sora0319.java new file mode 100644 index 000000000..ce4999cec --- /dev/null +++ b/find-minimum-in-rotated-sorted-array/sora0319.java @@ -0,0 +1,21 @@ +class Solution { + public int findMin(int[] nums) { + int start = 0; + int end = nums.length-1; + + + while(start < end){ + int mid = (start + end) / 2; + + if(nums[mid] < nums[end]){ + end = mid; + } + else{ + start = mid + 1; + } + } + + return nums[start]; + } +} + From 0e4d8c4963afd34ee95007dfd2eb03f8d1ee59ba Mon Sep 17 00:00:00 2001 From: sora0319 Date: Sat, 26 Apr 2025 22:16:42 +0900 Subject: [PATCH 6/7] solve word search --- word-search/sora0319.java | 42 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 word-search/sora0319.java diff --git a/word-search/sora0319.java b/word-search/sora0319.java new file mode 100644 index 000000000..2c3a62aa7 --- /dev/null +++ b/word-search/sora0319.java @@ -0,0 +1,42 @@ +class Solution { + public boolean exist(char[][] board, String word) { + boolean[][] visited = new boolean[board.length][board[0].length]; + boolean status = false; + + for(int i = 0; i < board.length; i++){ + for(int j = 0; j < board[0].length; j++){ + if(word.charAt(0) == board[i][j]){ + visited[i][j] = true; + status = checkingWord(board, word, visited, 1, i, j); + visited[i][j] = false; + } + if(status) return true; + } + } + + return false; + } + public boolean checkingWord(char[][] board, String word, boolean[][] visited, int same, int x, int y){ + if(same == word.length()) return true; + int[] mx = {-1,1,0,0}; + int[] my = {0,0,-1,1}; + + for(int k = 0; k < 4; k++){ + int nx = mx[k] + x; + int ny = my[k] + y; + + if(nx < 0 || ny < 0 || nx >= board.length || ny >= board[0].length) continue; + if(visited[nx][ny]) continue; + + boolean status = false; + + if(word.charAt(same) == board[nx][ny]){ + visited[nx][ny] = true; + if(checkingWord(board, word, visited, same + 1, nx, ny)) return true; + visited[nx][ny] = false; + } + } + return false; + } +} + From 0671d92fe4b034acbeef83cd766f71e88d4f294a Mon Sep 17 00:00:00 2001 From: sora0319 Date: Sat, 26 Apr 2025 23:20:05 +0900 Subject: [PATCH 7/7] solve coin change --- coin-change/sora0319.java | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 coin-change/sora0319.java diff --git a/coin-change/sora0319.java b/coin-change/sora0319.java new file mode 100644 index 000000000..e161a8487 --- /dev/null +++ b/coin-change/sora0319.java @@ -0,0 +1,21 @@ +import java.util.Arrays; + +class Solution { + public int coinChange(int[] coins, int amount) { + int[] dp = new int[amount + 1]; + Arrays.fill(dp, amount + 1); // 최대값으로 초기화 (amount + 1은 절대 나올 수 없는 큰 값) + dp[0] = 0; // 0원을 만들 때는 동전 0개 + + for (int i = 1; i <= amount; i++) { + for (int coin : coins) { + if (i - coin >= 0) { + dp[i] = Math.min(dp[i], dp[i - coin] + 1); + } + } + } + + return dp[amount] > amount ? -1 : dp[amount]; + } +} + +