-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from Sunjae95/week2
Week2
- Loading branch information
Showing
5 changed files
with
250 additions
and
0 deletions.
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
construct-binary-tree-from-preorder-and-inorder-traversal/sunjae95.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
/** | ||
* @description | ||
* time complexity: O(n^2) | ||
* space complexity: O(n) | ||
* | ||
* brainstorming: | ||
* stack, Drawing a graph | ||
* | ||
* strategy: | ||
* discover the rules | ||
* leftStack = left create , rightStack = right create | ||
*/ | ||
var buildTree = function (preorder, inorder) { | ||
let answer = null; | ||
let pointer = 0; | ||
|
||
const leftStack = []; | ||
const rightStack = []; | ||
|
||
preorder.forEach((val, i) => { | ||
const node = new TreeNode(val); | ||
|
||
if (i === 0) answer = node; | ||
|
||
const leftLen = leftStack.length; | ||
const rightLen = rightStack.length; | ||
|
||
if (leftLen && rightLen) { | ||
if (leftStack[leftLen - 1].left) rightStack[rightLen - 1].right = node; | ||
else leftStack[leftLen - 1].left = node; | ||
} | ||
if (leftLen && !rightLen) leftStack[leftLen - 1].left = node; | ||
if (!leftLen && rightLen) rightStack[rightLen - 1].right = node; | ||
|
||
leftStack.push(node); | ||
|
||
while (leftStack.length && pointer < inorder.length) { | ||
if (leftStack[leftStack.length - 1].val !== inorder[pointer]) break; | ||
rightStack.push(leftStack.pop()); | ||
pointer++; | ||
} | ||
}); | ||
|
||
return answer; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @description | ||
* time complexity: O(n log n) | ||
* space complexity: O(N) | ||
* | ||
* brainstorming: | ||
* convert integer to bit | ||
* for loop | ||
* | ||
* strategy: | ||
* string change to hash table | ||
*/ | ||
var countBits = function (n) { | ||
return Array.from({ length: n + 1 }, (_, i) => convertBitCount(i)); | ||
}; | ||
|
||
const convertBitCount = (n) => { | ||
let count = 0; | ||
while (n > 0) { | ||
count += n % 2; | ||
n = Math.floor(n / 2); | ||
} | ||
return count; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/** | ||
* @description | ||
* brainstorming: | ||
* 1. dp -> dp[i] = dp[i-1] + count | ||
* 2. recursive function | ||
* | ||
* strategy: | ||
* https://www.algodale.com/problems/decode-ways/ | ||
* | ||
* result: | ||
* 1. couldn't think of the conditions | ||
* true: 1~9, 10~27 | ||
* false: 0, 0N, 28↑ | ||
* 2. persist solution that is top down | ||
*/ | ||
|
||
// https://www.algodale.com/problems/decode-ways/ Solve 1 | ||
/** | ||
* time complexity: O(2^n) | ||
* space complexity: O(n) | ||
*/ | ||
var numDecodings = function (s) { | ||
const search = (start) => { | ||
if (start === s.length) return 1; | ||
if (s[start] === "0") return 0; | ||
if (start + 1 < s.length && Number(`${s[start]}${s[start + 1]}`) < 27) { | ||
return search(start + 1) + search(start + 2); | ||
} | ||
return search(start + 1); | ||
}; | ||
|
||
return search(0); | ||
}; | ||
|
||
// https://www.algodale.com/problems/decode-ways/ Solve 2 | ||
/** | ||
* time complexity: O(2^n) | ||
* space complexity: O(n) | ||
*/ | ||
var numDecodings = function (s) { | ||
const memo = new Map(); | ||
memo.set(s.length, 1); | ||
|
||
const search = (start) => { | ||
if (!!memo.get(start)) return memo.get(start); | ||
|
||
if (s[start] === "0") memo.set(start, 0); | ||
else if (start + 1 < s.length && Number(`${s[start]}${s[start + 1]}`) < 27) | ||
memo.set(start, search(start + 1) + search(start + 2)); | ||
else memo.set(start, search(start + 1)); | ||
|
||
return memo.get(start); | ||
}; | ||
|
||
return search(0); | ||
}; | ||
|
||
// https://www.algodale.com/problems/decode-ways/ Solve 3 | ||
/** | ||
* time complexity: O(n) | ||
* space complexity: O(n) | ||
*/ | ||
var numDecodings = function (s) { | ||
const dp = Array.from({ length: s.length + 1 }, (_, i) => | ||
i === s.length ? 1 : 0 | ||
); | ||
|
||
for (let i = s.length - 1; i >= 0; i--) { | ||
if (s[i] === "0") dp[i] = 0; | ||
else if (i + 1 < s.length && Number(`${s[i]}${s[i + 1]}`) < 27) | ||
dp[i] = dp[i + 1] + dp[i + 2]; | ||
else dp[i] = dp[i + 1]; | ||
} | ||
|
||
return dp[0]; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
/** | ||
* @description | ||
* time complexity: O(N) | ||
* space complexity: O(N) | ||
* | ||
* brainstorming: | ||
* 1. hash table value compare to count | ||
* | ||
* strategy: | ||
* string change to hash table | ||
*/ | ||
var isAnagram = function (s, t) { | ||
if (s.length !== t.length) return false; | ||
|
||
let answer = true; | ||
const tableS = convertHashTable(s); | ||
const tableT = convertHashTable(t); | ||
|
||
tableS.forEach((_, key) => { | ||
if (tableT.get(key) && tableT.get(key) === tableS.get(key)) return; | ||
answer = false; | ||
}); | ||
|
||
return answer; | ||
}; | ||
|
||
const convertHashTable = (str) => | ||
str.split("").reduce((map, s) => { | ||
map.set(s, (map.get(s) ?? 0) + 1); | ||
return map; | ||
}, new Map()); |