【leetcode】561. Array Partition I
原题:
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.
解析:
数组分割1
给一组2n个整数,分割为n组,一组2个数,使min(a,b)之和最大
Example:
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).
我的解法
很明显每组数值越接近,浪费的数值越小,所得的总和最大,我的解法如下
public class ArrayPartitionI {
public static int arrayPartitionI(int[] array) {
Arrays.sort(array);
int sum = 0;
for (int i = 0; i < array.length - 1; i += 2) {
sum += array[i];
}
return sum;
}
}
最优解法
public class Solution {
public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int result = 0;
for (int i = 0; i < nums.length; i += 2) {
result += nums[i];
}
return result;
}
}
哈哈几乎一样,后来想了一下不用length-1也可以的
【leetcode】561. Array Partition I的更多相关文章
- 【LeetCode】561. Array Partition I 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...
- 【easy】561. Array Partition I
Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...
- 【LeetCode】954. Array of Doubled Pairs 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】565. Array Nesting 解题报告(Python & C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 日期 题目地址:https://leetcode.c ...
- 【LeetCode】Rotate Array
Rotate Array Rotate an array of n elements to the right by k steps. For example, with n = 7 and k = ...
- 【leetcode】1243. Array Transformation
题目如下: Given an initial array arr, every day you produce a new array using the array of the previous ...
- 【leetcode】954. Array of Doubled Pairs
题目如下: Given an array of integers A with even length, return true if and only if it is possible to re ...
- 【leetcode】565. Array Nesting
You are given an integer array nums of length n where nums is a permutation of the numbers in the ra ...
- 【LEETCODE】39、第561题 Array Partition I
package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * ...
随机推荐
- SQL Server 高级函数汇总【转】
看到一个帖子,博主收集的很全,里面涵盖了一些常用的内置函数,特此收藏下: 原文链接:https://blog.csdn.net/wang1127248268/article/details/53406 ...
- ProbCog mlnlearn的探索
which mlnlearn: /home/panjinbo/Desktop/MLN/probcog/Toolbox/ProbCog-1.1/apps/mlnlearn ==> /home/pa ...
- 学用 ASP.Net 之 System.Collections.ArrayList 类
ArrayList 是 .Net 的动态数组. 主要成员: /* 静态方法 */ ArrayList.Adapter() //把其他 IList 对象包装为 ArrayList 使用 ArrayLis ...
- 【c++基础】二进制格式输出char类型
前言 使用CAN通信时将信号转换为char类型进行传输,要查看传输的信息是否正确需要将char类型数据以二进制格式输出: code #include <iostream> int main ...
- web布局收集整理
/*样式文件*/ .fgw-right-p{ height: 38px; line-height: 38px; margin-bottom: 20px; padding-left: 24px; spa ...
- HelloCube:IJobForEach
此示例演示了基于作业的ECS系统,该系统可旋转一对立方体. 它显示了什么? 此示例基于ForEach示例构建,并说明如何在多线程作业中执行相同的工作,而不是在主线程上执行相同的工作. 与前面的示例一样 ...
- linux日常常用命令分析
日志处理: 测试网络路由: xargs使用方法: tcpdump命令分析: dd 写入磁盘测试 cpu个数: 查看网卡流量 查看端口联通情况,临时加端口 ntpdate同步: 可以取出变动的密码: 导 ...
- [OpenCV] sift demo
运行环境:vs2012+opencv320 sift 需要的头文件为 <opencv2/xfeatures2d.hpp> #include <opencv2/opencv.hpp&g ...
- 【51nod】2589 快速讨伐
51nod 2589 快速讨伐 又是一道倒着推改变世界的题... 从后往前考虑,设\(dp[i][j]\)表示还有\(i\)个1和\(j\)个\(2\)没有填,那么填一个1的话直接转移过来 \(dp[ ...
- .net操作数据库
1. 自动生化曾的一些查询 可以自定义查询语句 2.自定义查询过程调用 USE [mydb] GO /****** Object: StoredProcedure [dbo].[procLogin] ...