-
-
Notifications
You must be signed in to change notification settings - Fork 195
[hsskey] Week 02 Solutions #1231
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
60cd657
665564d
a4874d3
fb3974b
036de93
0449874
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[][]} | ||
*/ | ||
var threeSum = function(nums) { | ||
const result = []; | ||
const n = nums.length; | ||
|
||
if (n < 3) return result; | ||
|
||
nums.sort((a, b) => a - b); | ||
|
||
const uniqueTriplets = new Set(); | ||
|
||
for (let i = 0; i < n - 2; i++) { | ||
if (nums[i] > 0) break; | ||
|
||
if (i > 0 && nums[i] === nums[i - 1]) continue; | ||
|
||
const target = -nums[i]; | ||
const seen = new Set(); | ||
|
||
for (let j = i + 1; j < n; j++) { | ||
const complement = target - nums[j]; | ||
|
||
if (seen.has(complement)) { | ||
const triplet = [nums[i], complement, nums[j]].toString(); | ||
|
||
if (!uniqueTriplets.has(triplet)) { | ||
uniqueTriplets.add(triplet); | ||
result.push([nums[i], complement, nums[j]]); | ||
} | ||
} | ||
|
||
seen.add(nums[j]); | ||
} | ||
} | ||
|
||
return result; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var climbStairs = function(n) { | ||
if(n <= 2) { | ||
return n | ||
} | ||
|
||
let dp = new Array(n + 1) | ||
dp[1] = 1 | ||
dp[2] = 2 | ||
|
||
for(let i = 3; i <= n; i++) { | ||
dp[i] = dp[i - 1] + dp[i - 2] | ||
} | ||
|
||
return dp[n] | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[]} | ||
*/ | ||
var productExceptSelf = function(nums) { | ||
const n = nums.length | ||
const result = Array(nums.length) | ||
|
||
let left = 1 | ||
|
||
for(let i = 0; i < n; i++) { | ||
result[i] = left | ||
left *= nums[i] | ||
} | ||
|
||
let right = 1 | ||
for(let i = n - 1; i >= 0; i--) { | ||
result[i] *= right | ||
right *= nums[i] | ||
} | ||
|
||
return result | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
*/ | ||
var isAnagram = function(s, t) { | ||
if(s.length !== t.length) { | ||
return false | ||
} | ||
const sMap = new Map() | ||
const tMap = new Map() | ||
|
||
for(let i = 0; i < s.length; i++) { | ||
if(sMap.has(s[i])) { | ||
const prevVal = sMap.get(s[i]) | ||
sMap.set(s[i], prevVal + 1) | ||
} else { | ||
sMap.set(s[i], 1) | ||
} | ||
} | ||
|
||
for(let i = 0; i < t.length; i++) { | ||
if(tMap.has(t[i])) { | ||
const prevVal = tMap.get(t[i]) | ||
tMap.set(t[i], prevVal + 1) | ||
} else { | ||
tMap.set(t[i], 1) | ||
} | ||
} | ||
|
||
for(const[char, count] of sMap) { | ||
if(!tMap.has(char) || tMap.get(char) !== count) { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* 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 {TreeNode} root | ||
* @return {boolean} | ||
*/ | ||
var isValidBST = function(root) { | ||
const dfs = (node, left = null, right = null) => { | ||
if(!node) { | ||
return true | ||
} | ||
|
||
if((left !== null && node.val <= left) || (right !== null && node.val >= right)) { | ||
return false | ||
} | ||
|
||
return dfs(node.left, left, node.val) && dfs(node.right, node.val, right) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 안녕하세요! 좋은 주말입니다. 요 알고리즘 시간복잡도는 O(logN) 으로 생각이 되는데, 아니라면 얼마나 되는지 궁금합니다! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 안녕하세요 @YoungSeok-Choi 님 해당 풀이는 O(logN)이 아닌 O(n) 풀이입니다. 풀이 중 조건문을 통과한 코드들은 마지막 return문에서 재귀적으로 동작하게되는데, 따라서 해당 풀이는 O(n)풀이가 됩니다. ...
...
return dfs(node.left, left, node.val) && dfs(node.right, node.val, right) |
||
} | ||
|
||
return dfs(root) | ||
}; |
Uh oh!
There was an error while loading. Please reload this page.