Skip to content

[reach0908] WEEK 02 solutions #1745

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 5 commits into from
Aug 3, 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
46 changes: 46 additions & 0 deletions 3sum/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* 시간복잡도: O(n²)
* 공간복잡도: O(1) (결과 배열 제외)
* 풀이 방법: 정렬 후 투 포인터 방식
* @param {number[]} nums
* @return {number[][]}
*/
const threeSum = function (nums) {
const sortedNums = nums.sort((a, b) => a - b);
const result = [];

for (let i = 0; i < sortedNums.length; i += 1) {
// 첫 번째 요소의 중복 제거
if (i > 0 && sortedNums[i] === sortedNums[i - 1]) {
continue;
}

let left = i + 1;
let right = sortedNums.length - 1;

while (left < right) {
const threeSum = sortedNums[i] + sortedNums[left] + sortedNums[right];

if (threeSum > 0) {
right -= 1;
} else if (threeSum < 0) {
left += 1;
} else {
result.push([sortedNums[i], sortedNums[left], sortedNums[right]]);

// 중복 제거
while (left < right && sortedNums[left] === sortedNums[left + 1]) {
left += 1;
}
while (left < right && sortedNums[right] === sortedNums[right - 1]) {
right -= 1;
}

left += 1;
right -= 1;
}
}
}

return result;
};
18 changes: 18 additions & 0 deletions climbing-stairs/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @description
* time complexity: O(n)
* space complexity: O(n)
* runtime: 0ms
* 풀이 방법: 기본적인 DP 풀이 방법
* @param {number} n
* @return {number}
*/
const climbStairs = function (n) {
const dp = [1, 2];

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

return dp[n - 1];
};
28 changes: 28 additions & 0 deletions product-of-array-except-self/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @description
* time complexity: O(n)
* space complexity: O(n)
* runtime: 5ms
* 풀이 방법 : 40분고민하다가 다른사람 풀이보고 누적합을 이용하면 된다해서 구현함
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function (nums) {
const result = new Array(nums.length).fill(1);

let prefix = 1;

for (let i = 0; i < nums.length; i += 1) {
result[i] = prefix;
prefix *= nums[i];
}

let postfix = 1;

for (let i = nums.length - 1; i >= 0; i -= 1) {
result[i] *= postfix;
postfix *= nums[i];
}

return result;
};
40 changes: 40 additions & 0 deletions valid-anagram/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* @description
* time complexity: O(nlogn) split 시 새로운 배열을 생성하고 sort 시 nlogn 시간 소요
* space complexity: O(n) split 시 새로운 배열을 생성함
* runtime: 32ms
* 풀이 방법: 두 문자열을 정렬하여 비교하는 방법
* @param {string} s
* @param {string} t
* @return {boolean}
*/
const isAnagram = function (s, t) {
return s.split("").sort().join("") === t.split("").sort().join("");
};

/**
* @description
* time complexity: O(n)
* space complexity: O(n)
* runtime: 15ms
* 풀이 방법: 해쉬맵을 통해 카운트를 추가하거나 제거하는 방식, 유니코드도 대응가능
* @param {string} s
* @param {string} t
* @return {boolean}
*/
const isAnagramSolution2 = function (s, t) {
if (s.length !== t.length) return false;

const map = new Map();

for (let i = 0; i < s.length; i += 1) {
map.set(s[i], (map.get(s[i]) || 0) + 1);
map.set(t[i], (map.get(t[i]) || 0) - 1);
}

for (const value of map.values()) {
if (value !== 0) return false;
}

return true;
};
26 changes: 26 additions & 0 deletions validate-binary-search-tree/reach0908.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* 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, low = -Infinity, high = Infinity) {
if (!root) {
return true;
}

if (root.val <= low || root.val >= high) {
return false;
}

return (
isValidBST(root.left, low, root.val) &&
isValidBST(root.right, root.val, high)
);
};