-
-
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
Conversation
ys-han00
left a comment
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.
안녕하세요~! 가이드에 따르면 리뷰 경험이 없는 제가 리뷰어더라구요 😅
제가 아는 것도 적고 이번이 첫 리뷰다 보니, 리뷰에 많이 부족한 점이 있을 것 같아 미리 사죄드립니다....
최대한 제가 생각할 수 있는 선에서 궁금한 점과 피드백 남겨요!
이와 관련해서 질문 있으실 경우, Discord나 PR 댓글로 멘션 주세요! (이상하다 싶으시면 넘어가셔도 괜찮습니다 😄)
첫 주 문제 풀이 수고하셨습니다!
| 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); |
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 하도록 처리하면 중복이 맨 뒤에 있을 때 좀 더 효율적으로 처리 가능할 것 같습니다!
| }; | ||
| // 두번째 제출 | ||
| var containsDuplicate = function (nums) { | ||
| return new Set(nums).size !== nums.length; |
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.
간단하고 깔끔한 풀이네요!
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.
말씀해주신 것처럼 중복을 발견하는 즉시 return 하면,
특히 중복 값이 배열 뒤쪽에 있을 때 전체 순회를 하지 않아도 되어 더 효율적이고,
시간 단축 + 불필요한 순회 제거라는 큰 이점이 있어 공감합니다!
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.
반복문 하나만 사용해도 풀리는 문제였네요.
저는 dp로 풀기 위해 반복문을 하나 더 사용했는데, 하나 알아갑니다!
| let [rob1, rob2] = [0, 0]; | ||
| for (let num of nums) { | ||
| let temp = Math.max(num + rob1, rob2); | ||
| rob1 = rob2; | ||
| rob2 = temp; | ||
| } |
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.
아래와 같이 로직을 이해했는데, 맞을까요?
num + rob1: 위치가i인 집을 털고,i-2집까지 털어온 최댓값을 더한 값rob2: 위치i집을 포기하고,i-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.
네, 정확하게 이해하신 게 맞습니다!
- num + rob1 → 현재 집(i)을 털었을 때, 두 칸 전(i-2)까지의 최대 금액인 rob1과 현재 집의 금액을 더한 값
- rob2 → 현재 집(i)을 털지 않았을 때, 바로 전 집(i-1)까지의 최대 금액
그래서 두 값 중 더 큰 값을 선택해 temp에 저장하고,
rob1, rob2를 각각 i-1, 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.
간단 명료한 풀이네요👍
| * @return {number[]} | ||
| */ | ||
| var topKFrequent = function (nums, k) { | ||
| nums = nums.sort((a, b) => a - b); |
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.
nums를 정렬하신 이유가 궁금합니다!
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.
앗 다른 풀이 방법으로 해보다가 지우지 않은 코드가 남아있었네요!!
| 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]; | ||
| } | ||
| } | ||
| } | ||
| }; |
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.
브루트포스로도 시간초과가 안 나는 문제였네요!
간단하고 확실한 방법이죠~
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.
맞아요! 이 문제는 가장 직관적이고 구현도 간단한 풀이로 브루트포스 방식을 사용해보았는데
입력 크기가 커도 이중 반복문으로 시간 초과가 나지 않았습니다. ㅎㅎ
나중에 확장하거나 최적화할 때는 해시맵 풀이로 바꿔서도 풀이해보았습니다 🙂
| 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); | ||
| } | ||
| }; |
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.
제가 생각하지 못한 접근법이네요!
저는 투포인터로 풀었는데, 해시맵 풀이도 하나 배워갑니다 📝
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.
오 저는 투포인터 방식은 생각하지 못했는데, 서로 다른 방식으로 풀이 공유할 수 있어서 좋네요.
코멘트 감사합니다! 🙌
|
코멘트 다 읽고 Approved 변경했습니다! 첫 주 문제 풀이 수고하셨어요~ (상태 변경 후 말씀드리는걸 까먹어서 급하게 지금 달아요 ㅎㅎ) |
답안 제출 문제
작성자 체크 리스트
In Review로 설정해주세요.검토자 체크 리스트
Important
본인 답안 제출 뿐만 아니라 다른 분 PR 하나 이상을 반드시 검토를 해주셔야 합니다!