From eaeb2f9b40f5cc0b45262a79ae25660650c99de9 Mon Sep 17 00:00:00 2001 From: Yn3-3xh Date: Thu, 1 May 2025 16:40:00 +0900 Subject: [PATCH 1/3] best time to buy and sell stock solution --- best-time-to-buy-and-sell-stock/Yn3-3xh.java | 23 ++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 best-time-to-buy-and-sell-stock/Yn3-3xh.java diff --git a/best-time-to-buy-and-sell-stock/Yn3-3xh.java b/best-time-to-buy-and-sell-stock/Yn3-3xh.java new file mode 100644 index 000000000..9ef646212 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/Yn3-3xh.java @@ -0,0 +1,23 @@ +/** +[문제풀이] +- 작은수를 두고, 큰수에 뺀 값을 구하자. +time: O(N), space: O(1) + +[회고] +이번 문제는 난이도가 easy인 덕분에 무리없이 풀었던 것 같다. + */ +class Solution { + public int maxProfit(int[] prices) { + int min = prices[0]; + int max = 0; + for (int i = 1; i < prices.length; i++) { + if (min > prices[i]) { + min = prices[i]; + continue; + } + max = Math.max(max, prices[i] - min); + } + return max; + } +} + From 293aa8381555fd9bc8f64c22f1355c4ba19b0fd9 Mon Sep 17 00:00:00 2001 From: Yn3-3xh Date: Fri, 2 May 2025 00:29:15 +0900 Subject: [PATCH 2/3] best time to buy and sell stock solution apply feedback from minji-go --- best-time-to-buy-and-sell-stock/Yn3-3xh.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/best-time-to-buy-and-sell-stock/Yn3-3xh.java b/best-time-to-buy-and-sell-stock/Yn3-3xh.java index 9ef646212..fbc50a58e 100644 --- a/best-time-to-buy-and-sell-stock/Yn3-3xh.java +++ b/best-time-to-buy-and-sell-stock/Yn3-3xh.java @@ -11,13 +11,9 @@ public int maxProfit(int[] prices) { int min = prices[0]; int max = 0; for (int i = 1; i < prices.length; i++) { - if (min > prices[i]) { - min = prices[i]; - continue; - } + min = Math.min(min, prices[i]); max = Math.max(max, prices[i] - min); } return max; } } - From f5b97b22b286ac06f976a5c476721bb54bdeb183 Mon Sep 17 00:00:00 2001 From: Yn3-3xh Date: Sat, 3 May 2025 00:34:09 +0900 Subject: [PATCH 3/3] group anagrams solutions --- group-anagrams/Yn3-3xh.java | 83 +++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 group-anagrams/Yn3-3xh.java diff --git a/group-anagrams/Yn3-3xh.java b/group-anagrams/Yn3-3xh.java new file mode 100644 index 000000000..7a80db28f --- /dev/null +++ b/group-anagrams/Yn3-3xh.java @@ -0,0 +1,83 @@ +/** +[문제풀이] +- 이전에 배열로 풀었던 아나그램 솔루션을 참고해보자. +time: O(N^2 * M), space: O(N * M) + class Solution { + public List> groupAnagrams(String[] strs) { + List> result = new ArrayList<>(); + boolean[] used = new boolean[strs.length]; + + for (int i = 0; i < strs.length; i++) { + if (used[i]) { + continue; + } + + List sub = new ArrayList<>(); + String current = strs[i]; + sub.add(current); + used[i] = true; + + for (int j = i + 1; j < strs.length; j++) { + String next = strs[j]; + if (isAnagram(current, next)) { + sub.add(next); + used[j] = true; + } + } + result.add(sub); + } + return result; + } + + private boolean isAnagram(String current, String next) { + if (current.length() != next.length()) { + return false; + } + + int[] counting = new int[26]; + for (char ch : current.toCharArray()) { + counting[ch - 'a']++; + } + for (char ch : next.toCharArray()) { + counting[ch - 'a']--; + } + + for (int count : counting) { + if (count != 0) { + return false; + } + } + return true; + } + } + +- sorting +time: O(N * M log M), space: O(N * M) + +[회고] +중첩 for문을 써서 시간복잡도 커트라인에 턱걸이했다.. + +이전에 풀었던 아나그램을 베이스로 풀다 보니 최적화하기가 어려운 것 같다. +문제에 맞는 풀이법을 생각해보자.. + +주어진 문자열을 문자 배열로 만들고, 문자 배열을 sorting한다면 아나그램의 key가 된다. +key가 없으면 list를 만들어주고, 있으면 그 key에 문자열을 넣어주자. +해설을 보고, 이해하고 풀고나면 쉬운데.. +왜 처음에 못 풀까.. + */ +class Solution { + public List> groupAnagrams(String[] strs) { + Map> result = new HashMap<>(); + + for (String str : strs) { + char[] tempArray = str.toCharArray(); + Arrays.sort(tempArray); + String key = new String(tempArray); + + result.putIfAbsent(key, new ArrayList<>()); + result.get(key).add(str); + } + return new ArrayList<>(result.values()); + } +} +