Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

How to realize Array splitting in leetcode

Shulou Source: shulou.com Published: 2022-06-02 05:28:17 09月19日 Update

In this issue, the editor will bring you about how to split arrays in leetcode. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.

I. the content of the topic

Given an integer array nums of length 2n, your task is to divide these numbers into n pairs, such as (A1, b1), (a2, b2),..., (an, bn), so that the sum of min (ai, bi) from 1 to n is maximum.

Returns the maximum sum.

Example 1:

Input: nums = [1, 4, 3, 2]

Output: 4

Explanation: all possible divisions (ignoring the order of elements) are:

1. (1,4), (2,3)-> min (1,4) + min (2,3) = 1 + 2 = 3

2. (1,3), (2,4)-> min (1,3) + min (2,4) = 1 + 2 = 3

3. (1,2), (3,4)-> min (1,2) + min (3,4) = 1 + 3 = 4

So the maximum sum is 4.

Example 2:

Input: nums = [6, 2, 6, 5, 1, 2]

Output: 9

Explanation: the optimal method is (2, 1), (2, 5), (6, 6). Min (2,1) + min (2,5) + min (6,6) = 1 + 2 + 6 = 9

Tip:

one

Tags: Array maximum content sum example analysis interpretation input output professional small and medium code task location element rich in content odd that is ideas integers Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi MySQL Linux OPPO Reno Shulou Information