[lintcode medium]Count of Smaller Number

Count of Smaller Number

Give you an integer array (index from 0 to n-1, where n is the size of this array, value from 0 to 10000) and an query list. For each query, give you an integer, return the number of element in the array that are smaller than the given integer.

Example

For array [1,2,7,8,5], and queries [1,8,5], return [0,4,2]

Note

We suggest you finish problem Segment Tree Build and Segment Tree Query II first.

Challenge

Could you use three ways to do it.

  1. Just loop
  2. Sort and binary search
  3. Build Segment Tree and Search.
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> list=new ArrayList<Integer>();
        //if(A.length==0 || queries.length==0) return list;
        
        int m=A.length;
        int n=queries.length;

        for(int i=0;i<n;i++)
        {
            int count=0;
            for(int j=0;j<m;j++)
            {
                if(A[j]<queries[i])
                {
                    count++;
                }
            }
            list.add(count);
        }
        return list;
    }
}

 

posted on 2015-12-10 06:11  一心一念  阅读(184)  评论(0编辑  收藏  举报

导航