-
-
Notifications
You must be signed in to change notification settings - Fork 245
[TONY] WEEK 5 Solution #451
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
Changes from 2 commits
73a6d43
a0c0755
02fdf7a
e8ee61d
2e85a37
6762aa0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// TC: O(n) | ||
// SC: O(1) | ||
class Solution { | ||
public int maxProfit(int[] prices) { | ||
int bestProfit = 0; | ||
int buyPrice = prices[0]; | ||
for (int i = 1; i < prices.length; i++) { | ||
if (buyPrice > prices[i]) { | ||
buyPrice = prices[i]; | ||
continue; | ||
} | ||
bestProfit = Math.max(bestProfit, prices[i] - buyPrice); | ||
} | ||
return bestProfit; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
// TC: O(n) | ||
// SC: O(n * m) | ||
class Solution { | ||
public List<List<String>> groupAnagrams(String[] strs) { | ||
List<List<String>> output = new ArrayList<>(); | ||
Map<String, List<String>> map = new HashMap<>(); | ||
|
||
for (int i = 0; i < strs.length; i++) { | ||
char[] charArray = strs[i].toCharArray(); | ||
Arrays.sort(charArray); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 찾아보니 JAVA의 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 앗 감사합니다! |
||
String target = new String(charArray); | ||
|
||
if (map.containsKey(target)) { | ||
map.get(target).add(strs[i]); | ||
} else { | ||
List<String> inside = new ArrayList<>(); | ||
inside.add(strs[i]); | ||
map.put(target, inside); | ||
} | ||
} | ||
|
||
for (String key : map.keySet()) output.add(map.get(key)); | ||
return output; | ||
} | ||
} |
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.
질문
m
은strs
배열 내 원소의 평균~최대 크기라고 보면 될까요?Uh oh!
There was an error while loading. Please reload this page.
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.
아뇨! 제 풀이 방식에서는
map
을 사용하고 있고Key: String, Value: List<String>
을 사용하고 있습니다.즉 n개의 key 값들에 m개의 value가 공간을 차지하는걸 최대로 보고
n * m
으로 공간 복잡도를 구했습니다!