LintCode:统计比给定整数小的数的个数

27 篇文章 0 订阅

LintCode:统计比给定整数小的数的个数

尝试用线段树,超时。

public class Solution {
   /**
     * @param A: An integer array
     * @return: The number of element in the array that 
     *          are smaller that the given integer
     */
     int res;
    public ArrayList<Integer> countOfSmallerNumber(int[] A, int[] queries) {
        // write your code here
        ArrayList<Integer> ans = new ArrayList<Integer> ();
        for(int i=0; i < queries.length; i++){
            ans.add(0);
        }
        if(A.length == 0){
            return ans;
        }
        int n = A.length - 1;
        int root_val = max(A, 0, n);
        SegmentTreeNode root = new SegmentTreeNode(0, n, root_val);
        buildSegmentTree(root, A);
        int m = 0;
        for(int query: queries){
            res = 0;
            myQuery(root, query);
            ans.set(m++, res);
        }
        return ans;
    }
    public void buildSegmentTree(SegmentTreeNode root, int[] A){
        if(root.start == root.end){
            return;
        }
        int left_start = root.start;
        int left_end = (root.start + root.end) / 2;
        int left_val = max(A, left_start, left_end);

        int right_start = (root.start + root.end) / 2 + 1;
        int right_end = root.end;
        int right_val = max(A, right_start, right_end);

        root.left = new SegmentTreeNode(left_start, left_end, left_val);
        root.right = new SegmentTreeNode(right_start, right_end, right_val);

        buildSegmentTree(root.left, A);
        buildSegmentTree(root.right, A);

    }
    public int max(int[] A, int start, int end){
        int maxNum = A[start];
        for(int i=start; i<=end; i++){
            if(A[i] > maxNum){
                maxNum = A[i];
            }
        }
        return maxNum;
    }

    public void myQuery(SegmentTreeNode root, int num){
        if(root == null){
            return;
        }
        if(root.max < num){
            res += root.end - root.start + 1;
            return;
        }
        myQuery(root.left, num);
        myQuery(root.right, num);
    }
}

那就用二分法吧

public class Solution {
   /**
     * @param A: An integer array
     * @return: The number of element in the array that 
     *          are smaller that the given integer
     */
    public ArrayList<Integer> countOfSmallerNumber(int[] A, int[] queries) {
        // write your code here
        ArrayList<Integer> ans = new ArrayList<Integer> ();
        for(int i=0; i < queries.length; i++){
            ans.add(0);
        }
        if(A.length == 0){
            return ans;
        }
        quickSort(A, 0, A.length-1);
        int m = 0;
        for(int query:queries){
            int res = myQuery(A, query);
            ans.set(m++, res);
        }
        return ans;

    }
    public void quickSort(int[] A, int m, int n){
        if(m >= n){
            return;
        }
        int i = m;
        int j = n;
        int tmp = A[i];
        while (i < j){
            while(i < j && A[j] >= tmp){
                j --;
            }
            A[i] = A[j];
            while(i < j && A[i] <= tmp){
                i ++;
            }
            A[j] = A[i];
        }
        A[i] = tmp;
        quickSort(A, m, i-1);
        quickSort(A, i+1, n);
    }
    public int myQuery(int[] A, int query){
        int high = A.length - 1;
        while(A[high] >= query && high > 0){
            high /= 2;
        }
        while(A[high] < query && high < A.length - 1){
            high += 1;
        }
        return high;

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值