BS on result
Find maximum or minimum
No list, find result based on binary search
69 Sqrt(x)
Similar code, but
if (mid < x/mid)Jump out the loop.
if (end*end <= x) return end
public int mySqrt(int x) {
if(x == 0) return 0;
int start = 0, end = x;
while(start+1<end){
int mid = start + (end-start)/2;
if(mid == x/mid) return mid; // mid移項避免int越界
else if(mid > x/mid) end = mid;
else start = mid;
}
if(end <= x/end) return end;
return start;
}First bad version
Given a boolean function isBadVersion
Wood cut
Given an integer array representing different length of wood. Cut them into small pieces to guarantee you could have equal or more than k pieces with the same length
create function to count the piece sum of L
if count >= k, means k is small, set
start = mid
Last updated
Was this helpful?