Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[jaejeong1] week 5 #454

Merged
merged 4 commits into from
Sep 16, 2024
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
27 changes: 27 additions & 0 deletions best-time-to-buy-and-sell-stock/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
class SolutionJaeJeong {

public int maxProfit(int[] prices) {
// 주어진 가격 배열이 주어지고 prices[i]는 i번째 날의 주어진 주식 가격
// 한 주식을 매수할 단일 날짜를 선택하고, 매도할 미래의 다른 날짜를 선택해 수익을 극대화
// 이 거래에서 얻을 수 있는 최대 수익을 반환해라, 수익을 얻을 수 없으면 0을 반환해라
// 싸게 매수해서 비싸게 매도해라

// 3번째 풀이
// 최대 이익: 현재 가격 - 이전 가격 중 최저 가격
// 시간복잡도: O(N), 공간복잡도: O(1)
int minPrice = prices[0];
int maxProfit = 0;
for (int i = 1; i < prices.length; i++) {
if (prices[i - 1] < minPrice) {
minPrice = prices[i - 1];
}

var profit = prices[i] - minPrice;
if (profit > maxProfit) {
maxProfit = profit;
}
}

return maxProfit;
}
}
52 changes: 52 additions & 0 deletions group-anagrams/jaejeong1.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class SolutionGroupAnagram {
public List<List<String>> groupAnagrams(String[] strs) {
// char, integer map으로 strs의 원소들을 분류한다
// 분류 후 알파벳 순으로 정렬해 알파벳 + 개수 조합의 str으로 만든다
// str 과 인덱스를 맵에 넣는다
// 맵 keyset을 돌면서 value에 해당하는 strs 인덱스로 접근해 정답으로 반환
// 시간복잡도: O(N^2), 공간복잡도: O(N)
Map<String, List<Integer>> map = new HashMap<>();

for (int i=0; i<strs.length; i++) {
var anagramData = createAnagramData(strs[i]);
var value = map.getOrDefault(anagramData, new ArrayList<>());
value.add(i);
map.put(anagramData, value);
}

List<List<String>> answer = new ArrayList<>();

for (String key : map.keySet()) {
List<String> value = new ArrayList<>();
for (int index : map.get(key)) {
value.add(strs[index]);
}

answer.add(value);
}

return answer;
}

private String createAnagramData(String str) {
Map<Character, Integer> map = new HashMap<>();

for (int i=0; i<str.length(); i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

여기서 str.length() 만큼 순회가 이루어지니, 시간 복잡도가 O(N) 보다 더 큰 값이어야 할 것 같습니다! (N: strs.length 라는 가정 하에)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bky373
피드백 감사합니다! N이 아닌 N^2 이 되어야 맞겠군요 :) 수정했습니다!

var element = str.charAt(i);
map.put(element, map.getOrDefault(element, 0)+1);
}

var keySet = map.keySet().stream().sorted().toList();
StringBuilder anagramData = new StringBuilder();
for (char key : keySet) {
anagramData.append(key).append(map.get(key));
}

return anagramData.toString();
}
}