Guest User

Untitled

a guest
Mar 17th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. /**
  2. * @author Chaos
  3. * @version my solution
  4. * 561. Array Partition I
  5. * Given an array of 2n integers, your task is to group these integers into n pairs of integer, 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.
  6. *
  7. * Example 1:
  8. * Input: [1,4,3,2]
  9. *
  10. * Output: 4
  11. * Explanation: n is 2, and the maximum sum of pairs is 4 = min(1, 2) + min(3, 4).
  12. * Note:
  13. * n is a positive integer, which is in the range of [1, 10000].
  14. * All the integers in the array will be in the range of [-10000, 10000].
  15. *
  16. *
  17. * use integrated sort package, then get the sum of even number
  18. */
  19.  
  20. class Solution {
  21. public int arrayPairSum(int[] nums) {
  22. Arrays.sort(nums);
  23. int sum=0;
  24. for (int i=0; i<nums.length; i+=2){
  25. sum += nums[i];
  26. }
  27. return sum;
  28. }
  29. }
Add Comment
Please, Sign In to add comment