-
-
Notifications
You must be signed in to change notification settings - Fork 304
[sujeong-dev] WEEK 01 solutions #2004
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,46 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {boolean} | ||
| * | ||
| * 시간복잡도 계산 | ||
| * 입력 크기(n)가 커질 때 연산 횟수가 n에 비례해서 증가 | ||
| * => O(n) | ||
| * | ||
| * 공간복잡도 계산 | ||
| * indices가 nums에 비례해서 할당 | ||
| * 상수 i 할당 | ||
| * => O(n) | ||
| */ | ||
| var containsDuplicate = function (nums) { | ||
| let indices = {}; | ||
| nums.forEach((num, index) => { | ||
| indices[num] = index; | ||
| }); | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| if (nums[i] in indices && indices[nums[i]] !== i) return true; | ||
| } | ||
|
|
||
| return false; | ||
| }; | ||
|
|
||
| /** | ||
| * @param {number[]} nums | ||
| * @return {boolean} | ||
| * | ||
| * 시간복잡도 계산 | ||
| * Set의 size속성은 입력 크기와 관계없이 일정한 시간이 걸림 | ||
| * => O(1) | ||
| * | ||
| * 공간복잡도 계산 | ||
| * indices가 nums에 비례해서 할당 | ||
| * 상수 i 할당 | ||
| * => O(n) | ||
| */ | ||
| var containsDuplicate = function (nums) { | ||
| const indices = new Set(nums); | ||
|
|
||
| if (indices.size !== nums.length) return true; | ||
|
|
||
| return false; | ||
|
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. 다음과 같이 return 문을 분기 없이 간결하게 나타낼 수도 있을 것 같아요! return indices.size !== nums.length; |
||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @param {number} k | ||
| * @return {number[]} | ||
| * | ||
| * 시간복잡도 계산 | ||
| * for loop가 O(n) | ||
| * reduce가 O(n) | ||
| * sort가 O(n log n) | ||
| * => O(n log n) | ||
|
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. 현재 코드에서는 for 문 내에 |
||
| */ | ||
| var topKFrequent = function (nums, k) { | ||
| let 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. 배열이 아닌 hash map을 이용해서 값 별 frequency를 기록한다면 |
||
| for (let i = 0; i < nums.length; i++) { | ||
| if (result.find((el) => el.key == nums[i])) continue; | ||
| const count = nums.reduce((cnt, el) => cnt + (nums[i] === el), 0); | ||
| result.push({ key: nums[i], value: count }); | ||
| } | ||
|
|
||
| return result | ||
| .sort((a, b) => b.value - a.value) | ||
| .slice(0, k) | ||
| .map((el) => el.key); | ||
| }; | ||
|
|
||
| /** | ||
| * Follow up: Your algorithm's time complexity must be better than O(n log n), where n is the array's size. | ||
| */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @param {number} target | ||
| * @return {number[]} | ||
| * | ||
| * 시간복잡도 계산 | ||
| * 입력 크기(n)가 커질 때 연산 횟수가 n^2에 비례해서 증가 | ||
| * => O(n^2) | ||
| * | ||
| * 공간복잡도 계산 | ||
| * 상수 i 할당 | ||
| * 상수 j 할당 | ||
| * => o(1) | ||
| */ | ||
| var twoSum = function (nums, target) { | ||
| for (let i = 0; i < nums.length; i++) { | ||
| for (let j = 0; j < nums.length; j++) { | ||
|
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.
|
||
| if (i === j) continue; | ||
| if (nums[i] + nums[j] === target) return [i, j]; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| /** | ||
| * @param {number[]} nums | ||
| * @param {number} target | ||
| * @return {number[]} | ||
| * | ||
| * 시간복잡도 계산 | ||
| * 입력 크기(n)가 커질 때 연산 횟수가 n에 비례해서 증가 | ||
| * => O(n) | ||
| * | ||
| * 공간복잡도 계산 | ||
| * result가 nums에 비례해서 할당 | ||
| * 상수 i, findNum 할당 | ||
| * => O(n) | ||
| */ | ||
| var twoSum = function (nums, target) { | ||
| let result = {}; | ||
|
|
||
| nums.forEach((num, index) => { | ||
| result[num] = index; | ||
| }); | ||
|
|
||
| for (let i = 0; i < nums.length; i++) { | ||
| const findNum = target - nums[i]; | ||
|
|
||
| if (findNum in result && result[findNum] !== i) { | ||
|
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.
|
||
| return [i, result[findNum]]; | ||
| } | ||
| } | ||
| }; | ||
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.
indices.size의 경우 O(1) time이 소요되지만new Set(nums)에서 내부적으로nums를 순회하며 원소를 삽입하기 때문에 O(n) time이 소요되므로, 전체 시간복잡도가 O(n)이 될 것 같습니다!