diff --git a/best-time-to-buy-and-sell-stock/std-freejia.java b/best-time-to-buy-and-sell-stock/std-freejia.java new file mode 100644 index 000000000..dd8eeeea8 --- /dev/null +++ b/best-time-to-buy-and-sell-stock/std-freejia.java @@ -0,0 +1,21 @@ +/** + * 121. Best Time to Buy and Sell Stock + * 반복문 돌면서 가장 작은 값을 기억해둡니다 + * 매번 최소값과의 차이를 비교합니다 + */ +class Solution { + public int maxProfit(int[] prices) { + int maxProfit = 0; + int minPrice = Integer.MAX_VALUE; + + for (int price: prices) { + if (price < minPrice) { // 최소 값 찾기 + minPrice = price; + } + if (price - minPrice > maxProfit) { + maxProfit = price - minPrice; + } + } + return maxProfit; + } +} diff --git a/group-anagrams/std-freejia.java b/group-anagrams/std-freejia.java new file mode 100644 index 000000000..413c53030 --- /dev/null +++ b/group-anagrams/std-freejia.java @@ -0,0 +1,23 @@ +class Solution { + public List> groupAnagrams(String[] strs) { + List> answer = new ArrayList<>(); + // <정렬한 문자, 원본 문자 리스트> + HashMap> map = new HashMap<>(); + + for (String str : strs) { + char[] arr = str.toCharArray(); + Arrays.sort(arr); + String strKey = String.valueOf(arr); + + if (!map.containsKey(strKey)) { + ArrayList list = new ArrayList<>(); + list.add(str); + map.put(strKey, list); + } else { + map.get(strKey).add(str); + } + } + answer.addAll(map.values()); + return answer; + } +}