Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions 3sum/sadie100.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
배열을 정렬한 뒤 엘리먼트 하나를 고정하고, 해당 엘리먼트보다 큰 범위에서 투 포인터 순회로 합이 -{엘리먼트값} 이 되는 쌍을 찾는 로직을 모든 배열 엘리먼트에 순회하며 적용한다

시간복잡도 : O(N^2)
=> 엘리먼트 고정 순회 n * 투 포인터 순회 n

공간복잡도 : O(N)
=> 정렬된 배열
*/

function threeSum(nums: number[]): number[][] {
const sortedNums = nums.sort((a, b) => a - b)
const result = []
let beforeIVal

for (let i = 0; i < nums.length; i++) {
const idxVal = sortedNums[i]
if (beforeIVal !== undefined && beforeIVal === idxVal) {
continue
}
beforeIVal = idxVal
const shouldBe = -idxVal
let start = i + 1
let end = nums.length - 1

while (start < end) {
const startVal = sortedNums[start]
const endVal = sortedNums[end]
const value = startVal + endVal
if (value === shouldBe) {
result.push([idxVal, startVal, endVal])
start += 1
end -= 1
while (startVal === sortedNums[start]) {
start += 1
}
while (endVal === sortedNums[end]) {
end -= 1
}
} else if (value > shouldBe) {
end -= 1
} else {
start += 1
}
}
}

return result
}
22 changes: 22 additions & 0 deletions climbing-stairs/sadie100.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*

n+1개의 배열을 만들고 순회를 돌리며 i-1, i-2를 합한다

시간복잡도 : O(N) => n 배열 순회
공간복잡도 : O(N) => n+1 배열
*/

function climbStairs(n: number): number {
const stair = new Array(n + 1).fill(0)

stair[0] = 1
stair[1] = 1

if (n < 1) return stair[n]

for (let i = 2; i < n + 1; i++) {
stair[i] = stair[i - 1] + stair[i - 2]
}

return stair[n]
}
26 changes: 26 additions & 0 deletions product-of-array-except-self/sadie100.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
answer[i] = numbers[0], ... numbers[i-1], numbers[i+1], ... number[n-1]의 곱임을 활용하여
nums를 1부터 n-1까지 순회하며 i-1까지의 곱을 answer[i]에 채우고,
반대로 n-2부터 0까지 순회하며 i+1까지의 곱을 answer[i]에 채운다

시간복잡도 : O(N) => 2N번 순회
공간복잡도 : O(N) => number[] 배열 사용
*/

function productExceptSelf(nums: number[]): number[] {
const answer = new Array(nums.length).fill(1)

let multiVal = 1
for (let i = 1; i < nums.length; i++) {
multiVal *= nums[i - 1]
answer[i] *= multiVal
}

multiVal = 1
for (let i = nums.length - 2; i >= 0; i--) {
multiVal *= nums[i + 1]
answer[i] *= multiVal
}

return answer
}
24 changes: 24 additions & 0 deletions valid-anagram/sadie100.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
s의 문자 빈도를 기록하는 charMap을 만들고, t를 순회하면서 charMap의 빈도를 1씩 줄여 가며 같은지 비교한다.

시간복잡도: O(N) (s의 length N)

*/

function isAnagram(s: string, t: string): boolean {
if (s.length !== t.length) return false
const charMap = new Map()

for (let char of s) {
charMap.set(char, (charMap.get(char) || 0) + 1)
}

for (let char of t) {
if (!charMap.has(char) || charMap.get(char) === 0) {
return false
}
charMap.set(char, charMap.get(char) - 1)
}

return true
}
44 changes: 44 additions & 0 deletions validate-binary-search-tree/sadie100.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/**
* 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)
* }
* }
*/

/*
큐를 만들고 루트에서 아래로 내려가면서 노드, max값(left로 빠질 때 현재 노드값으로 갱신), min값(right로 빠질 때 현재 노드값으로 갱신)을 전달하고,
하나씩 꺼내면서 min max 한도를 검사, 조건에 맞지 않는 노드가 있으면 false를 반환한다

시간복잡도 : O(N) (N은 트리의 노드 개수)
공간복잡도 : O(N) (queue 사용)
*/

function isValidBST(root: TreeNode | null): boolean {
if (root === null) return true
const queue: [TreeNode, number | null, number | null][] = [[root, null, null]]
let idx = 0

while (idx < queue.length) {
const [node, min, max] = queue[idx]

if (min !== null && node.val <= min) return false
if (max !== null && node.val >= max) return false

if (node.left) {
queue.push([node.left, min, node.val])
}
if (node.right) {
queue.push([node.right, node.val, max])
}
idx += 1
}

return true
}
Loading