forked from DaleStudy/leetcode-study
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9bfe4f6
commit c026aed
Showing
5 changed files
with
229 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,40 @@ | ||
// ν¬ν¬μΈν°λ₯Ό μΈ νμ μμ. κ·Έλ₯ μ΅μκ°μ μ°Ύμ λΉΌμ£Όλ©΄ O(N)μ μκ° λ³΅μ‘λλ₯Ό μ»μ μ μλ€. | ||
// ν¬ν¬μΈν°μ ν° μ°¨μ΄κ° μλ μ΄μ λ ν¬ν¬μΈν°λ O(N)μ΄κΈ° λλ¬Έ. μμ£Ό μ½κ°μ 곡κ°λ³΅μ‘λ μ°¨μ΄λ§ μμ λ― | ||
// κ²°κ³Όλ ν° μ°¨μ΄ μμΌλ μκ³ λ¦¬μ¦ μμ²΄κ° λ μ½λ€. | ||
class Solution { | ||
public int maxProfit(int[] prices) { | ||
int minPrice = Integer.MAX_VALUE; | ||
int maxProfit = 0; | ||
|
||
for (int price : prices) { | ||
if (price < minPrice) { | ||
minPrice = price; | ||
} else { | ||
maxProfit = Math.max(maxProfit, price - minPrice); | ||
} | ||
} | ||
|
||
return maxProfit; | ||
} | ||
} | ||
|
||
// μ²μ μκ°ν ν¬ν¬μΈν° λ°©μ | ||
class Solution { | ||
public int maxProfit(int[] prices) { | ||
int left = 0; | ||
int right = 1; | ||
int maxProfit = 0; | ||
|
||
while (right < prices.length) { | ||
if (prices[left] < prices[right]) { | ||
int profit = prices[right] - prices[left]; | ||
maxProfit = Math.max(maxProfit, profit); | ||
} else { | ||
left = right; | ||
} | ||
right++; | ||
} | ||
|
||
return maxProfit; | ||
} | ||
} |
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,61 @@ | ||
// μμ΄λμ΄λ‘ νΈλ λ¬Έμ λΌ μ νΈνμ§ μλ λ¬Έμ ... | ||
// κ·Έλ₯ μ¬μ©νμ§ μλ κ²μ ꡬλΆμλ‘ λκ³ μ€νλ¦Ώνλκ² κ°μ₯ νΈνλ€. μμ λμ€μ§ μμ λ¬Έμλ₯Ό κΈ°μ€μΌλ‘ λλ©΄ κΈΈμ΄λ₯Ό μ νμκ° μκΈ° λλ¬Έ | ||
public class Solution { | ||
// μΈμ½λ© λ©μλ | ||
public String encode(List<String> strs) { | ||
StringBuilder encodedString = new StringBuilder(); | ||
|
||
for (String str : strs) { | ||
encodedString.append(str.length()).append("#").append(str); | ||
} | ||
|
||
return encodedString.toString(); | ||
} | ||
|
||
// λμ½λ© λ©μλ | ||
public List<String> decode(String s) { | ||
List<String> decodedList = 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)); | ||
decodedList.add(s.substring(j + 1, j + 1 + length)); | ||
|
||
i = j + 1 + length; | ||
} | ||
|
||
return decodedList; | ||
} | ||
} | ||
// πλ₯Ό κΈ°μ€μΌλ‘ λ¬Έμμ΄μ λΆλ¦¬ | ||
// @!#$@#$ μ΄λ°κ±Έ μ€νλ¦Ώ λ¬Έμλ‘ λλ λ°©λ²λ μλ€.μμ΄μ¨ | ||
public class Solution { | ||
|
||
public String encode(List<String> strs) { | ||
StringBuilder encodedString = new StringBuilder(); | ||
|
||
for (String str : strs) { | ||
encodedString.append(str).append("π"); | ||
} | ||
|
||
return encodedString.toString(); | ||
} | ||
|
||
public List<String> decode(String s) { | ||
String[] parts = s.split("π"); | ||
List<String> decodedList = new ArrayList<>(); | ||
for (String part : parts) { | ||
if (!part.isEmpty()) { | ||
decodedList.add(part); | ||
} | ||
} | ||
|
||
return decodedList; | ||
} | ||
} |
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,16 @@ | ||
// λͺ¨λ λ¨μ΄λ₯Ό μ λ ¬ν΄μΌνκΈ° λλ¬Έμ μκ° λ³΅μ‘λκ° κ½€ λμ€λ λ¬Έμ | ||
// νμ§λ§ λͺ¨λ λ¨μ΄λ₯Ό μ λ ¬ν΄λ λλ€λ건 κΈμ μκ° 100μ μ νμ΄ μμ΄μ μ μΆν μ μλ€. | ||
// O(N) * O(MlogM) N λ°°μ΄ κΈΈμ΄, M κΈμμμ μκ°λ³΅μ‘λκ° λμ΄ | ||
class Solution { | ||
public List<List<String>> groupAnagrams(String[] strs) { | ||
HashMap<String,List<String>> anagrams = new HashMap<>(); | ||
for(String str: strs) { | ||
char[] charArray = str.toCharArray(); | ||
Arrays.sort(charArray); | ||
String sortedWord = new String(charArray); | ||
anagrams.computeIfAbsent(sortedWord, k -> new ArrayList<>()).add(str); | ||
} | ||
|
||
return new ArrayList<>(anagrams.values()); | ||
} | ||
} |
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,92 @@ | ||
// GPTμ νμ΄. νΈλ¦¬λ₯Ό μ΄μ©ν΄ O(m)μΌλ‘ ν΄κ²°νλ€. | ||
public class Trie { | ||
private class TrieNode { | ||
TrieNode[] children; | ||
boolean isEndOfWord; | ||
|
||
public TrieNode() { | ||
children = new TrieNode[26]; // μνλ²³ a~z (26κ°μ μμ λ Έλ) | ||
isEndOfWord = false; // ν΄λΉ λ Έλκ° λ¨μ΄μ λμΈμ§ μλμ§ λνλ | ||
} | ||
} | ||
|
||
private TrieNode root; | ||
|
||
public Trie() { | ||
root = new TrieNode(); // λ£¨νΈ λ Έλ μ΄κΈ°ν | ||
} | ||
|
||
public void insert(String word) { | ||
TrieNode node = root; | ||
for (char c : word.toCharArray()) { | ||
int index = c - 'a'; // μνλ²³μ 0-25μ μ«μλ‘ λ³ν | ||
if (node.children[index] == null) { | ||
node.children[index] = new TrieNode(); // ν΄λΉ λ¬Έμκ° μμΌλ©΄ μ λ Έλλ₯Ό μμ± | ||
} | ||
node = node.children[index]; // μμ λ Έλλ‘ μ΄λ | ||
} | ||
node.isEndOfWord = true; // λ¨μ΄μ λμ νμ | ||
} | ||
|
||
public boolean search(String word) { | ||
TrieNode node = root; | ||
for (char c : word.toCharArray()) { | ||
int index = c - 'a'; | ||
if (node.children[index] == null) { | ||
return false; // ν΄λΉ λ¬Έμκ° μμΌλ©΄ false λ°ν | ||
} | ||
node = node.children[index]; // μμ λ Έλλ‘ μ΄λ | ||
} | ||
return node.isEndOfWord; // λ¨μ΄μ λμΈμ§λ₯Ό νμΈ | ||
} | ||
|
||
public boolean startsWith(String prefix) { | ||
TrieNode node = root; | ||
for (char c : prefix.toCharArray()) { | ||
int index = c - 'a'; | ||
if (node.children[index] == null) { | ||
return false; // ν΄λΉ μ λμ¬λ‘ μμνλ λ¨μ΄κ° μμΌλ©΄ false λ°ν | ||
} | ||
node = node.children[index]; // μμ λ Έλλ‘ μ΄λ | ||
} | ||
return true; // μ λμ¬λ‘ μμνλ λ¨μ΄κ° μμΌλ©΄ true λ°ν | ||
} | ||
} | ||
|
||
// λ°±νΈλνΉμΌλ‘ νμ΄λ΄€λλ°, μλκ° λ무 μλμμ | ||
// O(n*m)μ μκ°λ³΅μ‘λκ° λμ΄. nμ κΈμμ mμ κΈμκΈΈμ΄ | ||
class Trie { | ||
|
||
private List<String> words; | ||
|
||
public Trie() { | ||
words = new ArrayList<>(); | ||
} | ||
|
||
public void insert(String word) { | ||
words.add(word); | ||
} | ||
|
||
public boolean search(String word) { | ||
return backtrack(word, true); | ||
} | ||
|
||
public boolean startsWith(String prefix) { | ||
return backtrack(prefix, false); | ||
} | ||
|
||
private boolean backtrack(String target, boolean exactMatch) { | ||
for (String word : words) { | ||
if (exactMatch) { | ||
if (word.equals(target)) { | ||
return true; | ||
} | ||
} else { | ||
if (word.startsWith(target)) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} |
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,20 @@ | ||
// dp λ°°μ΄ (dp[i] = s[0...i]κ° λ¨μ΄λ€λ‘ λλ μ§ μ μλμ§ μ¬λΆ) | ||
// μ λ ₯ λ°μ΄ν°μ ν¬κΈ°κ° ν¬μ§ μμμ O(N^2)λ κ°λ₯ν λ¬Έμ . | ||
class Solution { | ||
public boolean wordBreak(String s, List<String> wordDict) { | ||
Set<String> wordSet = new HashSet<>(wordDict); | ||
boolean[] dp = new boolean[s.length() + 1]; | ||
dp[0] = true; | ||
|
||
for (int i = 1; i <= s.length(); i++) { | ||
for (int j = 0; j < i; j++) { | ||
if (dp[j] && wordSet.contains(s.substring(j, i))) { | ||
dp[i] = true; | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return dp[s.length()]; | ||
} | ||
} |