Skip to content

Commit 8e95fdc

Browse files
authored
Merge pull request #734 from gwbaik9717/main
[ganu] Week 2
2 parents cc98766 + 3168fa4 commit 8e95fdc

File tree

5 files changed

+158
-0
lines changed

5 files changed

+158
-0
lines changed

3sum/gwbaik9717.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Time complexity: O(n^2)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* @param {number[]} nums
6+
* @return {number[][]}
7+
*/
8+
var threeSum = function (nums) {
9+
const sumsDict = new Set();
10+
const sortedNums = nums.toSorted((a, b) => a - b);
11+
12+
const n = nums.length;
13+
14+
for (let i = 0; i < n - 2; i++) {
15+
let left = i + 1;
16+
let right = n - 1;
17+
const fixed = sortedNums[i];
18+
19+
const targetSum = 0 - fixed;
20+
21+
while (left < right) {
22+
const currentSum = sortedNums[left] + sortedNums[right];
23+
24+
if (currentSum < targetSum) {
25+
left++;
26+
} else if (currentSum > targetSum) {
27+
right--;
28+
} else {
29+
const key = [fixed, sortedNums[left], sortedNums[right]];
30+
sumsDict.add(key.join(","));
31+
left++;
32+
right--;
33+
}
34+
}
35+
}
36+
37+
return Array.from(sumsDict).map((nums) => nums.split(",").map(Number));
38+
};

climbing-stairs/gwbaik9717.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(1)
3+
4+
/**
5+
* @param {number} n
6+
* @return {number}
7+
*/
8+
var climbStairs = function (n) {
9+
const dp = [1, 1];
10+
11+
for (let i = 2; i <= n; i++) {
12+
dp[i % 2] = dp[0] + dp[1];
13+
}
14+
15+
return dp[n % 2];
16+
};
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Time complexity: O(n^2)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* Definition for a binary tree node.
6+
* function TreeNode(val, left, right) {
7+
* this.val = (val===undefined ? 0 : val)
8+
* this.left = (left===undefined ? null : left)
9+
* this.right = (right===undefined ? null : right)
10+
* }
11+
*/
12+
/**
13+
* @param {number[]} preorder
14+
* @param {number[]} inorder
15+
* @return {TreeNode}
16+
*/
17+
var buildTree = function (preorder, inorder) {
18+
if (!preorder.length || !inorder.length) return null;
19+
20+
const root = new TreeNode(preorder[0]);
21+
const mid = inorder.indexOf(root.val);
22+
23+
root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid));
24+
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1));
25+
26+
return root;
27+
};

decode-ways/gwbaik9717.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* @param {string} s
6+
* @return {number}
7+
*/
8+
var numDecodings = function (s) {
9+
const n = s.length;
10+
const dp = Array.from({ length: n + 2 }, () => 0);
11+
dp[1] = 1;
12+
13+
for (let i = 2; i < n + 2; i++) {
14+
// 한자리
15+
const charCode = Number(s[i - 2]);
16+
17+
if (charCode > 0) {
18+
dp[i] += dp[i - 1];
19+
}
20+
21+
// 두자리
22+
if (i <= 2) {
23+
continue;
24+
}
25+
26+
if (Number(s[i - 3]) == 0) {
27+
continue;
28+
}
29+
30+
const strCode = Number(s.slice(i - 3, i - 1));
31+
32+
if (strCode > 0 && strCode <= 26) {
33+
dp[i] += dp[i - 2];
34+
}
35+
}
36+
37+
return dp.at(-1);
38+
};

valid-anagram/gwbaik9717.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Time complexity: O(n)
2+
// Space complexity: O(n)
3+
4+
/**
5+
* @param {string} s
6+
* @param {string} t
7+
* @return {boolean}
8+
*/
9+
var isAnagram = function (s, t) {
10+
if (s.length !== t.length) {
11+
return false;
12+
}
13+
14+
const createDictFromString = (str) => {
15+
const dict = new Map();
16+
17+
for (const chr of str) {
18+
if (dict.has(chr)) {
19+
dict.set(chr, dict.get(chr) + 1);
20+
continue;
21+
}
22+
23+
dict.set(chr, 1);
24+
}
25+
26+
return dict;
27+
};
28+
29+
const dictS = createDictFromString(s);
30+
const dictT = createDictFromString(t);
31+
32+
for (const [key, value] of dictS) {
33+
if (dictT.get(key) !== value) {
34+
return false;
35+
}
36+
}
37+
38+
return true;
39+
};

0 commit comments

Comments
 (0)