Skip to content

Commit ca8b8a5

Browse files
authored
Merge pull request #1743 from jun0811/main
[jun0811] WEEK 02 solutions
2 parents 69a3855 + a072f43 commit ca8b8a5

File tree

5 files changed

+128
-0
lines changed

5 files changed

+128
-0
lines changed

3sum/jun0811.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[][]}
4+
*/
5+
6+
// 중복 제거 중점..
7+
var threeSum = function (nums) {
8+
nums.sort((a, b) => a - b);
9+
const result = [];
10+
11+
for (let i = 0; i < nums.length - 2; i++) {
12+
if (i > 0 && nums[i] === nums[i - 1]) continue;
13+
14+
let left = i + 1;
15+
let right = nums.length - 1;
16+
17+
while (left < right) {
18+
const sum = nums[i] + nums[left] + nums[right];
19+
20+
if (sum === 0) {
21+
result.push([nums[i], nums[left], nums[right]]);
22+
23+
// 중복 미리 제거
24+
while (left < right && nums[left] === nums[left + 1]) left++;
25+
while (left < right && nums[right] === nums[right - 1]) right--;
26+
27+
left++;
28+
right--;
29+
} else if (sum < 0) {
30+
left++;
31+
} else {
32+
right--;
33+
}
34+
}
35+
}
36+
37+
return result;
38+
};

climbing-stairs/jun0811.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/**
2+
* @param {number} n
3+
* @return {number}
4+
*/
5+
6+
// Time Complexity : O(N)
7+
var climbStairs = function (n) {
8+
if (n == 1) return 1;
9+
if (n == 2) return 2;
10+
11+
const dp = [1, 2];
12+
13+
for (let i = 2; i < n; i++) {
14+
dp[i] = dp[i - 2] + dp[i - 1];
15+
}
16+
return dp[n - 1];
17+
};
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/**
2+
* @param {number[]} nums
3+
* @return {number[]}
4+
*/
5+
var productExceptSelf = function (nums) {
6+
const res = new Array(nums.length).fill(1);
7+
8+
for (let i = 1; i < nums.length; i++) {
9+
res[i] = res[i - 1] * nums[i - 1];
10+
}
11+
12+
let right = 1;
13+
for (let i = nums.length - 1; i >= 0; i--) {
14+
res[i] *= right;
15+
right *= nums[i];
16+
}
17+
18+
return res;
19+
};

valid-anagram/jun0811.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/**
2+
* @param {string} s
3+
* @param {string} t
4+
* @return {boolean}
5+
*/
6+
7+
// Time Complexity : O(N)
8+
var isAnagram = function (s, t) {
9+
if (s.length != t.length) return false;
10+
11+
const hashMap = {};
12+
13+
for (const chr of s) {
14+
if (chr in hashMap) {
15+
hashMap[chr] += 1;
16+
} else {
17+
hashMap[chr] = 1;
18+
}
19+
}
20+
21+
for (const chr of t) {
22+
if (chr in hashMap && hashMap[chr] > 0) {
23+
hashMap[chr] -= 1;
24+
} else {
25+
return false;
26+
}
27+
}
28+
29+
return true;
30+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/**
2+
* Definition for a binary tree node.
3+
* function TreeNode(val, left, right) {
4+
* this.val = (val===undefined ? 0 : val)
5+
* this.left = (left===undefined ? null : left)
6+
* this.right = (right===undefined ? null : right)
7+
* }
8+
*/
9+
/**
10+
* @param {TreeNode} root
11+
* @return {boolean}
12+
*/
13+
function check(node, min, max) {
14+
if (node === null) return true;
15+
if ((min !== null && node.val <= min) || (max !== null && node.val >= max)) {
16+
return false;
17+
}
18+
19+
return check(node.left, min, node.val) && check(node.right, node.val, max);
20+
}
21+
22+
function isValidBST(root) {
23+
return check(root, null, null);
24+
}

0 commit comments

Comments
 (0)