Le migliori 150 intervisteFacile

Trova il minimo nell'array ordinato ruotato

Guida dettagliata e implementazione Python per il problema "Trova il minimo nell'array ordinato ruotato".

Dichiarazione del problema

Facile

Supponiamo che un array di lunghezza n ordinato in ordine crescente venga ruotato tra 1 e n volte. Ad esempio, l'array [0,1,2,4,5,6,7] potrebbe diventare [4,5,6,7,0,1,2] se venisse ruotato 4 volte.

Si noti che la rotazione di un array [a[0], a[1], ..., a[n-1]] 1 volta dà come risultato [a[n-1], a[0], a[1], ..., a[n-2]].

Dato l'array ruotato ordinato nums di elementi univoci, restituisce l'elemento minimo di questo array.

È necessario scrivere un algoritmo che venga eseguito in tempo O(log n).

Scrivi una funzione findMin(nums: List[int]) -> int.

Vincoli
  • n == len(nums)
  • 1 <= n <= 5000
  • -5000 <= nums[i] <= 5000
  • All integers in nums are unique
  • nums is sorted and rotated between 1 and n times

Esempi

Example 1
Input
nums = [3, 4, 5, 1, 2]
Output
1
Explanation

The original array was [1,2,3,4,5] rotated 3 times. The minimum is 1.

Example 2
Input
nums = [4, 5, 6, 7, 0, 1, 2]
Output
0
Explanation

The original array was [0,1,2,4,5,6,7] rotated 4 times. The minimum is 0.

Example 3
Input
nums = [11, 13, 15, 17]
Output
11
Explanation

Array is not rotated (or rotated n times). The minimum is the first element.

Need a Hint?
Prendi in considerazione l'utilizzo di strutture dati specifiche della ricerca binaria come set o heap.
Edge Cases to Watch
  • Strutture di input vuote
  • Ingressi a elemento singolo
  • Grandi limiti numerici

Pronto a risolvere?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

Apri nell'editor
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

Risorse Python consigliate

Espandi le tue conoscenze con tutorial interattivi, foglietti illustrativi e confronti di codici correlati.