-
Notifications
You must be signed in to change notification settings - Fork 12
/
GoodStones.cpp
36 lines (36 loc) · 978 Bytes
/
GoodStones.cpp
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
class Solution{
public:
int goodStones(int n,vector<int> &arr){
// Code here
vector<int>dp(n,0);
for(int i=0;i<n;i++){
if(dp[i]==0){
int temp=i;
bool good =false;
while(dp[temp]!=1){
if(arr[temp]==0)break;
//mark visited
dp[temp]=1;
//next index
temp+=arr[temp];
if(temp<0 || temp>=n || dp[temp]==2){
good =true;
break;
}
}
if(good){
temp=i;
while(temp>=0 && temp<n){
dp[temp]=2;
temp+=arr[temp];
}
}
}
}
int ans=0;
for(int i=0;i<n;i++){
if(dp[i]==2)ans++;
}
return ans;
}
};