-
-
Notifications
You must be signed in to change notification settings - Fork 245
[HoonDongKang] WEEK 01 solutions #1160
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b73b77f
containsDuplicate solution
HoonDongKang 5b79511
Two Sum Solution
HoonDongKang 3161ca5
Product of Array Except Self solution
HoonDongKang 7d2eb70
Longest Consecutive Sequence solution
HoonDongKang 4460eee
House Robber solution
HoonDongKang File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/** | ||
* [Problem]: [217] Contains Duplicate | ||
* (https://leetcode.com/problems/contains-duplicate/description/) | ||
*/ | ||
|
||
function containsDuplicate(nums: number[]): boolean { | ||
// 시간복잡도: O(n^2) | ||
// 공간복잡도: O(1) | ||
const doubleLoopFunc = (nums: number[]) => { | ||
let isDuplicated = false; | ||
for (let i = 0; i < nums.length; i++) { | ||
for (let j = i + 1; j < nums.length; j++) { | ||
if (nums[i] === nums[j]) isDuplicated = true; | ||
} | ||
} | ||
return isDuplicated; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) | ||
const setFunc = (nums: number[]) => { | ||
const numsSet = new Set<number>(nums); | ||
|
||
return nums.length !== numsSet.size; | ||
}; | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) | ||
const mapFunc = (nums: number[]) => { | ||
const numsMap = new Map<number, boolean>(); | ||
|
||
for (const num of nums) { | ||
if (numsMap.get(num)) return true; | ||
numsMap.set(num, true); | ||
} | ||
|
||
return false; | ||
}; | ||
|
||
return mapFunc(nums); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/** | ||
* [Problem]: [198] House Robber | ||
* (https://leetcode.com/problems/house-robber/description/) | ||
*/ | ||
function rob(nums: number[]): number { | ||
// 시간 복잡도 O(2^n) | ||
// 공간 복잡도 O(n) | ||
// 시간 초과 | ||
function recursionFunc(nums: number[]): number { | ||
function getMax(start: number): number { | ||
if (nums.length - 1 < start) return 0; | ||
return Math.max(nums[start] + getMax(start + 2), getMax(start + 1)); | ||
} | ||
|
||
return getMax(0); | ||
} | ||
|
||
// 메모이제이션 | ||
// 시간복잡도 O(n) | ||
// 공간복잡도 O(n) | ||
function memoizationFunc(nums: number[]): number { | ||
let memoArr = new Array(nums.length).fill(-1); | ||
function getMax(start: number): number { | ||
if (nums.length - 1 < start) return 0; | ||
if (memoArr[start] !== -1) return memoArr[start]; | ||
|
||
memoArr[start] = Math.max(nums[start] + getMax(start + 2), getMax(start + 1)); | ||
|
||
return memoArr[start]; | ||
} | ||
|
||
return getMax(0); | ||
} | ||
|
||
// DP | ||
// 시간복잡도 O(n) | ||
// 공간복잡도 O(1) | ||
function dpSolution(nums: number[]): number { | ||
if (nums.length === 1) return nums[0]; | ||
|
||
let prev2 = 0; | ||
let prev1 = 0; | ||
|
||
for (let num of nums) { | ||
let current = Math.max(prev1, prev2 + num); | ||
prev2 = prev1; | ||
prev1 = current; | ||
} | ||
|
||
return prev1; | ||
} | ||
|
||
return dpSolution(nums); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
/** | ||
* [Problem]: [128] Longest Consecutive Sequence | ||
* (https://leetcode.com/problems/longest-consecutive-sequence/description/) | ||
*/ | ||
|
||
function longestConsecutive(nums: number[]): number { | ||
// 시간 복잡도 O(n) | ||
// 공간 복잡도 O(n) | ||
function sortFunc(nums: number[]): number { | ||
const setArr = new Set(nums); | ||
let longestCount = 0; | ||
|
||
for (let num of setArr) { | ||
if (!setArr.has(num - 1)) { | ||
let current = num; | ||
let count = 1; | ||
|
||
while (setArr.has(current + 1)) { | ||
current++; | ||
count++; | ||
} | ||
|
||
longestCount = Math.max(count, longestCount); | ||
} | ||
} | ||
return longestCount; | ||
} | ||
|
||
return sortFunc(nums); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/** | ||
* [Problem]: [238] Product of Array Except Self | ||
* (https://leetcode.com/problems/product-of-array-except-self/description/) | ||
*/ | ||
|
||
function productExceptSelf(nums: number[]): number[] { | ||
// 시간 복잡도 O(n^2) | ||
// 공간 복잡도 O(n) | ||
// 시간 초과로 실패 | ||
function doubleLoopFunc(nums: number[]): number[] { | ||
return nums.map((_, i) => nums.reduce((acc, cur, j) => (i === j ? acc : acc * cur), 1)); | ||
} | ||
|
||
// 시간 복잡도 O(n) | ||
// 공간 복잡도 O(n) | ||
function separateFunc(nums: number[]): number[] { | ||
const length = nums.length; | ||
const result: number[] = new Array(length).fill(1); | ||
let leftProduct = 1; | ||
let rightProduct = 1; | ||
|
||
for (let i = 0; i < length; i++) { | ||
result[i] = leftProduct; | ||
leftProduct *= nums[i]; | ||
} | ||
|
||
for (let i = length - 1; i >= 0; i--) { | ||
result[i] *= rightProduct; | ||
rightProduct *= nums[i]; | ||
} | ||
|
||
return result; | ||
} | ||
|
||
return separateFunc(nums); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* [Problem]: [001] Two Sum | ||
* (https://leetcode.com/problems/two-sum/description/) | ||
*/ | ||
|
||
function twoSum(nums: number[], target: number): number[] { | ||
// 시간 복잡도 O(n^2) | ||
// 공간 복잡도 O(1) | ||
function doubleLoopFunc(num: 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 []; | ||
} | ||
|
||
// 시간 복잡도 O(n) | ||
// 공간 복잡도 O(n) | ||
function differenceMap(nums: number[], target: number): number[] { | ||
const diffMap = new Map<number, number>(); | ||
|
||
for (let [i, num] of nums.entries()) { | ||
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. entries를 사용해서 for문 돌리는 방법도 있었네요. |
||
const diff = target - num; | ||
if (diffMap.has(diff)) return [i, diffMap.get(diff)!]; | ||
diffMap.set(num, i); | ||
} | ||
|
||
return []; | ||
} | ||
|
||
// 시간 복잡도 O(nlog n) - sort | ||
// 공간 복잡도 O(1) | ||
// 정렬을 통해 인덱스 값을 유지할 수 없어서 실패 | ||
// 인덱스 값이 아닌 배열의 요소를 반환하는 문제에서는 사용이 가능할 듯? | ||
function twoPointerFunc(nums: number[], target: number): number[] { | ||
nums.sort((a, b) => a - b); | ||
let leftPointer = 0; | ||
let rightPointer = nums.length - 1; | ||
|
||
while (leftPointer < rightPointer) { | ||
const twoSum = nums[leftPointer] + nums[rightPointer]; | ||
if (twoSum === target) return [leftPointer, rightPointer]; | ||
|
||
if (twoSum < target) leftPointer++; | ||
if (twoSum > target) rightPointer--; | ||
} | ||
|
||
return []; | ||
} | ||
|
||
return twoPointerFunc(nums, target); | ||
} | ||
|
||
console.log(twoSum([2, 7, 11, 15], 9)); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
풀이 별로 함수 분리, 알고리즘 복잡도 주석 설명
위 두 가지 부분이 너무 좋다고 느꼈습니다!!
정리가 잘 되어 있어서 코드를 읽기도 쉬웠고,
나중에 다시 볼 때도 좋은 방식이라고 느낍니다.
저도 다음부터는 이렇게 정리하면 좋겠다고 느꼈고,
깔끔하게 작성하는 것에 대해서 좋은 공부가 되었습니다.
감사합니다!