-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathday18_3.java
More file actions
55 lines (40 loc) · 1015 Bytes
/
day18_3.java
File metadata and controls
55 lines (40 loc) · 1015 Bytes
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
class Solution {
public static int goodStones(int n, int[] arr) {
// code here
int count =0;
int[] visit = new int[n];
for(int i=0; i<n; i++)
{
if(visit[i]==0)
{
check(arr, visit, i);
}
}
for(int i=0; i<n ; i++)
{
if(visit[i]==2)
{
count++;
}
}
return count;
}
public static int check(int[] arr, int[] visit , int index)
{
if(index<0 || index>=arr.length)
{
return 2; //safe
}
if(arr[index]==0)
{
return 1;
}
if(visit[index]!=0)
{
return visit[index];
}
visit[index]=1; //visited
int new_i = index+ arr[index];
return visit[index]=check(arr, visit, new_i);
}
}