Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions best-time-to-buy-and-sell-stock/YuuuuuuYu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* Runtime: 1ms
* Time Complexity: O(n)
*
* Memory: 94.08MB
* Space Complexity: O(1)
*
* Approach: 그리디 알고리즘
* - 주어진 prices 배열을 순회하며 최소 가격(min)을 갱신
*/
class Solution {
public int maxProfit(int[] prices) {
int maxProfit = 0;
int min = prices[0];

for (int i=1; i<prices.length; i++) {
if (min >= prices[i]) {
min = prices[i];
} else {
maxProfit = Math.max(maxProfit, prices[i]-min);
}
}

return maxProfit;
}
}
29 changes: 29 additions & 0 deletions group-anagrams/YuuuuuuYu.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Runtime: 6ms
* Time Complexity: O(n x k log k)
* - n: 문자열 개수
* - k: 문자열 최대 길이
* - k log k: 각 문자열을 정렬하는 시간
*
* Memory: 49.30MB
* Space Complexity: O(n x k)
* - 최대 n개의 키-값
*
* Approach: 정렬된 문자열을 키로 사용해서 같은 애나그램끼리 그룹화
*/
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> anagramMap = new HashMap<>();

for (String str: strs) {
char[] chs = str.toCharArray();
Arrays.sort(chs);
String key = new String(chs);

anagramMap.computeIfAbsent(key, k -> new ArrayList<>())
.add(str);
}

return new ArrayList<>(anagramMap.values());
}
}