Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
21 changes: 21 additions & 0 deletions best-time-to-buy-and-sell-stock/std-freejia.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
23 changes: 23 additions & 0 deletions group-anagrams/std-freejia.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
List<List<String>> answer = new ArrayList<>();
// <정렬한 문자, 원본 문자 리스트>
HashMap<String, List<String>> map = new HashMap<>();

for (String str : strs) {
char[] arr = str.toCharArray();
Arrays.sort(arr);
String strKey = String.valueOf(arr);

if (!map.containsKey(strKey)) {
ArrayList<String> list = new ArrayList<>();
list.add(str);
map.put(strKey, list);
} else {
map.get(strKey).add(str);
}
}
answer.addAll(map.values());
return answer;
}
}