Array split I array-partition Leetcode python

Source: Internet
Author: User

1. Topics

Given an array of length 2n , your task is to divide these numbers into n pairs, for example (A1, B1), (A2, B2), ..., (an, bn), so that the sum of min (AI, bi) from 1 to n is the largest.

Example 1:

Input: [1,4,3,2] Output: 4 Explanation: N equals 2, the maximum sum is 4 = min (1, 2) + min (3, 4).

Tips:

    1. n is a positive integer range in [1, 10000].

    2. The range of elements in the array is [-10000, 10000].

2. Solutions
Class solution (Object): Def arraypairsum (self, Nums): "" ": Type Nums:list[int]: rtype:int            "" "ListNum = List (nums) listnum.sort () sum = 0 for i in range (0, Len (listNum), 2): Sum + = Listnum[i] return sum

The principle is that, to sort first, such a small element and the smallest combination except it will not sacrifice the big. This is the biggest. This is done using the sort method in the Python list.

http://www.codeblogbt.com/archives/39689

Array split I array-partition Leetcode python

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.