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
43 changes: 43 additions & 0 deletions best-time-to-buy-and-sell-stock/hyeri0903.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class Solution {
public int maxProfit(int[] prices) {
/**
1. 하루에 팔아서 가장 최대 이익을 구하도록하는 max price return
2. 조건
- 미래 다른날짜에 판매 (이전 날짜에 판매 x)
- choosing a single day
- 배열 길이 min = 1, max = 10^5
- 원소값 : min = 0, max = 10^4
3. 풀이
- 1)brtueforce: time complexity O(n^2), space: O(1)
- 2)현재 값 - 이전 값 중 가장 최소값 -> maxProfit , 즉 min값을 계속 기억하다가 현재 값과의 차이 중 가장 큰 값을 구하면된다.
- time: O(n)
- space: O(1)
*/

int maxProfit = 0;
int minStock = Integer.MAX_VALUE;
int n = prices.length;

for(int i = 0; i<n; i++) {
if(prices[i] < minStock) {
minStock = prices[i];
}
if(i > 0 && prices[i] - minStock > maxProfit) {
maxProfit = Math.max(maxProfit, prices[i] - minStock);
}
}
return maxProfit;


// for(int i = 0; i < n; i++) {
// int curStock = prices[i];
// for(int j= i + 1; j < n; j++) {
// if(curStock < prices[j]) {
// int curProfit = prices[j] - curStock;
// maxProfit = Math.max(maxProfit, curProfit);
// }
// }
// }
// return maxProfit;
}
}
42 changes: 42 additions & 0 deletions group-anagrams/hyeri0903.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Solution {
public List<List<String>> groupAnagrams(String[] strs) {
/**
1.anagram 끼리 그룹화해서 return
2.조건
- answer in any order
- strs 길이 최소 = 1, 최대 = 10^4
- 원소 하나당 길이 최소 = 0, 최대 = 100
- 모두 소문자로 구성
3.풀이
- 1) i번째 이후 단어를 비교해가면서 anagram check, time: O(n^2)
- 2) HashMap: string element 를 정렬해서 key 로 사용, 중복되는 Key 있으면 anagram, time: O(n)
*/

int n = strs.length;
//anagram 체크할 Map
Map<String, List<String>> map = new HashMap<>();

for(int i = 0; i < n; i++) {
String curStr = strs[i];
char[] tmp = curStr.toCharArray();
Arrays.sort(tmp);
String key = new String(tmp);
// System.out.println("curStr:" + curStr + ", key: " + key);
//Map 에 저장
map.putIfAbsent(key, new ArrayList<>());
map.get(key).add(curStr);
// if(!map.containsKey(key)) {
// List<String> words = new ArrayList<>();
// words.add(curStr);
// map.put(key, words);
// } else {
// List<String> words = map.get(key);
// words.add(curStr);
// map.put(key, words);
// }
}


return new ArrayList<>(map.values());
}
}
Loading