DP State Machine Advanced¶
Table of Contents¶
- 1262. Greatest Sum Divisible by Three (Medium)
- 1363. Largest Multiple of Three (Hard)
- 2771. Longest Non-decreasing Subarray From Two Arrays (Medium)
- 1594. Maximum Non Negative Product in a Matrix (Medium)
- 3196. Maximize Total Cost of Alternating Subarrays (Medium)
- 935. Knight Dialer (Medium)
- 1537. Get the Maximum Score (Hard)
- 2919. Minimum Increment Operations to Make Array Beautiful (Medium)
- 801. Minimum Swaps To Make Sequences Increasing (Hard)
- 3434. Maximum Frequency After Subarray Operation (Medium)
- 1955. Count Number of Special Subsequences (Hard)
- 3068. Find the Maximum Sum of Node Values (Hard)
- 2272. Substring With Largest Variance (Hard)
- 276. Paint Fence (Medium) 👑
- 1746. Maximum Subarray Sum After One Operation (Medium) 👑
- 2036. Maximum Alternating Subarray Sum (Medium) 👑
- 2361. Minimum Costs Using the Train Line (Hard) 👑
- 3269. Constructing Two Increasing Arrays (Hard) 👑
1262. Greatest Sum Divisible by Three¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming, greedy, sorting
1363. Largest Multiple of Three¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, dynamic programming, greedy
2771. Longest Non-decreasing Subarray From Two Arrays¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming
1594. Maximum Non Negative Product in a Matrix¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming, matrix
3196. Maximize Total Cost of Alternating Subarrays¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming
935. Knight Dialer¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: dynamic programming
1537. Get the Maximum Score¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, two pointers, dynamic programming, greedy
2919. Minimum Increment Operations to Make Array Beautiful¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming
801. Minimum Swaps To Make Sequences Increasing¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, dynamic programming
3434. Maximum Frequency After Subarray Operation¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, dynamic programming, greedy, prefix sum
1955. Count Number of Special Subsequences¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, dynamic programming
3068. Find the Maximum Sum of Node Values¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, dynamic programming, greedy, bit manipulation, tree, sorting
2272. Substring With Largest Variance¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, dynamic programming
from itertools import permutations
from math import inf
from string import ascii_lowercase
# DP State Machine
def largestVariance(s: str) -> int:
res = 0
for a, b in permutations(ascii_lowercase, 2):
f0, f1 = 0, -inf
for ch in s:
if ch == a:
f0 = max(f0, 0) + 1
f1 += 1
elif ch == b:
f1 = f0 = max(f0, 0) - 1
res = max(res, f1)
return res
if __name__ == "__main__":
s = "aababbb"
print(largestVariance(s)) # 3
276. Paint Fence¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: dynamic programming
1746. Maximum Subarray Sum After One Operation¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming
2036. Maximum Alternating Subarray Sum¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming
2361. Minimum Costs Using the Train Line¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, dynamic programming
3269. Constructing Two Increasing Arrays¶
-
LeetCode | LeetCode CH (Hard)
-
Tags: array, dynamic programming