Skip to content

[jun0811] WEEK 02 solutions #1743

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 4, 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
38 changes: 38 additions & 0 deletions 3sum/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* @param {number[]} nums
* @return {number[][]}
*/

// 중복 제거 중점..
var threeSum = function (nums) {
nums.sort((a, b) => a - b);
const result = [];

for (let i = 0; i < nums.length - 2; i++) {
if (i > 0 && nums[i] === nums[i - 1]) continue;

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

while (left < right) {
const sum = nums[i] + nums[left] + nums[right];

if (sum === 0) {
result.push([nums[i], nums[left], nums[right]]);

// 중복 미리 제거
while (left < right && nums[left] === nums[left + 1]) left++;
while (left < right && nums[right] === nums[right - 1]) right--;

left++;
right--;
} else if (sum < 0) {
left++;
} else {
right--;
}
}
}

return result;
};
17 changes: 17 additions & 0 deletions climbing-stairs/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @param {number} n
* @return {number}
*/

// Time Complexity : O(N)
var climbStairs = function (n) {
if (n == 1) return 1;
if (n == 2) return 2;

const dp = [1, 2];

for (let i = 2; i < n; i++) {
dp[i] = dp[i - 2] + dp[i - 1];
}
return dp[n - 1];
};
19 changes: 19 additions & 0 deletions product-of-array-except-self/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* @param {number[]} nums
* @return {number[]}
*/
var productExceptSelf = function (nums) {
const res = new Array(nums.length).fill(1);

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

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

return res;
};
30 changes: 30 additions & 0 deletions valid-anagram/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* @param {string} s
* @param {string} t
* @return {boolean}
*/

// Time Complexity : O(N)
var isAnagram = function (s, t) {
if (s.length != t.length) return false;

const hashMap = {};

for (const chr of s) {
if (chr in hashMap) {
hashMap[chr] += 1;
} else {
hashMap[chr] = 1;
}
}

for (const chr of t) {
if (chr in hashMap && hashMap[chr] > 0) {
hashMap[chr] -= 1;
} else {
return false;
}
}

return true;
};
24 changes: 24 additions & 0 deletions validate-binary-search-tree/jun0811.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/**
* 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}
*/
function check(node, min, max) {
if (node === null) return true;
if ((min !== null && node.val <= min) || (max !== null && node.val >= max)) {
return false;
}

return check(node.left, min, node.val) && check(node.right, node.val, max);
}

function isValidBST(root) {
return check(root, null, null);
}