-
-
Notifications
You must be signed in to change notification settings - Fork 245
[jun0811] WEEK 01 solutions #1686
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 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
baa48d8
contains-duplicate
jun0811 3cdb226
number-of-1-bits
jun0811 d6279de
top k frequent elements
jun0811 9418c8f
two-sum
jun0811 f477bbf
longest-common-subsequence
jun0811 af0ff74
house-robber
jun0811 416793e
띄어쓰기
jun0811 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,18 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = function (nums) { | ||
const count = []; | ||
let res = false; | ||
|
||
for (const num of nums) { | ||
if (count[num]) { | ||
res = true; | ||
break; | ||
jun0811 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} else { | ||
count[num] = 1; | ||
} | ||
} | ||
return res; | ||
jun0811 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}; |
jun0811 marked this conversation as resolved.
Show resolved
Hide resolved
|
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,29 @@ | ||
/** | ||
* @param {number[]} nums | ||
* @return {number} | ||
*/ | ||
var longestConsecutive = function (nums) { | ||
const set = new Set(); | ||
|
||
// set 생성 : O(n) | ||
for (const num of nums) { | ||
set.add(num); | ||
} | ||
|
||
let res = 0; | ||
|
||
// set 순회 : O(n) | ||
for (const num of set) { | ||
if (set.has(num - 1)) continue; | ||
let tmp = 1; | ||
let cur = num; | ||
// While 루프 전체: O(n) | ||
while (true) { | ||
const v = (cur += 1); | ||
if (set.has(v)) tmp += 1; | ||
else break; | ||
} | ||
if (tmp > res) res = tmp; | ||
} | ||
return res; | ||
}; |
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,12 @@ | ||
/** | ||
* @param {number} n | ||
* @return {number} | ||
*/ | ||
var hammingWeight = function (n) { | ||
let res = 0; | ||
while (n > 0) { | ||
res += n & 1; | ||
n = n >> 1; // 나누기 2 | ||
} | ||
return res; | ||
}; |
jun0811 marked this conversation as resolved.
Show resolved
Hide resolved
|
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 countMap = new Map(); | ||
for (const num of nums) { | ||
if (countMap.has(num)) { | ||
countMap.set(num, countMap.get(num) + 1); | ||
} else { | ||
countMap.set(num, 1); | ||
} | ||
} | ||
const countArr = [...countMap]; | ||
countArr.sort((a, b) => b[1] - a[1]); | ||
|
||
const res = countArr.slice(0, k).map((count) => count[0]); | ||
return res; | ||
}; |
jun0811 marked this conversation as resolved.
Show resolved
Hide resolved
|
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 @@ | ||
// Time Complexity : O(N^2) | ||
|
||
var twoSum = function (nums, target) { | ||
const l = nums.length; | ||
for (let i = 0; i < l; i++) { | ||
for (let j = i + 1; j < l; j++) { | ||
const value = nums[i] + nums[j]; | ||
if (value == target) return [i, j]; | ||
} | ||
} | ||
}; | ||
|
||
// Time Complexity : O(N) | ||
|
||
var twoSum = function (nums, target) { | ||
const Map = {}; | ||
|
||
nums.forEach((num, idx) => { | ||
Map[num] = idx; | ||
}); | ||
|
||
for (const idx in nums) { | ||
const value = target - nums[idx]; | ||
if (Map[value] >= 0 && Map[value] != idx) return [+idx, Map[value]]; | ||
} | ||
}; |
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.
Uh oh!
There was an error while loading. Please reload this page.