forked from MTrajK/coding-problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_ascending_sublists.py
More file actions
65 lines (48 loc) · 1.59 KB
/
reverse_ascending_sublists.py
File metadata and controls
65 lines (48 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
'''
Reverse Every Ascending Sublist
Create and return a new list that contains the same elements as the argument list items, but
reversing the order of the elements inside every maximal strictly ascending sublist
Input: [5, 7, 10, 4, 2, 7, 8, 1, 3]
Output: [10, 7, 5, 4, 8, 7, 2, 3, 1]
Output explanation: 5, 7, 10 => 10, 7, 5 ; 4 => 4; 2, 7, 8 => 8, 7, 2; 1, 3 => 3, 1
=========================================
Find the start and end of each sublist and reverse it in-place.
Time Complexity: O(N)
Space Complexity: O(1)
'''
############
# Solution #
############
def reverse_ascending_sublists(arr):
n = len(arr)
if n == 0:
return []
start = 0
for i in range(1, n):
# check if this the end of the strictly ascending sublist
if arr[i] < arr[i - 1]:
reverse_arr(arr, start, i - 1)
# a new sublist starts
start = i
reverse_arr(arr, start, n - 1)
return arr
def reverse_arr(arr, start, end):
while start < end:
# reverse the array from the start index to the end index by
# swaping each element with the pair from the other part of the array
arr[start], arr[end] = arr[end], arr[start]
start += 1
end -= 1
return arr
###########
# Testing #
###########
# Test 1
# Correct result => [5, 4, 3, 2, 1]
print(reverse_ascending_sublists([1, 2, 3, 4, 5]))
# Test 2
# Correct result => [5, 4, 3, 2, 1]
print(reverse_ascending_sublists([5, 4, 3, 2, 1]))
# Test 3
# Correct result => [10, 7, 5, 4, 8, 7, 2, 3, 1]
print(reverse_ascending_sublists([5, 7, 10, 4, 2, 7, 8, 1, 3]))