-
-
Notifications
You must be signed in to change notification settings - Fork 245
[mike2ox] Week1 #653
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
[mike2ox] Week1 #653
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f3d0259
feat: Upload contains-duplicate solution (typescript)
mike2ox b0e37bc
docs: Add comments to problem solving
mike2ox dd7c18f
feat: Upload valid-palindrome solution (typescript)
mike2ox 04e2345
feat: Upload top-k-frequent-element solution (typescript)
mike2ox 226e7b3
Merge branch 'DaleStudy:main' into main
mike2ox 29e30d2
feat: Upload longest-consecutive-sequence solution (typescript)
mike2ox 5879dfd
feat: Upload house-router solution (typescript)
mike2ox a8796b3
refactor: Improve space complexity
mike2ox 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,16 @@ | ||
/** | ||
* Source: https://leetcode.com/problems/contains-duplicate/ | ||
* 풀이방법: Set을 이용하여 중복된 값이 있는지 확인 | ||
* 시간복잡도: O(n) | ||
* 공간복잡도: O(n) | ||
* | ||
* 생각나는 풀이방법 | ||
* 1. 단순하게 sorted를 이용하여 이전값과 비교하여 중복된 값이 있는지 확인 | ||
* 2. 정렬하지 않고 nums의 길이만큼의 배열을 만들어서 중복된 값이 있는지 저장하면서 확인 | ||
*/ | ||
function containsDuplicate(nums: number[]): boolean { | ||
// 중복된 값이 없는 자료구조 Set 활용 | ||
const set = new Set<number>(nums); | ||
// Set의 size와 nums의 length를 비교하여 중복된 값이 있는지 확인 | ||
return set.size !== nums.length; | ||
} |
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,24 @@ | ||
/** | ||
* Source: https://leetcode.com/problems/house-robber/ | ||
* 풀이방법: DP를 이용하여 집을 털 때 최대값을 구함 | ||
* 시간복잡도: O(n) | ||
* 공간복잡도: O(n) | ||
* | ||
* 생각나는 풀이방법 | ||
*/ | ||
function rob(nums: number[]): number { | ||
if (nums.length === 0) return 0; | ||
if (nums.length === 1) return nums[0]; | ||
if (nums.length === 2) return Math.max(nums[0], nums[1]); | ||
|
||
const dp: number[] = new Array(nums.length); | ||
|
||
dp[0] = nums[0]; | ||
dp[1] = Math.max(nums[0], nums[1]); | ||
|
||
// 남은 집을 순회하면서 최대값을 구함 | ||
for (let i = 2; i < nums.length; i++) | ||
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]); | ||
|
||
return dp[nums.length - 1]; | ||
} |
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,32 @@ | ||
/** | ||
* Source: https://leetcode.com/problems/longest-consecutive-sequence/ | ||
* 풀이방법: 정렬 후 순회를 통해 연속된 값이 있는지 확인 | ||
* 시간복잡도: O(nlogn) | ||
* 공간복잡도: O(1) | ||
* | ||
* 생각나는 풀이방법 | ||
*/ | ||
|
||
function longestConsecutive(nums: number[]): number { | ||
if (nums.length === 0) return 0; | ||
const sorted = nums.sort((a, b) => a - b); | ||
let prev = sorted[0]; | ||
let result = 1; | ||
let candiResult = 1; | ||
|
||
for (let current of sorted) { | ||
if (prev === current) continue; | ||
if (current === prev + 1) { | ||
candiResult += 1; | ||
} else { | ||
if (candiResult > result) { | ||
result = candiResult; | ||
} | ||
candiResult = 1; | ||
} | ||
prev = current; | ||
} | ||
|
||
if (candiResult > result) result = candiResult; | ||
return result; | ||
} |
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,22 @@ | ||
/** | ||
* Source: https://leetcode.com/problems/top-k-frequent-elements/ | ||
* 풀이방법: 순회를 통해 빈도수를 저장, Object.entries를 통해 정렬하여 k개까지 반환 | ||
* 시간복잡도: O(nlogn) | ||
* 공간복잡도: O(n) | ||
* | ||
* 생각나는 풀이방법 | ||
*/ | ||
function topKFrequent(nums: number[], k: number): number[] { | ||
const KFrequentObject = new Object(); | ||
const result = new Array(); | ||
for (let num of nums) { | ||
if (!Object.hasOwn(KFrequentObject, num)) KFrequentObject[num] = 0; | ||
KFrequentObject[num]++; | ||
} | ||
// Object.entries를 통해 key, value를 배열로 반환 (포인트) | ||
let sorted = Object.entries(KFrequentObject).sort((a, b) => b[1] - a[1]); | ||
for (let node of sorted) { | ||
result.push(parseInt(node[0])); | ||
} | ||
return result.slice(0, k); | ||
} |
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,29 @@ | ||
/** | ||
* Source: https://leetcode.com/problems/valid-palindrome/ | ||
* 풀이방법: 문자열을 조건에 맞게 정제한 후 reverse하여 비교 | ||
* 시간복잡도: O(n) | ||
* 공간복잡도: O(n) | ||
* | ||
* 생각나는 풀이방법 | ||
* 1. 정규식을 이용하여 문자열을 정제한 후 reverse하여 비교 => 정규식 작성을 못해서 배제 | ||
* 2. 문자열을 순회하면서 조건에 맞는 문자만 배열에 저장한 후 reverse하여 비교 | ||
*/ | ||
function isPalindrome(s: string): boolean { | ||
// 미리 길이가 2보다 작은 경우는 true로 반환(Palindrome 조건에 맞음) | ||
if (s.length < 2) return true; | ||
const formatted: string[] = []; | ||
|
||
// 문자열을 순회하면서 조건에 맞는 문자만 소문자로 변환해서 배열에 저장 | ||
for (let c of s) { | ||
if ( | ||
(c >= "a" && c <= "z") || | ||
(c >= "A" && c <= "Z") || | ||
(c >= "0" && c <= "9") | ||
) | ||
formatted.push(c.toLowerCase()); | ||
} | ||
// formatted 배열을 reverse하여 JSON.stringify로 비교 (중요) | ||
// ? toReverse()가 chrome console에서는 작동하는데 여기서는 왜 안되는지 모르겠음 | ||
const reversed = [...formatted].reverse(); | ||
return JSON.stringify(reversed) === JSON.stringify(formatted); | ||
} |
Oops, something went wrong.
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.
안녕하세요~ 점화식을 잘 보면 반복문 내에서
dp[i], dp[i - 1], dp[i - 2]
이렇게 세 요소만 사용하고 있다는 것을 알 수 있는데요아직 제출까지 시간이 좀 남았으니 공간복잡도 최적화를 도전해보시면 어떨까요? :)
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.
한번 해보겠습니다. 🙆♂️