#include<bits/stdc++.h> usingnamespace std; vector<int> arr = {5,7,7,8,8,10}; intmain(){ int len = arr.size(); int l = 0, r = len-1; while(l<=r){ int mid = l+(r-l)/2; if (arr[mid]>=8) r = mid-1; else l = mid+1; } cout<<l; return0; }
#include<bits/stdc++.h> usingnamespace std; vector<int> arr = {5,7,7,8,8,10}; intmain(){ int len = arr.size(); int l = 0, r = len; while(l<r){ int mid = l+(r-l)/2; if(arr[mid] >= 8) r = mid; else l = mid+1; } cout<< l; return0; }
#include<bits/stdc++.h> usingnamespace std; vector<int> arr = {5,7,7,8,8,10}; intmain(){ int len = arr.size(); int l = -1, r = len; while(l+1<r){ int mid = l + (r-l)/2; if (mid>=8) r = mid; else l = mid; } cout << r; return0; }
classSolution { public: vector<int> searchRange(vector<int>& nums, int target){ int len = nums.size(); int l = 0, r = len; while(l<r){ int mid = l+(r-l)/2; if (nums[mid]>=target) r=mid; else l = mid+1; } if (l == len || nums[l]!=target){ vector<int> nw = {-1,-1}; return nw; } vector<int> ans; ans.emplace_back(l); l = 0; r = len; while(l<r){ int mid= l+(r-l)/2; if(nums[mid]>=target+1) r=mid; else l = mid+1; } ans.emplace_back(l-1); return ans; } };
当然这道题使用lower_bound和upper_bound也是可以的,具体使用方法见C++部分。
1 2 3 4 5 6 7 8 9
classSolution { public: vector<int> searchRange(vector<int>& nums, int target){ auto it = lower_bound(nums.begin(),nums.end(),target); if (it == nums.end() || *it != target) return vector<int>{-1,-1}; auto it2 = upper_bound(nums.begin(), nums.end(), target); return vector<int>{int(it-nums.begin()),int(it2-nums.begin())-1}; } };
classSolution { public: intsmallestDivisor(vector<int>& nums, int threshold){ function<bool(int)> judge = [&](int x){ int ans = 0; for (auto &num:nums) { if (num%x) ans = ans+num/x+1; else ans += num/x; } return ans<=threshold; }; int l=0,r=1000001; while (l+1<r) { int mid = l+(r-l)/2; if (judge(mid)) r=mid; else l=mid; } return r; } };
classSolution { public: longlongminimumTime(vector<int>& time, int totalTrips){ longlong r = 100000000000003; longlong l = 0; function<bool(longlong)> judge=[&](longlong x){ longlong ans = 0; for (auto tim:time) { ans += x / tim; } return ans>=(longlong)totalTrips?true:false; }; while (l <= r) { longlong mid = l + (r - l)/2; if (judge(mid)) r = mid-1; else l = mid+1; } return l; } };
classSolution { public: intshipWithinDays(vector<int>& weights, int days){ function<bool(int)> judge = [&](int x){ int cur=0,cur_day=1; for (auto weight:weights) { if(cur+weight <= x){ cur = cur+weight; } else { cur_day++; cur = weight; } } return cur_day<=days?true:false; }; int l = *max_element(weights.begin(), weights.end()); int r = accumulate(weights.begin(),weights.end(),0)+1; while (l<r) { int mid = l + (r-l)/2; if (judge(mid)) r = mid; else l = mid+1; } return l; } };
classSolution { public: intmaximumRemovals(string s, string p, vector<int>& removable){ int len = s.size(); function<bool(int)> judge=[&](int x){ vector<bool> vis(len,true); for (int i=0;i<=x;++i) vis[removable[i]] = false; int pp = 0; for(int i=0;i<len;++i){ if (s[i]==p[pp]&&vis[i]){ pp++; if (pp == p.size()) returntrue; } } returnfalse; }; int l = -1, r = removable.size(); while (l+1<r) { int mid = l + (r-l)/2; if (judge(mid)) l = mid; else r = mid; } return l+1; } };
classSolution { public: intmaximumRemovals(string s, string p, vector<int>& removable){ int len = s.size(); function<bool(int)> judge=[&](int x){ vector<bool> vis(len,true); for (int i=0;i<x;++i) vis[removable[i]] = false; int pp = 0; for(int i=0;i<len;++i){ if (s[i]==p[pp]&&vis[i]){ pp++; if (pp == p.size()) returntrue; } } returnfalse; }; int l = -1, r = removable.size()+1; while (l+1<r) { int mid = l + (r-l)/2; if (judge(mid)) l = mid; else r = mid; } return l; } };
classSolution { public: intminDays(vector<int>& bloomDay, int m, int k){ auto judge = [&](int x) { int cnt=0,cur=0; for (auto bloom:bloomDay) { if (bloom <= x) { cur++; if (cur == k) { cur = 0; cnt ++; if (cnt == m) returntrue; } } else cur = 0; } returnfalse; }; int l = 1, r = *max_element(bloomDay.begin(),bloomDay.end()); while (l <= r) { int mid = l + (r-l)/2; if (judge(mid)) r=mid-1; else l=mid+1; } returnjudge(l)?l:-1; } };
classSolution { public: intfurthestBuilding(vector<int>& heights, int bricks, int ladders){ auto judge = [&](int x) { vector<int> h_diffs; for(int i=1;i<=x;++i){ if (heights[i] > heights[i-1]) h_diffs.emplace_back(heights[i]-heights[i-1]); } if (ladders>=h_diffs.size()) returntrue; sort(h_diffs.begin(),h_diffs.end()); int sum_diff = accumulate(h_diffs.begin(),h_diffs.end()-ladders,0); return sum_diff <= bricks?true:false; }; int l=0,r=heights.size(); while(l<r){ int mid = l + (r-l)/2; if (judge(mid)) l = mid+1; else r=mid; } return l-1; } };