【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 * ...
随机推荐
- kafka----简单的脚本命令重点
kafka命令如下: kafka-shell基本命令 在节点hadoop-2,hadoop-3,hadoop-5,启动kafka 启动命令如下 kafka-server-start.sh /usr/l ...
- MapReduce编程实例
MapReduce常见编程实例集锦. WordCount单词统计 数据去重 倒排索引 1. WordCount单词统计 (1) 输入输出 输入数据: file1.csv内容 hellod world ...
- linux扩展根目录空间
转自:http://blog.chinaunix.net/uid-363820-id-2181838.html Linux用户如何扩展磁盘空间? 这里以B型VPS为例,说明磁盘空间的具体扩展方法如下: ...
- ubuntu kylin 18.04安装docker笔记
删除原有的docker应用(如果有的话): sudo apt-get remove docker docker-engine docker.io 更新一下: sudo apt-get update 下 ...
- 依赖管理系统 go modules
golang在1.11版本中引入了新的包管理工具 go mod 类似于maven包管理(多项目公用),而之前的vendor类似于node的node_modules管理(各个项目一份) 依赖信息添加到g ...
- 工作采坑札记: 4. linux指定目录使用df和du的统计结果相差很大
1. 背景 近日,线上的服务出现异常,调用服务返回的JSON格式数据不完整,导致客户端解析异常,因此记录了本次的填坑之旅(nnd)... 2. 排查过程 2.1 服务器分析 登录到服务所在linux服 ...
- flask上下文管理相关-LocalStack 对象维护栈
LocalStack 对象维护栈 模拟 import threading """ storage = { 1232: {stack:[123,456]} } " ...
- C++通过Swig跨线程回调Python代码
C++ 定义 Callback 类. PyThreadStateLock 保证垮线程调用成功: #include <Python/Python.h> class Callback { pu ...
- Mysql 索引失效场景
例如:一张USER表 有字段属性 name,age 其中name为索引 下面列举几个索引失效的情况 1. select * from USER where name=‘xzz’ or age= ...
- Reactor系列(十一)take获取
#java#reactor#take#获取# 获取Flux订阅数量 视频讲解: https://www.bilibili.com/video/av80322616/ FluxMonoTestCase. ...