-
-
Notifications
You must be signed in to change notification settings - Fork 195
[HoonDongKang] WEEK 03 solutions #1326
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
9aaee2e
add Valid Palindrome solution
HoonDongKang f29b91f
add Number of 1 Bits solution
HoonDongKang c341117
add Combination Sum solution
HoonDongKang 13a8ffe
add Decode Ways solution
HoonDongKang f92dbb5
add Maximum Subarray 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,54 @@ | ||
/** | ||
* [Problem]: [39] Combination Sum | ||
* | ||
* (https://leetcode.com/problems/combination-sum/description/) | ||
*/ | ||
function combinationSum(candidates: number[], target: number): number[][] { | ||
// 시간복잡도: O(c^t) | ||
// 공간복잡도 O(t) | ||
function dfsFunc(candidates: number[], target: number): number[][] { | ||
let result: number[][] = []; | ||
let nums: number[] = []; | ||
|
||
function dfs(start: number, total: number): void | number[][] { | ||
if (total > target) return; | ||
if (total === target) { | ||
result.push([...nums]); | ||
return result; | ||
} | ||
for (let i = start; i < candidates.length; i++) { | ||
let num = candidates[i]; | ||
nums.push(num); | ||
dfs(i, total + num); | ||
nums.pop(); | ||
} | ||
} | ||
|
||
dfs(0, 0); | ||
return result; | ||
} | ||
|
||
// 시간복잡도: O(c*t) | ||
// 공간복잡도 O(c*t) | ||
function dpFunc(candidates: number[], target: number): number[][] { | ||
const dp: number[][][] = Array(target + 1) | ||
.fill(null) | ||
.map(() => []); | ||
|
||
dp[0] = [[]]; | ||
|
||
for (const num of candidates) { | ||
for (let t = num; t <= target; t++) { | ||
for (const comb of dp[t - num]) { | ||
dp[t].push([...comb, num]); | ||
} | ||
} | ||
} | ||
|
||
return dp[target]; | ||
} | ||
|
||
return dpFunc(candidates, target); | ||
} | ||
|
||
console.log(combinationSum([2, 3, 6, 7], 7)); | ||
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,51 @@ | ||
/** | ||
* [Problem]: [91] Decode Ways | ||
* | ||
* (https://leetcode.com/problems/decode-ways/description/) | ||
*/ | ||
function numDecodings(s: string): number { | ||
//시간복잡도 O(n) | ||
//공간복잡도 O(n) | ||
function memoizationFunc(s: string): number { | ||
const memoization: Record<number, number> = {}; | ||
|
||
function dfs(index: number): number { | ||
if (index in memoization) return memoization[index]; | ||
if (index === s.length) return 1; | ||
if (s[index] === "0") return 0; | ||
|
||
let result = dfs(index + 1); | ||
if (index + 1 < s.length && +s.slice(index, index + 2) <= 26) { | ||
result += dfs(index + 2); | ||
} | ||
|
||
memoization[index] = result; | ||
return result; | ||
} | ||
|
||
return dfs(0); | ||
} | ||
|
||
//시간복잡도 O(n) | ||
//공간복잡도 O(1) | ||
function optimizedFunc(s: string): number { | ||
let prev2 = 1; | ||
let prev1 = s[0] === "0" ? 0 : 1; | ||
|
||
for (let i = 1; i < s.length; i++) { | ||
let curr = 0; | ||
|
||
const one = +s.slice(i, i + 1); | ||
const two = +s.slice(i - 1, i + 1); | ||
|
||
if (one >= 1 && one <= 9) curr += prev1; | ||
if (two >= 10 && two <= 26) curr += prev2; | ||
|
||
prev2 = prev1; | ||
prev1 = curr; | ||
} | ||
|
||
return prev1; | ||
} | ||
return optimizedFunc(s); | ||
Comment on lines
+31
to
+50
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. 이 방식으로 공간 복잡도도 줄일 수 있네요 |
||
} |
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]: [53] Maximum Subarray | ||
* | ||
* (https://leetcode.com/problems/maximum-subarray/description/) | ||
*/ | ||
|
||
function maxSubArray(nums: number[]): number { | ||
//시간복잡도 O(n) | ||
//공간복잡도 O(1) | ||
function getMax(nums: number[]): number { | ||
let result = nums[0]; | ||
let sum = 0; | ||
|
||
nums.forEach((num) => { | ||
sum = Math.max(num, sum + num); | ||
result = Math.max(sum, result); | ||
}); | ||
|
||
return result; | ||
} | ||
|
||
//시간복잡도 O(n) | ||
//공간복잡도 O(1) | ||
function dpFunc(nums: number[]): number { | ||
const dp = Array(nums.length).fill(0); | ||
dp[0] = nums[0]; | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]); | ||
} | ||
|
||
return Math.max(...dp); | ||
} | ||
|
||
return dpFunc(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,31 @@ | ||
/** | ||
* [Problem]: [191] Number of 1 Bits | ||
* | ||
* (https://leetcode.com/problems/number-of-1-bits/description/) | ||
*/ | ||
function hammingWeight(n: number): number { | ||
// 시간 복잡도 O(log n) | ||
// 공간 복잡도 O(1) | ||
function divisionFunc(n: number): number { | ||
let count = 0; | ||
while (n > 0) { | ||
count += n % 2; | ||
n = Math.floor(n / 2); | ||
} | ||
|
||
return count; | ||
} | ||
|
||
// 시간 복잡도 O(log n) | ||
// 공간 복잡도 O(1) | ||
function bitwiseFunc(n: number): number { | ||
let count = 0; | ||
while (n !== 0) { | ||
count += n & 1; | ||
n = n >>> 1; | ||
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. 논리 연산자를 사용하는 방법이 char를 하나씩 비교하는 것보다 더 직관적인 방법 같아요! |
||
} | ||
|
||
return count; | ||
} | ||
return divisionFunc(n); | ||
} |
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,49 @@ | ||
/** | ||
* [Problem]: [125] Valid Palindrome | ||
* | ||
* (https://leetcode.com/problems/valid-palindrome/description/) | ||
*/ | ||
|
||
function isPalindrome(s: string): boolean { | ||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(n) | ||
function twoPointerFunc(s: string): boolean { | ||
let stringArr: string[] = [...s.replace(/[^a-zA-Z0-9]/g, "")]; | ||
let left = 0; | ||
let right = stringArr.length - 1; | ||
|
||
while (left < right) { | ||
if (stringArr[left].toLowerCase() === stringArr[right].toLowerCase()) { | ||
left++; | ||
right--; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
// 시간복잡도: O(n) | ||
// 공간복잡도: O(1) | ||
function optimizedTwoPointerFunc(s: string): boolean { | ||
let left = 0; | ||
let right = s.length - 1; | ||
|
||
while (left < right) { | ||
while (left < right && !isLetterOrDigit(s[left])) left++; | ||
while (left < right && !isLetterOrDigit(s[right])) right--; | ||
|
||
if (s[left].toLowerCase() !== s[right].toLowerCase()) return false; | ||
left++; | ||
right--; | ||
} | ||
|
||
return true; | ||
|
||
function isLetterOrDigit(c: string): boolean { | ||
return /[a-zA-Z0-9]/.test(c); | ||
} | ||
} | ||
return optimizedTwoPointerFunc(s); | ||
} |
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 방식으로도 할 수 있네요!!
백트레킹 보다 코드 이해는 쉽네요!