-
Notifications
You must be signed in to change notification settings - Fork 126
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 #734 from gwbaik9717/main
[ganu] Week 2
- Loading branch information
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
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,38 @@ | ||
// Time complexity: O(n^2) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* @param {number[]} nums | ||
* @return {number[][]} | ||
*/ | ||
var threeSum = function (nums) { | ||
const sumsDict = new Set(); | ||
const sortedNums = nums.toSorted((a, b) => a - b); | ||
|
||
const n = nums.length; | ||
|
||
for (let i = 0; i < n - 2; i++) { | ||
let left = i + 1; | ||
let right = n - 1; | ||
const fixed = sortedNums[i]; | ||
|
||
const targetSum = 0 - fixed; | ||
|
||
while (left < right) { | ||
const currentSum = sortedNums[left] + sortedNums[right]; | ||
|
||
if (currentSum < targetSum) { | ||
left++; | ||
} else if (currentSum > targetSum) { | ||
right--; | ||
} else { | ||
const key = [fixed, sortedNums[left], sortedNums[right]]; | ||
sumsDict.add(key.join(",")); | ||
left++; | ||
right--; | ||
} | ||
} | ||
} | ||
|
||
return Array.from(sumsDict).map((nums) => nums.split(",").map(Number)); | ||
}; |
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,16 @@ | ||
// Time complexity: O(n) | ||
// Space complexity: O(1) | ||
|
||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var climbStairs = function (n) { | ||
const dp = [1, 1]; | ||
|
||
for (let i = 2; i <= n; i++) { | ||
dp[i % 2] = dp[0] + dp[1]; | ||
} | ||
|
||
return dp[n % 2]; | ||
}; |
27 changes: 27 additions & 0 deletions
27
construct-binary-tree-from-preorder-and-inorder-traversal/gwbaik9717.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,27 @@ | ||
// Time complexity: O(n^2) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* 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 {number[]} preorder | ||
* @param {number[]} inorder | ||
* @return {TreeNode} | ||
*/ | ||
var buildTree = function (preorder, inorder) { | ||
if (!preorder.length || !inorder.length) return null; | ||
|
||
const root = new TreeNode(preorder[0]); | ||
const mid = inorder.indexOf(root.val); | ||
|
||
root.left = buildTree(preorder.slice(1, mid + 1), inorder.slice(0, mid)); | ||
root.right = buildTree(preorder.slice(mid + 1), inorder.slice(mid + 1)); | ||
|
||
return root; | ||
}; |
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,38 @@ | ||
// Time complexity: O(n) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* @param {string} s | ||
* @return {number} | ||
*/ | ||
var numDecodings = function (s) { | ||
const n = s.length; | ||
const dp = Array.from({ length: n + 2 }, () => 0); | ||
dp[1] = 1; | ||
|
||
for (let i = 2; i < n + 2; i++) { | ||
// 한자리 | ||
const charCode = Number(s[i - 2]); | ||
|
||
if (charCode > 0) { | ||
dp[i] += dp[i - 1]; | ||
} | ||
|
||
// 두자리 | ||
if (i <= 2) { | ||
continue; | ||
} | ||
|
||
if (Number(s[i - 3]) == 0) { | ||
continue; | ||
} | ||
|
||
const strCode = Number(s.slice(i - 3, i - 1)); | ||
|
||
if (strCode > 0 && strCode <= 26) { | ||
dp[i] += dp[i - 2]; | ||
} | ||
} | ||
|
||
return dp.at(-1); | ||
}; |
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,39 @@ | ||
// Time complexity: O(n) | ||
// Space complexity: O(n) | ||
|
||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
*/ | ||
var isAnagram = function (s, t) { | ||
if (s.length !== t.length) { | ||
return false; | ||
} | ||
|
||
const createDictFromString = (str) => { | ||
const dict = new Map(); | ||
|
||
for (const chr of str) { | ||
if (dict.has(chr)) { | ||
dict.set(chr, dict.get(chr) + 1); | ||
continue; | ||
} | ||
|
||
dict.set(chr, 1); | ||
} | ||
|
||
return dict; | ||
}; | ||
|
||
const dictS = createDictFromString(s); | ||
const dictT = createDictFromString(t); | ||
|
||
for (const [key, value] of dictS) { | ||
if (dictT.get(key) !== value) { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
}; |