Skip to content

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
merged 6 commits into from
Apr 20, 2021
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions 紀錄/25 20210420.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
# 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);
}
};
```

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
```swift
//swift by SamTienz
//上次讀書會聽了神Q的想法先寫了一些,後來再參考louis222220的,自己再寫一遍,不過有些test case過不了 😢
func searchRange(_ nums: [Int], _ target: Int) -> [Int] {
var left = 0, right = nums.count - 1
var ans = [-1, -1]
if nums.count == 0 {return ans}
func findLeft(_ left:Int, _ right:Int) -> Int {
if left >= right { return left }
let mid = (right - left) / 2 + left
if nums[mid] == target {
if nums[mid - 1] != target { return mid }
else { return findLeft(left, mid - 1) }
}else{
return findLeft(mid + 1, right)
}
}
func findRight(_ left:Int, _ right:Int) -> Int {
if right <= left { return right }
let mid = (right - left) / 2 + left
if nums[mid] == target {
if nums[mid + 1] != target { return mid }
else { return findLeft(mid + 1, right) }
}else{
return findLeft(left, mid - 1)
}
}
while true {
if left >= right { return ans } //Wrong Answer [1] 1
let mid = (right - left) / 2 + left
if nums[mid] == target{ //找到了 切兩邊
ans[0] = findLeft(left, mid) //找左
ans[1] = findRight(mid, right) //找右
return ans
}else if nums[mid] > target{
right = mid - 1
}else{
left = mid + 1
}
// if left >= right { return ans } Wrong Answer [1,4] 4
}
return ans
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SamTienz
幫補充 這個題目順序上有一點問題,其實這題是 MEDIUM 34

* Template III

* Article Binary Search Template III

* MEDIUM    34    Find First and Last Position of Element in Sorted Array    https://leetcode.com/problems/find-first-and-last-position-of-element-in-sorted-array/ 

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SamTienz 你要不要解釋一下你怎麼想的?可以幫助你思考喔

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ytyubox
OK 大概是這樣

  1. 92行開始 : 先用binary search看能不能在nums找到target,找不到=>return ans (這時ans為初始值[-1,-1])
  2. 如果有找到target,將找到的target的index為基準,把nums拆成兩邊 (兩邊都含target index)
  3. 兩邊分別執行binary search
  4. ans[0] = 左邊找到nums(index)為 && nums(target - 1)不為target,此時index = First Position of Target
  5. ans[1] = 右邊找到nums(index)為 && nums(target + 1)不為target,此時index = Last Position of Target
  6. return ans

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@SamTienz
看起來是 func findLeft(_ , _)findRight(_ , _) 裡面有點問題

* 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

* EASY 367 Valid Perfect Square https://leetcode.com/problems/valid-perfect-square

* 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