Question

561. Array Partition I

Solution

题目大意是,给的数组大小是2n,把数组分成n组,每组2个元素,每个组取最小值,这样就能得到n个值,怎样分组才能使这n个数相加最小。

思路:有点田忌赛马的意思,肯定最大和第二大一组,取最小值即第二大的数,依次类推。。。这样就需要排序,隔一个取一个。

Java实现:

public int arrayPairSum(int[] nums) {
Arrays.sort(nums);
int total = 0;
for (int i=0; i<nums.length; i+=2) {
total += nums[i];
}
return total;
}

561. Array Partition I - LeetCode的更多相关文章

  1. Leetcode#561. Array Partition I(数组拆分 I)

    题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最 ...

  2. 561. Array Partition I【easy】

    561. Array Partition I[easy] Given an array of 2n integers, your task is to group these integers int ...

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

  4. 【LeetCode】561. Array Partition I 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序 日期 题目地址:https://leetcod ...

  5. 【LeetCode】数组-6(561)-Array Partition I(比较抽象的题目)

    题目描述:两句话发人深思啊.... Given an array of 2n integers, your task is to group these integers into n pairs o ...

  6. leetcode 561.Array Partition I-easy

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  7. LeetCode 561 Array Partition I 解题报告

    题目要求 Given an array of 2n integers, your task is to group these integers into n pairs of integer, sa ...

  8. [LeetCode] 561. Array Partition I_Easy tag: Sort

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

  9. [LeetCode&Python] Problem 561. Array Partition I

    Given an array of 2n integers, your task is to group these integers into n pairs of integer, say (a1 ...

随机推荐

  1. Numpy计算逆矩阵求解线性方程组

    对于这样的线性方程组: x + y + z = 6 2y + 5z = -4 2x + 5y - z = 27 可以表示成矩阵的形式: 用公式可以表示为:Ax=b,其中A是矩阵,x和b都是列向量 逆矩 ...

  2. Numpy求解线性方程组

    Numpy求解线性方程组 对于Ax=b,已知A和b,怎么算出x? 1. 引入包 2. 求解 验证

  3. 纯干货数学推导_傅里叶级数与傅里叶变换_Part3_周期为2L的函数展开

  4. PCB中加入任意LOGO图文说明 精心制作

    防静电图 首先我们要对下载下来的图片进行处理否则Altium designer6.9会提示装载的图片不是单色的,用Photoshop CS打开开始下载的图片 选择 图像→模式→灰度 在选择 图像→模式 ...

  5. display:inline-block两端对齐 实现列表

    做一个ul li 列表类似这样的平时经常会用到 要是用浮动做还是比较简单的直接左右浮动,清除浮动就可以搞定了,因为最近用display:inline-block用的比较顺手,所以就尝试一下.通过tex ...

  6. 从CSS盒子模型说起

    前言 总括: 对于盒子模型,BFC,IFC和外边距合并等概念和问题的总结 原文地址:从CSS盒子模型说起 知乎专栏:前端进击者 博主博客地址:Damonare的个人博客 为学之道,莫先于穷理:穷理之要 ...

  7. Amaze UI 模版中心上线丨十几款高质量优秀模版免费提供!

    Amaze UI模版中心终于上线了,目前汇聚了包含企业门户.新闻资讯.管理后台等多个领域的模版,全都可以免费下载. Amaze UI模版中心后续还会增加更多的模版以及领域,请各位持续关注. 模版中心的 ...

  8. Android地图化实现

    今天在Android上实现了地图化,可以通过记录用户位置和体温是否异常来实现地图区域变色,并显示正常人数,与体温是否异常,且可以地图下钻. 效果展示:

  9. Column ‘name’ in where clause is ambiguous;

    内容 一.异常信息 严重: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw except ...

  10. 微信h5静默和非静默授权获取用户openId和用户信息的方法和步骤:

    原文链接:https://blog.csdn.net/qq_35430000/article/details/79299529 一.openId是什么?openId是用户在当前公众号下的唯一标识('身 ...