Skip to content

Commit

Permalink
Merge pull request #721 from YeomChaeeun/main
Browse files Browse the repository at this point in the history
[YeomChaeeun] Week 2
  • Loading branch information
SamTheKorean authored Dec 22, 2024
2 parents 1a83e1d + 0ae2dba commit 672bf62
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 0 deletions.
42 changes: 42 additions & 0 deletions 3sum/YeomChaeeun.ts
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
}
26 changes: 26 additions & 0 deletions climbing-stairs/YeomChaeeun.ts
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;
}
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)
}
24 changes: 24 additions & 0 deletions valid-anagram/YeomChaeEun.ts
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;
}

0 comments on commit 672bf62

Please sign in to comment.