算法--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.

题意:

给出一个长度为2n的数组,将他们两个一组,分为n组,求每一组中的较小值,求这些较小值相加的最大和。

输入输入样例:

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

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

Python 解:

思路:使用自带函数sorted排序,将索引为0,2,4,6....n-2的数相加(即奇数顺序的数),时间复杂度为nlog(n)

class Solution(object):
    def arrayPairSum(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        return sum(sorted(nums)[::2])
        

C++解:

思路:不懂,时间复杂度为 O(n)

语法要点:使用了vector容器,vector<int>& nums直接将 nums 数组赋值给vector容器。

vector意为向量,可以理解为数组的增强版,封装了许多对自身操作的函数。

class Solution {
public:
    int arrayPairSum(vector<int>& nums) {
        int ret = 0;
        bool flag = true;
        array<int, 20001> hashtable{ 0 };
        for (const auto n : nums) {
            ++hashtable[n + 10000];
        }
        for (int i = 0; i < 20001;) {
            if (hashtable[i] > 0) {
                if (flag) {
                    flag = false;
                    ret += (i - 10000);
                    --hashtable[i];
                } else  {
                    flag = true;
                    --hashtable[i];
                }
            } else
                ++i;
        }
        return ret;
    }
};
时间: 2024-07-30 11:24:06

算法--leetcode 561. Array Partition I的相关文章

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

题目描述 给定长度为 2n 的数组, 你的任务是将这些数分成 n 对, 例如 (a1, b1), (a2, b2), ..., (an, bn) ,使得从1 到 n 的 min(ai, bi) 总和最大. 示例 1: 输入: [1,4,3,2] 输出: 4 解释: n 等于 2, 最大总和为 4 = min(1, 2) + min(3, 4). 提示: n 是正整数,范围在 [1, 10000]. 数组中的元素范围在 [-10000, 10000]. 思路 分组之后min(ai, bi)的和最大

LeetCode 561. Array Partition I(easy难度c++)

题目: 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: 4 Expl

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. Example 1: Input: [1,4,3,2] Output: 4 Explan

Leetcode刷题记录[python]——561 Array Partition I

一.前言 二.题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. Example 1: Inp

【LeetCode】数组-6(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. 感觉题目的大致意思就是把数组分成很多个二元数组,对它

561. Array Partition I

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 14.0px "Helvetica Neue"; color: #323333 } p.p2 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px Menlo; color: #323333; background-color: #f5f5f5 } p.p3 { margin: 0.0px 0.0px 0.0px 0.0px; font: 13.0px

【LEETCODE】39、第561题 Array Partition I

package y2019.Algorithm.array; /** * @ProjectName: cutter-point * @Package: y2019.Algorithm.array * @ClassName: ArrayPairSum * @Author: xiaof * @Description: 561. Array Partition I * Given an array of 2n integers, your task is to group these integers

算法 - leetcode 292 Nim Game

算法 - leetcode 292 Nim Game  一丶题目 你和你的朋友,两个人一起玩 Nim 游戏:桌子上有一堆石头,每次你们轮流拿掉 1 - 3 块石头. 拿掉最后一块石头的人就是获胜者.你作为先手. 你们是聪明人,每一步都是最优解. 编写一个函数,来判断你是否可以在给定石头数量的情况下赢得游戏. 输入: 4 输出: false 解释: 如果堆中有 4 块石头,那么你永远不会赢得比赛: 因为无论你拿走 1 块.2 块 还是 3 块石头,最后一块石头总是会被你的朋友拿走. 二丶思路 1)

前端与算法 leetcode 28.实现 strStr()

# 前端与算法 leetcode 28.实现 strStr() 题目描述 28.移除元素 概要 这道题的意义是实现一个api,不是调api,尽管很多时候api的速度比我们写的快(今天这个我们可以做到和indexOf一样快),但我们还是要去了解api内实现的原理,在我们所熟悉的v8引擎中,indexOf使用了kmp和bm两种算法,在主串长度小于7时使用kmp,大于7的时候使用bm,bf咱就不说了哈,那个其实就是爆破算法, 提示 数据结构,kmp,bm 解析 kmp算法的核心其实就是动态规划,明确了