Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 73 additions & 0 deletions house-robber/Baekwangho.ts
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);
Comment on lines +54 to +69
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

제가 파이썬은 잘 모르지만 num 값이 원소보다는 인덱스 용도로 쓰이는거 같은데 맞을까요?
만약 맞다면 직관적인 변수명(index 등)이 좋을 것 같습니다

}

return f(0);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

마지막 라인에 개행 문자가 빠져있습니다

14 changes: 14 additions & 0 deletions two-sum/Baekwangho.ts
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 [];
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

마지막 라인에 개행 문자가 빠져있습니다

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ci 를 정상통과 하는 것으로 보아, 적용이 되어 있는 것 같습니다. 확인 감사합니다!