Skip to content

Commit 6c4c57f

Browse files
authored
Create check-if-there-is-a-valid-partition-for-the-array.py
1 parent 3c716de commit 6c4c57f

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Time: O(n)
2+
# Space: O(1)
3+
4+
# dp
5+
class Solution(object):
6+
def validPartition(self, nums):
7+
"""
8+
:type nums: List[int]
9+
:rtype: bool
10+
"""
11+
dp = [False]*4
12+
dp[0] = True
13+
for i in xrange(len(nums)):
14+
dp[(i+1)%4] = False
15+
if i-1 >= 0 and nums[i] == nums[i-1]:
16+
dp[(i+1)%4] |= dp[((i+1)-2)%4]
17+
if i-2 >= 0 and (nums[i] == nums[i-1] == nums[i-2] or
18+
nums[i] == nums[i-1]+1 == nums[i-2]+2):
19+
dp[(i+1)%4] |= dp[((i+1)-3)%4]
20+
return dp[len(nums)%4]

0 commit comments

Comments
 (0)