-
Notifications
You must be signed in to change notification settings - Fork 54
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
[jaejeong1] week 5 #454
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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++) { | ||
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(); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 라는 가정 하에)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bky373
피드백 감사합니다! N이 아닌 N^2 이 되어야 맞겠군요 :) 수정했습니다!