-
-
Notifications
You must be signed in to change notification settings - Fork 195
[Wan] Week 2 #741
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
[Wan] Week 2 #741
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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,45 @@ | ||
/** | ||
* 15. 3Sum | ||
* Given an integer array nums, return all the triplets [nums[i], nums[j], nums[k]] such that i != j, i != k, and j != k, and nums[i] + nums[j] + nums[k] == 0. | ||
* Notice that the solution set must not contain duplicate triplets. | ||
* | ||
* https://leetcode.com/problems/3sum/description/ | ||
*/ | ||
function threeSum(nums: number[]): number[][] { | ||
nums.sort((a, b) => a - b); | ||
const triplets: number[][] = []; | ||
|
||
for (let i = 0; i < nums.length - 2; i++) { | ||
if (nums[i] > 0 || nums[i] === nums[i - 1]) { | ||
continue; | ||
} | ||
|
||
let low = i + 1; | ||
let high = nums.length - 1; | ||
|
||
while (low < high) { | ||
const sum = nums[i] + nums[low] + nums[high]; | ||
if (sum < 0) { | ||
low++; | ||
} else if (sum > 0) { | ||
high--; | ||
} else { | ||
triplets.push([nums[i], nums[low], nums[high]]); | ||
|
||
while (low < high && nums[low] === nums[low + 1]) { | ||
low++; | ||
} | ||
while (low < high && nums[high] === nums[high - 1]) { | ||
high--; | ||
} | ||
low++; | ||
high--; | ||
} | ||
} | ||
} | ||
|
||
return triplets; | ||
} | ||
|
||
// O(n^2) time | ||
// O(n) space |
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,26 @@ | ||
/** | ||
* 70. Climbing Stairs | ||
* You are climbing a staircase. It takes n steps to reach the top. | ||
* Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? | ||
* | ||
* https://leetcode.com/problems/climbing-stairs/description/ | ||
*/ | ||
function climbStairs(n: number): number { | ||
if (n <= 2) { | ||
return n; | ||
} | ||
|
||
let prev = 1; | ||
let cur = 2; | ||
|
||
for (let i = 3; i < n + 1; i++) { | ||
const next = prev + cur; | ||
prev = cur; | ||
cur = next; | ||
} | ||
|
||
return cur; | ||
} | ||
|
||
// O(n) time | ||
// O(1) space |
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 @@ | ||
/** | ||
* 242. Valid Anagram | ||
* Given two strings s and t, return true if t is an anagram of s, and false otherwise. | ||
* | ||
* https://leetcode.com/problems/valid-anagram/description/ | ||
*/ | ||
function isAnagram(s: string, t: string): boolean { | ||
if (s.length !== t.length) { | ||
return false; | ||
} | ||
|
||
const charMap = new Map<string, number>(); | ||
|
||
for (const char of s) { | ||
const count = charMap.get(char); | ||
if (count) { | ||
charMap.set(char, count + 1); | ||
} else { | ||
charMap.set(char, 1); | ||
} | ||
} | ||
|
||
for (const char of t) { | ||
const count = charMap.get(char); | ||
if (count) { | ||
charMap.set(char, count - 1); | ||
} else { | ||
return false; | ||
} | ||
} | ||
Comment on lines
+12
to
+30
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. map 풀이 방법이군요 :) 저는 문자열을 정렬해서 비교하는 방법을 떠올렸는데 javascript가 코드가 훨씬 깔끔하게 나오네요. return s.split('').sort().join('') === t.split('').sort().join(''); 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. 그렇게도 풀이할 수 있겠군요! |
||
|
||
return true; | ||
} | ||
|
||
// O(n) time | ||
// O(n) space |
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.
n을 기준으로 prev는 1, cur는 2로 하신 것 같습니다. (다른 풀이나 chatgpt도 위와 같았던 것으로 기억합니다.)
결과는 사실 상 같으나 약간 생각을 해봤을 때 n을 1부터 보는게 아니라 0부터 봐서
prev 1, cur 1로 설정하여 for문을 2 ~ n 까지 구할 수 있는 방법으로도 생각할 수 있을 것 같습니다 :)