-
Notifications
You must be signed in to change notification settings - Fork 2
/
Que1365.java
63 lines (53 loc) · 1.51 KB
/
Que1365.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
56
57
58
59
60
61
62
63
import sun.font.FontRunIterator;
import java.util.*;
/**
* @author: hyl
* @date: 2020/03/09
**/
public class Que1365 {
/**
* 暴力
* 时间复杂度 : O(N^2)
* 空间复杂度 : O(N)
*/
public int[] smallerNumbersThanCurrent(int[] nums) {
int result[] = new int[nums.length];
for (int i = 0; i < nums.length; i++) {
int sum = 0;
for (int j = 0; j < nums.length; j++) {
if (i != j && nums[i] > nums[j]){
sum++;
}
}
result[i] = sum;
}
return result;
}
/**
* 暴力
* 时间复杂度 : O(N * log(N))
* 空间复杂度 : O(N)
*/
public int[] smallerNumbersThanCurrent1(int[] nums) {
int len = nums.length;
Map<Integer, Set<Integer>> valueIndex = new HashMap<>(len);
for (int i = 0; i < len; i++) {
if (!valueIndex.containsKey(nums[i])){
valueIndex.put(nums[i] , new HashSet<>());
}
valueIndex.get(nums[i]).add(i);
}
int[] sortArr = Arrays.copyOf(nums , len) , res = new int[len];
Arrays.sort(sortArr);
for (int index = len-1; index >=0 ; index--) {
for (int i : valueIndex.get(sortArr[index])) {
res[i] = index;
}
}
return res;
}
public static void main(String[] args) {
int[] arr = {8 ,1,2,2,3};
//smallerNumbersThanCurrent1(arr);
}
}