-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBulbShine.java
More file actions
73 lines (54 loc) · 1.38 KB
/
BulbShine.java
File metadata and controls
73 lines (54 loc) · 1.38 KB
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
package kent.alg.leetcode;
import java.util.HashMap;
public class BulbShine {
public int bulbShineTimes(int[] a) {
int lightupTimes = 0;
HashMap<Integer, Boolean> map = new HashMap<Integer, Boolean>();
for(int i=0 ; i<a.length; i++) {
int currentValue = a[i];
// System.out.println(currentValue);
if(currentValue == 1)
{
map.put(a[i], true);
lightupTimes++;
// backward
for(int j=0; j<i; j++) {
if(map.get(a[j]) != null && map.get(a[j]) == false)
{
map.put(a[j], true);
} else
break;
}
}
else if (currentValue > 1 && map.get(currentValue-1) != null && map.get(currentValue-1) == true)
{
map.put(currentValue, true);
lightupTimes ++;
// backward
for(int j=0; j<i; j++) {
if(map.get(a[j]) != null || map.get(a[j]) == false)
{
map.put(a[j], true);
} else
break;
}
}
else
{
map.put(currentValue, false);
}
}
return lightupTimes;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
//int[] a = {2,3,4,1,5}; // 2
//int[] a = {2,4,5,1,3}; // 2
//int[] a = {2,3,4,5,1}; // 1
//int[] a = {1,3,4,2,5}; // 3
//int[] a = {1,2,3,4,5}; // 5
int[] a = {2,1,3,5,4}; // 3
BulbShine bs = new BulbShine();
System.out.println ("Lightup times: " +bs.bulbShineTimes(a));
}
}