[Swift]LeetCode561. 數組拆分 I | Array Partition I

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-xhbzdeph-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

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.git

Example 1:github

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 is in the range of [1, 10000].
  2. All the integers in the array will be in the range of [-10000, 10000].

給定長度爲 2n 的數組, 你的任務是將這些數分紅 n 對, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得從1 到 n 的 min(ai, bi) 總和最大。微信

示例 1:app

輸入: [1,4,3,2]

輸出: 4
解釋: n 等於 2, 最大總和爲 4 = min(1, 2) + min(3, 4).

提示:ide

  1. n 是正整數,範圍在 [1, 10000].
  2. 數組中的元素範圍在 [-10000, 10000].

128msui

 1 class Solution {
 2     func arrayPairSum(_ nums: [Int]) -> Int {
 3         //升序排序,使每一對的兩個數字儘量接近
 4         var arr:[Int] = nums.sorted(by: <)
 5         var sum:Int = 0
 6         //奇數位置(偶數索引)求和
 7         for i in stride(from: 0,to: arr.count,by: 2)
 8         {
 9             sum += arr[i]
10         }
11         return sum
12     }
13 }

192msspa

 1 class Solution {
 2     func arrayPairSum(_ nums: [Int]) -> Int {
 3         var tempSum = 0
 4         let sortedArray = nums.sorted(by: <)
 5         var tempPair = [Int]()
 6         for number in sortedArray {
 7             if tempPair.count < 1 {
 8                 tempPair.append(number)
 9             } else {
10                 if let min = tempPair.min() {
11                     tempSum += min
12                 }
13                 tempPair.removeAll()
14             }
15         }
16         return tempSum  
17     }
18 }

196mscode

 1 class Solution {
 2     func arrayPairSum(_ nums: [Int]) -> Int {
 3         var temps = nums
 4         quickSort(numbers: &temps, start: 0, end: temps.count - 1)
 5         var sum = 0
 6         for (index, num) in temps.enumerated() {
 7             if index % 2 == 0 {
 8                 sum = sum + num
 9             }
10         }
11         return sum
12     }
13     
14     func quickSort(numbers: inout [Int], start: Int, end: Int) {
15         if start > end {
16             return
17         }
18 
19         let pivot = partition(numbers: &numbers, start: start, end: end)
20 
21         quickSort(numbers: &numbers, start: start, end: pivot - 1)
22         quickSort(numbers: &numbers, start: pivot + 1, end: end)
23     }
24 
25     func partition(numbers: inout [Int], start: Int, end: Int) -> Int {
26         let pivot = numbers[start]
27         var left = start
28         var right = end
29 
30         var index = start
31         while left <= right  {
32             while right >= left {
33                 if numbers[right] < pivot {
34                     numbers[index] = numbers[right]
35                     index = right
36                     left = left + 1
37                     break
38                 }
39                 right = right - 1
40             }
41 
42             while right >= left {
43                 if numbers[left] > pivot {
44                     numbers[index] = numbers[left]
45                     index = left
46                     right = right - 1
47                     break
48                 }
49                 left = left + 1
50             }
51         }
52 
53         numbers[index] = pivot
54         return index
55     }
56 }
相關文章
相關標籤/搜索