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

[mintheon] Week 5 #850

Merged
merged 3 commits into from
Jan 12, 2025
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
17 changes: 17 additions & 0 deletions best-time-to-buy-and-sell-stock/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution {
/**
시간복잡도: O(n)
공간복잡도: O(1)
*/
public int maxProfit(int[] prices) {
int buyPrice = prices[0];
int maxProfit = 0;

for(int i = 0; i < prices.length; i++) {
buyPrice = Math.min(buyPrice, prices[i]);
maxProfit = Math.max(maxProfit, prices[i] - buyPrice);
}

return maxProfit;
}
}
39 changes: 39 additions & 0 deletions encode-and-decode-strings/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import java.util.ArrayList;
import java.util.List;

public class Codec {
String SPLIT = ":";

// Encodes a list of strings to a single string.
public String encode(List<String> strs) {
StringBuilder encoded = new StringBuilder();

for(String str : strs) {
encoded.append(str.length()).append(SPLIT).append(str);
}

return encoded.toString();
}

// Decodes a single string to a list of strings.
public List<String> decode(String s) {
List<String> decoded = new ArrayList<>();

int pointer = 0;
while(pointer < s.length()) {
int index = s.indexOf(SPLIT, pointer);
int length = Integer.parseInt(s.substring(pointer, index));

String str = s.substring(index + 1, index + 1 + length);
decoded.add(str);

pointer = index + 1 + length;
}

return decoded;
}
}

// Your Codec object will be instantiated and called as such:
// Codec codec = new Codec();
// codec.decode(codec.encode(strs));
30 changes: 30 additions & 0 deletions group-anagrams/mintheon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

class Solution {
/**
시간복잡도: O(n)
공간복잡도: O(n)
*/
Comment on lines +8 to +11
Copy link
Member

Choose a reason for hiding this comment

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

for문 내부의 문자열 정렬이 O(k log k)이라서
전체 시간 복잡도는 O(n * k log k)로 수정하는 것이 더 정확할 것 같습니다:)

public List<List<String>> groupAnagrams(String[] strs) {
Map<String, List<String>> anagramMap = new HashMap<>();

for(String str : strs) {
char[] charStr = str.toCharArray();
Arrays.sort(charStr);
String sortedStr = String.valueOf(charStr);

if(!anagramMap.containsKey(sortedStr)) {
anagramMap.put(sortedStr, new ArrayList<>());

}

anagramMap.get(sortedStr).add(str);
Comment on lines +20 to +25
Copy link
Member

Choose a reason for hiding this comment

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

해시맵에서 키가 없는 경우 새로운 리스트를 생성하는 computeIfAbsent라는 메소드가 있더라구요.
코드를 간결하게 작성할 수 있어서 사용해보시는 걸 추천드립니다!

anagramMap.computeIfAbsent(key, k -> new ArrayList<>()).add(str);

}

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