-
Notifications
You must be signed in to change notification settings - Fork 0
/
How_Many_Number.java
55 lines (46 loc) · 1.53 KB
/
How_Many_Number.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package com.leet_code;
import java.util.Arrays;
import java.util.HashMap;
public class How_Many_Number {
public static void main(String[] args) {
int[] arr={8,1,2,2,3};
System.out.println(Arrays.toString(smallerNumbersThanCurrent(arr)));
}
static int[] samll_the_current(int[] nums){
HashMap<Integer,Integer> capitalCities = new HashMap<Integer,Integer>(nums.length);
int[] xxx=new int[nums.length];
for (int i = 0; i < nums.length ; i++) {
int count=0;
for (int j = 0; j < nums.length; j++) {
if (nums[i] > nums[j]) {
count+=1;
}
}
xxx[i]=count;
}
return xxx;
}
////////////////////////////////////////////////////////////////////////////////////////////
// more optimise code from leetcode
static int[] smallerNumbersThanCurrent(int[] nums) {
// Create buckets for counting sort
int [] buckets = new int [102];
// Get Frequency of each element
for(int num : nums) {
buckets [num]++;
}
// Count smaller numbers than each element
for (int i =1; i<buckets.length; i++) {
buckets[i] += buckets[i-1];
}
// populate the result
int[] result = new int[nums.length];
for (int i=0; i< result.length; i++) {
if(nums[i] == 0)
result[i] = 0;
else
result[i] = buckets[nums[i]-1];
}
return result;
}
}