-
-
Notifications
You must be signed in to change notification settings - Fork 195
[choidabom] WEEK 03 solutions #1311
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
7 commits
Select commit
Hold shift + click to select a range
24570a3
fea: valid-anagram
choidabom bf02135
feat: climbing-stairs
choidabom 914f09c
feat: valid-palindrome
choidabom 705cb38
Merge branch 'DaleStudy:main' into main
choidabom 95fd201
fix: lint
choidabom 629deff
feat: number-of-1-bits
choidabom beba7eb
feat: maximum-subarray
choidabom 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 @@ | ||
// https://leetcode.com/problems/climbing-stairs/ | ||
|
||
// TC: O(N) | ||
// SC: O(N) | ||
|
||
var climbStairs = function (n) { | ||
const stairs = [1, 2]; | ||
|
||
for (let i = 2; i < n; i++) { | ||
stairs[i] = stairs[i - 1] + stairs[i - 2]; | ||
} | ||
|
||
return stairs[n - 1]; | ||
}; | ||
|
||
console.log(climbStairs(5)); |
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 @@ | ||
// https://leetcode.com/problems/maximum-subarray/ | ||
|
||
// Time Limit Exceeded | ||
function maxSubArray(nums: number[]): number { | ||
const acc = [] | ||
const len = nums.length | ||
|
||
for (let size = 1; size <= len; size++) { | ||
for (let start = 0; start <= len - size; start++) { | ||
const sub = nums.slice(start, start + size) | ||
const sum = sub.reduce((acc, num)=> acc += num, 0) | ||
acc.push(sum) | ||
} | ||
} | ||
|
||
return acc.sort((a, b) => b - a)[0] | ||
}; | ||
|
||
// TC: O(n) | ||
// SC: O(n) | ||
|
||
function maxSubArray(nums: number[]): number { | ||
const dp = [...nums]; | ||
let max = dp[0]; | ||
|
||
for (let i = 1; i < nums.length; i++) { | ||
dp[i] = Math.max(nums[i], dp[i - 1] + nums[i]); | ||
max = Math.max(max, dp[i]); | ||
} | ||
|
||
return max; | ||
} |
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,15 @@ | ||
// https://leetcode.com/problems/number-of-1-bits/ | ||
|
||
// TC: O(logN) - n을 2로 나누며 1을 셈 | ||
// SC: O(1) - 배열을 사용하지 않고, 변수만 사용하여 추가 메모리 x | ||
|
||
function hammingWeight(n: number): number { | ||
let answer = 0 | ||
|
||
while (n > 0) { | ||
answer += n % 2 | ||
n = Math.floor(n / 2) | ||
} | ||
|
||
return answer | ||
} |
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,33 @@ | ||
// https://leetcode.com/problems/valid-anagram/submissions/1603502655/ | ||
|
||
// TC: O(NlogN) | ||
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. 이런 식으로 시간 복잡도 적으면 서 하는 것 좋네요! 참고하겠습니다. |
||
// SC: O(N) | ||
|
||
var isAnagram = function (s, t) { | ||
return s.split("").sort().join("") === t.split("").sort().join("") | ||
}; | ||
|
||
// TC: O(N) | ||
// SC: O(N) | ||
|
||
var isAnagram = function (s, t) { | ||
const map = new Map() | ||
|
||
for (const char of s) { | ||
if (map.has(char)) map.set(char, map.get(char) + 1) | ||
else map.set(char, 1) | ||
} | ||
|
||
for (const char of t) { | ||
if (!map.has(char)) return false | ||
else map.set(char, map.get(char) - 1) | ||
} | ||
|
||
for (const value of map.values()) { | ||
if (value !== 0) return false | ||
} | ||
|
||
return true | ||
}; | ||
|
||
console.log(isAnagram(s = "anagram", t = "nagaram")) |
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,11 @@ | ||
// https://leetcode.com/problems/valid-palindrome/ | ||
|
||
// TC: O(n) | ||
// SC: O(n) | ||
|
||
function isPalindrome(s: string): boolean { | ||
const str = (s.toLowerCase().match(/[a-z0-9]/g) || []).join(""); | ||
const reverse = str.split("").reverse().join(""); | ||
|
||
return str === reverse; | ||
} |
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로 푸니 코드가 좀 더 깔끔하네요! 참고하겠습니다.