diff --git a/best-time-to-buy-and-sell-stock/hyunolike.java b/best-time-to-buy-and-sell-stock/hyunolike.java new file mode 100644 index 0000000000..cd39041d68 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/hyunolike.java @@ -0,0 +1,14 @@ +class Solution { + public int maxProfit(int[] prices) { + // 주식 최대 이익 + int minPrice = Integer.MAX_VALUE; + int maxProfit = 0; + + for (int price : prices) { + minPrice = Math.min(minPrice, price); + maxProfit = Math.max(maxProfit, price - minPrice); + } + + return maxProfit; + } +} diff --git a/encode-and-decode-strings/hyunolike.java b/encode-and-decode-strings/hyunolike.java new file mode 100644 index 0000000000..8839fac545 --- /dev/null +++ b/encode-and-decode-strings/hyunolike.java @@ -0,0 +1,39 @@ +public class Codec { + + // Encodes a list of strings to a single string. + public String encode(List strs) { + StringBuilder sb = new StringBuilder(); + + for (String s : strs) { + sb.append(s.length()).append('#').append(s); + } + + return sb.toString(); + } + + // Decodes a single string to a list of strings. + public List decode(String s) { + List res = new ArrayList<>(); + int i = 0; + + while (i < s.length()) { + int j = i; + + // find '#' + while (s.charAt(j) != '#') { + j++; + } + + int length = Integer.parseInt(s.substring(i, j)); + j++; // move past '#' + + String word = s.substring(j, j + length); + res.add(word); + + i = j + length; // move to next encoded segment + } + + return res; + } +} + diff --git a/group-anagrams/hyunolike.java b/group-anagrams/hyunolike.java new file mode 100644 index 0000000000..1dec22a661 --- /dev/null +++ b/group-anagrams/hyunolike.java @@ -0,0 +1,22 @@ +class Solution { + public List> groupAnagrams(String[] strs) { + // 같은 문자열 끼리 묶기 + // 그룹이 가장 적은 순으로 정렬 + + Map> map = new HashMap<>(); + + // 각 문자열 char로 바꿔서 정렬하고 다시 문자열 + for(String str : strs) { + char[] arr = str.toCharArray(); + Arrays.sort(arr); + + String key = new String(arr); // 정렬된 문자열 키로 하기 + + map.computeIfAbsent(key, k -> new ArrayList<>()).add(str); + } + + return new ArrayList<>(map.values()); + // map 자료구조, 정렬까지 생각 + // 이후 프로세스 생각 어려움 + } +} diff --git a/implement-trie-prefix-tree/hyunolike.java b/implement-trie-prefix-tree/hyunolike.java new file mode 100644 index 0000000000..b53371f2ce --- /dev/null +++ b/implement-trie-prefix-tree/hyunolike.java @@ -0,0 +1,55 @@ +class TrieNode { + TrieNode[] children = new TrieNode[26]; + boolean isEnd; +} + +class Trie { + + private TrieNode root; + + public Trie() { + root = new TrieNode(); + } + + public void insert(String word) { + TrieNode node = root; + for (char c : word.toCharArray()) { + int idx = c - 'a'; + if(node.children[idx] == null) { + node.children[idx] = new TrieNode(); + } + node = node.children[idx]; + } + node.isEnd = true; + } + + public boolean search(String word) { + TrieNode node = root; + for(char c : word.toCharArray()) { + int idx = c - 'a'; + if(node.children[idx] == null) return false; + node = node.children[idx]; + } + + return node.isEnd; + } + + public boolean startsWith(String prefix) { + TrieNode node = root; + for(char c : prefix.toCharArray()) { + int idx = c - 'a'; + if(node.children[idx] == null) return false; + node = node.children[idx]; + } + 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); + */ + diff --git a/word-break/hyunolike.java b/word-break/hyunolike.java new file mode 100644 index 0000000000..0bf282749d --- /dev/null +++ b/word-break/hyunolike.java @@ -0,0 +1,20 @@ +class Solution { + public boolean wordBreak(String s, List wordDict) { + int n = s.length(); + boolean[] dp = new boolean[n+1]; + dp[0] = true; + + Set wordSet = new HashSet<>(wordDict); + + for(int i = 1; i <= n; i++) { + for(int j = 0; j < i; j++) { + if(dp[j] && wordSet.contains(s.substring(j, i))) { + dp[i] = true; + break; + } + } + } + return dp[n]; + } +} + diff --git a/word-break/hyunolike.ts b/word-break/hyunolike.ts new file mode 100644 index 0000000000..6b5390e1c8 --- /dev/null +++ b/word-break/hyunolike.ts @@ -0,0 +1,17 @@ +function wordBreak(s: string, wordDict: string[]): boolean { + const n = s.length; + const dp:boolean[] = Array(n+1).fill(false); + dp[0] = true; + + const wordSet = new Set(wordDict); + + for(let i = 1; i <= n; i++) { + for(let j = 0; j < i; j++) { + if(dp[j] && wordSet.has(s.substring(j, i))) { + dp[i] = true; + break; + } + } + } + return dp[n]; +};