-
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 #740 from kdh-92/main
[κΆλν] Week 2
- Loading branch information
Showing
4 changed files
with
158 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,38 @@ | ||
/** | ||
* νΉμ΄μ¬ν | ||
* λ¬Έμ λ₯Ό μ λλ‘ μ΄ν΄ λͺ»ν΄μ νμ΄λ₯Ό 보며 μ΄ν΄νλ€. | ||
* ν΅μ¬μ μ€λ³΅ μ²λ¦¬μ λ¬Έμ λ₯Ό μ°Ύμμ λ μ’ λ£κ° μλλΌ λ€μ νλͺ©μ μ°Ύλ λΆλΆμΈ κ² κ°λ€. | ||
* (μΆν λ³΅μ΅ μμ ) | ||
*/ | ||
|
||
class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
|
||
// (1) ArrayList | ||
// μκ°λ³΅μ‘λ : O(N^2), 곡κ°λ³΅μ‘λ : O(N) | ||
List<List<Integer>> result = new ArrayList<>(); | ||
Arrays.sort(nums); | ||
|
||
for (int i = 0; i < nums.length; i++) { | ||
if (i > 0 && nums[i] == nums[i - 1]) continue; | ||
|
||
int j = i + 1; | ||
int k = nums.length - 1; | ||
|
||
while (j < k) { | ||
int sumNum = nums[i] + nums[j] + nums[k]; | ||
|
||
if (sumNum > 0) k--; | ||
else if (sumNum < 0) j++; | ||
else { | ||
result.add(Arrays.asList(nums[i], nums[j], nums[k])); | ||
j++; | ||
|
||
while (nums[j] == nums[j - 1] && j < k) j++; | ||
} | ||
} | ||
} | ||
|
||
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,18 @@ | ||
class Solution { | ||
public int climbStairs(int n) { | ||
// dp μμ© λ²μ | ||
// μκ°λ³΅μ‘λ : O(N), 곡κ°λ³΅μ‘λ : O(1) | ||
|
||
int prev = 1, curr = 1; | ||
|
||
if (n == 1) return prev; | ||
|
||
for (int i = 2; i < n; i++) { | ||
int now = curr + prev; | ||
prev = curr; | ||
curr = now; | ||
} | ||
|
||
return curr; | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
construct-binary-tree-from-preorder-and-inorder-traversal/kdh-92.java
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 @@ | ||
/** | ||
* νΉμ΄μ¬ν | ||
* TreeNode μ΄ν΄ & right μ€μ λΆλΆ μ΄ν΄ νμ | ||
* - μ¬κ· νΈμΆ ννλ‘ if 쑰건 μΆκ° (preStart > preorder.length -1 || inStart > inEnd) μΌ κ²½μ°μ null λ°ν | ||
* - nullμΌ λ μν© ν μ€νΈνλ©° μ’ λ μ΄ν΄λ³΄κΈ° | ||
* | ||
* - right | ||
* - preStart : preStart + inIdx - inStart + 1 (left λμΌνκ² preStart + 1μμ inIdx - inStart λ₯Ό μΆκ°ν΄μ μ€λ₯Έμͺ½ μμ νμ) | ||
*/ | ||
|
||
class Solution { | ||
public static TreeNode buildTree(int[] preorder, int[] inorder) { | ||
// (1) helper λ©μλλ₯Ό μ¬κ·νΈμΆ μ¬μ© | ||
// μκ°λ³΅μ‘λ : O(N^2), 곡κ°λ³΅μ‘λ : O(N) | ||
|
||
return helper(0, 0, inorder.length - 1, preorder, inorder); | ||
} | ||
|
||
public static TreeNode helper(int preStart, int inStart, int inEnd, int[] preorder, int[] inorder) { | ||
if (preStart > preorder.length - 1 || inStart > inEnd) return null; | ||
TreeNode node = new TreeNode(preorder[preStart]); | ||
int inIdx = 0; | ||
|
||
for (int i = inStart; i <= inEnd; i++) { | ||
if (node.val == inorder[i]) { | ||
inIdx = i; | ||
break; | ||
} | ||
} | ||
|
||
node.left = helper(preStart + 1, inStart, inIdx - 1, preorder, inorder); | ||
node.right = helper(preStart + inIdx - inStart + 1, inIdx + 1, inEnd, preorder, inorder); | ||
|
||
return node; | ||
} | ||
} |
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,66 @@ | ||
/** | ||
* νΉμ΄μ¬ν | ||
* μκ°λ³΅μ‘λκ° (1)λ² νμ΄λ O(N log N) / (2), (3)λ² νμ΄λ O(N)μΈλ° | ||
* 리νΈμ½λμμ Runtime - (1)λ² : 3ms, (3)λ² : 6ms, (2)λ² : 13ms | ||
* | ||
* μκ°λ³΅μ‘λλ‘λ§ λ°μ§λ©΄ λΉμ°ν (2), (3)λ² νμ΄κ° λΉ¨λΌμΌνλλ°λ λΆκ΅¬νκ³ μ€νλ € (1)λ² νμ΄κ° λ λΉ λ₯Έ κ²°κ³Όκ° λμ¨λ€. | ||
* chatgpt νμΈνμ λ κ°μ₯ ν° μ°¨μ΄μ μ λ¬Έμμ΄μ λ°°μ΄λ‘ λ³κ²½ν΄μ μ λ ¬μ νλλ° μ΄ λ μ λ ¬ μκ³ λ¦¬μ¦μ μ±λ₯μ λ°λΌ μ°¨μ΄κ° λλ€λ λ΄μ©μ΄ μμλ€. | ||
* Nμ΄ λμμ§μλ‘ (2), (3)μ μ±λ₯μ΄ μ’μμ§ κ²μΌλ‘ μμλλ ν΄λΉ λ¬Έμ μ κ²°κ³Όμμλ (1)μ μ±λ₯μ΄ μ’κ² λμ¬ μ μλ€λ κ²μ μκ² λμλ€. | ||
*/ | ||
|
||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
// (1) λ¬Έμ λ°°μ΄ - μ λ ¬ & λΉκ΅ | ||
// μκ°λ³΅μ‘λ : O(N log N), 곡κ°λ³΅μ‘λ : O(N) | ||
|
||
// char[] sArr = s.toCharArray(); | ||
// char[] tArr = t.toCharArray(); | ||
// Arrays.sort(sArr); | ||
// Arrays.sort(tArr); | ||
|
||
// return new String(sArr).equals(new String(tArr)); // λ¬Έμμ΄μ λΉκ΅νλ€ μκ°νμ λ λ°©λ² | ||
// return Arrays.equals(sArr, tArr); // char λ°°μ΄ μ체λ₯Ό λΉκ΅ | ||
|
||
// (2) HashMap μ΄μ©ν΄ μνλ²³ κ°μ μ²΄ν¬ | ||
// μκ°λ³΅μ‘λ : O(N), 곡κ°λ³΅μ‘λ : O(N) | ||
|
||
// Map<Character, Integer> count = new HashMap<>(); | ||
|
||
// for (char x : s.toCharArray()) { | ||
// count.put(x, count.getOrDefault(x, 0) + 1); | ||
// } | ||
|
||
// for (char x : t.toCharArray()) { | ||
// count.put(x, count.getOrDefault(x, 0) - 1); | ||
// } | ||
|
||
// for (int val : count.values()) { | ||
// if (val != 0) { | ||
// return false; | ||
// } | ||
// } | ||
|
||
// return true; | ||
|
||
// (3) λ°°μ΄λ‘ μνλ²³ κ°μ μ²΄ν¬ | ||
// μκ°λ³΅μ‘λ : O(N), 곡κ°λ³΅μ‘λ : O(N) | ||
|
||
// if (s.length() != t.length()) { | ||
// return false; | ||
// } | ||
|
||
// int[] freq = new int[26]; | ||
// for (int i = 0; i < s.length(); i++) { | ||
// freq[s.charAt(i) - 'a']++; | ||
// freq[t.charAt(i) - 'a']--; | ||
// } | ||
|
||
// for (int i = 0; i < freq.length; i++) { | ||
// if (freq[i] != 0) { | ||
// return false; | ||
// } | ||
// } | ||
|
||
// return true; | ||
} | ||
} |