-
Notifications
You must be signed in to change notification settings - Fork 2
LeetCode 讀書會第 25 次聚會 #18
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
Changes from 3 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2b851c5
Create 25 20210420.md
ytyubox cb8e399
修正日期錯誤
ytyubox 984b7e5
Add Solution for Pow(x, n) in TS
louis222220 19036df
Add iterative solution for Pow(x, n) in TS
louis222220 1af0510
Create 25 20210420.md
ytyubox ff9f656
prepare to move to meetup 26
ytyubox 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,110 @@ | ||
# LeetCode 讀書會第 25 次聚會 2021/04/20 | ||
|
||
leetcode 讀書會通知 | ||
1. 項目: 第 25 次聚會 | ||
2. 目的: 線上一起寫題目, 由有想法的人帶領, 先解題, 再看該題有趣的解法 | ||
3. 時間: 4/20 (二) 20:00 ~ 21:00 | ||
4. 地點: google meet 線上 (前 10 分鐘預備鏈接) | ||
5. 解題項目: [Binary search](https://leetcode.com/explore/learn/card/binary-search) | ||
6. 共筆: GitHub https://github.com/programmingbookclub/Leetcode-club | ||
7. 備註: 上次有稍微聊一下 658 題目,這次會重頭開始聊658,會強調如何理解題目、題目預期的答案,與如何實作。 | ||
|
||
|
||
* MEDIUM 658 Find K Closest Elements https://leetcode.com/problems/find-k-closest-elements | ||
|
||
```typescript | ||
// `Search for a Range` in TypeScript by iteration and resursion mixed | ||
// Time Complexity: O(log n), Space Complexity: O(1) | ||
// by louis222220 | ||
function searchRange(nums: number[], target: number): number[] { | ||
let left = 0; | ||
let right = nums.length - 1; | ||
let middle: number; | ||
while (left <= right) { | ||
middle = getMiddleIndex(left, right); | ||
if (nums[middle] === target) { | ||
const leftTargerBoundary = searchLeftTargetBoundary(left, middle); | ||
const rightTargetBoundary = searchRightTargetBoundary(middle, right); | ||
|
||
const result = [leftTargerBoundary, rightTargetBoundary]; | ||
return result; | ||
} | ||
else if (nums[middle] > target) right = middle - 1; | ||
else left = middle + 1; | ||
} | ||
return [-1, -1]; | ||
|
||
// helper functions | ||
function getMiddleIndex(left: number, right: number) { | ||
if (left > right) return -1; | ||
if (left === right) return left; | ||
return left + Math.floor((right - left) / 2); | ||
} | ||
/** Search left boundary of target by Recursion */ | ||
function searchLeftTargetBoundary(left: number, right: number) { | ||
if (nums[left] === target) return left; | ||
const middle = getMiddleIndex(left, right); | ||
|
||
if (nums[middle] === target && nums[middle - 1] !== target) return middle; | ||
else if (nums[middle] !== target) return searchLeftTargetBoundary(middle + 1, right); | ||
else return searchLeftTargetBoundary(left, middle - 1); | ||
} | ||
/** Search right boundary of target by Recursion */ | ||
function searchRightTargetBoundary(left: number, right: number) { | ||
if (nums[right] === target) return right; | ||
const middle = getMiddleIndex(left, right); | ||
|
||
if (nums[middle] === target && nums[middle + 1] !== target) return middle; | ||
else if (nums[middle] !== target) return searchRightTargetBoundary(left, middle - 1); | ||
else return searchRightTargetBoundary(middle + 1, right); | ||
} | ||
}; | ||
``` | ||
|
||
* MEDIUM 162 Find Peak Element https://leetcode.com/problems/find-peak-element | ||
|
||
* Template Analysis | ||
|
||
* Article Binary Search Template Analysis | ||
|
||
* 🔓 EASY 270 Closest Binary Search Tree Value https://leetcode.com/problems/closest-binary-search-tree-value | ||
|
||
* 🔓 MEDIUM 702 Search in a Sorted Array of Unknown Size https://leetcode.com/problems/search-in-a-sorted-array-of-unknown-size | ||
|
||
* Conclusion | ||
|
||
* MEDIUM 50 Pow(x, n) https://leetcode.com/problems/powx-n | ||
|
||
louis222220 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
```typescript | ||
/** Recursion in TS by Louis. It's inspired by [the discussion](https://leetcode.com/explore/learn/card/binary-search/137/conclusion/982/discuss/19546/Short-and-easy-to-understand-solution) */ | ||
function myPow(x: number, n: number): number { | ||
if (n == 0) return 1; | ||
if (n < 0) return myPow(1 / x, -n); | ||
if (n % 2 == 0) return myPow(x * x, n / 2); | ||
else return x * myPow(x * x, Math.floor(n / 2)); | ||
}; | ||
louis222220 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* EASY 367 Valid Perfect Square https://leetcode.com/problems/valid-perfect-square | ||
|
||
ytyubox marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* EASY 744 Find Smallest Letter Greater Than Target https://leetcode.com/problems/find-smallest-letter-greater-than-target | ||
|
||
* More Practices | ||
|
||
* MEDIUM 153 Find Minimum in Rotated Sorted Array https://leetcode.com/problems/find-minimum-in-rotated-sorted-array | ||
|
||
* HARD 154 Find Minimum in Rotated Sorted Array II https://leetcode.com/problems/find-minimum-in-rotated-sorted-array-ii | ||
|
||
* EASY 349 Intersection of Two Arrays https://leetcode.com/problems/intersection-of-two-arrays | ||
|
||
* EASY 350 Intersection of Two Arrays II https://leetcode.com/problems/intersection-of-two-arrays-ii | ||
|
||
* EASY 167 Two Sum II - Input array is sorted https://leetcode.com/problems/two-sum-ii-input-array-is-sorted | ||
|
||
* More Practices II | ||
|
||
* MEDIUM 287 Find the Duplicate Number https://leetcode.com/problems/find-the-duplicate-number | ||
|
||
* HARD 4 Median of Two Sorted Arrays https://leetcode.com/problems/median-of-two-sorted-arrays | ||
|
||
* HARD 719 Find K-th Smallest Pair Distance https://leetcode.com/problems/find-k-th-smallest-pair-distance | ||
|
||
* HARD 410 Split Array Largest Sum https://leetcode.com/problems/split-array-largest-sum |
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.
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.
@SamTienz
幫補充 這個題目順序上有一點問題,其實這題是 MEDIUM 34
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.
@SamTienz 你要不要解釋一下你怎麼想的?可以幫助你思考喔
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.
@ytyubox
OK 大概是這樣
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.
@SamTienz
看起來是
func findLeft(_ , _)
與findRight(_ , _)
裡面有點問題