-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathShortestImpossibleRollSequence.java
93 lines (85 loc) · 2.47 KB
/
ShortestImpossibleRollSequence.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*https://leetcode.com/problems/shortest-impossible-sequence-of-rolls/*/
class Pair implements Comparable<Pair>
{
int a, b;
Pair(int a, int b)
{
this.a = a;
this.b = b;
}
@Override
public int compareTo(Pair p)
{
return 0;
}
@Override
public String toString()
{
return "("+this.a+","+this.b+")";
}
}
class Solution {
public int shortestSequence(int[] rolls, int k) {
Map<Integer,TreeSet<Integer>> map = new HashMap<Integer,TreeSet<Integer>>();
int n = rolls.length, i;
//store all the list of indices for all rolls
for (i = 0; i < n; ++i)
{
if (!map.containsKey(rolls[i]))
map.put(rolls[i],new TreeSet<Integer>());
map.get(rolls[i]).add(i);
}
int len = 0, curr, prev, currIn, prevIn;
//for length 1
if (!map.containsKey(1)) return 1;
prevIn = map.get(1).first();
prev = 1;
for (i = 2; i <= k; ++i) //get the farthest first roll
{
if (!map.containsKey(i)) return 1;
if (map.get(i).first() > prevIn)
{
prevIn = map.get(i).first();
prev = i;
}
}
len = 2;
for (int loop = 1; loop <= n; ++loop) //for next iterations
{
currIn = -1;
curr = -1;
for (i = 1; i <= k; ++i) //for all rolls find the farthest first roll
{
Integer nextIn = map.get(i).higher(prevIn);
if (nextIn == null) return len; //if couldn't find, return the result
if (nextIn > currIn)
{
currIn = nextIn;
curr = i;
}
}
prevIn = currIn;
prev = curr;
++len; //increment length
}
return len+1; //program won't come here
}
}
//optimized solution
class Solution {
public int shortestSequence(int[] rolls, int k) {
int n = rolls.length;
int res = 0;
Set<Integer> set = new HashSet<>();
for (int i = 0; i < n; i++) //for each roll given
{
set.add(rolls[i]); //add to set
if (set.size() == k) //when set size is complete
{
set.clear(); //remove everything
res++; //increment length by 1
}
}
return res+1; //return the next length
}
}