경쟁 프로그래밍쉬움

두 개의 하위 배열로 분할

'두 개의 하위 배열로 분할' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

쉬움

n 크기의 배열 arr과 정수 k을 사용하는 함수 max_difference_subarrays(arr, k)을 작성하세요. 선택한 요소의 합계와 나머지 n-k 요소의 합계 간의 절대 차이가 최대화되도록 arr에서 k 요소의 하위 집합을 선택하고 이 최대 절대 차이를 반환합니다.

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

Example 1
Input
max_difference_subarrays([8, 4, 5, 2, 10], 2)
Output
17
Explanation

If we select the 2 smallest elements {2, 4} (sum 6), the remaining elements are {8, 5, 10} (sum 23). The difference is |23 - 6| = 17.

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

Select 3 elements (sum 3), remaining 2 sum to 2. The difference is 1.

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 리소스

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