[US Giants] 三. Binary Search

Sqrt(x):点击打开链接

Implement int sqrt(int x).

Compute and return the square root of x.

Example

sqrt(3) = 1

sqrt(4) = 2

sqrt(5) = 2

sqrt(10) = 3

Challenge O(log(x))
class Solution {
    /**
     * @param x: An integer
     * @return: The sqrt of x
     */
    public int sqrt(int x) {
        if(x<=1){
            return x;
        }
        
        long start=1;
        long end=x;
        while(start+1<end){
            long mid=start+(end-start)/2;
            if(mid*mid==x){
                return (int)mid;
            }else if(mid*mid<x){
                start=mid;
            }else{
                end=mid;
            }
        }
        
        if(end*end<=x){
            return (int)end;
        }else{
            return (int)start;
        }
    }
}
Search Insert Position: 点击打开链接

Given a sorted array 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.

You may assume NO duplicates in the array.

Example

[1,3,5,6], 5 → 2

[1,3,5,6], 2 → 1

[1,3,5,6], 7 → 4

[1,3,5,6], 0 → 0

Challenge  O(log(n)) time

思路:找到first position>=target

public class Solution {
    /** 
     * param A : an integer sorted array
     * param target :  an integer to be inserted
     * return : an integer
     */
    public int searchInsert(int[] A, int target) {
        if(A==null || A.length==0){
            return 0;
        }
        
        int start=0;
        int end=A.length-1;
        while(start+1<end){
            int mid=start+(end-start)/2;
            if(A[mid]==target){
                return mid;
            }else if(A[mid]<target){
                start=mid;
            }else{
                end=mid;
            }
        }
        
        if(A[start]>=target){                   //如果A[start]=target,start就是所求
            return start;                       //如果A[start]>target,target应该放在start的位置,start还是所求
        }else if(A[end]>=target){               //end也是
            return end;
        }else{                                  //不然就是放数组最后位置,end+1
            return end+1;     
        }
    }
}
Wood Cut:点击打开链接

Given n pieces of wood with length L[i] (integer array). Cut them into small pieces to guarantee you could have equal or more than k pieces with the same length. What is the longest length you can get from the n pieces of wood? Given L & k, return the maximum length of the small pieces.

You couldn't cut wood into float length.

If you couldn't get >= k pieces, return 0.

Example

For L=[232, 124, 456]k=7, return 114.

Challenge O(n log Len), where Len is the longest length of the wood.
复杂度分析:遍历求和时间复杂度:O(n),二分搜索时间复杂度:O(log max),因此,总时间复杂度为:nO(log max) ,空间复杂度为O(1)              
public class Solution {
    /** 
     *@param L: Given n pieces of wood with length L[i]
     *@param k: An integer
     *return: The maximum length of the small pieces.
     */
    public int woodCut(int[] L, int k) {
        if(L==null || L.length==0){
            return 0;
        }
        
        int max=0;
        for(Integer len:L){
            max=Math.max(len,max);                         //遍历数组找到最大值元素,然后在长度1~max上二叉中分
        }
        
        int start=1;
        int end=max;
        while(start+1<end){
            int mid=start+(end-start)/2;
            if(count(L,mid)>=k){                           //只要看最后的总结果数:能在保证大于k的基础上长度尽量长
                start=mid;
            }else{
                end=mid;
            }
        }
        if(count(L,end)>=k){                               //因为要尽量长,还是要先考虑end值是不是符合要求,再考虑start值
            return end;
        }else if(count(L,start)>=k){
            return start;
        }else{
            return 0;
        }
    }
        
    private int count(int[] L, int len) {                  //二分到每一个长度len下的能切分木头总个数
        int sum = 0;
        for (int i = 0; i < L.length; i++) {
            sum += L[i]/len;
        }
        return sum;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值