-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #851 from forest000014/main
[forest000014] Week 5
- Loading branch information
Showing
5 changed files
with
205 additions
and
0 deletions.
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,30 @@ | ||
/* | ||
Time complexity: O(n) | ||
Space complexity: O(1) | ||
i <= j 인 두 인덱스 i, j에 대해서, prices[j] - prices[i]를 최대화해야 한다. | ||
1. i = 0부터 시작하여, 오른쪽으로 순회한다. | ||
2. 현재 값이 max보다 크다면, max를 갱신하고, min과의 차이를 계산한다. | ||
3. 현재 값이 min보다 작다면, min을 갱신하고, max 역시 같은 값으로 갱신한다. (과거로 돌아가서 팔 수는 없으므로) | ||
*/ | ||
class Solution { | ||
public int maxProfit(int[] prices) { | ||
int min = 999999; | ||
int max = 0; | ||
int ans = 0; | ||
for (int i = 0; i < prices.length; i++) { | ||
if (prices[i] > max) { | ||
max = prices[i]; | ||
if (max - min > ans) { | ||
ans = max - min; | ||
} | ||
} | ||
if (prices[i] < min) { | ||
min = max = prices[i]; | ||
} | ||
} | ||
|
||
return ans; | ||
} | ||
} |
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,32 @@ | ||
/* | ||
Time Complexity: O(n) | ||
Space Complexity: O(1) | ||
non-ASCII 유니코드 문자를 사용함 | ||
To-Do : escaping 방법 학습하기 | ||
*/ | ||
public class Codec { | ||
|
||
// Encodes a list of strings to a single string. | ||
public String encode(List<String> strs) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (int i = 0; i < strs.size(); i++) { | ||
if (i > 0) { | ||
sb.append('\u2764'); | ||
} | ||
sb.append(strs.get(i)); | ||
} | ||
|
||
return sb.toString(); | ||
} | ||
|
||
// Decodes a single string to a list of strings. | ||
public List<String> decode(String s) { | ||
return new ArrayList<>(Arrays.asList(s.split("\u2764"))); | ||
} | ||
} | ||
|
||
// Your Codec object will be instantiated and called as such: | ||
// Codec codec = new Codec(); | ||
// codec.decode(codec.encode(strs)); |
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,39 @@ | ||
/* | ||
Time Complexity: O(n) | ||
Space Complexity: O(n) | ||
*/ | ||
class Solution { | ||
public List<List<String>> groupAnagrams(String[] strs) { | ||
Map<String, List<String>> map = new HashMap<>(); | ||
|
||
for (String str : strs) { | ||
int[] cnt = new int[26]; | ||
for (int i = 0; i < str.length(); i++) { | ||
cnt[str.charAt(i) - 'a']++; | ||
} | ||
char[] chars = new char[str.length()]; | ||
int idx = 0; | ||
for (int i = 0; i < 26; i++) { | ||
while (cnt[i] > 0) { | ||
chars[idx++] = (char)(i + 'a'); | ||
cnt[i]--; | ||
} | ||
} | ||
String sorted = new String(chars); | ||
if (!map.containsKey(sorted)) { | ||
map.put(sorted, new ArrayList<>()); | ||
} | ||
map.get(sorted).add(str); | ||
} | ||
|
||
List<List<String>> ans = new ArrayList<>(); | ||
for (String key : map.keySet()) { | ||
ans.add(new ArrayList<>()); | ||
for (String str : map.get(key)) { | ||
ans.get(ans.size() - 1).add(str); | ||
} | ||
} | ||
|
||
return ans; | ||
} | ||
} |
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,71 @@ | ||
/* | ||
Time Complexity (n = length of word/prefix) | ||
- initialization: O(1) | ||
- insert: O(n) | ||
- search: O(n) | ||
- startsWith: O(n) | ||
Space Complexity: O(n * c) (c = calls) | ||
- 길이 3인 알파벳 소문자 문자열의 가짓수 : 26^3 = 17,576 | ||
- 길이 4인 알파벳 소문자 문자열의 가짓수 : 26^4 = 456,976 | ||
만약 n이 3 이하였다면, 3만 번의 call 동안 trie가 포화되어 공간 복잡도가 O(26 * n) = O(n) 이었을 것. | ||
하지만 n이 2,000으로 충분히 크기 때문에 trie가 포화되지는 않을 것이므로, 공간 복잡도는 O(n * c). | ||
*/ | ||
class Trie { | ||
|
||
class Node { | ||
public char val; | ||
public boolean ends; | ||
public HashMap<Character, Node> children; | ||
|
||
Node() { | ||
this.children = new HashMap<>(); | ||
} | ||
} | ||
|
||
public Node root; | ||
|
||
public Trie() { | ||
this.root = new Node(); | ||
} | ||
|
||
public void insert(String word) { | ||
Node curr = this.root; | ||
|
||
for (char ch : word.toCharArray()) { | ||
curr = curr.children.computeIfAbsent(ch, c -> new Node()); | ||
curr.val = ch; | ||
} | ||
curr.ends = true; | ||
} | ||
|
||
public boolean search(String word) { | ||
Node curr = this.root; | ||
|
||
for (char ch : word.toCharArray()) { | ||
curr = curr.children.get(ch); | ||
if (curr == null) | ||
return false; | ||
} | ||
return curr.ends; | ||
} | ||
|
||
public boolean startsWith(String prefix) { | ||
Node curr = this.root; | ||
|
||
for (char ch : prefix.toCharArray()) { | ||
curr = curr.children.get(ch); | ||
if (curr == null) | ||
return false; | ||
} | ||
return true; | ||
} | ||
} | ||
|
||
/** | ||
* Your Trie object will be instantiated and called as such: | ||
* Trie obj = new Trie(); | ||
* obj.insert(word); | ||
* boolean param_2 = obj.search(word); | ||
* boolean param_3 = obj.startsWith(prefix); | ||
*/ |
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,33 @@ | ||
/* | ||
Time Complexity: O(s * L^2) | ||
- L is max length of wordDict[i] | ||
Space Complexity: O(s + w * L) | ||
*/ | ||
class Solution { | ||
public boolean wordBreak(String s, List<String> wordDict) { | ||
Set<String> wordSet = new HashSet<>(); | ||
int maxLen = 0; | ||
for (String word : wordDict) { | ||
wordSet.add(word); | ||
maxLen = Math.max(maxLen, word.length()); | ||
} | ||
|
||
boolean[] dp = new boolean[s.length() + 1]; | ||
dp[0] = true; | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
for (int j = 1; j <= maxLen; j++) { | ||
int beginIdx = i - j + 1; | ||
if (beginIdx < 0) | ||
continue; | ||
if (wordSet.contains(s.substring(beginIdx, i + 1)) && dp[beginIdx]) { | ||
dp[i + 1] = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return dp[s.length()]; | ||
} | ||
} |