-
-
Notifications
You must be signed in to change notification settings - Fork 301
[dev_qorh] WEEK1 solutions #2001
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,73 @@ | ||
| /** 1차 시도 | ||
| function rob(nums: number[]): number { | ||
| const jumpArray = [] | ||
|
|
||
| if (nums.length<3) { | ||
| nums.sort((a,b)=> b-a) | ||
| return nums[0] | ||
| } | ||
|
|
||
| for (let i = 0; i<=nums.length-3; i++) { | ||
| jumpArray.push(nums[i]+nums[i+2]) | ||
| } | ||
|
|
||
| let maximum = 0 | ||
|
|
||
| for (let i = 0; i<jumpArray.length-2; i+=3) { | ||
| if(jumpArray[i] >= jumpArray[i+1]) { | ||
| maximum += jumpArray[i] | ||
| }else{ | ||
| maximum += jumpArray[i+1] | ||
| } | ||
| } | ||
|
|
||
| return maximum | ||
| }; | ||
| */ | ||
|
|
||
| /** 2차 시도, 풀이 참고 | ||
| f(nums) => nums 를 털어 구할 수 있는 가장 큰 금액 | ||
| 첫집부터 털었을 때: nums[0] + f(nums[2:]) | ||
| 둘째집부터 털었을 때: f(nums[1:]) | ||
|
|
||
| 첫 시작 | ||
| f(nums) = MAX(nums[0] + f(nums[2:]), f(nums[1:])) | ||
|
|
||
| function rob(nums: number[]): number { | ||
| function f (num: number) { | ||
| if(nums.length - 1 < num) { | ||
| return 0 | ||
| } | ||
|
|
||
| const first = nums[num] + f(num + 2) | ||
| const second = f(num + 1) | ||
| return first >= second ? first : second | ||
| } | ||
|
|
||
| return f(0) | ||
| } | ||
| */ | ||
|
|
||
| function rob(nums: number[]): number { | ||
| const resultMap = new Map(); | ||
|
|
||
| function f(num: number) { | ||
| if (resultMap.has(num)) { | ||
| return resultMap.get(num); | ||
| } | ||
|
|
||
| if (nums.length - 1 < num) { | ||
| resultMap.set(num, 0); | ||
| } else { | ||
| const first = nums[num] + f(num + 2); | ||
| const second = f(num + 1); | ||
|
|
||
| const result = first >= second ? first : second; | ||
| resultMap.set(num, result); | ||
| } | ||
|
|
||
| return resultMap.get(num); | ||
| } | ||
|
|
||
| return f(0); | ||
| } | ||
|
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,14 @@ | ||
| /** | ||
| * 가장 단순한 O(n^2) 로 풀이하였습니다 | ||
| */ | ||
| function twoSum(nums: number[], target: number): number[] { | ||
| 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]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| 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. 마지막 라인에 개행 문자가 빠져있습니다
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. ci 를 정상통과 하는 것으로 보아, 적용이 되어 있는 것 같습니다. 확인 감사합니다! |
||
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값이 원소보다는 인덱스 용도로 쓰이는거 같은데 맞을까요?만약 맞다면 직관적인 변수명(
index등)이 좋을 것 같습니다