-
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 #721 from YeomChaeeun/main
[YeomChaeeun] Week 2
- Loading branch information
Showing
4 changed files
with
134 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,42 @@ | ||
/** | ||
* ์ธ ์ซ์์ ํฉ์ด 0์ด ๋๋ ์กฐํฉ ์ฐพ๊ธฐ | ||
* ์๊ณ ๋ฆฌ์ฆ ๋ณต์ก๋: | ||
* - ์๊ฐ๋ณต์ก๋: O(n^2) | ||
* - ๊ณต๊ฐ๋ณต์ก๋: O(1) | ||
* @param nums | ||
*/ | ||
function threeSum(nums: number[]): number[][] { | ||
// ์ ๋ ฌ | ||
nums.sort((a, b) => a - b) | ||
let result: number[][] = [] | ||
|
||
// ํฌํฌ์ธํฐ | ||
for (let i = 0; i < nums.length - 2; i++) { | ||
if (i > 0 && nums[i] === nums[i - 1]) continue; | ||
|
||
let start = i + 1 | ||
let end = nums.length - 1 | ||
const target = -nums[i] // ๊ณ ์ ์ซ์๋ฅผ ์ด์ฉ | ||
// start + end + target === 0 ์ด๋ฏ๋ก, start + end === -target | ||
|
||
while (start < end) { | ||
const sum = nums[start] + nums[end] | ||
if (sum === target) { | ||
result.push([nums[i], nums[start], nums[end]]) | ||
|
||
// ๋ฐฐ์ด ์ค๋ณต ๊ฐ ๊ฑด๋๋ฐ๊ธฐ | ||
while (start < end && nums[start] === nums[start + 1]) start++ | ||
while (start < end && nums[start] === nums[end - 1]) end-- | ||
|
||
// ํฌ์ธํฐ ์ด๋ | ||
start++ | ||
end-- | ||
} else if (sum < target) { | ||
start++ | ||
} else { | ||
end-- | ||
} | ||
} | ||
} | ||
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,26 @@ | ||
/** | ||
* ๊ณ๋จ ์ค๋ฅด๊ธฐ | ||
* ์๊ณ ๋ฆฌ์ฆ ๋ณต์ก๋: | ||
* - ์๊ฐ๋ณต์ก๋: O(n) - ์ ๋ ฅ๊ฐ ํฌ๊ธฐ์ ๋น๋กํ๋ ๋จ์ผ ๋ฐ๋ณต๋ฌธ | ||
* - ๊ณต๊ฐ๋ณต์ก๋: O(1) - ์์ ๊ฐ์ ๋ณ์๋ง ์ฌ์ฉ | ||
* @param n | ||
*/ | ||
function climbStairs(n: number): number { | ||
// 1 - 2 - 3 - 5 - 8 ... ๊ท์น ๋ฐ์ | ||
if(n <= 3) return n | ||
|
||
// ์ ๊ทผ (1) - ์๊ฐ๋ณต์ก๋๊ฐ ๋๋ฌด ํผ | ||
// return climbStairs(n - 1) + climbStairs(n - 2) // ์๊ฐ | ||
|
||
// ์ ๊ทผ (2) | ||
// ํผ๋ณด๋์น ์์ด๊ณผ ๋น์ทํ ์์ ๋ ์ซ์๋ฅผ ๋ํด์ ๋ฐฐ์ด ๊ตฌ์กฐ๋ฅผ ๋ง๋ฌ | ||
let current = 1; // ํ์ฌ ๋ฐฉ๋ฒ | ||
let prev = 1; // ์ด์ ๋จ๊ณ์ ๋ฐฉ๋ฒ | ||
|
||
// n-1๋ฒ ๋ฐ๋ณตํ์ฌ ๊ณ์ฐ | ||
for (let i = 1; i < n; i++) { | ||
[current, prev] = [current + prev, current]; | ||
} | ||
|
||
return current; | ||
} |
42 changes: 42 additions & 0 deletions
42
construct-binary-tree-from-preorder-and-inorder-traversal/YeomChaeeun.ts
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,42 @@ | ||
/** | ||
* Definition for a binary tree node. | ||
* class TreeNode { | ||
* val: number | ||
* left: TreeNode | null | ||
* right: TreeNode | null | ||
* constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) { | ||
* this.val = (val===undefined ? 0 : val) | ||
* this.left = (left===undefined ? null : left) | ||
* this.right = (right===undefined ? null : right) | ||
* } | ||
* } | ||
*/ | ||
/** | ||
* ์ด์งํธ๋ฆฌ ๋ง๋ค๊ธฐ | ||
* ์๊ณ ๋ฆฌ์ฆ ๋ณต์ก๋: | ||
* - ์๊ฐ๋ณต์ก๋: O(n^2) | ||
* - ๊ณต๊ฐ๋ณต์ก๋: O(n^2) | ||
*/ | ||
function buildTree(preorder: number[], inorder: number[]): TreeNode | null { | ||
// ์ ์ ์ํ(preorder): ์ต์์ ๋ ธ๋ -> ์ข์ธก ์๋ธํธ๋ฆฌ -> ์ฐ์ธก ์๋ธํธ๋ฆฌ | ||
// ์ค์ ์ํ(inorder): ์ข์ธก ์๋ธํธ๋ฆฌ -> ์ต์์ ๋ ธ๋ -> ์ฐ์ธก ์๋ธํธ๋ฆฌ | ||
|
||
// ์ฌ๊ท์ ์ผ๋ก ํธ์ถํ๊ธฐ ์ํด ๋ฐฐ์ด์ด ๋น์์๋ null์ ๋ฐํํ๋ฉฐ ์ข ๋ฃ์ํด | ||
if (preorder.length === 0 || inorder.length === 0) { | ||
return null | ||
} | ||
|
||
let root = preorder[0] | ||
let mid = inorder.findIndex((value) => value === root) | ||
|
||
let leftInorder = inorder.slice(0, mid) | ||
let rightInorder = inorder.slice(mid+1) | ||
|
||
let leftPreorder = preorder.slice(1, 1 + leftInorder.length) | ||
let rightPreorder = preorder.slice(1 + leftInorder.length) | ||
|
||
let left = buildTree(leftPreorder, leftInorder) | ||
let right = buildTree(rightPreorder, rightInorder) | ||
|
||
return new TreeNode(root, left, right) | ||
} |
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,24 @@ | ||
/** | ||
* ์๊ณ ๋ฆฌ์ฆ ๋ณต์ก๋: | ||
* - ์๊ฐ๋ณต์ก๋: O(n) | ||
* - ๊ณต๊ฐ๋ณต์ก๋: O(k) | ||
* @param s | ||
* @param t | ||
*/ | ||
function isAnagram(s: string, t: string): boolean { | ||
if(s.length !== t.length) return false | ||
|
||
let counter = {} | ||
for(let sValue of s) { | ||
if(!counter[sValue]) { counter[sValue] = 1 } | ||
else { counter[sValue]++ } | ||
|
||
} | ||
|
||
for(let tValue of t) { | ||
if(!counter[tValue]) return false | ||
else counter[tValue]-- | ||
} | ||
|
||
return Object.values(counter).findIndex(value => value !== 0) < 0; | ||
} |