下面我們來看一道稍微變化的題目,leetcode: 878. 第 N 個神奇數字[26],雖然是 hard 級別,但是難度仍然不高:
def gcd(a, b): return gcd(b, a % b) if b else a def check(x): cnt = x // A + x // B - x // LCM return cnt >= N l = 1 r = N * min(A, B) LCM = A // gcd(A,B) * B 求最小公倍數 ,我們就直接通過歐幾裡得算法 求出最大公約數 來得到了。
課後習題(共 5 題) :
leetcode: 786. 第 K 個最小的素數分數[27]其他涉及題目在算法題目中結合多種技巧解決一道題目是很常見的事,下面就給大家列舉一些二分法 結合其他算法的題目,這些題目就不講解了,有問題可以在答疑群裡面提出來。
數學+二分 :
628. 美麗的數(google kickstart 2016 round E problem B)[31]二分+dp: acwing: 472. 跳房子[32]
二分+差分: acwing: 503. 借教室[33]
貪心+二分: LIS 問題(動態規劃專題講解)
總結 二分法專題到這裡就基本結束了,本專題總共涉及到 60 道 題目,以 LeetCode 難度劃分基本屬於 hard 和 medium 偏上的題目,類似的題目還有很多,但是基本都大同小異。掌握了本篇的這幾種主要題型,相信大家以後再面對二分類題目時就可以信手拈來了。
最後我們再回顧下我們的內容:通常我們將二分法分為整數域上的二分和實數域上的二分。
整數二分的細節在於終止邊界、左右區間取捨時的開閉情況 ,來避免漏掉答案或者造成死循環 。模板一 和 模板二 通用 5 種二分類型題目為:
最大/最小平均值(max/min average)參考資料[1]leetcode: 1011. 在 D 天內送達包裹的能力: https://leetcode-cn.com/problems/capacity-to-ship-packages-within-d-days/
[2]leetcode: 778. 水位上升的泳池中遊泳: https://leetcode-cn.com/problems/swim-in-rising-water/
[3]leetcode: 1482. 製作 m 束花所需的最少天數: https://leetcode-cn.com/problems/minimum-number-of-days-to-make-m-bouquets/
[4]leetcode: 1292. 元素和小於等於閾值的正方形的最大邊長: https://leetcode-cn.com/problems/maximum-side-length-of-a-square-with-sum-less-than-or-equal-to-threshold/
[5]leetcode: 875. 愛吃香蕉的珂珂: https://leetcode-cn.com/problems/koko-eating-bananas/
[6]leetcode: 287. 尋找重複數: https://leetcode-cn.com/problems/find-the-duplicate-number/
[7]acwing(頭條 2019 筆試):680. 剪繩子: https://www.acwing.com/problem/content/682/
[8]acwing(第八屆藍橋杯): 1227. 分巧克力: https://www.acwing.com/problem/content/description/1229/
[9]acwing(頭條 2019 筆試): 730. 機器人跳躍問題: https://www.acwing.com/problem/content/description/732/
[10]acwing: 1028. 複製書稿: https://www.acwing.com/problem/content/description/1030/
[11]codeforces edu 8 道題目: https://codeforces.com/edu/course/2/lesson/6/2/practice
[12]leetcode: 793. 階乘函數後K個零: https://leetcode-cn.com/problems/preimage-size-of-factorial-zeroes-function/
[13]leetcode: 719. 找出第 k 小的距離對: https://leetcode-cn.com/problems/find-k-th-smallest-pair-distance/
[14]leetcode: 410. 分割數組的最大值: https://leetcode-cn.com/problems/split-array-largest-sum/
[15]acwing: 519. 跳石頭: https://www.acwing.com/problem/content/521/
[16]leetcode plus: 774. minimize-max-distance-to-gas-station: https://leetcode-cn.com/problems/minimize-max-distance-to-gas-station/
[17]acwing: 2436. 串分割: https://www.acwing.com/problem/content/description/2438/
[18]codeforces 4 道經典題目: https://codeforces.com/edu/course/2/lesson/6/3/practice
[19]a_0, a_1, ... , a_{n-1}],我們期望找到一個連續區間[l, r],該區間的大小至少是m$,需要求所有可能區間的最大平均值。這也是 [luogu: 1404. 求平均值(poj2018): https://www.luogu.com.cn/problem/P1404
[20]acwing: 02. 最佳牛圍欄: https://www.acwing.com/problem/content/description/104/
[21]acwing: 2430. 送禮物: https://www.acwing.com/problem/content/description/2432/
[22]某公司筆試題: https://www.acwing.com/community/content/537/
[23]acwing: 361. 觀光奶牛(圖+二分): https://www.acwing.com/problem/content/description/363/
[24]codeforces 3 道經典題目: https://codeforces.com/edu/course/2/lesson/6/4/practice
[25]leetcode: 668. 乘法表中第k小的數: https://leetcode-cn.com/problems/kth-smallest-number-in-multiplication-table/
[26]leetcode: 878. 第 N 個神奇數字: https://leetcode-cn.com/problems/nth-magical-number/
[27]leetcode: 786. 第 K 個最小的素數分數: https://leetcode-cn.com/problems/k-th-smallest-prime-fraction/
[28]acwing: 1236. 遞增三元組: https://www.acwing.com/problem/content/description/1238/
[29]codeforces 3 道經典題目: https://codeforces.com/edu/course/2/lesson/6/5/practice
[30]leetcode: 483. 最小好進位: https://leetcode-cn.com/problems/smallest-good-base/
[31]628. 美麗的數(google kickstart 2016 round E problem B): https://www.acwing.com/problem/content/description/630/
[32]acwing: 472. 跳房子: https://www.acwing.com/problem/content/description/474/
[33]acwing: 503. 借教室: https://www.acwing.com/problem/content/description/505/