Grouped Loop¶
Table of Contents¶
- 1446. Consecutive Characters (Easy)
- 1869. Longer Contiguous Segments of Ones than Zeros (Easy)
- 2414. Length of the Longest Alphabetical Continuous Substring (Medium)
- 3456. Find Special Substring of Length K (Easy)
- 1957. Delete Characters to Make Fancy String (Easy)
- 674. Longest Continuous Increasing Subsequence (Easy)
- 978. Longest Turbulent Subarray (Medium)
- 2110. Number of Smooth Descent Periods of a Stock (Medium)
- 228. Summary Ranges (Easy)
- 2760. Longest Even Odd Subarray With Threshold (Easy)
- 1887. Reduction Operations to Make the Array Elements Equal (Medium)
- 845. Longest Mountain in Array (Medium)
- 2038. Remove Colored Pieces if Both Neighbors are the Same Color (Medium)
- 1759. Count Number of Homogenous Substrings (Medium)
- 3011. Find if Array Can Be Sorted (Medium)
- 1578. Minimum Time to Make Rope Colorful (Medium)
- 1839. Longest Substring Of All Vowels in Order (Medium)
- 2765. Longest Alternating Subarray (Easy)
- 3255. Find the Power of K-Size Subarrays II (Medium)
- 3350. Adjacent Increasing Subarrays Detection II (Medium)
- 3105. Longest Strictly Increasing or Strictly Decreasing Subarray (Easy)
- 467. Unique Substrings in Wraparound String (Medium)
- 2948. Make Lexicographically Smallest Array by Swapping Elements (Medium)
- 2593. Find Score of an Array After Marking All Elements (Medium)
- 2393. Count Strictly Increasing Subarrays (Medium) 👑
- 2436. Minimum Split Into Subarrays With GCD Greater Than One (Medium) 👑
- 2495. Number of Subarrays Having Even Product (Medium) 👑
- 3063. Linked List Frequency (Easy) 👑
1446. Consecutive Characters¶
1869. Longer Contiguous Segments of Ones than Zeros¶
2414. Length of the Longest Alphabetical Continuous Substring¶
3456. Find Special Substring of Length K¶
1957. Delete Characters to Make Fancy String¶
674. Longest Continuous Increasing Subsequence¶
978. Longest Turbulent Subarray¶
from typing import List
# DP - Kadane
def maxTurbulenceSize(arr: List[int]) -> int:
n = len(arr)
up = [1 for _ in range(n)]
down = [1 for _ in range(n)]
maxLen = 1
for i in range(1, n):
if arr[i - 1] < arr[i]:
up[i] = down[i - 1] + 1
down[i] = 1
elif arr[i - 1] > arr[i]:
down[i] = up[i - 1] + 1
up[i] = 1
else:
up[i] = 1
down[i] = 1
maxLen = max(maxLen, up[i], down[i])
return maxLen
arr = [9, 4, 2, 10, 7, 8, 8, 1, 9]
print(maxTurbulenceSize(arr)) # 5
2110. Number of Smooth Descent Periods of a Stock¶
228. Summary Ranges¶
from typing import List
# Variable Sliding Window
def summaryRanges(nums: List[int]) -> List[str]:
left, right = 0, 0
n = len(nums)
res = []
while left < n:
while right + 1 < n and nums[right] + 1 == nums[right + 1]:
right += 1
if left == right:
res.append(f"{nums[left]}")
else:
res.append(f"{nums[left]}->{nums[right]}")
right += 1
left = right
return res
if __name__ == "__main__":
print(summaryRanges([0, 1, 2, 4, 5, 7]))
# ["0->2", "4->5", "7"]
print(summaryRanges([0, 2, 3, 4, 6, 8, 9]))
# ["0", "2->4", "6", "8->9"]
2760. Longest Even Odd Subarray With Threshold¶
1887. Reduction Operations to Make the Array Elements Equal¶
845. Longest Mountain in Array¶
from typing import List
# Left Right Pointers
def longestMountain(arr: List[int]) -> int:
n = len(arr)
res = 0
left = 0
while left < n:
right = left
if right < n - 1 and arr[right] < arr[right + 1]:
while right < n - 1 and arr[right] < arr[right + 1]:
right += 1
if right < n - 1 and arr[right] > arr[right + 1]:
while right < n - 1 and arr[right] > arr[right + 1]:
right += 1
res = max(res, right - left + 1)
left = max(right, left + 1)
return res
arr = [2, 1, 4, 7, 3, 2, 5]
print(longestMountain(arr)) # 5