경쟁 프로그래밍쉬움

배열의 합을 최대화

'배열의 합 최대화' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

정확히 k 부정을 수행한 후 배열의 가능한 최대 합계를 반환하는 함수 maximize_sum(arr, k)을 작성하세요. 각 부정에서 요소를 선택하고 해당 요소를 음수 값으로 바꿔야 합니다. 동일한 요소를 여러 번 부정할 수 있습니다.

제약
  • 1 <= len(arr) <= 10^5
  • 1 <= k <= 10^5
  • -10^4 <= arr[i] <= 10^4

Example 1
Input
maximize_sum([-2, 0, 5, -1, 2], 4)
Output
10
Explanation

Negate -2 to 2, -1 to 1. The remaining 2 negations can be performed on 0. The array becomes [2, 0, 5, 1, 2], summing to 10.

Example 2
Input
maximize_sum([3, -1, 0, 2], 3)
Output
6
Explanation

Negate -1 to 1. The remaining 2 negations are performed on 0. Sum is 3 + 1 + 0 + 2 = 6.

Need a Hint?
세트나 힙과 같은 Greedy 관련 데이터 구조를 사용하는 것을 고려해보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

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

편집기에서 열기
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

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.