![](https://img-blog.csdnimg.cn/20190918140158853.png?x-oss-process=image/resize,m_fixed,h_224,w_224)
二分与三分
文章平均质量分 86
介绍多种二分、三分搜索和二分法的拓展,相应习题的解答
memcpy0
希望探索文理结合的自由之路。
展开
-
LeetCode 3143. 正方形中的最多点数【切比雪夫距离,次小距离最小值】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2024-08-04 01:36:01 · 893 阅读 · 0 评论 -
LeetCode 1235. 规划兼职工作【排序+动规+二分】2022
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2024-05-06 01:34:49 · 446 阅读 · 0 评论 -
LeetCode 1146. 快照数组【哈希表+二分查找】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2024-04-27 13:17:40 · 693 阅读 · 0 评论 -
LeetCode 1095. 山脉数组中查找目标值【数组,二分】1827
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-10-15 21:58:49 · 159 阅读 · 0 评论 -
LeetCode 1488. 避免洪水泛滥【贪心,二分,有序集合】1973
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-10-13 15:23:02 · 122 阅读 · 0 评论 -
LeetCode 1150. Check If a Number Is Majority Element in a Sorted Array【摩尔投票法;二分】简单
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-10-07 20:06:20 · 55 阅读 · 0 评论 -
LeetCode 1337. The K Weakest Rows in a Matrix【数组,二分,堆,快速选择,排序】1224
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。为了方便在PC上运行调试、分享代码文件,我还建立了相关的。原创 2023-09-18 23:28:58 · 193 阅读 · 0 评论 -
LeetCode 729. My Calendar I【设计;有序集合,二分查找;线段树】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。为了方便在PC上运行调试、分享代码文件,我还建立了相关的。原创 2023-09-10 20:20:03 · 252 阅读 · 0 评论 -
LeetCode 2594. Minimum Time to Repair Cars【数组,二分】1915
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-09-08 08:56:41 · 313 阅读 · 0 评论 -
LeetCode 436. Find Right Interval【排序,二分;双指针,莫队】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-07-29 13:31:43 · 252 阅读 · 0 评论 -
LeetCode 1552. Magnetic Force Between Two Balls【二分,排序,贪心】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-01 23:44:08 · 837 阅读 · 0 评论 -
LeetCode 1552. Magnetic Force Between Two Balls【二分,排序,贪心】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-06-01 23:41:37 · 369 阅读 · 0 评论 -
LeetCode 2389. Longest Subsequence With Limited Sum【贪心,排序,前缀和,二分上界】简单
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。。原创 2023-03-18 10:34:39 · 397 阅读 · 0 评论 -
LeetCode 69. Sqrt(x)【数学/二分】简单
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conqu.原创 2021-11-04 09:42:34 · 170 阅读 · 0 评论 -
LeetCode 162. Find Peak Element【数组/二分】中等
本文属于「征服LeetCode」系列文章之一,这一系列正式开始于2021/08/12。由于LeetCode上部分题目有锁,本系列将至少持续到刷完所有无锁题之日为止;由于LeetCode还在不断地创建新题,本系列的终止日期可能是永远。在这一系列刷题文章中,我不仅会讲解多种解题思路及其优化,还会用多种编程语言实现题解,涉及到通用解法时更将归纳总结出相应的算法模板。为了方便在PC上运行调试、分享代码文件,我还建立了相关的仓库:https://github.com/memcpy0/LeetCode-Conqu.原创 2021-10-16 01:41:47 · 144 阅读 · 0 评论 -
LeetCode 剑指 Offer II 069. 山峰数组的顶部【二分/三分/数组】简单
符合下列属性的数组 arr 称为 山峰数组(山脉数组) : arr.length >= 3 存在 i(0 < i < arr.length - 1)使得: arr[0] < arr[1] < ... arr[i-1] < arr[i] arr[i] > arr[i+1] > ... > arr[arr.length - 1] 给定由整数组成的山峰数组 arr ,返回任何满足 arr[0] < arr[1] < .原创 2021-10-15 23:13:18 · 159 阅读 · 0 评论 -
LeetCode 275. H-Index II【数组,二分】中等
Given an array of integers citations where citations[i] is the number of citations a researcher received for their ith paper and citations is sorted in an ascending order, return compute the researcher's h-index.According to the definition of h-index原创 2021-07-12 23:48:32 · 143 阅读 · 1 评论 -
LeetCode 1894. Find the Student that Will Replace the Chalk【数组/前缀和/二分】中等
There are n students in a class numbered from 0 to n - 1. The teacher will give each student a problem starting with the student number 0, then the student number 1, and so on until the teacher reaches the student number n - 1. After that, the teacher will原创 2021-06-16 00:39:46 · 264 阅读 · 0 评论 -
LeetCode 374. Guess Number Higher or Lower【二分】简单
We are playing the Guess Game. The game is as follows:I pick a number from 1 to n. You have to guess which number I picked.Every time you guess wrong, I will tell you whether the number I picked is higher or lower than your guess.You call a pre-defined原创 2021-06-14 01:33:41 · 223 阅读 · 0 评论 -
洛谷 P3382 【模板】三分法
题目描述如题,给出一个 NNN 次函数,保证在范围 [l,r][l, r][l,r] 内存在一点 xxx ,使得 [l,x][l, x][l,x] 上单调增,[x,r][x, r][x,r] 上单调减。试求出 xxx 的值。输入格式第一行一次包含一个正整数 NNN 和两个实数 l,rl, rl,r,含义如题目描述所示。第二行包含 N+1N + 1N+1 个实数,从高到低依次表示该 NNN 次函数各项的系数。输出格式输出为一行,包含一个实数,即为 xxx 的值。若你的答案与标准答案的相对或绝对误差原创 2021-06-05 03:29:00 · 566 阅读 · 0 评论 -
LeetCode C++ 1855. Maximum Distance Between a Pair of Values【Binary Search/Two Pointers】中等
You are given two non-increasing 0-indexed integer arrays nums1 and nums2.A pair of indices (i, j), where 0 <= i < nums1.length and 0 <= j < nums2.length, is valid if both i <= j and nums1[i] <= nums2[j]. The distance of the原创 2021-05-14 01:17:21 · 189 阅读 · 1 评论 -
LeetCode C++ 1648. Sell Diminishing-Valued Colored Balls【Heap/Greedy/二分】中等
You have an inventory of different colored balls, and there is a customer that wants orders balls of any color.The customer weirdly values the colored balls. Each colored ball's value is the number of balls of that color you currently have in your in原创 2021-05-07 16:41:30 · 218 阅读 · 0 评论 -
LeetCode C++ 1011. Capacity To Ship Packages Within D Days【Binary Search/Array】中等
A conveyor belt has packages that must be shipped from one port to another within D days.The ith package on the conveyor belt has a weight of weights[i]. Each day, we load the ship with packages on the conveyor belt (in the order given by weights). We may原创 2021-04-26 16:32:34 · 196 阅读 · 0 评论 -
LeetCode C++ 面试题 10.09. Sorted Matrix Search LCCI【Array/Binary Search】中等
Given an M x N matrix in which each row and each column is sorted in ascending order, write a method to find an element.Example:Given matrix:[ [1, 4, 7, 11, 15], [2, 5, 8, 12, 19], [3, 6, 9, 16, 22], [10, 13, 14, 17, 24], [18, 21, 23原创 2021-04-14 01:01:42 · 145 阅读 · 0 评论 -
LeetCode C++ 300. Longest Increasing Subsequence【Dynamic Programming/Binary Search/String】中等
Given an integer array nums, return the length of the longest strictly increasing subsequence.A subsequence is a sequence that can be derived from an array by deleting some or no elements without changing the order of the remaining elements. For example,原创 2020-12-25 21:02:42 · 173 阅读 · 0 评论 -
LeetCode C++ 1064. Fixed Point【Array/Binary Search】简单
Given an array A of distinct integers sorted in ascending order, return the smallest index i that satisfies A[i] == i. Return -1 if no such i exists.Example 1:Input: [-10,-5,0,3,7]Output: 3Explanation: For the given array, A[0] = -10, A[1] = -5, A[2]原创 2020-12-17 02:28:03 · 321 阅读 · 0 评论 -
LeetCode C++ 154. Find Minimum in Rotated Sorted Array II【Array/Binary Search】困难
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2]).Find the minimum element. The array may contain duplicates.Example 1:Input: [1,3,5]Output: 1Example 2原创 2020-12-16 01:05:44 · 174 阅读 · 0 评论 -
LeetCode C++ 35. Search Insert Position【Binary Search】简单
Given a sorted array of distinct integers and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order.Example 1:Input: nums = [1,3,5,6], target = 5Output: 2Example 2:Input: nums原创 2020-12-01 13:11:12 · 223 阅读 · 0 评论 -
LeetCode C++ 278. First Bad Version【Binary Search】简单
You are a product manager and currently leading a team to develop a new product. Unfortunately, the latest version of your product fails the quality check. Since each version is developed based on the previous version, all the versions after a bad version原创 2020-12-01 13:01:15 · 660 阅读 · 1 评论 -
LeetCode 441. Arranging Coins【数学/二分】简单
You have a total of n coins that you want to form in a staircase shape, where every k-th row must have exactly k coins.Given n, find the total number of full staircase rows that can be formed.n is a non-negative integer and fits within the range of a 32-原创 2020-11-25 00:47:23 · 387 阅读 · 0 评论 -
LeetCode C++ 面试题 10.01. Sorted Merge LCCI【Array/Two Pointers】简单
You are given two sorted arrays, A and B, where A has a large enough buffer at the end to hold B. Write a method to merge B into A in sorted order.Initially the number of elements in A and B are m and n respectively.Example:Input:A = [1,2,3,0,0,0], m =原创 2020-11-15 16:11:00 · 155 阅读 · 0 评论 -
LeetCode C++ 面试题 10.05. Sparse Array Search LCCI【String/Binary Search】简单
Given a sorted array of strings that is interspersed with empty strings, write a method to find the location of a given string.Example1: Input: words = ["at", "", "", "", "ball", "", "", "car", "", "","dad", "", ""], s = "ta" Output: -1 Explanation: Re原创 2020-11-15 15:52:04 · 136 阅读 · 0 评论 -
LeetCode C++ 1385. Find the Distance Value Between Two Arrays【Array/Binary Search】简单
Given two integer arrays arr1 and arr2, and the integer d, return the distance value between the two arrays.The distance value is defined as the number of elements arr1[i] such that there is not any element arr2[j] where |arr1[i]-arr2[j]| <= d.Example原创 2020-11-14 21:11:50 · 209 阅读 · 1 评论 -
LeetCode 852. Peak Index in a Mountain Array【二分/三分/数组】简单
Let’s call an array arr a mountain if the following properties hold:arr.length >= 3There exists some i with 0 < i < arr.length - 1 such that:arr[0] < arr[1] < ... arr[i-1] < arr[i]arr[i] > arr[i+1] > ... > arr[arr.length -原创 2020-11-14 20:32:19 · 207 阅读 · 0 评论 -
LeetCode 367. Valid Perfect Square【数学/二分】简单
Given a positive integer num, write a function which returns True if num is a perfect square else False.Follow up: Do not use any built-in library function such as sqrt.Example 1:Input: num = 16Output: trueExample 2:Input: num = 14Output: falseCo原创 2020-10-29 01:14:24 · 169 阅读 · 0 评论 -
LeetCode C++ 1365. How Many Numbers Are Smaller Than the Current Number【排序/前缀和】简单
Given the array nums, for each nums[i] find out how many numbers in the array are smaller than it. That is, for each nums[i] you have to count the number of valid j's such that j != i and nums[j] < nums[i] . Return the answer in an array.Example 1:Inp原创 2020-10-26 19:59:28 · 169 阅读 · 0 评论 -
LeetCode C++ 153. Find Minimum in Rotated Sorted Array【Array/二分】中等
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2] ).Find the minimum element. You may assume no duplicate exists in the array.Example 1:Input: [3,4,5,1,2]原创 2020-10-22 19:15:41 · 175 阅读 · 0 评论 -
LeetCode C++ 81. Search in Rotated Sorted Array II【数组/二分】中等
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e., [0,0,1,2,2,5,6] might become [2,5,6,0,0,1,2] ). You are given a target value to search. If found in the array return true , otherwise return false .Examp原创 2020-10-21 23:13:56 · 199 阅读 · 0 评论 -
LeetCode C++ 33. Search in Rotated Sorted Array【二分】中等
You are given an integer array nums sorted in ascending order, and an integer target .Suppose that nums is rotated at some pivot unknown to you beforehand (i.e., [0,1,2,4,5,6,7] might become [4,5,6,7,0,1,2] ).If target is found in the array return its in原创 2020-10-21 12:25:53 · 230 阅读 · 0 评论 -
LeetCode C++ 222. Count Complete Tree Nodes【Tree/Recursion/二分】中等
Given a complete binary tree, count the number of nodes.Note: In a complete binary tree every level, except possibly the last, is completely filled, and all nodes in the last level are as far left as possible. It can have between 1 and 2h nodes inclusive原创 2020-10-20 02:07:41 · 164 阅读 · 0 评论