-
-
Notifications
You must be signed in to change notification settings - Fork 248
[lhc0506] WEEK 02 solutions #1237
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
Merged
+171
−0
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,50 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[][]} | ||
*/ | ||
var threeSum = function(nums) { | ||
const result = []; | ||
const numsMap = {}; | ||
|
||
nums.forEach((num, index) => { | ||
numsMap[num] = (numsMap[num] || 0) + 1; | ||
|
||
}); | ||
|
||
for (let i = 0; i < nums.length - 1; i++) { | ||
for (let j = i + 1; j < nums.length; j++) { | ||
const a = nums[i]; | ||
const b = nums[j]; | ||
const targetNum = -(a + b); | ||
|
||
if (!numsMap[targetNum]) { | ||
continue; | ||
} | ||
|
||
if ((targetNum === a || targetNum === b) && numsMap[targetNum] === 1) { | ||
continue; | ||
} | ||
|
||
if (targetNum === a && targetNum === b && numsMap[targetNum] <= 2) { | ||
continue; | ||
} | ||
|
||
const triplet = [a, b, targetNum].sort((x, y) => x - y); | ||
const key = triplet.join(','); | ||
|
||
if (seen.has(key)) { | ||
continue; | ||
} | ||
|
||
seen.add(key); | ||
|
||
result.push(triplet); | ||
} | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
|
||
//시간 복잡도 O(n²) , 공간 복잡도 O(n²) 라고 생각했는데 Time Limit Exceeded 에러 발생 | ||
// 추후 다시 풀어볼 예정 |
This file contains hidden or 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,17 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var climbStairs = function(n) { | ||
let preStairs = 0; | ||
let curStairs = 1; | ||
|
||
|
||
for (let i = 0; i < n; i++) { | ||
let sum = preStairs + curStairs; | ||
preStairs = curStairs; | ||
curStairs = sum; | ||
} | ||
|
||
return curStairs; | ||
}; |
This file contains hidden or 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,54 @@ | ||
|
||
// 풀이 1 | ||
// /** | ||
// * @param {number[]} nums | ||
// * @return {number[]} | ||
// */ | ||
// var productExceptSelf = function(nums) { | ||
// let hasZero = false; | ||
// const allProductNums = nums.reduce((acc, cur) => { | ||
// if (cur === 0) { | ||
// if (!hasZero) { | ||
// hasZero = true; | ||
// return acc; | ||
// } | ||
|
||
// return 0; | ||
// } | ||
// return acc * cur; | ||
// }, 1); | ||
|
||
// return nums.map(num => { | ||
// if (num === 0) { | ||
// return allProductNums; | ||
// } | ||
|
||
// return hasZero ? 0 : allProductNums / num; | ||
// }); | ||
// }; | ||
|
||
//풀이 2 | ||
/** | ||
* @param {number[]} nums | ||
* @return {number[]} | ||
*/ | ||
var productExceptSelf = function(nums) { | ||
const result = Array(nums.length).fill(1); | ||
|
||
let beforeProductNum = 1; | ||
for (let i = 0; i < nums.length; i++) { | ||
result[i] *= beforeProductNum; | ||
beforeProductNum *= nums[i]; | ||
} | ||
|
||
let afterProductNum = 1; | ||
for (let i = nums.length - 1; i >= 0; i--) { | ||
result[i] *= afterProductNum; | ||
afterProductNum *= nums[i]; | ||
} | ||
|
||
return result; | ||
}; | ||
|
||
|
||
//두 풀이 모두 시간 복잡도 O(n), 공간 복잡도는 O(n) |
This file contains hidden or 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,26 @@ | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* @return {boolean} | ||
*/ | ||
var isAnagram = function(s, t) { | ||
if (s.length !== t.length) { | ||
return false; | ||
} | ||
|
||
const charCountMap = {}; | ||
|
||
for (let char of s) { | ||
charCountMap[char] = (charCountMap[char] || 0) + 1; | ||
} | ||
|
||
for (let char of t) { | ||
if (!charCountMap[char]) { | ||
return false; | ||
} | ||
|
||
charCountMap[char] -= 1; | ||
} | ||
|
||
return true; | ||
}; |
This file contains hidden or 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 @@ | ||
/** | ||
* 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, min, max) => { | ||
if (!node) return true; | ||
if (!((node.val > min) && (node.val < max))) return false; | ||
|
||
return dfs(node.left, min, node.val) && dfs(node.right, node.val, max); | ||
} | ||
|
||
return dfs(root, Number.MIN_SAFE_INTEGER, Number.MAX_SAFE_INTEGER); | ||
}; | ||
|
||
// 시간 복잡도 O(n) , 공간 복잡도 최악의 경우 O(n) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
var
를 이용해서 함수를 정의하고 있는데,var
는 재선언, 재할당을 허용하니까let
이나const
가 더 좋을 것 같아요. 물론 리트코드에서 기본적으로 제시하는 코드가var 함수명
으로 되어 있긴 하지만요..! 저는const
로 바꿔서 사용하고 있습니다.