Aquí hay dos ideas:
Primero, el método basado en la función Partición. La
función Partición puede cambiar el orden de una lista, de
modo que después de que la lista sea reemplazada, el estado de la lista sea
LISTA = LISTA1 + [A] +
LISTA2. Todos los elementos en LISTA1 no son Si es mayor que A, todos los elementos en LIST2 no son menores que A.
Por lo tanto, solo necesitamos
ordenar primero cualquier elemento en LIST (como el primer elemento) como A para ver si A es el elemento más grande en LIST.
Si A es el enésimo más grande, m = k, devuelve A.
Si A es el enésimo más grande, m> k, solo necesitamos ejecutar la función de partición en LIST2.
Si A es el enésimo más grande, m <k, solo necesitamos LISTA1 luego ejecuta la función de partición
class Solution:
def findKthLargest(self, nums: List[int], k: int) -> int:
dijida,temp,dy,xy,index = self.partition(nums)#,index
if dijida == k:
return temp
lastlen = 0
while dijida != k:
if dijida > k:
dijida,temp,dy,xy,index = self.partition(dy)
if index == 1:
return temp
elif dijida < k:
lastlen += len(dy)+1
dijida,temp,dy,xy,index = self.partition(xy)
dijida += lastlen
if index == 1:
return temp
return temp
def partition(self,inputlist):
if len(inputlist) == 1:
return 1,inputlist[0],[],[],1
temp = inputlist[0]
xiaoyulist = []
dayulist = []
length = len(inputlist)
for i in range(length-1):
if inputlist[i+1] > temp:
dayulist.append(inputlist[i+1])
else:
xiaoyulist.append(inputlist[i+1])
return len(dayulist) + 1,temp,dayulist,xiaoyulist,0
El segundo método basado en el montón, que se agregará