Skip to content
Open
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
58 changes: 58 additions & 0 deletions best-time-to-buy-and-sell-stock/hjeomdev.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
class Solution {
public int maxProfit(int[] prices) {
// 1. 0번을 제외하고 max 를 찾고
// 2. 0 ~ max 범위에서 min을 찾는다 => 2n
// 3. max - min > 0 이면 값 리턴, max - min <=0 이면 0 리턴
/*
if (prices.length == 1) {
return 0;
}

int max = 1;
for (int i = 1; i < prices.length; i++) {
if (prices[max] <= prices[i]) {
max = i;
}
}

int min = max - 1;
for (int i = max - 1; i >= 0; i--) {
if (prices[i] < prices[min]) {
min = i;
}
}
int result = prices[max] - prices[min];
return result > 0 ? result : 0;
*/
// 위 풀이 실패 케이스 : [3,3,5,0,0,3,1,4]
// -> i가 max 일 때 i 이전까지 반복문으로 최대값 구하기 => n^2
// -> Time Limit Exceeded 발생
// -> i 이전까지 범위에서 최소값을 구해서 값을 구할까?
/*
int result = 0;
for (int i = 1; i < prices.length; i++) {
for (int j = 0; j < i; j++) {
int cur = prices[i] - prices[j];
if (result < cur) {
result = cur;
}
}
}
return result;
*/
// i 이전까지 범위에서 최소값을 구해서 배열로 만들기
int curMin = prices[0];
int result = 0;
for (int i = 0; i < prices.length; i++) {
if (curMin > prices[i]) {
curMin = prices[i];
}
// System.out.println(curMin);
if (result < prices[i] - curMin) {
result = prices[i] - curMin;
}
// System.out.println(">>" + result);
}
return result;
}
}
21 changes: 21 additions & 0 deletions group-anagrams/hjeomdev.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
// 감이 안 와서 해설참조
// 26자리 배열을 사용해서 알파벳 카운트 -> 문자열로 만들어서 Map의 키로 사용
Map<String, List<String>> anagrams = new HashMap<>();
for (String str : strs) {
int[] count = new int[26];
for (char ch : str.toCharArray()) {
int idx = ch - 'a';
count[idx] = count[idx] + 1;
}
String key = Arrays.toString(count);
if (!anagrams.containsKey(key)) {
anagrams.put(key, new LinkedList<>());
}
List<String> words = anagrams.get(key);
words.add(str);
}
return new ArrayList(anagrams.values());
}
}