-
-
Notifications
You must be signed in to change notification settings - Fork 305
[JangAyeon] WEEK 01 solutions #1972
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
7ee5ca1
7074bf4
85c0c33
2a7b029
dfe88cf
5f89ea1
5304e39
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,19 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {boolean} | ||
| */ | ||
|
|
||
| // 첫번째 제출 | ||
| var containsDuplicate = function (nums) { | ||
| const counter = new Map(); | ||
| for (let e of nums) { | ||
| const v = counter.has(e) ? counter.get(e) + 1 : 1; | ||
| counter.set(e, v); | ||
| } | ||
| const answer = [...counter.values()].some((item) => item >= 2); | ||
| return answer; | ||
| }; | ||
| // 두번째 제출 | ||
| var containsDuplicate = function (nums) { | ||
| return new Set(nums).size !== nums.length; | ||
|
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. 간단하고 깔끔한 풀이네요!
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 하면, |
||
| }; | ||
|
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. 반복문 하나만 사용해도 풀리는 문제였네요. 저는 dp로 풀기 위해 반복문을 하나 더 사용했는데, 하나 알아갑니다! |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
| var rob = function (nums) { | ||
| const N = nums.length; | ||
| let answer = 0; | ||
| let [rob1, rob2] = [0, 0]; | ||
| for (let num of nums) { | ||
| let temp = Math.max(num + rob1, rob2); | ||
| rob1 = rob2; | ||
| rob2 = temp; | ||
| } | ||
|
Comment on lines
+8
to
+13
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. 아래와 같이 로직을 이해했는데, 맞을까요?
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. 네, 정확하게 이해하신 게 맞습니다!
그래서 두 값 중 더 큰 값을 선택해 temp에 저장하고, 정확하게 로직을 짚어주셔서 감사합니다 |
||
| return rob2; | ||
| }; | ||
|
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,26 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
| var longestConsecutive = function (nums) { | ||
| if (nums.length == 0) return 0; | ||
| let answer = 0; | ||
| let numsSet = new Set(nums); | ||
| const N = nums.length; | ||
|
|
||
| for (let num of numsSet) { | ||
| if (!numsSet.has(num - 1)) { | ||
| let temp = num; | ||
| let length = 1; | ||
|
|
||
| while (numsSet.has(temp + 1)) { | ||
| length += 1; | ||
| temp += 1; | ||
| } | ||
| // console.log(length) | ||
| answer = Math.max(length, answer); | ||
| } | ||
| } | ||
|
|
||
| return answer; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @param {number} k | ||
| * @return {number[]} | ||
| */ | ||
| var topKFrequent = function (nums, k) { | ||
| nums = 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.
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. 앗 다른 풀이 방법으로 해보다가 지우지 않은 코드가 남아있었네요!! |
||
| const counter = new Map(); | ||
| for (let num of nums) { | ||
| const value = counter.has(num) ? counter.get(num) + 1 : 1; | ||
| counter.set(num, value); | ||
| } | ||
| const sorted = [...counter.entries()].sort((a, b) => b[1] - a[1]); | ||
| const answer = sorted.slice(0, k).map((item) => item[0]); | ||
| // console.log(counter, sorted, answer) | ||
|
|
||
| return answer; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @param {number} target | ||
| * @return {number[]} | ||
| */ | ||
|
|
||
| // 첫번째 통과 풀이 - 브루트포스 | ||
| var twoSum = function (nums, target) { | ||
| for (let i = 0; i < nums.length; i++) { | ||
| for (let j = i + 1; j < nums.length; j++) { | ||
| if (nums[i] + nums[j] == target) { | ||
| return [i, j]; | ||
| } | ||
| } | ||
| } | ||
| }; | ||
|
Comment on lines
+8
to
+16
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. 브루트포스로도 시간초과가 안 나는 문제였네요! 간단하고 확실한 방법이죠~
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. 맞아요! 이 문제는 가장 직관적이고 구현도 간단한 풀이로 브루트포스 방식을 사용해보았는데 |
||
|
|
||
| // 두번째 통과 풀이 - 해시맵 + 효율성 고려 | ||
| var twoSum = function (nums, target) { | ||
| const map = new Map(); // {값: 인덱스} | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| const complement = target - nums[i]; // 필요한 짝 계산 | ||
| if (map.has(complement)) { | ||
| return [map.get(complement), i]; // 이전에 complement가 있었으면 바로 반환 | ||
| } | ||
| map.set(nums[i], i); | ||
| } | ||
| }; | ||
|
Comment on lines
+19
to
+29
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. 제가 생각하지 못한 접근법이네요! 저는 투포인터로 풀었는데, 해시맵 풀이도 하나 배워갑니다 📝
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. 오 저는 투포인터 방식은 생각하지 못했는데, 서로 다른 방식으로 풀이 공유할 수 있어서 좋네요. |
||
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.
모두 순회하면서
counter에nums를 넣기 보다는, 조건문을 통해 1보다 커지는 경우를 바로return하도록 처리하면 중복이 맨 뒤에 있을 때 좀 더 효율적으로 처리 가능할 것 같습니다!