forked from oleg-cherednik/DailyCodingProblem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
49 lines (39 loc) · 1.33 KB
/
Solution.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
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
/**
* @author Oleg Cherednik
* @since 18.01.2019
*/
public class Solution {
public static void main(String... args) {
System.out.println(findNearestLargerNumberIndex(0));
}
private static final int[] arr = { 4, 1, 3, 5, 6, 4 };
private static final Map<Integer, Set<Integer>> mapNumPos = new HashMap<>();
private static final Map<Integer, Integer> mapNumNextNum = new HashMap<>();
static {
Set<Integer> unique = new TreeSet<>();
for (int i = 0; i < arr.length; i++) {
unique.add(arr[i]);
if (!mapNumPos.containsKey(arr[i]))
mapNumPos.put(arr[i], new HashSet<>());
mapNumPos.get(arr[i]).add(i);
}
Integer prv = null;
for (Integer num : unique) {
if (prv != null)
mapNumNextNum.put(prv, num);
prv = num;
}
}
public static int findNearestLargerNumberIndex(int i) {
int num = arr[i];
Integer nextNum = mapNumNextNum.get(num);
Set<Integer> nextNumPos = nextNum != null ? mapNumPos.get(nextNum) : Collections.emptySet();
return nextNumPos.isEmpty() ? -1 : nextNumPos.iterator().next();
}
}