Count of Smaller Number

本文介绍了一种算法解决方案,通过快速排序和二分查找来计算数组中小于特定查询值的元素数量。该方法适用于整数数组,并提供了一个具体的Java实现案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 that the given integer.

hint: 

Solution 1

  Quick Sort + Binary Search

Solution 2

  Segment Tree

 1 public class Smaller {
 2     
 3         public ArrayList<Integer> countOfSmallerNumber(int[] A, int[] queries) {
 4             ArrayList<Integer> list = new ArrayList<Integer>();
 5             if (queries == null || queries.length == 0)
 6                 return list;
 7             if(A == null || A.length == 0) {
 8                 for (int i = 0; i < queries.length; i++) {
 9                     list.add(0);
10                 }
11                 return list;
12             }
13 
14             sort(A, 0, A.length-1);
15             
16             for (int i = 0; i < queries.length; i++) {
17                 list.add(binary(A, queries[i]));
18             }
19             System.out.println(list);
20             return list;
21         }
22         
23         public int binary(int[] A, int k) {
24             int i = 0, j = A.length-1;
25             while (i <= j) {
26                 int mid = (j - i) / 2 + i;
27                 if (A[mid] >= k) j = mid - 1;
28                 else i = mid + 1;
29             }
30             return i;
31         }
32         
33         public void sort(int[] A, int start, int end) {
34             if (start >= end) return;
35             int i = start, j = end;
36             int tmp = A[start];
37             while (i < j) {
38                 while (i < j && A[j] >= tmp) j--;
39                 A[i] = A[j];
40                 while (i < j && A[i] < tmp) i++;
41                 A[j] = A[i];
42             }
43             A[i] = tmp;
44             sort(A, start, i-1);
45             sort(A, i+1, end);
46         }
47         public static void main(String[] args) {
48             Smaller sol = new Smaller();
49             int[] A = {55,81,56,91,35,92,10,53,27,94,64,45,19,44,52,19,79,12,16,90,97,33,73,2,20,68,19,7,17,62,45,48,62,26,85,4,63,67,56,16};
50             int[] queries = {10,43,2,17,28,75,75,12};
51             sol.countOfSmallerNumber(A, queries);
52         }
53     }

 

转载于:https://www.cnblogs.com/joycelee/p/4516165.html

TwoThreadGuessNumber.java public class TwoThreadGuessNumber { public static void main(String args[]) { Number number=new Number(); number.giveNumberThread.start(); number.guessNumberThread.start(); } } Number.java public class Number implements Runnable { final int SMALLER=-1,LARGER=1,SUCCESS=8; int realNumber,guessNumber,min=0,max=100,message=SMALLER; boolean pleaseGuess=false,isGiveNumber=false; Thread giveNumberThread,guessNumberThread; Number() { 【代码1】创建giveNumberThread,当前Number类的实例是giveNumberThread的目标对象 【代码2】创建guessNumberThread,当前Number类的实例是guessNumberThread的目标对象 } public void run() { for(int count=1;true;count++) { setMessage(count); if( message==SUCCESS) return; } } public synchronized void setMessage(int count) { if(Thread.currentThread()==giveNumberThread&&isGiveNumber==false) { realNumber=(int)(Math.random()*100)+1; System.out.println("随机给你一个1至100之间的数,猜猜是多少?"); isGiveNumber=true; pleaseGuess=true; } if(Thread.currentThread()==giveNumberThread) { while(pleaseGuess==true) try { wait(); //让出CPU使用权,让另一个线程开始猜数 } catch(InterruptedException e){} if(realNumber>guessNumber) { //结束等待后,根据另一个线程的猜测给出提示 message=SMALLER; System.out.println("你猜小了"); } else if(realNumber<guessNumber) { message=LARGER; System.out.println("你猜大了"); } else { message=SUCCESS; System.out.println("恭喜,你猜对了"); } pleaseGuess=true; } if(Thread.currentThread()==guessNumberThread&&isGiveNumber==true) { while(pleaseGuess==false) try { wait(); //让出CPU使用权,让另一个线程给出提示 } catch(InterruptedException e){} if(message==SMALLER) { min=guessNumber; guessNumber=(min+max)/2; System.out.println("我第"+count+"次猜这个数是:"+guessNumber); } else if(message==LARGER) { max=guessNumber; guessNumber=(min+max)/2; System.out.println("我第"+count+"次猜这个数是:"+guessNumber); } pleaseGuess=false; } notifyAll(); } } 补全上述代码中的代码一和代码二
05-10
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值