def sort_k_increasing_decreasing_array(A): sorted_subarrays = [] increasing = True start = 0 for i in range(1, len(A) + 1): if (i == len(A) or A[i] > A[i - 1]) and not increasing: sorted_subarrays.append(A[i - 1:start - 1:-1]) start = i increasing = True elif (i == len(A) or A[i] < A[i - 1]) and increasing: sorted_subarrays.append(A[start:i]) start = i increasing = False return merge_sorted_arrays(sorted_subarrays)
def sort_k_increasing_decreasing_array_pythonic(A): class Monotonic: def __init__(self): self._last = float('-inf') def __call__(self, curr): result = curr < self._last self._last = curr return result return merge_sorted_arrays([ list(group)[::-1 if is_decreasing else 1] for is_decreasing, group in itertools.groupby(A, Monotonic()) ])
def sort_k_increasing_decreasing_array(A): sorted_arrays, next_sorted_arr = [], [] direction = _INCREASING prev = float('-inf') for item in A: if continues_direction(item, prev, direction): next_sorted_arr.append(item) else: sorted_arrays.append(next_sorted_arr) next_sorted_arr = [item] direction *= 1 prev = item sorted_arrays.append(next_sorted_arr) return merge_sorted_arrays(sorted_arrays)
def sort_k_increasing_decreasing_array(A): # Decomposes A into a set of sorted arrays. sorted_subarrays = [] INCREASING, DECREASING = range(2) subarray_type = INCREASING start_idx = 0 for i in range(1, len(A) + 1): if (i == len(A) or # A is ended. Adds the last subarray. (A[i - 1] < A[i] and subarray_type == DECREASING) or (A[i - 1] >= A[i] and subarray_type == INCREASING)): sorted_subarrays.append(A[start_idx:i] if subarray_type == INCREASING else A[i - 1:start_idx - 1:-1]) start_idx = i subarray_type = (DECREASING if subarray_type == INCREASING else INCREASING) return merge_sorted_arrays(sorted_subarrays)
def sort_k_increasing_decreasing_array(A): # Decomposes A into a set of sorted arrays. sorted_subarrays = [] INCREASING, DECREASING = range(2) subarray_type = INCREASING start_idx = 0 for i in range(1, len(A) + 1): if (i == len(A) or # A is ended. Adds the last subarray. (A[i - 1] < A[i] and subarray_type == DECREASING) or (A[i - 1] >= A[i] and subarray_type == INCREASING)): sorted_subarrays.append(A[start_idx:i] if subarray_type == INCREASING else A[i - 1:start_idx - 1:-1]) start_idx = i subarray_type = (DECREASING if subarray_type == INCREASING else INCREASING) return merge_sorted_arrays(sorted_subarrays)
def sort_k_increasing_decreasing_array(A: List[int]) -> List[int]: # Decomposes A into a set of sorted arrays. sorted_subarrays = [] increasing, decreasing = range(2) subarray_type = increasing start_idx = 0 for i in range(1, len(A) + 1): if (i == len(A) or ( # A is ended. Adds the last subarray. A[i - 1] < A[i] and subarray_type == decreasing) or (A[i - 1] >= A[i] and subarray_type == increasing)): sorted_subarrays.append(A[start_idx:i] if subarray_type == increasing else A[i - 1:start_idx - 1:-1]) start_idx = i subarray_type = (decreasing if subarray_type == increasing else increasing) return merge_sorted_arrays(sorted_subarrays)
def sort_k_increasing_decreasing_array(A): INCREASING, DECREASING = 0, 1 n = len(A) inc = INCREASING sorted_arrays = [] start = 0 for i in range(1, n + 1): if (i == n) or (A[i] > A[i - 1] and inc == DECREASING) or (A[i] < A[i - 1] and inc == INCREASING): if inc == DECREASING: sorted_arrays.append(A[i - 1:start - 1:-1]) else: sorted_arrays.append(A[start:i]) inc = DECREASING start = i return sorted_arrays_merge.merge_sorted_arrays(sorted_arrays)
def sort_k_increasing_decreasing_array(A): if len(A) < 2: return A B = [] increase_flag, start = 1, 0 for i in range(len(A) - 1): if increase_flag == 1: if A[i + 1] < A[i]: B.append(A[start:i + 1]) increase_flag, start = 0, i + 1 continue if increase_flag == 0: if A[i + 1] > A[i]: B.append(list(reversed(A[start:i + 1]))) increase_flag, start = 1, i + 1 continue if increase_flag == 1: B.append(A[start:]) else: B.append(list(reversed(A[start:]))) return merge_sorted_arrays(B)
def sort_k_increasing_decreasing_array(A: List[int]) -> List[int]: # convert A to list of increasing and decreasing arrays sorted_arrays = split(A) return merge_sorted_arrays(sorted_arrays)
subarray_type = INCREASING ======= increasing, decreasing = range(2) subarray_type = increasing >>>>>>> upstream/master start_idx = 0 for i in range(1, len(A) + 1): if (i == len(A) or # A is ended. Adds the last subarray. (A[i - 1] < A[i] and subarray_type == decreasing) or (A[i - 1] >= A[i] and subarray_type == increasing)): sorted_subarrays.append(A[start_idx:i] if subarray_type == increasing else A[i - 1:start_idx - 1:-1]) start_idx = i subarray_type = (decreasing if subarray_type == increasing else increasing) return merge_sorted_arrays(sorted_subarrays) # Pythonic solution, uses a stateful object to trace the monotonic subarrays. def sort_k_increasing_decreasing_array_pythonic(A: List[int]) -> List[int]: class Monotonic: def __init__(self): self._last = float('-inf') def __call__(self, curr): result = curr < self._last self._last = curr return result return merge_sorted_arrays([ list(group)[::-1 if is_decreasing else 1]