Programowanie konkurencyjneŚredni

Leksykograficznie najmniejsza tablica

Szczegółowy przewodnik i implementacja Python dla problemu „Leksykograficznie najmniejsza tablica”.

Oświadczenie o problemie

Średni

Napisz funkcję lexicographically_smallest(arr, k), która zwraca najmniejszą leksykograficznie tablicę, jaką można uzyskać poprzez zamianę sąsiadujących elementów najwyżej k razy. Elementy w arr są unikalne.

Ograniczenia
  • 1 <= len(arr) <= 1000
  • 0 <= k <= 10^5
  • -10^9 <= arr[i] <= 10^9

Przykłady

Example 1
Input
lexicographically_smallest([7, 6, 9, 2, 1], 3)
Output
[2, 7, 6, 9, 1]
Explanation

To get the smallest possible element 2 to the front, we swap 9 and 2, then 6 and 2, then 7 and 2. This takes exactly 3 swaps, resulting in [2, 7, 6, 9, 1].

Example 2
Input
lexicographically_smallest([2, 1, 3], 1)
Output
[1, 2, 3]
Explanation

Swap 2 and 1 (1 swap) to get [1, 2, 3].

Need a Hint?
Rozważ użycie struktur danych specyficznych dla Greedy, takich jak zestawy lub sterty.
Edge Cases to Watch
  • Puste struktury wejściowe
  • Wejścia jednoelementowe
  • Duże granice liczbowe

Gotowy do rozwiązania?

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

Otwórz w Edytorze
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

Polecane zasoby Pythona

Poszerzaj swoją wiedzę dzięki powiązanym interaktywnym samouczkom, ściągawkom i porównaniom kodów.