-
-
Notifications
You must be signed in to change notification settings - Fork 339
[01-binary] WEEK 03 solutions #2457
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
base: main
Are you sure you want to change the base?
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,18 @@ | ||
| /** | ||
| * @param {number} n | ||
| * @return {number} | ||
| */ | ||
|
|
||
| var climbStairs = function (n) { | ||
| if (n === 1) return 1; | ||
| if (n === 2) return 2; | ||
| const arr = [0, 1, 2]; | ||
|
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. 배열을 활용하는 방법도 괜찮지만, 3개의 변수로 설정하는게 나을 것 같습니다. |
||
| let i = 3; | ||
| while (i <= 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문으로 작성하는 게 가독성과 안전 측면에서 더 좋을 것 같습니다. |
||
| const temp = arr[i - 1] + arr[i - 2]; | ||
| arr.push(temp); | ||
| i++; | ||
| } | ||
|
|
||
| return arr[n]; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
| var rob = function (nums) { | ||
| const memo = new Map(); | ||
| return dfs(nums, 0, memo); | ||
| }; | ||
|
|
||
| const dfs = (nums, i, memo) => { | ||
|
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. 보통 dfs 함수 선언 자체를 함수 안에 선언하여 클로져 스코프로 변수를 참조하고는 하는데, |
||
| if (i >= nums.length) return 0; | ||
| if (memo.has(i)) return memo.get(i); | ||
| const result = Math.max( | ||
| nums[i] + dfs(nums, i + 2, memo), | ||
| dfs(nums, i + 1, memo), | ||
| ); | ||
| memo.set(i, result); | ||
| return result; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number} | ||
| */ | ||
| var longestConsecutive = function (nums) { | ||
| const unique_arr = Array.from(new Set(nums)); | ||
|
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. JS에서는 카멜케이스를 쓰는 경우가 많아서 |
||
| const temp = unique_arr.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. sort는 mutable함수여서 지금 코드에서는 unqiue_arr까지 sort가 이루어지는데요. const temp = [...unique_arr].sort((a,b)=>a-b);와 같이 사이드이펙트를 제어할 수 있습니다. |
||
| let a = 1; | ||
|
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. a라는 변수가 의미를 추측하기 어려워서 특히 요새는 LLM으로 개발을 많이 하기 때문에, |
||
| let max = 0; | ||
| for (let i = 0; i < temp.length; i++) { | ||
| if (temp[i] + 1 === temp[i + 1]) { | ||
| a++; | ||
| } else { | ||
| max = Math.max(a, max); | ||
| a = 1; | ||
| } | ||
| } | ||
| return max; | ||
| }; | ||
|
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,13 @@ | ||
| /** | ||
| * @param {number} n | ||
| * @return {number} | ||
| */ | ||
| var hammingWeight = function (n) { | ||
| let cnt = 1; | ||
| while (n >= 2) { | ||
| const r = n % 2; | ||
| n = Math.floor(n / 2); | ||
| if (r === 1) cnt++; | ||
| } | ||
| return cnt; | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @return {number[]} | ||
| */ | ||
| var productExceptSelf = function (nums) { | ||
| const r = Array.from(nums.length); | ||
| const l = Array.from(nums.length); | ||
| const a = Array.from(nums.length); | ||
| for (let i = 0; i < nums.length; i++) { | ||
| const temp = r[i - 1] ?? 1; | ||
|
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. 기왕 위에서 초기화 해주는 김에 r.fill(1);
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. i - 1로 조회하는 부분이 있으니, for (let i = 1; ...위와 같이 1부터 조회하면 안전할 것 같습니다. |
||
| const temp2 = nums[i - 1] ?? 1; | ||
| r[i] = temp * temp2; | ||
| } | ||
| for (let i = nums.length - 1; i > -1; 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. 이 부분도 마찬가지로 nums.length - 2;로 설정하면 안전할 것 같습니다. |
||
| const temp = l[i + 1] ?? 1; | ||
| const temp2 = nums[i + 1] ?? 1; | ||
| l[i] = temp * temp2; | ||
| } | ||
| for (let i = 0; i < nums.length; i++) { | ||
| a[i] = l[i] * r[i]; | ||
| } | ||
|
|
||
| return a; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @param {number} k | ||
| * @return {number[]} | ||
| */ | ||
| var topKFrequent = function (nums, k) { | ||
| const map = new Map(); | ||
| for (let i = 0; i < nums.length; i++) { | ||
| const temp = map.get(nums[i]); | ||
| map.set(nums[i], temp !== undefined ? temp + 1 : 1); | ||
| } | ||
| const sortedMap = new Map([...map.entries()].sort((a, b) => b[1] - a[1])); | ||
|
|
||
| const iter = sortedMap.keys(); | ||
| const result = []; | ||
| for (let i = 0; i < k; 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. Array.from(sortedMap.keys()).slice(0, k);로 단순화 할 수 있을 것 같습니다. |
||
| result.push(iter.next().value); | ||
| } | ||
| return result; | ||
| }; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /** | ||
| * @param {number[]} nums | ||
| * @param {number} target | ||
| * @return {number[]} | ||
| */ | ||
| var twoSum = function (nums, target) { | ||
| for (let i = 0; i < nums.length - 1; i++) { | ||
| for (let j = i + 1; j < nums.length; j++) { | ||
| if (nums[i] + nums[j] === target) { | ||
| return [i, j]; | ||
| } | ||
| } | ||
| } | ||
| }; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| /** | ||
| * @param {string} s | ||
| * @return {boolean} | ||
| */ | ||
| var isPalindrome = function (s) { | ||
| const result = s.toLowerCase().replace(/[^a-z0-9]/g, ''); | ||
| return result === result.split('').reverse().join(''); | ||
| }; |
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.
으로 간결하게 표현 가능할 것 같아요.