-
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 #706 from eunhwa99/main
[eunhwa99] Week 02 Solutions
- Loading branch information
Showing
5 changed files
with
239 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,69 @@ | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
/** | ||
* ๋ฌธ์ ํ์ด | ||
*/ | ||
// -4 -1 -1 0 2 2 | ||
// p1 p2 p3 sum < 0 -> p2 ์์ผ๋ก | ||
// p1 p2 p3 sum < 0 -> p2 ์์ผ๋ก | ||
// p1 p2 p3 sum < 0 -> p2 ์์ผ๋ก | ||
// p1 p2p3 sum = 0 -> p1 ์์ผ๋ก | ||
// p1 p2 p3 sum = 0 -> p3 ๊ฐ ๋ค๋ฅธ ๊ฒ ๋์ฌ ๋๊น์ง ์ด๋ | ||
// p1 p2 p3 sum < 0 -> p2 ์์ผ๋ก ์ธ๋ฐ, p2 > p3 ๋๋ฏ๋ก p1 ์์ผ๋ก | ||
// p1 p2 p3 sum = 0 ๋ฐ๋ณต | ||
|
||
/** | ||
* ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ | ||
*/ | ||
// ์๊ฐ ๋ณต์ก๋ - ์ํ ํ์: n + (n-1) + (n-2) + .. => O(N^2) | ||
// ๊ณต๊ฐ ๋ณต์ก๋ - ๋ฐฐ์ด์ ์ ๋ ฌํ๋ ๋ฐ O(n log n)์ ๊ณต๊ฐ + ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ๋ answer ๋ฆฌ์คํธ๋ ๋ฌธ์ ์ ์๊ตฌ์ ๋ฐ๋ผ O(k)์ ๊ณต๊ฐ = O(n log n) (๋ฐฐ์ด ์ ๋ ฌ์ ์ํ ๊ณต๊ฐ) + O(k) (๊ฒฐ๊ณผ ์ ์ฅ ๊ณต๊ฐ) | ||
|
||
class Solution { | ||
public List<List<Integer>> threeSum(int[] nums) { | ||
Arrays.sort(nums); // Sort the array first | ||
List<List<Integer>> answer = new ArrayList<>(); | ||
|
||
for (int pointer1 = 0; pointer1 < nums.length - 2; pointer1++) { | ||
// pointer1 ์ ์ค๋ณต ๊ฐ skip | ||
if (pointer1 > 0 && nums[pointer1] == nums[pointer1 - 1]) { | ||
continue; | ||
} | ||
|
||
int pointer2 = pointer1 + 1; // pointer2 ๋ pointer1 ์ ํ ์นธ ์ | ||
int pointer3 = nums.length - 1; // pointer3 ๋ ๋์์ ๋ถํฐ | ||
|
||
while (pointer2 < pointer3) { | ||
int sum = nums[pointer1] + nums[pointer2] + nums[pointer3]; | ||
|
||
if (sum < 0) { | ||
pointer2++; | ||
} else if (sum > 0) { | ||
pointer3--; | ||
} else { | ||
// sum == 0 | ||
answer.add(Arrays.asList(nums[pointer1], nums[pointer2], nums[pointer3])); | ||
|
||
// pointer2 ์ค๋ณต ๊ฐ ์ ๊ฑฐ | ||
while (pointer2 < pointer3 && nums[pointer2] == nums[pointer2 + 1]) { | ||
pointer2++; | ||
} | ||
|
||
// pointer3 ์ค๋ณต ๊ฐ ์ ๊ฑฐ | ||
while (pointer2 < pointer3 && nums[pointer3] == nums[pointer3 - 1]) { | ||
pointer3--; | ||
} | ||
|
||
// ๋ ๊ฐ ๋ชจ๋ move | ||
pointer2++; | ||
pointer3--; | ||
} | ||
} | ||
} | ||
|
||
return answer; | ||
} | ||
} | ||
|
||
|
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,25 @@ | ||
/** | ||
* ๋ฌธ์ ํ์ด | ||
*/ | ||
// n=2 (1,1), (2) -> 2 ๊ฐ์ง | ||
// n=3 (n=2, 1), (n=1, 2) -> 2 + 1 = 3๊ฐ์ง | ||
// n=4 (n=3, 1), (n=2, 2) -> 3 + 2 = 5๊ฐ์ง | ||
// n=5 (n=4, 1) , (n=3, 2) | ||
// n=k (n=k-1, 1), (n=k-2, 2) | ||
|
||
/** | ||
* ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ | ||
*/ | ||
// ์๊ฐ ๋ณต์ก๋: ๊ฐ ์นธ์ ํ ๋ฒ์ฉ ๋ฐฉ๋ฌธ -> O(n) | ||
// ๊ณต๊ฐ ๋ณต์ก๋: DP ๋ฐฐ์ด ํฌ๊ธฐ -> O(n) | ||
class Solution { | ||
public int climbStairs(int n) { | ||
int[] cntArray = new int[n + 1]; | ||
cntArray[0] = 1; | ||
cntArray[1] = 1; | ||
for (int i = 2; i <= n; ++i) { | ||
cntArray[i] = cntArray[i - 1] + cntArray[i - 2]; | ||
} | ||
return cntArray[n]; | ||
} | ||
} |
64 changes: 64 additions & 0 deletions
64
construct-binary-tree-from-preorder-and-inorder-traversal/eunhwa99.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,64 @@ | ||
// ์๊ฐ ๋ณต์ก๋: ํธ๋ฆฌ์ ๋ชจ๋ ๋ ธ๋๋ฅผ ํ ๋ฒ์ฉ๋ง ๋ฐฉ๋ฌธ -> O(n) | ||
// ๊ณต๊ฐ ๋ณต์ก๋: ์ฌ๊ท์ ์ผ๋ก ํธ๋ฆฌ๋ฅผ ๊ตฌ์ฑ -> | ||
// ํธ๋ฆฌ๊ฐ ๊ท ํ ์กํ ๊ฒฝ์ฐ(์ฆ, ํธ๋ฆฌ์ ๋์ด๊ฐ log(n)์ธ ๊ฒฝ์ฐ), ์ฌ๊ท ํธ์ถ ์คํ์ ๊น์ด๋ O(log n) | ||
// ํธ๋ฆฌ๊ฐ ํธํฅ๋ ํํ(์: ๋ชจ๋ ์ผ์ชฝ ์์๋ง ์กด์ฌํ๋ ๊ฒฝ์ฐ)๋ผ๋ฉด, ์ฌ๊ท ๊น์ด๋ O(n) | ||
|
||
class TreeNode { | ||
int val; | ||
TreeNode left; | ||
TreeNode right; | ||
|
||
TreeNode() {} | ||
|
||
TreeNode(int val) { | ||
this.val = val; | ||
} | ||
|
||
TreeNode(int val, TreeNode left, TreeNode right) { | ||
this.val = val; | ||
this.left = left; | ||
this.right = right; | ||
} | ||
} | ||
|
||
class Solution { | ||
|
||
int preIdx = 0; | ||
|
||
public TreeNode buildTree(int[] preorder, int[] inorder) { | ||
if (preorder == null || inorder == null || preorder.length == 0 || inorder.length == 0) { | ||
return null; | ||
} | ||
|
||
return build(preorder, inorder, 0, inorder.length - 1); | ||
} | ||
|
||
|
||
private TreeNode build(int[] preorder, int[] inorder, int inStart, int inEnd) { | ||
// ์ฌ๊ท ์ข ๋ฃ ์กฐ๊ฑด | ||
// ํฌ์ธํฐ(์ธ๋ฑ์ค)๊ฐ ๋ฐฐ์ด ๊ธธ์ด๋ฅผ ๋์์ | ||
if (preIdx >= preorder.length || inStart > inEnd) { | ||
return null; | ||
} | ||
|
||
// preorder ์ฒซ ๋ฒ์งธ ๊ฐ์ ํด๋น ๋ถ๋ถ ํธ๋ฆฌ์ root ์ด๋ค. | ||
int rootVal = preorder[preIdx++]; | ||
TreeNode root = new TreeNode(rootVal); | ||
|
||
// inOrder ๋ฐฐ์ด์์ root ๊ฐ์ ์์น๋ฅผ ์ฐพ๋๋ค. | ||
int rootIndex = -1; | ||
for (int i = inStart; i <= inEnd; i++) { | ||
if (inorder[i] == rootVal) { | ||
rootIndex = i; | ||
break; | ||
} | ||
} | ||
|
||
// root ๊ฐ์ ๊ธฐ์ค์ผ๋ก inorder ๋ฐฐ์ด์ ์ผ์ชฝ ๋ถ๋ถ ๋ฐฐ์ด(inStart ~ rootIndex-1)์ root์ left tree, | ||
// ์ค๋ฅธ์ชฝ ๋ถ๋ถ ๋ฐฐ์ด(rootIndex+1 ~ inEnd)์ root์ right tree ๊ฐ ๋๋ค. | ||
root.left = build(preorder, inorder, inStart, rootIndex - 1); | ||
root.right = build(preorder, inorder, rootIndex + 1, inEnd); | ||
|
||
return root; | ||
} | ||
} |
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,49 @@ | ||
import java.util.Arrays; | ||
|
||
/** | ||
* ๋ฌธ์ ํ์ด | ||
* ์์ ) 11106 | ||
* ๊ฐ์ฅ ํฐ ์๋ 2์๋ฆฌ ์ด๋ฏ๋ก ํ ๋ฒ์ ๊ฐ ์ ์๋ ์นธ์ 1~2์นธ | ||
* ํ์ฌ ์นธ์ด 0์ผ ๊ฒฝ์ฐ๋ ์นธ ์ด๋ ๋ถ๊ฐ | ||
* ์ฝ๋ ๋ฒ์๋ 1~26 | ||
*/ | ||
|
||
//์๊ฐ ๋ณต์ก๋: ๋ฌธ์์ด์ ๊ฐ ์ธ๋ฑ์ค๋ฅผ ํ ๋ฒ์ฉ๋ง ์ฒ๋ฆฌํ๋ฏ๋ก ์ ์ฒด O(n) | ||
//๊ณต๊ฐ ๋ณต์ก๋: dp ๋ฐฐ์ด์ ๋ฌธ์์ด์ ๊ธธ์ด์ ๋น๋กํ์ฌ O(n) ๊ณต๊ฐ์ ์ฐจ์ง | ||
|
||
class Solution { | ||
|
||
int stringSize = 0; | ||
int[] dp; | ||
|
||
// DP ์ด์ฉ | ||
public int numDecodings(String s) { | ||
stringSize = s.length(); | ||
dp = new int[stringSize + 1]; | ||
Arrays.fill(dp, -1); | ||
return numDecodingHelper(s.toCharArray(), 0); | ||
} | ||
|
||
// dp -> O(N) | ||
private int numDecodingHelper(char[] s, int curIndex) { | ||
if (stringSize == curIndex) return 1; | ||
if (s[curIndex] == '0') return 0; // ํ์ฌ ์นธ์ด 0 -> ์ ์ง ๋ถ๊ฐ | ||
if (dp[curIndex] != -1) return dp[curIndex]; | ||
|
||
dp[curIndex] = 0; // ํ์ฌ ๋ ธ๋ ๋ฐฉ๋ฌธ ์ฒดํฌ | ||
dp[curIndex] += numDecodingHelper(s, curIndex + 1); // ํ ์นธ ์ ์ง | ||
|
||
if ((curIndex + 1 < stringSize) && checkRange(s[curIndex], s[curIndex + 1])) // 2์๋ฆฌ ์ฝ๋๊ฐ 10~26 ์์ ๋ค์ด๊ฐ๋ค๋ฉด | ||
dp[curIndex] += numDecodingHelper(s, curIndex + 2); // 2์นธ ์ ์ง | ||
|
||
return dp[curIndex]; | ||
} | ||
|
||
private boolean checkRange(char left, char right) { | ||
int leftNum = left - '0'; | ||
int rightNum = right - '0'; // ์ซ์๋ก ๋ณํ | ||
|
||
int num = leftNum * 10 + rightNum; | ||
return (num >= 10 && num <= 26); | ||
} | ||
} |
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 @@ | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
/** | ||
* ์๊ฐ/๊ณต๊ฐ ๋ณต์ก๋ | ||
*/ | ||
// ์๊ฐ ๋ณต์ก๋: ๋ฌธ์์ด์ ํ ๋ฒ์ฉ๋ง ๋ฐฉ๋ฌธ -> O(n) | ||
// ๊ณต๊ฐ ๋ณต์ก๋: ๋ฌธ์์ด ๊ธธ์ด๋งํผ ๊ณต๊ฐ ํ์(hashmap ํฌ๊ธฐ) -> O(n) | ||
class Solution { | ||
public boolean isAnagram(String s, String t) { | ||
|
||
// s ์์ ๋ฌธ์๋ค์ด t ์๋ ๋์ผํ ํ์๋ก ๋ฑ์ฅํ๋ ์ง ํ์ธ | ||
if (s.length() != t.length()) return false; // ๋ ๋ฌธ์์ด์ ๊ธธ์ด๊ฐ ๋ค๋ฅด๋ค๋ฉด ์๋๊ทธ๋จ์ด ์๋๋ค. | ||
|
||
// ๋ฌธ์๋ณ ํ์ ์ ์ฅ map | ||
Map<Character, Integer> sAlphabetCountMap = new HashMap<>(); | ||
for (char c : s.toCharArray()) { // ์๊ฐ๋ณต์ก๋: O(n) | ||
sAlphabetCountMap.put(c, sAlphabetCountMap.getOrDefault(c, 0) + 1); | ||
} | ||
|
||
for (char c : t.toCharArray()) { // ์๊ฐ๋ณต์ก๋: O(n) | ||
if (!sAlphabetCountMap.containsKey(c)) return false; // s์ t๊ฐ ๊ฐ์ง ๋ฌธ์์ด์ด ์๋ค๋ฉด ์๋๊ทธ๋จ์ด ์๋๋ค. | ||
|
||
int count = sAlphabetCountMap.get(c) - 1; | ||
if (count == 0) sAlphabetCountMap.remove(c); | ||
else sAlphabetCountMap.put(c, count); | ||
} | ||
|
||
// ๋ชจ๋ ๋ฌธ์๊ฐ ์ผ์นํ๋ฉด ํด์๋งต์ด ๋น์ด ์์ด์ผ ํจ | ||
return sAlphabetCountMap.isEmpty(); | ||
} | ||
} |