Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 16 additions & 0 deletions best-time-to-buy-and-sell-stock/TonyKim9401.java
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;
}
}
25 changes: 25 additions & 0 deletions group-anagrams/TonyKim9401.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// TC: O(n)
// SC: O(n * m)
Copy link
Contributor

Choose a reason for hiding this comment

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

질문

mstrs 배열 내 원소의 평균~최대 크기라고 보면 될까요?

Copy link
Contributor Author

@TonyKim9401 TonyKim9401 Sep 13, 2024

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 으로 공간 복잡도를 구했습니다!

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);
Copy link
Contributor

Choose a reason for hiding this comment

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

찾아보니 JAVA의 Arrays.sort method도 퀵소트를 사용하는 것 같은데, 이 부분에 대한 계산이 누락된 것 같아 보입니다

Copy link
Contributor Author

Choose a reason for hiding this comment

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

앗 감사합니다!
주어진 문자열 n에서 각 요소를 m으로 두고 sort하면 Java에서는 m log m 시간이 걸립니다.
따라서 시간 복잡도가 O(n * m log m)을 가지게 되겠네요.
통찰력 감탄합니다 감사합니다!! 💯

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;
}
}