-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
53 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,40 @@ | ||
""" | ||
Problem: | ||
Given an array of integers out of order, determine the bounds of the smallest window that must be sorted in order for the entire array to be sorted. For example, given [3, 7, 5, 6, 9], you should return (1, 3). | ||
Given an array of integers out of order, determine the bounds of the smallest window | ||
that must be sorted in order for the entire array to be sorted. For example, given | ||
[3, 7, 5, 6, 9], you should return (1, 3). | ||
""" | ||
|
||
|
||
def get_sort_range(arr): | ||
# sorting the array and checking if the input array requires sorting | ||
from typing import List, Tuple | ||
|
||
|
||
def get_sort_range(arr: List[int]) -> Tuple[int, int]: | ||
arr_sorted = sorted(arr) | ||
if arr_sorted == arr: | ||
return (-1, -1) | ||
|
||
return -1, -1 | ||
# getting the start and end of the unsorted part of the array | ||
start = 0 | ||
end = 0 | ||
start, end = 0, 0 | ||
for i in range(len(arr)): | ||
if arr[i] != arr_sorted[i]: | ||
start = i | ||
break | ||
for i in range(start, len(arr)): | ||
if arr[i] != arr_sorted[i]: | ||
end = i | ||
return (start, end) | ||
return start, end | ||
|
||
|
||
if __name__ == "__main__": | ||
print(get_sort_range([3, 5, 6, 7, 9])) | ||
print(get_sort_range([3, 7, 5, 6, 9])) | ||
print(get_sort_range([5, 4, 3, 2, 1])) | ||
|
||
# DRIVER CODE | ||
print(get_sort_range([3, 5, 6, 7, 9])) | ||
print(get_sort_range([3, 7, 5, 6, 9])) | ||
print(get_sort_range([5, 4, 3, 2, 1])) | ||
|
||
""" | ||
SPECS: | ||
TIME COMPLEXITY: O(n x log(n)) | ||
SPACE COMPLEXITY: O(n) | ||
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters