diff --git a/best-time-to-buy-and-sell-stock/juhui-jeong.java b/best-time-to-buy-and-sell-stock/juhui-jeong.java new file mode 100644 index 0000000000..2afdc7a2c7 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/juhui-jeong.java @@ -0,0 +1,34 @@ +/* +시간복잡도: O(n²) +공간복잡도: O(1) +class Solution { + public int maxProfit(int[] prices) { + int maxStock = 0; + for(int i = 0; i < prices.length; i++) { + for (int j = 0; j < prices.length; j++) { + if (i <= j) break; + if (prices[i] - prices[j] > maxStock) { + maxStock = prices[i] - prices[j]; + } + } + } + return maxStock; + } +} + */ +// 시간복잡도: O(n) +// 공간복잡도: O(1) +class Solution { + public int maxProfit(int[] prices) { + int maxStock = 0; + int minPrice = prices[0]; + for (int price : prices) { + if (price < minPrice) { + minPrice = price; + } else { + maxStock = Math.max(price - minPrice, maxStock); + } + } + return maxStock; + } +} diff --git a/group-anagrams/juhui-jeong.java b/group-anagrams/juhui-jeong.java new file mode 100644 index 0000000000..5582fa19aa --- /dev/null +++ b/group-anagrams/juhui-jeong.java @@ -0,0 +1,21 @@ +/* +시간복잡도: O(k log k) +공간복잡도: O(n * k) +*/ +class Solution { + public List> groupAnagrams(String[] strs) { + Map> map = new HashMap<>(); + + for (String str: strs) { + char[] chars = str.toCharArray(); + Arrays.sort(chars); + String key = new String(chars); + + if (!map.containsKey(key)) { + map.put(key, new ArrayList<>()); + } + map.get(key).add(str); + } + return new ArrayList<>(map.values()); + } +}