Subarray sort
Posted LilyLiya
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Subarray sort相关的知识,希望对你有一定的参考价值。
refer to : https://www.algoexpert.io/questions/Subarray%20Sort
Problem Statement
Analysis
Code
def subarraySort(array): minOutOfOrder = float("inf") maxOutOfOrder = float("-inf") for i in range(len(array)): num = array[i] if isOutOfOrder(i, num, array): minOutOfOrder = min(minOutOfOrder, num) maxOutOfOrder = max(maxOutOfOrder, num) if maxOutOfOrder == float("-inf"):# or if minOutOfOrder == float("inf"), which means line 6 never has been hit, no unsorted values return [-1,-1] #check the correct position for the smallest valur of all the out of order values, from left to right subarrayLeftIdx = 0 while minOutOfOrder >= array[subarrayLeftIdx]: subarrayLeftIdx += 1 #check the correct position for the greatest valur of all the out of order values, from right to left subarrayRightIdx = len(array) - 1 while maxOutOfOrder <= array[subarrayRightIdx]: subarrayRightIdx -= 1 return [subarrayLeftIdx, subarrayRightIdx] # compare the current value to its two adjcent values # the first value has no left value, the last one has no right value def isOutOfOrder(i, num, array): if i == 0: return num > array[i + 1] if i == len(array) - 1: return num < array[i - 1] return num > array[i + 1] or num < array[i - 1]
以上是关于Subarray sort的主要内容,如果未能解决你的问题,请参考以下文章
Educational Codeforces Round 67 D. Subarray Sorting
Codeforces1187 D. Subarray Sorting(思维,线段树)
[原创]java WEB学习笔记61:Struts2学习之路--通用标签 property,uri,param,set,push,if-else,itertor,sort,date,a标签等(代码片段