Skip to content

Commit cf533ea

Browse files
committed
Solution as on 12-09-2022 11:30 pm
1 parent 7d97c41 commit cf533ea

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution
2+
{
3+
// 2369.✅ Check if There is a Valid Partition For The Array
4+
5+
public:
6+
bool validPartition(vector<int> &nums)
7+
{
8+
int n = nums.size();
9+
vector<bool> dp(n + 1);
10+
11+
dp[0] = 1;
12+
13+
for (int i = 0; i < n; ++i)
14+
{
15+
if (dp[i])
16+
{
17+
if (i + 1 < n && nums[i] == nums[i + 1])
18+
dp[i + 2] = true;
19+
if (i + 2 < n && nums[i] == nums[i + 1] && nums[i] == nums[i + 2])
20+
dp[i + 3] = true;
21+
if (i + 2 < n && nums[i] == nums[i + 1] - 1 && nums[i] == nums[i + 2] - 2)
22+
dp[i + 3] = true;
23+
}
24+
}
25+
return dp[n];
26+
}
27+
};

0 commit comments

Comments
 (0)