From 9ab7f5fb566f793fa6df47d8ae28820e92337c04 Mon Sep 17 00:00:00 2001 From: Minji Go Date: Mon, 30 Dec 2024 16:51:47 +0900 Subject: [PATCH 1/8] refactor: complexity of combination sum --- combination-sum/minji-go.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/combination-sum/minji-go.java b/combination-sum/minji-go.java index ab895923a..fac01b813 100644 --- a/combination-sum/minji-go.java +++ b/combination-sum/minji-go.java @@ -2,10 +2,8 @@ Problem: https://leetcode.com/problems/combination-sum/ Description: return a list of all unique combinations of candidates where the chosen numbers sum to target Concept: Array, Backtracking - Time Complexity: O(Nⁿ), Runtime 2ms - Space Complexity: O(N), Memory 44.88MB - - - Time Complexity, Space Complexity를 어떻게 계산해야할지 어렵네요 :( + Time Complexity: O(Nᵀ), Runtime 2ms + Space Complexity: O(T), Memory 44.88MB */ class Solution { public List> answer = new ArrayList<>(); From 402d2bdcb6532e621e42bb4e214bc12c7dcfd60c Mon Sep 17 00:00:00 2001 From: Minji Go Date: Mon, 30 Dec 2024 16:52:05 +0900 Subject: [PATCH 2/8] feat: merge two sorted lists --- merge-two-sorted-lists/minji-go.java | 36 ++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 merge-two-sorted-lists/minji-go.java diff --git a/merge-two-sorted-lists/minji-go.java b/merge-two-sorted-lists/minji-go.java new file mode 100644 index 000000000..e701574fa --- /dev/null +++ b/merge-two-sorted-lists/minji-go.java @@ -0,0 +1,36 @@ +/* + Problem: https://leetcode.com/problems/merge-two-sorted-lists/ + Description: return the head of the merged linked list of two sorted linked lists + Concept: Linked List, Recursion + Time Complexity: O(N+M), Runtime 0ms + Space Complexity: O(1), Memory 42.74MB +*/ + +/** + * 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 head = new ListNode(0); + ListNode tail = head; + + while(list1 != null || list2 != null) { + if (list2 == null || (list1 != null && list1.val <= list2.val)) { + tail = tail.next = new ListNode(list1.val); + list1 = list1.next; + } else { + tail = tail.next = new ListNode(list2.val); + list2 = list2.next; + } + } + return head.next; + } +} From 75025a307d786dec1369998a963e1214bdd52832 Mon Sep 17 00:00:00 2001 From: Minji Go Date: Mon, 30 Dec 2024 16:52:21 +0900 Subject: [PATCH 3/8] feat: missing number --- missing-number/minji-go.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 missing-number/minji-go.java diff --git a/missing-number/minji-go.java b/missing-number/minji-go.java new file mode 100644 index 000000000..61cc2894f --- /dev/null +++ b/missing-number/minji-go.java @@ -0,0 +1,19 @@ +/* + Problem: https://leetcode.com/problems/missing-number/ + Description: return the only number in the range that is missing from the array. + Concept: Array, Hash Table, Math, Binary Search, Bit Manipulation, Sorting + Time Complexity: O(N), Runtime 0ms + Space Complexity: O(1), Memory 45.71MB +*/ +class Solution { + public int missingNumber(int[] nums) { + int n = nums.length; + int missing = n; + + for(int i=0; i Date: Tue, 31 Dec 2024 00:32:11 +0900 Subject: [PATCH 4/8] feat: word search --- word-search/minji-go.java | 47 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 word-search/minji-go.java diff --git a/word-search/minji-go.java b/word-search/minji-go.java new file mode 100644 index 000000000..51ca4eed7 --- /dev/null +++ b/word-search/minji-go.java @@ -0,0 +1,47 @@ +/* + Problem: https://leetcode.com/problems/word-search/ + Description: return true if word exists in the grid + Concept: Array, String, Backtracking, Matrix + Time Complexity: O(MN4ᵀ), Runtime 147ms + Space Complexity: O(MN), Memory 42.11MB +*/ +class Solution { + public char[][] board; + public String word; + public boolean[][] visited; + public int n, m; + + public boolean exist(char[][] board, String word) { + this.board = board; + this.word = word; + this.m = board.length; + this.n = board[0].length; + this.visited = new boolean[m][n]; + + for(int i=0; im-1||nc>n-1||visited[nr][nc]) continue; + if(board[nr][nc]!=word.charAt(i)) continue; + if(wordExists(nr, nc, i+1)) return true; + } + visited[cr][cc] = false; + + return false; + } +} From f625aacc3445b24ac5a7d00effc961f73e73de31 Mon Sep 17 00:00:00 2001 From: minji-go Date: Tue, 31 Dec 2024 01:29:58 +0900 Subject: [PATCH 5/8] feat: palindromic substrings --- palindromic-substrings/minji-go.java | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 palindromic-substrings/minji-go.java diff --git a/palindromic-substrings/minji-go.java b/palindromic-substrings/minji-go.java new file mode 100644 index 000000000..cecce3457 --- /dev/null +++ b/palindromic-substrings/minji-go.java @@ -0,0 +1,27 @@ +/* + Problem: https://leetcode.com/problems/palindromic-substrings/ + Description: return the number of palindromic substrings in it. + Concept: Two Pointers, String, Dynamic Programming + Time Complexity: O(N²), Runtime 6ms + Space Complexity: O(1), Memory 41.62MB +*/ +class Solution { + public int countSubstrings(String s) { + int totalCount = 0; + for(int i=0; i=0 && right Date: Fri, 3 Jan 2025 13:47:33 +0900 Subject: [PATCH 6/8] feat: coin change --- coin-change/minji-go.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 coin-change/minji-go.java diff --git a/coin-change/minji-go.java b/coin-change/minji-go.java new file mode 100644 index 000000000..fcc99e4c2 --- /dev/null +++ b/coin-change/minji-go.java @@ -0,0 +1,23 @@ +/* + Problem: https://leetcode.com/problems/coin-change/ + Description: return the fewest number of coins that you need to make up that amount + Concept: Array, Dynamic Programming, Breadth-First Search + Time Complexity: O(N²), Runtime 15ms - N is the amount + Space Complexity: O(N), Memory 44.28MB +*/ +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]; + } +} From ab5674bd09d0748fe131a164c0ec3591f1afe915 Mon Sep 17 00:00:00 2001 From: Minji Go Date: Fri, 3 Jan 2025 17:29:29 +0900 Subject: [PATCH 7/8] refactor: complexity of merge two sorted lists --- merge-two-sorted-lists/minji-go.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/merge-two-sorted-lists/minji-go.java b/merge-two-sorted-lists/minji-go.java index e701574fa..d70eac76f 100644 --- a/merge-two-sorted-lists/minji-go.java +++ b/merge-two-sorted-lists/minji-go.java @@ -3,7 +3,7 @@ Description: return the head of the merged linked list of two sorted linked lists Concept: Linked List, Recursion Time Complexity: O(N+M), Runtime 0ms - Space Complexity: O(1), Memory 42.74MB + Space Complexity: O(N+M), Memory 42.74MB */ /** From d55d0f775bfd62289b8e1b6330b3c7d37b6c8619 Mon Sep 17 00:00:00 2001 From: Minji Go Date: Fri, 3 Jan 2025 17:29:35 +0900 Subject: [PATCH 8/8] refactor: complexity of coin change --- coin-change/minji-go.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/coin-change/minji-go.java b/coin-change/minji-go.java index fcc99e4c2..527a23d8e 100644 --- a/coin-change/minji-go.java +++ b/coin-change/minji-go.java @@ -2,8 +2,8 @@ Problem: https://leetcode.com/problems/coin-change/ Description: return the fewest number of coins that you need to make up that amount Concept: Array, Dynamic Programming, Breadth-First Search - Time Complexity: O(N²), Runtime 15ms - N is the amount - Space Complexity: O(N), Memory 44.28MB + Time Complexity: O(NM), Runtime 15ms - M is the amount + Space Complexity: O(M), Memory 44.28MB */ class Solution { public int coinChange(int[] coins, int amount) {