-
-
Notifications
You must be signed in to change notification settings - Fork 339
[junzero741] WEEK 1 Solutions #2353
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
a0e762a
1b9b970
8b82af4
00c6257
fd1299d
da16ab2
517fe1a
8a79333
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,6 @@ | ||
| // TC: O(n) | ||
| // SC: O(n) | ||
| function containsDuplicate(nums: number[]): boolean { | ||
| const uniqueNums = new Set<number>(nums); | ||
| return uniqueNums.size < nums.length | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| // TC: O(N) | ||
| // SC: O(1) | ||
| function rob(nums: number[]): number { | ||
| if (nums.length === 0) return 0; | ||
|
|
||
| let prevMax: number = 0; | ||
| let currMax: number = 0; | ||
|
|
||
| for (const amount of nums) { | ||
| const nextMax: number = Math.max(currMax, prevMax + amount); | ||
|
|
||
| prevMax = currMax; | ||
| currMax = nextMax; | ||
| } | ||
|
|
||
| return currMax; | ||
| } |
|
Contributor
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. 문제 의도대로 잘 풀어주셨습니다! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| // TC: O(N) | ||
| // SC: O(N) | ||
| function longestConsecutive(nums: number[]): number { | ||
| let finalMaxSequence = 0; | ||
|
|
||
| // O(n) | ||
| const numsSet = new Set(nums); | ||
|
|
||
| // O(n) | ||
| for(const num of numsSet) { | ||
| if(numsSet.has(num - 1)) { | ||
| continue; | ||
| } | ||
| let currentMaxSequence = 1; | ||
| while(numsSet.has(num + currentMaxSequence)) { | ||
| currentMaxSequence++; | ||
| } | ||
| finalMaxSequence = Math.max(finalMaxSequence, currentMaxSequence); | ||
| } | ||
|
|
||
| return finalMaxSequence; | ||
|
|
||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
|
|
||
| // TC: O(NlogN) | ||
| // SC: O(N) | ||
| function topKFrequent(nums: number[], k: number): number[] { | ||
|
|
||
| /** | ||
| Map ( O(N) ) | ||
| 1 : 4 | ||
| 4 : 1 | ||
| 3 : 3 | ||
| 2 : 2 | ||
|
|
||
| -> convert it to array ( O(N) ) : [[1,4], [4,1], [3,3], [2,2]] | ||
| -> sort by value ( O(NlogN) ): [[1,4], [3,3], [2,2], [4,1]] | ||
| -> slice(0,k) ( (O(N) ) : [[1,4], [3,3]] | ||
| -> make array from keys ( (O(N) ) : [1,4] | ||
| */ | ||
|
|
||
| const numMap = new Map<number, number>(); | ||
| const n = nums.length; | ||
|
|
||
| for(let i = 0; i < n; i++) { | ||
| const num = nums[i]; | ||
| const frequency = (numMap.get(num) || 0) | ||
| numMap.set(num, frequency + 1); | ||
| } | ||
|
|
||
|
|
||
| const numFrequencyArr = Array.from(numMap); | ||
| const sortedNumFrequencyKeysArr = | ||
| numFrequencyArr | ||
| .sort((a,b) => b[1]-a[1]) | ||
| .map((entry) => entry[0]); | ||
|
Contributor
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. 작은 부분이긴한데, map전에 slice를 하면 실행시간면에서 약간이라도 줄일 수 있을 것 같습니다
Contributor
Author
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. 다시보니 그렇네요!! 감사합니다 |
||
|
|
||
|
|
||
| return sortedNumFrequencyKeysArr.slice(0, k) | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| // TC: O(n) | ||
| // SC: O(n) | ||
| function twoSum(nums: number[], target: number): number[] { | ||
| const n = nums.length; | ||
| const complementMap = new Map<number, number>(); | ||
|
|
||
| for(let aIndex = 0; aIndex < n; aIndex++) { | ||
| const a = nums[aIndex]; | ||
| const b = target - a; | ||
|
|
||
| if(complementMap.has(b)) { | ||
| return [complementMap.get(b) || -1, aIndex]; | ||
|
Contributor
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. 해당부분은 위에서 has로 존재가 먼저 평가되어서 or로 null 체크를 안해도 될 것 같습니다!
Contributor
Author
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. 타입을 좁히기 위해 사용했던 건데, 다시 생각해보니 for 문 안에서 아래와 같이 풀면 더 효율적이겠네요! |
||
| } | ||
| complementMap.set(a, aIndex); | ||
| } | ||
|
|
||
| return [-1, -1]; | ||
| }; | ||
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.
아주 깔끔하네요. ㅎㅎ