[Leetcode.Array]561. Array Partition I Add to List
来源:互联网 发布:电子屏编辑软件 编辑:程序博客网 时间:2024/05/22 04:41
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.
Example 1:Input: [1,4,3,2]Output: 4Explanation: n is 2, and the maximum sum of pairs is 4.
Solution
#define CATCH_CONFIG_MAIN#include "catch.hpp"#include <stdlib.h>#include <vector>#include <algorithm>using namespace std;class Solution{public: int arrayPairSum(vector<int> & nums){ int size = nums.size(); if (size==0) return 0; sort(nums.begin(), nums.end()); int res =0; for(int i=0;i<size;i+=2){ res += nums[i]; } return res; }};TEST_CASE("arrayPairSum", "[arrayPairSUm]"){ auto a = vector<int> {1, 4, 3,2}; Solution so; REQUIRE(so.arrayPairSum(a)==4);}
需要文件catch.hpp
➜ Array g++ -std=c++11 561ArrayPartition.cpp➜ Array ./a.out ===============================================================================All tests passed (1 assertion in 1 test case)
0 0
- [Leetcode.Array]561. Array Partition I Add to List
- Add to List 561. Array Partition I
- [LC][array] Add to List 561. Array Partition I
- LeetCode 561. Array Partition I
- LeetCode 561. Array Partition I
- [LeetCode]561. Array Partition I
- leetcode 561. Array Partition I
- LeetCode 561. Array Partition I
- Leetcode 561. Array Partition I
- [leetcode]561. Array Partition I
- LeetCode 561. Array Partition I
- LeetCode: 561. Array Partition I
- LeetCode 561. Array Partition I
- LeetCode: 561. Array Partition I
- LeetCode 561. Array Partition I
- LeetCode ** 561. Array Partition I
- leetcode.561.Array Partition I
- 【leetcode】561. Array Partition I
- ReentrantLock(三):公平锁与非公平锁
- rabbitmq消息和队列持久化
- 前端之工具
- HttpClient使用详解
- 100篇文章回想
- [Leetcode.Array]561. Array Partition I Add to List
- Git操作失败并提示Another git process seems to be running in this......
- poj2516(最小费最大流)
- fpga里小数乘法怎么搞?
- git使用笔记-找回误删除文件
- Web服务器之Nginx详解(理论部分)
- Unity 各端调试方法记录
- Android mvp 使用分析
- HTTP学习(一) HTTP基本知识