Skip to content

Commit a5dbbde

Browse files
authored
Merge pull request DaleStudy#339 from wogha95/main
[재호] WEEK 02 Solutions
2 parents 2712a2a + 6388615 commit a5dbbde

File tree

4 files changed

+117
-0
lines changed

4 files changed

+117
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// TC: O(N^2)
2+
// SC: O(N^2)
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 (inorder.length === 0) {
19+
return null;
20+
}
21+
22+
const rootValue = preorder[0];
23+
const leftNodeLength = inorder.findIndex((value) => value === rootValue);
24+
const leftNode = buildTree(
25+
preorder.slice(1, 1 + leftNodeLength),
26+
inorder.slice(0, leftNodeLength)
27+
);
28+
const rightNode = buildTree(
29+
preorder.slice(1 + leftNodeLength),
30+
inorder.slice(leftNodeLength + 1)
31+
);
32+
return new TreeNode(rootValue, leftNode, rightNode);
33+
};

counting-bits/wogha95.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// TC: O(N)
2+
// SC: O(N)
3+
4+
/**
5+
* @param {number} n
6+
* @return {number[]}
7+
*/
8+
var countBits = function (n) {
9+
const result = [0];
10+
let pointer = 0;
11+
let lastPointer = 0;
12+
13+
for (let num = 1; num <= n; num++) {
14+
result.push(result[pointer] + 1);
15+
16+
if (pointer === lastPointer) {
17+
lastPointer = result.length - 1;
18+
pointer = 0;
19+
} else {
20+
pointer += 1;
21+
}
22+
}
23+
24+
return result;
25+
};

decode-ways/wogha95.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
// TC: O(N)
2+
// SC: O(N)
3+
4+
/**
5+
* @param {string} s
6+
* @return {number}
7+
*/
8+
var numDecodings = function (s) {
9+
if (s[0] === "0") {
10+
return 0;
11+
}
12+
if (s.length === 1) {
13+
return 1;
14+
}
15+
16+
const dpTable = new Array(s.length).fill(0);
17+
if (s[0] !== "0") {
18+
dpTable[0] = 1;
19+
}
20+
if (s[1] !== "0") {
21+
dpTable[1] += 1;
22+
}
23+
if (isValid(`${s[0]}${s[1]}`)) {
24+
dpTable[1] += 1;
25+
}
26+
27+
for (let index = 2; index < s.length; index++) {
28+
if (s[index] !== "0") {
29+
dpTable[index] += dpTable[index - 1];
30+
}
31+
if (s[index - 1] !== "0" && isValid(`${s[index - 1]}${s[index]}`)) {
32+
dpTable[index] += dpTable[index - 2];
33+
}
34+
}
35+
36+
return dpTable[dpTable.length - 1];
37+
38+
function isValid(stringNumber) {
39+
const number = Number(stringNumber);
40+
if (number <= 0) {
41+
return false;
42+
}
43+
if (27 <= number) {
44+
return false;
45+
}
46+
return true;
47+
}
48+
};

valid-anagram/wogha95.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// TC: O(N * log N)
2+
// SC: O(N)
3+
4+
/**
5+
* @param {string} s
6+
* @param {string} t
7+
* @return {boolean}
8+
*/
9+
var isAnagram = function(s, t) {
10+
return s.split('').sort().join('') === t.split('').sort().join('');
11+
};

0 commit comments

Comments
 (0)