Skip to content

Commit edc31d0

Browse files
authored
Create 3152. Special Array II (#656)
2 parents 326509d + 679fb71 commit edc31d0

File tree

1 file changed

+40
-0
lines changed

1 file changed

+40
-0
lines changed

3152. Special Array II

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class Solution {
2+
public:
3+
bool check(int x, int y){
4+
if((x+y)%2)return false;
5+
return true;
6+
}
7+
vector<bool> isArraySpecial(vector<int>& a, vector<vector<int>>& q) {
8+
int n=a.size();
9+
vector<bool>ans;
10+
if(n==1){
11+
int size=q.size();
12+
while(size--){
13+
ans.push_back(true);
14+
}
15+
return ans;
16+
}
17+
18+
vector<int>v(n,0);
19+
if(check(a[0],a[1]))v[0]=1;
20+
for(int i=1;i<a.size()-1;i++){
21+
if(check(a[i],a[i+1]))v[i]=v[i-1]+1;
22+
else v[i]=v[i-1];
23+
}
24+
v[n-1]=v[n-2];
25+
26+
for(int i=0;i<q.size();i++){
27+
int l=q[i][0],r=q[i][1];
28+
bool b=true;
29+
if(l==r)b=true;
30+
else if(v[r-1]-v[l]==0){
31+
if(check(a[l],a[l+1]))b=false;
32+
else b=true;
33+
}
34+
else b=false;
35+
ans.push_back(b);
36+
}
37+
38+
return ans;
39+
}
40+
};

0 commit comments

Comments
 (0)