From 1d417b66d00d8d20dd722d576eeac15c1fea5af1 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Fri, 3 Jan 2025 14:43:12 +0900 Subject: [PATCH 1/6] feat : coin-change --- coin-change/ekgns33.java | 48 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 coin-change/ekgns33.java diff --git a/coin-change/ekgns33.java b/coin-change/ekgns33.java new file mode 100644 index 000000000..35cb39760 --- /dev/null +++ b/coin-change/ekgns33.java @@ -0,0 +1,48 @@ +/* +input : array of integers each element represents coins, single integer amount +output : fewest number of coin to make up the amount +constraint +1) elements are positive? +yes +2) coins are in integer range? +yes +3) range of amoutn +integer range? +[0, 10^4] +4) at least one valid answer exists? +nope. if there no exist than return -1 + +edge. case +1) if amount == 0 return 0; + + solution 1) top-down approach + +amount - each coin + until amount == 0 + return min +tc : O(n * k) when n is amount, k is unique coin numbers +sc : O(n) call stack + +solution 2) bottom-up +tc : O(n*k) +sc : O(n) +let dp[i] the minimum number of coins to make up amount i + + */ +class Solution { + public int coinChange(int[] coins, int amount) { + //edge + if(amount == 0) return 0; + int[] dp = new int[amount+1]; + dp[0] = 0; + for(int i = 1; i<= amount; i++) { + dp[i] = amount+1; + for(int coin: coins) { + if(i - coin >= 0) { + dp[i] = Math.min(dp[i], dp[i-coin] + 1); + } + } + } + return dp[amount] == amount+1 ? -1 : dp[amount]; + } +} From 8e079e018c5ac6509b328ba7059569649baa5752 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Fri, 3 Jan 2025 14:43:22 +0900 Subject: [PATCH 2/6] feat : merge-two-sorted-lists --- merge-two-sorted-lists/ekgns33.java | 58 +++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 merge-two-sorted-lists/ekgns33.java diff --git a/merge-two-sorted-lists/ekgns33.java b/merge-two-sorted-lists/ekgns33.java new file mode 100644 index 000000000..7cf1b02fd --- /dev/null +++ b/merge-two-sorted-lists/ekgns33.java @@ -0,0 +1,58 @@ +/** + * 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; } + * } + */ +/** + input : two sorted integer linked list + output : merged single linked list with sorted order + constraints: + 1) both linked list can be empty? + yes + 2) both given lists are sorted? + yes + edge : + 1) if both list are empty return null + 2) if one of input lists is empty return the other one + + solution 1) + using two pointer + + compare the current pointer's value. + get the smaller one, set to the next node + + until both pointer reach the end + + tc : O(n) sc : O(1) + */ +class Solution { + public ListNode mergeTwoLists(ListNode list1, ListNode list2) { + ListNode dummy = new ListNode(); + ListNode p1 = list1; + ListNode p2 = list2; + ListNode currNode = dummy; + while(p1 != null && p2 != null) { + if(p1.val < p2.val){ + currNode.next = p1; + p1 = p1.next; + } else { + currNode.next = p2; + p2 = p2.next; + } + currNode = currNode.next; + } + + if(p1 == null) { + currNode.next = p2; + } else { + currNode.next = p1; + } + + return dummy.next; + } +} From 2fa579be097732cc303eaef5bf0159b0e951d602 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Fri, 3 Jan 2025 14:43:31 +0900 Subject: [PATCH 3/6] feat : missing-number --- missing-number/ekgns33.java | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 missing-number/ekgns33.java diff --git a/missing-number/ekgns33.java b/missing-number/ekgns33.java new file mode 100644 index 000000000..1cfcb2a5b --- /dev/null +++ b/missing-number/ekgns33.java @@ -0,0 +1,35 @@ +/** + input : integer of array + output : return the only missing number + constraints: + 1) is there any duplicate? + no. + 2) range of element + [0, n] + + solution 1) + iterate through the array + save to hash set + iterate i from 0 to n + check if exists + + tc : O(n) sc : O(n) + + solution 2) + iterate throught the array + add all the elements + get sum of integer sequence 0 .. n with. n(n+1)/2 + get subtraction + tc : O(n) sc : O(1) + */ +class Solution { + public int missingNumber(int[] nums) { + int sum = 0; + int n = nums.length; + for(int num : nums) { + sum += num; + } + int totalSum = (n+1) * n / 2; + return totalSum - sum; + } +} From 0d8c748215035d2702800dd6ba3a876e837d7143 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Fri, 3 Jan 2025 14:43:41 +0900 Subject: [PATCH 4/6] feat : palindromic-substrings --- palindromic-substrings/ekgns33.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 palindromic-substrings/ekgns33.java diff --git a/palindromic-substrings/ekgns33.java b/palindromic-substrings/ekgns33.java new file mode 100644 index 000000000..b20e5f5e9 --- /dev/null +++ b/palindromic-substrings/ekgns33.java @@ -0,0 +1,24 @@ +/* +input : string s +output: the number of palindromic substrings of given string +tc : O(n^2) sc : o(n^2) when n is the length of string s + +optimize? +maybe nope.. atleast n^2 to select i and j + + */ +class Solution { + public int countSubstrings(String s) { + int n = s.length(); + int cnt = 0; + boolean[][] dp = new boolean[n][n]; + for(int i = n-1; i >= 0; i--) { + for(int j = i; j < n; j++) { + dp[i][j] = s.charAt(i) == s.charAt(j) && (j-i +1 < 3 || dp[i+1][j-1]); + if(dp[i][j]) cnt++; + } + } + return cnt; + } +} +// Compare this snippet from valid-palindrome-ii/ekgns33.java: \ No newline at end of file From 91a7dfb206ee45283cde4238a55f709370614085 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Fri, 3 Jan 2025 14:43:51 +0900 Subject: [PATCH 5/6] feat : word-search --- word-search/ekgns33.java | 58 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 word-search/ekgns33.java diff --git a/word-search/ekgns33.java b/word-search/ekgns33.java new file mode 100644 index 000000000..631b7b781 --- /dev/null +++ b/word-search/ekgns33.java @@ -0,0 +1,58 @@ +/* +input : m x n matrix and string word +output : return true if given word can be constructed + +solution 1) brute force +tc : O(n * 4^k) when n is the number of cells, k is the length of word +sc : O(k) call stack + */ +class Solution { + private int[][] directions = new int[][] {{-1,0}, {1,0}, {0,1}, {0,-1}}; + public boolean exist(char[][] board, String word) { + //edge case + int m = board.length; + int n = board[0].length; + + if(m * n < word.length()) return false; + + + //look for the starting letter and do dfs + for(int i = 0; i < m; i++) { + + for(int j = 0; j < n; j++) { + + if(board[i][j] == word.charAt(0)) { + //do dfs and get answer + board[i][j] = '0'; + boolean res = dfsHelper(board, word, i, j, 1); + board[i][j] = word.charAt(0); + if(res) return true; + } + } + } + + return false; + } + + public boolean dfsHelper(char[][] board, String word, int curR, int curC, int curP) { + + //endclause + if(curP == word.length()) return true; + + boolean ret = false; + + for(int[] direction : directions) { + int nextR = curR + direction[0]; + int nextC = curC + direction[1]; + + if(nextR < 0 || nextR >= board.length || nextC < 0 || nextC >= board[0].length) continue; + + if(board[nextR][nextC] == word.charAt(curP)) { + board[nextR][nextC] = '0'; + ret = ret || dfsHelper(board, word, nextR, nextC, curP + 1); + board[nextR][nextC] = word.charAt(curP); + } + } + return ret; + } +} From ceb26c54031f15ec757916dadcc2a03356cf96b9 Mon Sep 17 00:00:00 2001 From: ekgns33 Date: Fri, 3 Jan 2025 14:47:33 +0900 Subject: [PATCH 6/6] fix : lint error --- palindromic-substrings/ekgns33.java | 1 - 1 file changed, 1 deletion(-) diff --git a/palindromic-substrings/ekgns33.java b/palindromic-substrings/ekgns33.java index b20e5f5e9..5fa4dcdfa 100644 --- a/palindromic-substrings/ekgns33.java +++ b/palindromic-substrings/ekgns33.java @@ -21,4 +21,3 @@ public int countSubstrings(String s) { return cnt; } } -// Compare this snippet from valid-palindrome-ii/ekgns33.java: \ No newline at end of file