Skip to content

[hoyeongkwak] WEEK 02 Solutions #1268

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 13, 2025
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
30 changes: 30 additions & 0 deletions 3sum/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
time complexity : O(n^2)
space complexity : O(1)
*/
function threeSum(nums: number[]): number[][] {
const result: number[][] = []
const sortedNums = nums.sort((a, b) => a - b)

for(let i = 0; i < sortedNums.length - 2; i++) {
if (i > 0 && sortedNums[i] === sortedNums[i - 1]) continue
let low = i + 1
let high = sortedNums.length - 1
while (low < high) {
const threeSum = sortedNums[i] + sortedNums[low] + sortedNums[high]
if (threeSum < 0) {
low += 1
} else if (threeSum > 0) {
high -= 1
} else {
result.push([sortedNums[i], sortedNums[low], sortedNums[high]])
while (low < high && sortedNums[low] === sortedNums[low + 1]) low++
while (low < high && sortedNums[high] === sortedNums[high - 1]) high--

low += 1
high -= 1
}
}
}
return result
}
15 changes: 15 additions & 0 deletions climbing-stairs/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/*
시간복잡도 : O(n)
공간복잡도 : O(1)
*/
function climbStairs(n: number): number {
if (n < 3) return n
let prev = 1
let curr = 2
for (let i = 0; i < n - 2; i++) {
const tempPrev = prev
prev = curr
curr = tempPrev + curr
}
return curr
}
19 changes: 19 additions & 0 deletions product-of-array-except-self/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
time complexity : O(n)
space complexity : O(1)
*/
function productExceptSelf(nums: number[]): number[] {
const results = new Array(nums.length).fill(1)
let before = 1
let after = 1
for (let i = 0; i < nums.length - 1; i++) {
before *= nums[i]
results[i + 1] *= before
}
for (let i = nums.length - 1; i > 0; i--) {
after *= nums[i]
results[i - 1] *= after
}

return results
}
17 changes: 17 additions & 0 deletions valid-anagram/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function isAnagram(s: string, t: string): boolean {
// 시간복잡도 O(nlogn), 공간복잡도 O(n)
// const sSorted = s.split('').sort().join(',')
// const tSorted = t.split('').sort().join(',')
// return sSorted === tSorted

/*
시간복잡도 O(n), 공간복잡도 O(1)
*/
if (s.length != t.length) return false
const count = new Array(26).fill(0)
for (let i = 0; i < s.length; i++) {
count[s.charCodeAt(i) - 97]++
count[t.charCodeAt(i) - 97]--
Comment on lines +13 to +14
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아스키 코드를 활용해서 푼게 인상적이네요! 👍👍

}
return count.every(c => c === 0)
}
34 changes: 34 additions & 0 deletions validate-binary-search-tree/hoyeongkwak.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* 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)
* }
* }
*/
/*
time complexity : O(n)
space complexity : O(n)
*/
function isValidBST(root: TreeNode | null): boolean {
let prev = -Infinity
let isValid = true
const inOrder = (node: TreeNode | null): void => {
if (!isValid || !node) return
inOrder(node.left)

if (prev >= node.val) {
isValid = false
return
}
prev = node.val
inOrder(node.right)
}
inOrder(root)
return isValid
}