Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to find length of longest strictly increasing then decreasing sublist in Python
Suppose we have a list of numbers called nums. We have to find the length of the longest sublist (minimum length 3) whose values are strictly increasing and then strictly decreasing.
So, if the input is like nums = [8, 2, 4, 6, 3, 1], then the output will be 5, as the sublist [2, 4, 6, 3, 1] is strictly increasing then decreasing.
Algorithm
To solve this, we will follow these steps ?
- Initialize variables:
i = 0,n = size of array,res = -infinity - While
i , do:- Mark starting position:
st = i - Initialize counters:
linc = 0,ldec = 0 - Count increasing elements while
a[i] - Count decreasing elements while
a[i] > a[i + 1] - If both increasing and decreasing parts exist, update result
- Skip duplicate elements
- Mark starting position:
- Return
resifres >= 0, otherwise return 0
Implementation
class Solution:
def solve(self, nums):
i, n, res = 0, len(nums), float("-inf")
while i < n - 2:
st = i
linc, ldec = 0, 0
# Count strictly increasing part
while i < n - 1 and nums[i] < nums[i + 1]:
linc += 1
i += 1
# Count strictly decreasing part
while i < n - 1 and nums[i] > nums[i + 1]:
ldec += 1
i += 1
# Update result if we have both increasing and decreasing parts
if linc > 0 and ldec > 0:
res = max(res, i - st + 1)
# Skip equal elements
while i < n - 1 and nums[i] == nums[i + 1]:
i += 1
# Move to next position if no progress made
if i == st:
i += 1
return res if res >= 0 else 0
# Test the solution
ob = Solution()
nums = [8, 2, 4, 6, 3, 1]
result = ob.solve(nums)
print(f"Input: {nums}")
print(f"Length of longest increasing-decreasing sublist: {result}")
Input: [8, 2, 4, 6, 3, 1] Length of longest increasing-decreasing sublist: 5
How It Works
The algorithm works by scanning the array and finding sequences that first increase strictly, then decrease strictly. For the example [8, 2, 4, 6, 3, 1]:
- Starting at index 1:
[2, 4, 6](increasing) ?[6, 3, 1](decreasing) - This gives us the sublist
[2, 4, 6, 3, 1]with length 5 - The first element
8is skipped as it doesn't form a valid pattern
Additional Example
# Test with different inputs
ob = Solution()
test_cases = [
[1, 3, 5, 4, 2], # Length 5: [1,3,5,4,2]
[1, 2, 3], # Length 0: only increasing
[3, 2, 1], # Length 0: only decreasing
[1, 3, 2, 4, 2, 1], # Length 3: [3,2,1] or [4,2,1]
]
for i, nums in enumerate(test_cases):
result = ob.solve(nums)
print(f"Test {i+1}: {nums} ? Length: {result}")
Test 1: [1, 3, 5, 4, 2] ? Length: 5 Test 2: [1, 2, 3] ? Length: 0 Test 3: [3, 2, 1] ? Length: 0 Test 4: [1, 3, 2, 4, 2, 1] ? Length: 3
Conclusion
This algorithm efficiently finds the longest sublist that is strictly increasing then decreasing with O(n) time complexity. The key insight is to track both increasing and decreasing sequences and ensure both parts exist for a valid result.
