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¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: string
1869. Longer Contiguous Segments of Ones than Zeros¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: string
2414. Length of the Longest Alphabetical Continuous Substring¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string
3456. Find Special Substring of Length K¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: string
1957. Delete Characters to Make Fancy String¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: string
674. Longest Continuous Increasing Subsequence¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array
from typing import List
def findLengthOfLCIS(nums: List[int]) -> int:
n = len(nums)
if n <= 1:
return n
dp = [1 for _ in range(n)]
for i in range(1, n):
if nums[i] > nums[i - 1]:
dp[i] = dp[i - 1] + 1
return max(dp)
print(findLengthOfLCIS([1, 3, 5, 4, 7])) # 3
978. Longest Turbulent Subarray¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, dynamic programming, sliding window
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¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, math, dynamic programming
228. Summary Ranges¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array
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¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, sliding window
1887. Reduction Operations to Make the Array Elements Equal¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, sorting
845. Longest Mountain in Array¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, two pointers, dynamic programming, enumeration
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
2038. Remove Colored Pieces if Both Neighbors are the Same Color¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, string, greedy, game theory
1759. Count Number of Homogenous Substrings¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: math, string
3011. Find if Array Can Be Sorted¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, bit manipulation, sorting
1578. Minimum Time to Make Rope Colorful¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, string, dynamic programming, greedy
1839. Longest Substring Of All Vowels in Order¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, sliding window
2765. Longest Alternating Subarray¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array, enumeration
3255. Find the Power of K-Size Subarrays II¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, sliding window
3350. Adjacent Increasing Subarrays Detection II¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, binary search
3105. Longest Strictly Increasing or Strictly Decreasing Subarray¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: array
467. Unique Substrings in Wraparound String¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: string, dynamic programming
2948. Make Lexicographically Smallest Array by Swapping Elements¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, union find, sorting
2593. Find Score of an Array After Marking All Elements¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, hash table, sorting, heap priority queue, simulation
2393. Count Strictly Increasing Subarrays¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, math, dynamic programming
2436. Minimum Split Into Subarrays With GCD Greater Than One¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, math, dynamic programming, greedy, number theory
2495. Number of Subarrays Having Even Product¶
-
LeetCode | LeetCode CH (Medium)
-
Tags: array, math, dynamic programming
3063. Linked List Frequency¶
-
LeetCode | LeetCode CH (Easy)
-
Tags: hash table, linked list, counting