From 835ce9f18d7f6b011ef9cf9afc18949d7ab329ea Mon Sep 17 00:00:00 2001 From: forest000014 Date: Mon, 6 Jan 2025 05:49:19 +0900 Subject: [PATCH 1/5] Best Time to Buy and Sell Stock --- .../forest000014.java | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/forest000014.java diff --git a/best-time-to-buy-and-sell-stock/forest000014.java b/best-time-to-buy-and-sell-stock/forest000014.java new file mode 100644 index 000000000..82e0a9754 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/forest000014.java @@ -0,0 +1,30 @@ +/* +Time complexity: O(n) +Space complexity: O(1) + +i <= j 인 두 인덱스 i, j에 대해서, prices[j] - prices[i]를 최대화해야 한다. + +1. i = 0부터 시작하여, 오른쪽으로 순회한다. +2. 현재 값이 max보다 크다면, max를 갱신하고, min과의 차이를 계산한다. +3. 현재 값이 min보다 작다면, min을 갱신하고, max 역시 같은 값으로 갱신한다. (과거로 돌아가서 팔 수는 없으므로) +*/ +class Solution { + public int maxProfit(int[] prices) { + int min = 999999; + int max = 0; + int ans = 0; + for (int i = 0; i < prices.length; i++) { + if (prices[i] > max) { + max = prices[i]; + if (max - min > ans) { + ans = max - min; + } + } + if (prices[i] < min) { + min = max = prices[i]; + } + } + + return ans; + } +} From 764f4a14002fb7ecbd50568c7c84814e06fcb6cd Mon Sep 17 00:00:00 2001 From: forest000014 Date: Mon, 6 Jan 2025 05:50:22 +0900 Subject: [PATCH 2/5] Group Anagrams --- group-anagrams/forest000014.java | 39 ++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 group-anagrams/forest000014.java diff --git a/group-anagrams/forest000014.java b/group-anagrams/forest000014.java new file mode 100644 index 000000000..ce5533d81 --- /dev/null +++ b/group-anagrams/forest000014.java @@ -0,0 +1,39 @@ +/* +Time Complexity: O(n) +Space Complexity: O(n) +*/ +class Solution { + public List> groupAnagrams(String[] strs) { + Map> map = new HashMap<>(); + + for (String str : strs) { + int[] cnt = new int[26]; + for (int i = 0; i < str.length(); i++) { + cnt[str.charAt(i) - 'a']++; + } + char[] chars = new char[str.length()]; + int idx = 0; + for (int i = 0; i < 26; i++) { + while (cnt[i] > 0) { + chars[idx++] = (char)(i + 'a'); + cnt[i]--; + } + } + String sorted = new String(chars); + if (!map.containsKey(sorted)) { + map.put(sorted, new ArrayList<>()); + } + map.get(sorted).add(str); + } + + List> ans = new ArrayList<>(); + for (String key : map.keySet()) { + ans.add(new ArrayList<>()); + for (String str : map.get(key)) { + ans.get(ans.size() - 1).add(str); + } + } + + return ans; + } +} From 5b12185501dfab1d17ab8fc7051a75fab05f1bda Mon Sep 17 00:00:00 2001 From: forest000014 Date: Mon, 6 Jan 2025 05:51:15 +0900 Subject: [PATCH 3/5] Encode and Decode Strings --- encode-and-decode-strings/forest000014.java | 32 +++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 encode-and-decode-strings/forest000014.java diff --git a/encode-and-decode-strings/forest000014.java b/encode-and-decode-strings/forest000014.java new file mode 100644 index 000000000..460beda49 --- /dev/null +++ b/encode-and-decode-strings/forest000014.java @@ -0,0 +1,32 @@ +/* +Time Complexity: O(n) +Space Complexity: O(1) + +non-ASCII 유니코드 문자를 사용함 + +To-Do : escaping 방법 학습하기 +*/ +public class Codec { + + // Encodes a list of strings to a single string. + public String encode(List strs) { + StringBuilder sb = new StringBuilder(); + for (int i = 0; i < strs.size(); i++) { + if (i > 0) { + sb.append('\u2764'); + } + sb.append(strs.get(i)); + } + + return sb.toString(); + } + + // Decodes a single string to a list of strings. + public List decode(String s) { + return new ArrayList<>(Arrays.asList(s.split("\u2764"))); + } +} + +// Your Codec object will be instantiated and called as such: +// Codec codec = new Codec(); +// codec.decode(codec.encode(strs)); From 67b111717b9546da39f969286b5941fb35c5debb Mon Sep 17 00:00:00 2001 From: forest000014 Date: Mon, 6 Jan 2025 05:52:01 +0900 Subject: [PATCH 4/5] Implement Trie (Prefix Tree) --- implement-trie-prefix-tree/forest000014.java | 71 ++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 implement-trie-prefix-tree/forest000014.java diff --git a/implement-trie-prefix-tree/forest000014.java b/implement-trie-prefix-tree/forest000014.java new file mode 100644 index 000000000..02244e140 --- /dev/null +++ b/implement-trie-prefix-tree/forest000014.java @@ -0,0 +1,71 @@ +/* +Time Complexity (n = length of word/prefix) +- initialization: O(1) +- insert: O(n) +- search: O(n) +- startsWith: O(n) + +Space Complexity: O(n * c) (c = calls) +- 길이 3인 알파벳 소문자 문자열의 가짓수 : 26^3 = 17,576 +- 길이 4인 알파벳 소문자 문자열의 가짓수 : 26^4 = 456,976 +만약 n이 3 이하였다면, 3만 번의 call 동안 trie가 포화되어 공간 복잡도가 O(26 * n) = O(n) 이었을 것. +하지만 n이 2,000으로 충분히 크기 때문에 trie가 포화되지는 않을 것이므로, 공간 복잡도는 O(n * c). +*/ +class Trie { + + class Node { + public char val; + public boolean ends; + public HashMap children; + + Node() { + this.children = new HashMap<>(); + } + } + + public Node root; + + public Trie() { + this.root = new Node(); + } + + public void insert(String word) { + Node curr = this.root; + + for (char ch : word.toCharArray()) { + curr = curr.children.computeIfAbsent(ch, c -> new Node()); + curr.val = ch; + } + curr.ends = true; + } + + public boolean search(String word) { + Node curr = this.root; + + for (char ch : word.toCharArray()) { + curr = curr.children.get(ch); + if (curr == null) + return false; + } + return curr.ends; + } + + public boolean startsWith(String prefix) { + Node curr = this.root; + + for (char ch : prefix.toCharArray()) { + curr = curr.children.get(ch); + if (curr == null) + return false; + } + return true; + } +} + +/** + * Your Trie object will be instantiated and called as such: + * Trie obj = new Trie(); + * obj.insert(word); + * boolean param_2 = obj.search(word); + * boolean param_3 = obj.startsWith(prefix); + */ From 222ba9604ed00b0519d5843040970d3c0a25a9eb Mon Sep 17 00:00:00 2001 From: forest000014 Date: Mon, 6 Jan 2025 06:28:38 +0900 Subject: [PATCH 5/5] Word Break --- word-break/forest000014.java | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 word-break/forest000014.java diff --git a/word-break/forest000014.java b/word-break/forest000014.java new file mode 100644 index 000000000..0e2cbc617 --- /dev/null +++ b/word-break/forest000014.java @@ -0,0 +1,33 @@ +/* + +Time Complexity: O(s * L^2) + - L is max length of wordDict[i] +Space Complexity: O(s + w * L) +*/ +class Solution { + public boolean wordBreak(String s, List wordDict) { + Set wordSet = new HashSet<>(); + int maxLen = 0; + for (String word : wordDict) { + wordSet.add(word); + maxLen = Math.max(maxLen, word.length()); + } + + boolean[] dp = new boolean[s.length() + 1]; + dp[0] = true; + + for (int i = 0; i < s.length(); i++) { + for (int j = 1; j <= maxLen; j++) { + int beginIdx = i - j + 1; + if (beginIdx < 0) + continue; + if (wordSet.contains(s.substring(beginIdx, i + 1)) && dp[beginIdx]) { + dp[i + 1] = true; + break; + } + } + } + + return dp[s.length()]; + } +}