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 lexicographically largest mountain list in Python
A mountain list is a sequence that strictly increases to a peak and then strictly decreases. Given three positive numbers n, lower, and upper, we need to find the lexicographically largest mountain list of length n with all values in range [lower, upper].
The challenge is ensuring both increasing and decreasing parts are non-empty while maximizing the lexicographic order.
Problem Analysis
For a valid mountain list of length n:
Must have at least one increasing element and one decreasing element
Total possible unique values = upper − lower + 1
Maximum mountain length = 2 * (unique values) − 1
Algorithm Steps
The solution follows these steps:
Check if mountain is possible: n ? 2 * (upper − lower) + 1
Calculate available range: c = upper − lower
Determine increasing part length: d = max(1, n − c − 1)
Build increasing part: from (upper − d) to (upper − 1)
Build decreasing part: from upper down to (upper − n + d)
Implementation
def solve(n, lower, upper):
# Check if mountain is possible
if n > 2 * (upper - lower) + 1:
return []
c = upper - lower # Available range
d = 1 # Default increasing part length
# Adjust increasing part length if needed
if c < n:
d = n - c - 1
# Ensure at least one increasing element
if d == 0:
d = 1
# Build increasing part (excluding peak)
increasing_part = list(range(upper - d, upper))
# Build decreasing part (including peak)
decreasing_part = list(range(upper, upper - n + d, -1))
return increasing_part + decreasing_part
# Test the function
n = 5
lower = 3
upper = 7
result = solve(n, lower, upper)
print(f"Mountain list: {result}")
Mountain list: [6, 7, 6, 5, 4]
Step-by-Step Trace
For n=5, lower=3, upper=7:
def trace_solution(n, lower, upper):
print(f"Input: n={n}, lower={lower}, upper={upper}")
# Step 1: Check possibility
max_possible = 2 * (upper - lower) + 1
print(f"Maximum possible length: {max_possible}")
if n > max_possible:
print("Mountain not possible")
return []
# Step 2: Calculate parameters
c = upper - lower
d = max(1, n - c - 1) if c < n else 1
print(f"Range size (c): {c}")
print(f"Increasing part length (d): {d}")
# Step 3: Build parts
increasing_part = list(range(upper - d, upper))
decreasing_part = list(range(upper, upper - n + d, -1))
print(f"Increasing part: {increasing_part}")
print(f"Decreasing part: {decreasing_part}")
result = increasing_part + decreasing_part
print(f"Final mountain: {result}")
return result
trace_solution(5, 3, 7)
Input: n=5, lower=3, upper=7 Maximum possible length: 9 Range size (c): 4 Increasing part length (d): 1 Increasing part: [6] Decreasing part: [7, 6, 5, 4] Final mountain: [6, 7, 6, 5, 4]
Edge Cases
# Test edge cases
test_cases = [
(3, 1, 2), # Minimum mountain
(6, 1, 2), # Impossible case
(4, 5, 8), # Larger range
]
for n, lower, upper in test_cases:
result = solve(n, lower, upper)
print(f"n={n}, range=[{lower},{upper}]: {result}")
n=3, range=[1,2]: [1, 2, 1] n=6, range=[1,2]: [] n=4, range=[5,8]: [7, 8, 7, 6]
Conclusion
The algorithm creates the lexicographically largest mountain list by starting the increasing part as high as possible while ensuring both parts remain non-empty. The key insight is balancing the lengths of increasing and decreasing sections to maximize the overall sequence value.
---