-
-
Notifications
You must be signed in to change notification settings - Fork 195
[froggy1014] WEEK 01 Solutions #1171
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
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,9 @@ | ||
// Set을 사용한 중복값 제거 후 길이 비교 | ||
function containsDuplicate(nums) { | ||
const numSet = new Set(nums); | ||
return numSet.size !== nums.length; | ||
} | ||
|
||
console.log(containsDuplicate([1, 2, 3, 1])); // true | ||
console.log(containsDuplicate([1, 2, 3, 4])); // false | ||
console.log(containsDuplicate([1, 1, 1, 3, 3, 4, 3, 2, 4, 2])); // 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,20 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var rob = function (nums) { | ||
if (nums.length === 0) return 0; | ||
if (nums.length === 1) return nums[0]; | ||
|
||
const dp = new Array(nums.length + 1); | ||
dp[0] = 0; | ||
dp[1] = nums[0]; | ||
for (let i = 2; i < dp.length; i++) { | ||
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i - 1]); | ||
} | ||
return dp[dp.length - 1]; | ||
}; | ||
|
||
const nums = [2, 7, 9, 3, 1]; | ||
|
||
console.log(rob(nums)); |
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,31 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
|
||
// 시간복잡도: O(n) | ||
var longestConsecutive = function (nums) { | ||
let longest = 0; | ||
|
||
let set = new Set(nums); | ||
|
||
for (let num of nums) { | ||
if (set.has(num - 1)) { | ||
continue; | ||
} | ||
|
||
let count = 1; | ||
let currentNum = num; | ||
|
||
while (set.has(currentNum + 1)) { | ||
count++; | ||
currentNum++; | ||
} | ||
longest = Math.max(longest, count); | ||
} | ||
return longest; | ||
}; | ||
|
||
console.log(longestConsecutive([100, 4, 200, 1, 3, 2])); // 4 | ||
console.log(longestConsecutive([0, 3, 7, 2, 5, 8, 4, 6, 0, 1])); // 9 | ||
console.log(longestConsecutive([1, 0, 1, 2])); // 3 |
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,20 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} k | ||
* @return {number[]} | ||
*/ | ||
var topKFrequent = function (nums, k) { | ||
const map = new Map(); | ||
for (let n = 0; n < nums.length; n++) { | ||
map.has(nums[n]) | ||
? map.set(nums[n], map.get(nums[n]) + 1) | ||
: map.set(nums[n], 1); | ||
} | ||
|
||
return Array.from(map.entries()) | ||
.sort(([key1, value1], [key2, value2]) => value2 - value1) | ||
.slice(0, k) | ||
.map((v) => v[0]); | ||
}; | ||
|
||
console.log(topKFrequent([1, 1, 1, 2, 2, 3], 2)); |
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,18 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @param {number} target | ||
* @return {number[]} | ||
*/ | ||
var twoSum = function (nums, target) { | ||
let map = new Map(); | ||
for (let idx = 0; idx < nums.length; idx++) { | ||
const rest = target - nums[idx]; | ||
if (map.has(rest)) { | ||
return [map.get(rest), idx]; | ||
} | ||
map.set(nums[idx], idx); | ||
} | ||
return []; | ||
}; | ||
|
||
console.log(twoSum([2, 11, 15, 7], 9)); |
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.
set 생성자함수를 사용할 수도 있군요!
덕분에 set에 대해 공부하게 되었습니다. 감사합니다!
좋은 코드였습니다 :)