2369. Check if There is a Valid Partition For The Array
Read the full problem statement on LeetCode.
Difficulty: medium Acceptance: 52% Topics: Array, Dynamic Programming
View full problem on LeetCode Reading material
Reference solution (spoiler · python)
# Time: O(n)
# Space: O(1)
# dp
class Solution(object):
def validPartition(self, nums):
"""
:type nums: List[int]
:rtype: bool
"""
dp = [False]*4
dp[0] = True
for i in xrange(len(nums)):
dp[(i+1)%4] = False
if i-1 >= 0 and nums[i] == nums[i-1]:
dp[(i+1)%4] |= dp[((i+1)-2)%4]
if i-2 >= 0 and (nums[i] == nums[i-1] == nums[i-2] or
nums[i] == nums[i-1]+1 == nums[i-2]+2):
dp[(i+1)%4] |= dp[((i+1)-3)%4]
return dp[len(nums)%4]
Solution from kamyu104/LeetCode-Solutions · MIT
Similar questions