-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Wan] Week 1 #683
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
[Wan] Week 1 #683
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,21 @@ | ||
/** | ||
* 217. Contains Duplicate | ||
* Given an integer array nums, return true if any value appears at least twice in the array, and return false if every element is distinct. | ||
* https://leetcode.com/problems/contains-duplicate/description/ | ||
*/ | ||
function containsDuplicate(nums: number[]): boolean { | ||
const set = new Set<number>(); | ||
|
||
for (let i = 0; i < nums.length; i++) { | ||
if (set.has(nums[i])) { | ||
return true; | ||
} | ||
set.add(nums[i]); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
// TC: O(n) | ||
// https://262.ecma-international.org/15.0/index.html#sec-get-set.prototype.size | ||
// Set objects must be implemented using either hash tables or other mechanisms that, on average, provide access times that are sublinear on the number of elements in the collection. The data structure used in this specification is only intended to describe the required observable semantics of Set objects. It is not intended to be a viable implementation model. |
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,33 @@ | ||
/** | ||
* 128. Longest Consecutive Sequence | ||
* Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence. | ||
* | ||
* You must write an algorithm that runs in O(n) time. | ||
* https://leetcode.com/problems/longest-consecutive-sequence/description/ | ||
*/ | ||
function longestConsecutive(nums: number[]): number { | ||
const set = new Set(nums); | ||
const sorted = [...set].sort((a, b) => a - b); | ||
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. set을 사용하신김에 has로 원하는 수가 set안에 있는지 확인하는 방법을 사용할 수도 있을것같습니다! |
||
|
||
if (sorted.length === 0) { | ||
return 0; | ||
} | ||
|
||
let longestSequence = 1; | ||
let currentSequence = 1; | ||
for (let i = 0; i - 1 < sorted.length; i++) { | ||
if (Math.abs(sorted[i + 1] - sorted[i]) === 1) { | ||
currentSequence++; | ||
} else { | ||
if (currentSequence > longestSequence) { | ||
longestSequence = currentSequence; | ||
} | ||
currentSequence = 1; | ||
} | ||
} | ||
|
||
return longestSequence; | ||
} | ||
|
||
// O(n) time | ||
// O(n) space |
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 @@ | ||
/** | ||
* 347. Top K Frequent Elements | ||
* Given an integer array nums and an integer k, return the k most frequent elements. You may return the answer in | ||
* any order. | ||
* https://leetcode.com/problems/top-k-frequent-elements/description/ | ||
*/ | ||
function topKFrequent(nums: number[], k: number): number[] { | ||
const map = new Map<number, number>(); | ||
|
||
nums.forEach((n) => { | ||
const count = map.get(n); | ||
if (count) { | ||
map.set(n, count + 1); | ||
return; | ||
} | ||
map.set(n, 1); | ||
}); | ||
|
||
const kvArray = [...map]; | ||
const sorted = kvArray.sort(([, v1], [, v2]) => v2 - v1); | ||
|
||
const result: number[] = []; | ||
for (let i = 0; i < k; i++) { | ||
result.push(sorted[i][0]); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
// O(n) time | ||
// O(n) space |
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,16 @@ | ||
/** | ||
* 125. Valid Palindrome | ||
* A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all | ||
* non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters | ||
* and numbers. | ||
* Given a string s, return true if it is a palindrome, or false otherwise. | ||
* https://leetcode.com/problems/valid-palindrome/description/ | ||
*/ | ||
function isPalindrome(s: string): boolean { | ||
const chars = s.replace(/[^a-z0-9]/gi, "").toLowerCase(); | ||
|
||
return chars === [...chars].reverse().join(""); | ||
Comment on lines
+10
to
+12
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(n) time | ||
// O(n) space |
Oops, something went wrong.
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.
아래와 같이 한줄로 풀이하는 방법도 있을것 같습니다 :)
new Set(nums).size !== nums.length;