Leetcode Brush title Record [python]--561 Array Partition I

Source: Internet
Author: User

First, preface

Second, question 561 Array Partition I

Given an array of 2n integers, your task was to group these integers into n pairs of integers, say (A1, B1 ), (A2, B2), ..., (an, bn) which makes sum of min (AI, bi) for all I from 1 to n as large as possible.

Example 1:

Input: [1,4,3,2] Output:4 explanation:n is 2, and the maximum sum of pairs is 4 = min (1, 2) + min (3, 4).

Note:

  1. n is a positive integer, which are in the range of [1, 10000].
  2. All the integers in the array would be in the range of [-10000, 10000].

Three, the thinking of solving problems

1 classSolution {2      Public intArraypairsum (int[] nums) {3 Arrays.sort (nums);4         intresult = 0;5          for(inti = 0; i < nums.length; i + = 2) {6Result + =Nums[i];7         }8         returnresult; 9     }Ten}

Leetcode Brush title Record [python]--561 Array Partition I

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.