Skip to content

[hsskey] Week 02 Solutions #1231

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 6 commits into from
Apr 12, 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
40 changes: 40 additions & 0 deletions 3sum/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @param {number[]} nums
* @return {number[][]}
*/
var threeSum = function(nums) {
const result = [];
const n = nums.length;

if (n < 3) return result;

nums.sort((a, b) => a - b);

const uniqueTriplets = new Set();

for (let i = 0; i < n - 2; i++) {
if (nums[i] > 0) break;

if (i > 0 && nums[i] === nums[i - 1]) continue;

const target = -nums[i];
const seen = new Set();

for (let j = i + 1; j < n; j++) {
const complement = target - nums[j];

if (seen.has(complement)) {
const triplet = [nums[i], complement, nums[j]].toString();

if (!uniqueTriplets.has(triplet)) {
uniqueTriplets.add(triplet);
result.push([nums[i], complement, nums[j]]);
}
}

seen.add(nums[j]);
}
}

return result;
};
19 changes: 19 additions & 0 deletions climbing-stairs/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param {number} n
* @return {number}
*/
var climbStairs = function(n) {
if(n <= 2) {
return n
}

let dp = new Array(n + 1)
dp[1] = 1
dp[2] = 2

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

return dp[n]
};
23 changes: 23 additions & 0 deletions product-of-array-except-self/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function(nums) {
const n = nums.length
const result = Array(nums.length)

let left = 1

for(let i = 0; i < n; i++) {
result[i] = left
left *= nums[i]
}

let right = 1
for(let i = n - 1; i >= 0; i--) {
result[i] *= right
right *= nums[i]
}

return result
};
38 changes: 38 additions & 0 deletions valid-anagram/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/
var isAnagram = function(s, t) {
if(s.length !== t.length) {
return false
}
const sMap = new Map()
const tMap = new Map()

for(let i = 0; i < s.length; i++) {
if(sMap.has(s[i])) {
const prevVal = sMap.get(s[i])
sMap.set(s[i], prevVal + 1)
} else {
sMap.set(s[i], 1)
}
}

for(let i = 0; i < t.length; i++) {
if(tMap.has(t[i])) {
const prevVal = tMap.get(t[i])
tMap.set(t[i], prevVal + 1)
} else {
tMap.set(t[i], 1)
}
}

for(const[char, count] of sMap) {
if(!tMap.has(char) || tMap.get(char) !== count) {
return false
}
}

return true
};
27 changes: 27 additions & 0 deletions validate-binary-search-tree/hsskey.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Definition for a binary tree node.
* function TreeNode(val, left, right) {
* this.val = (val===undefined ? 0 : val)
* this.left = (left===undefined ? null : left)
* this.right = (right===undefined ? null : right)
* }
*/
/**
* @param {TreeNode} root
* @return {boolean}
*/
var isValidBST = function(root) {
const dfs = (node, left = null, right = null) => {
if(!node) {
return true
}

if((left !== null && node.val <= left) || (right !== null && node.val >= right)) {
return false
}

return dfs(node.left, left, node.val) && dfs(node.right, node.val, right)
Copy link
Contributor

@YoungSeok-Choi YoungSeok-Choi Apr 12, 2025

Choose a reason for hiding this comment

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

안녕하세요! 좋은 주말입니다.

요 알고리즘 시간복잡도는 O(logN) 으로 생각이 되는데,
제 계산이 맞을까요??

아니라면 얼마나 되는지 궁금합니다!

Copy link
Member Author

Choose a reason for hiding this comment

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

안녕하세요 @YoungSeok-Choi

해당 풀이는 O(logN)이 아닌 O(n) 풀이입니다.
이 문제를 일반적인 이진탐색인 O(logN)으로 보일 수 있으나, 이 문제는 탐색 구현이 아니라
문제의 요구사항은 트리 전체를 확인하는 검증과정이라고 볼 수 있습니다.

풀이 중 조건문을 통과한 코드들은 마지막 return문에서 재귀적으로 동작하게되는데,
이를 시각화해서 보면 왼쪽과 오른쪽으로 코드가 마치 배열을 선형적으로 순회하듯이 거치는것을 확인할 수 있습니다.

따라서 해당 풀이는 O(n)풀이가 됩니다.

...
...
return dfs(node.left, left, node.val) && dfs(node.right, node.val, right)

}

return dfs(root)
};