Programowanie konkurencyjneCiężko

Maksymalna suma różnicy bezwzględnej

Szczegółowy przewodnik i implementacja Python dla problemu „Maksymalna różnica bezwzględna sumy”.

Oświadczenie o problemie

Ciężko

Napisz funkcję max_sum_abs_diff(arr), która zwraca maksymalną sumę różnic bezwzględnych kolejnych elementów w dowolnej permutacji kołowej tablicy arr.

Ograniczenia
  • 2 <= len(arr) <= 1000
  • -10^4 <= arr[i] <= 10^4

Przykłady

Example 1
Input
max_sum_abs_diff([1, 2, 4, 8])
Output
18
Explanation

The circular permutation [1, 8, 2, 4] yields |1-8| + |8-2| + |2-4| + |4-1| = 7 + 6 + 2 + 3 = 18.

Example 2
Input
max_sum_abs_diff([10, 12])
Output
4
Explanation

The only circular permutation is [10, 12] yielding |10-12| + |12-10| = 2 + 2 = 4.

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.