-
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 #841 from eunhwa99/main
[eunhwa99] 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,55 @@ | ||
// ํ์ด ๋ฐฉ๋ฒ | ||
// 1. ์ธ๋ฑ์ค x๊น์ง์ ์ต์๊ฐ์ ์ ์ฅํ๋ ๋ฐฐ์ด 1๊ฐ์, ์ธ๋ฑ์ค x๋ถํฐ์ ์ต๋๊ฐ์ ์ ์ฅํ๋ ๋ฐฐ์ด 1๊ฐ๋ฅผ ๋ง๋ ๋ค. | ||
// 2. 1๋ฒ์์ ๋ง๋ ๋ ๋ฐฐ์ด์ ๊ฐ์ ์ฑ์ด๋ค. | ||
// 3. ๋ ๋ฐฐ์ด์ ๊ฐ๊ฐ ์ธ๋ฑ์ค ๋ณ๋ก ์ฐจ๋ฅผ ๊ตฌํ๊ณ , ๊ทธ ์ค ์ต๋๊ฐ์ ๊ตฌํ๋ค. | ||
|
||
// ์๊ฐ ๋ณต์ก๋ | ||
// O(n) : ๋ฐฐ์ด์ 2๋ฒ ์ํํ๋ฏ๋ก O(n)์ด๋ค. | ||
// ๊ณต๊ฐ ๋ณต์ก๋ | ||
// O(n) : ์ต์๊ฐ๊ณผ ์ต๋๊ฐ์ ์ ์ฅํ๋ ๋ฐฐ์ด์ ๋ง๋ค์์ผ๋ฏ๋ก O(n)์ด๋ค. | ||
|
||
class Solution { | ||
public int maxProfit(int[] prices) { | ||
int len = prices.length; | ||
int[] minArr = new int[len]; | ||
int[] maxArr = new int[len]; | ||
|
||
for(int i=0;i<len;i++){ | ||
if(i==0){ | ||
minArr[i] = prices[i]; | ||
maxArr[len-i-1] = prices[len-i-1]; | ||
}else{ | ||
minArr[i] = Math.min(minArr[i-1], prices[i]); | ||
maxArr[len-i-1] = Math.max(maxArr[len-i], prices[len-i-1]); | ||
} | ||
} | ||
|
||
int result = 0; | ||
for(int i=0;i<len;i++){ | ||
result = Math.max(result, maxArr[i]-minArr[i]); | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
|
||
// 2nd solution | ||
// ์๊ฐ ๋ณต์ก๋: O(n) | ||
// ๊ณต๊ฐ ๋ณต์ก๋: O(1) | ||
|
||
class Solution{ | ||
public int maxProfit(int[] prices){ | ||
int len = prices.length; | ||
int buy = prices[0]; | ||
int result = 0; | ||
|
||
for(int i=1;i<len;i++){ | ||
if(prices[i]<buy){ // ๋ ์ ๋ ดํ ์ฃผ์์ด ์์ผ๋ฏ๋ก | ||
buy = prices[i]; // ์ด ์ฃผ์์ ์ฐ๋ค. | ||
}else{ | ||
result = Math.max(result, prices[i]-buy); // ํ์ฌ ์ฃผ์์ ํ์์ ๋ ์ด๋์ด ๋ ํฌ๋ค๋ฉด ํ๋ค. | ||
} | ||
} | ||
return result; | ||
} | ||
} |
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,48 @@ | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
// n: ๋ฌธ์์ด ๋ฆฌ์คํธ ๊ธธ์ด, m: ๊ฐ ๋ฌธ์์ด์ ๊ธธ์ด. | ||
|
||
// ์๊ฐ ๋ณต์ก๋ | ||
// Encoding: O(n * m) | ||
// Decoding: O(n * m) | ||
// ๊ณต๊ฐ ๋ณต์ก๋ | ||
// Encoding: O(n * m), | ||
// Decoding: O(n * m) | ||
class Solution { | ||
|
||
// ๋ฐ์์จ string ๋ค์ ํ ๋ฌธ์์ด๋ก ํฉ์นจ | ||
// (๋ฌธ์์ด๊ธธ์ด)#(๋ฌธ์์ด) ํ์ | ||
public String encode(List<String> strs) { | ||
StringBuilder encodedStr = new StringBuilder(); | ||
for (String str : strs) { | ||
encodedStr.append(str.length()).append('#').append(str); | ||
} | ||
return encodedStr.toString(); | ||
} | ||
|
||
// Decodes a single string to a list of strings | ||
public List<String> decode(String s) { | ||
List<String> result = new ArrayList<>(); | ||
int i = 0; | ||
|
||
while (i < s.length()) { | ||
|
||
int j = i; | ||
while (s.charAt(j) != '#') { // # ๋ฌธ์ ์ฐพ๊ธฐ | ||
j++; | ||
} | ||
|
||
// ๋ฌธ์์ด ๊ธธ์ด ์ถ์ถ | ||
int length = Integer.parseInt(s.substring(i, j)); | ||
|
||
// ์์์ ๊ตฌํ ๋ฌธ์์ด ๊ธธ์ด๋งํผ ๋ฌธ์์ด ์ถ์ถ | ||
result.add(s.substring(j + 1, j + 1 + length)); | ||
|
||
// i ์์น ๋ณ๊ฒฝ | ||
i = j + 1 + length; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
} |
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,31 @@ | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
// ํด๊ฒฐ๋ฒ | ||
// 1. ๋ฐฐ์ด ์์ ์์๋ค์ ์ํํ๋ค. | ||
// 2. ๊ฐ ์์์ ๋ํ์ฌ ๋ฌธ์์ด์ ์ ๋ ฌํ ํ, hashmap์ ๋ฃ๋๋ค. ์ด ๋ hashMap ์๋ฃ๊ตฌ์กฐ๋ <String, List<String>> ํํ๋ก ๋ง๋ ๋ค. | ||
|
||
// ์๊ฐ ๋ณต์ก๋: | ||
// 1. ํฌ๊ธฐ๊ฐ m์ธ ๋ฌธ์์ด์ ์ ๋ ฌํ ๋ O(mlogm)์ด ์์ | ||
// 2. ๋ฐฐ์ด์ ํฌ๊ธฐ๊ฐ n ์ด๋ฏ๋ก ์ํํ๋๋ฐ O(n) | ||
// 3. hashmap ์ฝ์ ์ O(1) | ||
// ์ ์ฌ์ค๋ค์ ์กฐํฉํ๋ฉด ์ด O(nmlogm)์ด ์์๋๋ค. | ||
// ์ฐธ๊ณ ๋ก, ์ ์ฒด ๋ฐฐ์ด ํฌ๊ธฐ๋ ์ต๋ 10^4 ์ด๊ณ , ๊ฐ ๋ฌธ์์ด์ ๊ธธ์ด๋ ์ต๋ 100์ด๋ฏ๋ก, ์์ ๊ฐ์ด ์ ๊ทผํ ๊ฒฝ์ฐ, ์ต๋ 10^6์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋ค. | ||
|
||
// ๊ณต๊ฐ ๋ณต์ก๋: hashmap ํฌ๊ธฐ๋งํผ ์ฌ์ฉ -> O(nm) | ||
|
||
class Solution{ | ||
public List<List<String>> groupAnagrams(String[] strs) { | ||
Map<String, List<String>> map = new HashMap<>(); | ||
for(String str: strs){ | ||
char[] charArr = str.toCharArray(); | ||
Arrays.sort(charArr); | ||
|
||
map.computeIfAbsent(String.valueOf(charArr), key -> new ArrayList<>()).add(str); | ||
} | ||
return map.values().stream().toList(); | ||
} | ||
} |
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,35 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
// insert ์, ๋ฌธ์์ด์ prefix๋ฅผ ๋ค Map์ ์ ์ฅํ๊ณ , ํด๋น ๋ฌธ์์ด์ prefix ์ด๋ฏ๋ก boolean false ๋ก ์ค์ | ||
// prefix๊ฐ ์๋ ์จ์ ํ ๋ฌธ์์ด ์ฝ์ ์ true ๋ก ์ ์ฅ | ||
|
||
// search ์, Map์ ํด๋น ๋จ์ด๊ฐ ์๋์ง ํ์ธํ๊ณ , boolean ๊ฐ์ด true ์ธ์ง ํ์ธ | ||
// startsWith๋ ๊ทธ๋ฅ Map ์ ํด๋น ๋ฌธ์์ด์ด ์๋์ง ํ์ธํ๋ฉด ๋๋ค. | ||
|
||
// ๊ณต๊ฐ ๋ณต์ก๋: Map ํฌ๊ธฐ -> O(N) | ||
// ์๊ฐ ๋ณต์ก๋: ์ ์ฒด ํธ์ถ ์ * String ๊ธธ์ด -> O(N*M) | ||
// ์ฐธ๊ณ ) ์ต๋ ์๊ฐ ๋ณต์ก๋ : 2000 * 3*10^4 = 6*10^7 | ||
class Trie { | ||
|
||
Map<String,Boolean> stringSet; | ||
public Trie() { | ||
stringSet = new HashMap<>(); | ||
} | ||
|
||
public void insert(String word) { | ||
for(int i=0;i<word.length();i++){ | ||
stringSet.putIfAbsent(word.substring(0, i), false); | ||
} | ||
stringSet.put(word, true); | ||
} | ||
|
||
public boolean search(String word) { | ||
return stringSet.containsKey(word) && stringSet.get(word)==true; | ||
} | ||
|
||
public boolean startsWith(String prefix) { | ||
|
||
return stringSet.containsKey(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,36 @@ | ||
import java.util.List; | ||
|
||
|
||
// ํ์ด | ||
// dfs + memo | ||
// memo[i] = true/false : i ์ธ๋ฑ์ค์์ ์์ํด์ wordDict์ ์กด์ฌํ๋ ๋ชจ๋ ๋ถ๋ถ๋ฌธ์์ด ์ฐพ์ ์ ์๋ ๊ฒฝ๋ก๊ฐ ์๋์ง ์ ๋ฌด | ||
//์๊ฐ ๋ณต์ก๋: O(n*n) = start~end(์ฌ๊ท) + start~end(๋ฐ๋ณต๋ฌธ) | ||
// ๊ณต๊ฐ ๋ณต์ก๋: O(n) - ์ฌ๊ท ๊น์ด | ||
|
||
class Solution { | ||
|
||
public boolean wordBreak(String s, List<String> wordDict) { | ||
return dfs(0, s.length(), s, wordDict, new Boolean[s.length()]); | ||
|
||
} | ||
public boolean dfs(int start, int len, String s, List<String> wordDict, Boolean[] memo){ | ||
if(start==len){ | ||
return true; | ||
} | ||
|
||
if(memo[start]!=null) return memo[start]; | ||
|
||
for(int end=start+1; end<=len;end++){ | ||
if(wordDict.contains(s.substring(start, end))){ | ||
if(dfs(end, len, s, wordDict, memo)){ | ||
memo[start] = true; | ||
return true; | ||
} | ||
} | ||
} | ||
|
||
memo[start]=false; | ||
return false; | ||
|
||
} | ||
} |