算法-array-6-ReshapetheMatrix
来源:互联网 发布:seo零基础 编辑:程序博客网 时间:2024/06/04 19:51
package leet.array;import java.util.Arrays;public class ArrayPartition1 {/*题目要求: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 = 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].【思路】1.先排序2.因为是两两一组,那么统计奇数下标就可以了。*/ public int arrayPartition(int[] nums){ int result = 0; Arrays.sort(nums); for (int i = 0; i < nums.length; i+=2) { result += nums[i]; } return result; } public static void main(String[] args){ ArrayPartition1 arrayPartition1 = new ArrayPartition1(); int[] nums = {1,3,4,20}; System.out.println(arrayPartition1.arrayPartition(nums)); }}
阅读全文
0 0
- 算法-array-6-ReshapetheMatrix
- ReshapeTheMatrix
- 算法-Array
- 算法 Merge Sorted Array
- 【Leetcode算法】- Rotate Array
- 算法:Merge Sorted Array
- 算法<Array Partition I>
- 【算法】【Greedy】Patching Array
- 算法细节系列(6):410. Split Array Largest Sum
- array, vector 排序算法(1)
- array, vector 排序算法 2
- double-array trie算法实现
- Merge Sorted Array算法详解
- Array的一些排序算法
- codewars算法题-Array.diff
- 算法题-Array random element
- 洗牌算法-Shuffle an Array
- 2017 icpc 沈阳赛区 1004.array array array(最长序列nlogn算法)
- 从日常开发说起,浅谈HTTP协议是做什么的
- js的image()循环创建,src始终为最近创建的那个解决办法(给image.onload传参)
- Javascript之Object.assign()
- 第十七章:容器深入研究
- codeforces 835C(二维前缀和)
- 算法-array-6-ReshapetheMatrix
- cmd命令行显示中文乱码
- Java学习笔记---实现文件随机读写-RandomAccessFile
- ASP.NET Web Pages – 文件
- RobotFramework 使用远程测试库
- tomcat报错:严重: The required Server component failed to start so Tomcat is unable to start. org.apache.
- js空连接点击不跳转
- CentOS6.8 SVN服务器管理多项目
- Linux下安装 Kafka