Skip to content

Commit

Permalink
Merge pull request #706 from eunhwa99/main
Browse files Browse the repository at this point in the history
[eunhwa99] Week 02 Solutions
  • Loading branch information
eunhwa99 authored Dec 21, 2024
2 parents 2007a35 + b09bf5b commit 8aa48c4
Show file tree
Hide file tree
Showing 5 changed files with 239 additions and 0 deletions.
69 changes: 69 additions & 0 deletions 3sum/eunhwa99.java
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;
}
}


25 changes: 25 additions & 0 deletions climbing-stairs/eunhwa99.java
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];
}
}
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;
}
}
49 changes: 49 additions & 0 deletions decode-ways/eunhwa99.java
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);
}
}
32 changes: 32 additions & 0 deletions valid-anagram/eunhwa99.java
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();
}
}

0 comments on commit 8aa48c4

Please sign in to comment.