-
-
Notifications
You must be signed in to change notification settings - Fork 303
[prgmr99] WEEK 01 solutions #1987
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
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,10 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {boolean} | ||
| */ | ||
| var containsDuplicate = function (nums) { | ||
| const numArrLength = nums.length; | ||
| const numSetSize = new Set(nums).size; | ||
|
|
||
| return numArrLength !== numSetSize; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
| var rob = function (nums) { | ||
| let maxMoneyAtPrevHouse = 0; | ||
| let maxMoneyAtTwoHousesBack = 0; | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| let currentMax = Math.max( | ||
| nums[i] + maxMoneyAtTwoHousesBack, | ||
| maxMoneyAtPrevHouse | ||
| ); | ||
|
|
||
| maxMoneyAtTwoHousesBack = maxMoneyAtPrevHouse; | ||
| maxMoneyAtPrevHouse = currentMax; | ||
| } | ||
|
|
||
| return maxMoneyAtPrevHouse; | ||
| }; | ||
|
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. 간결하게 잘 푸신 것 같아요! 다음과 같은 식을 알게됐어요. currentMax = max(
nums[i] + DP[i-2], // 이번 집을 털면 → i-1 집은 못 턴다
DP[i-1] // 이번 집을 안 털면 → i-1 집까지의 최대값 그대로
)이런 방식으로 구현하신게 맞을까요? 추가로 변수명을 직관적으로 잘 적어주셔서 이해하기 더 편했습니다👍
Member
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. 넵! 맞습니다! |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
| var longestConsecutive = function (nums) { | ||
| let result = 1; | ||
| let maxLength = 1; | ||
|
|
||
| if (nums.length === 0) return 0; | ||
|
|
||
| const sortedNums = nums.sort((a, b) => a - b); | ||
|
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. 저는 이 부분에서 별도로 변수에 할당을 하진 않았는데 배열이 어떤 상태인지 생각할 때 덜 헷갈리게하려면 이렇게 작성하는 습관을 들이는게 좋을거같네요..!
Member
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 (let i = 1; i < sortedNums.length; i++) { | ||
| const prevNum = sortedNums[i - 1]; | ||
| const currentNum = sortedNums[i]; | ||
|
|
||
| const diff = currentNum - prevNum; | ||
|
|
||
| if (diff === 1) { | ||
| result += 1; | ||
|
|
||
| if (maxLength < result) { | ||
| maxLength = result; | ||
|
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. 이 부분이 최대 길이를 갱신해주는 부분이군요! |
||
| } | ||
| } else if (diff === 0) { | ||
| continue; | ||
| } else { | ||
| result = 1; | ||
| } | ||
| } | ||
|
|
||
| return maxLength; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @param {number} k | ||
| * @return {number[]} | ||
| */ | ||
| var topKFrequent = function (nums, k) { | ||
| const result = []; | ||
| const numMap = new Map(); | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| const currentValue = numMap.get(nums[i]); | ||
|
|
||
| if (currentValue) { | ||
| numMap.set(nums[i], currentValue + 1); | ||
| } else { | ||
| numMap.set(nums[i], 1); | ||
| } | ||
| } | ||
|
|
||
| const mapToArr = [...numMap.entries()]; | ||
|
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. 이터러블의 entries 함수와 스프레드 연산자로 배열을 만드셨네요. |
||
|
|
||
| mapToArr.sort((a, b) => { | ||
| if (a[1] < b[1]) return 1; | ||
| if (a[1] > b[1]) return -1; | ||
| return 0; | ||
| }); | ||
|
|
||
| for (let i = 0; i < k; i++) { | ||
| result.push(mapToArr[i][0]); | ||
| } | ||
|
|
||
| return result; | ||
| }; | ||
|
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. 깔끔하게 잘 푸신 것 같아요! 고생하셨습니다~
Member
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. 감사합니다~~ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @param {number} target | ||
| * @return {number[]} | ||
| */ | ||
| var twoSum = function (nums, target) { | ||
| const numMap = new Map(); | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| const firstNum = nums[i]; | ||
| const secondNum = target - nums[i]; | ||
|
|
||
| if (numMap.has(secondNum)) { | ||
| return [numMap.get(secondNum), i]; | ||
| } | ||
|
|
||
| numMap.set(firstNum, i); | ||
| } | ||
| }; |
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 객체의 길이와 기존 배열의 길이를 비교하셨네요!!
한 눈에 봤을 때 이해가 잘 되어서 좋았습니다 👍
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.
감사합니다 :)