[LeetCode]628. Maximum Product of Three Numbers

电磁场仿真软件:Sonnet_(3).Sonnet用户界面与操作 Sonnet支持用户通过二次开发自定义工具,例如:编写Python脚本,实现自定义功能。将脚本保存为工具插件。在Sonnet中加载并使用自定义工具。# 示例:自定义工具 import sonnet def custom_tool(design , object , parameter , value) : """自定义工具示例,设置对象的指定参数。 阅读详情

[LeetCode]628. Maximum Product of Three Numbers

题目描述

这里写图片描述

思路

简单的做法,扫描一次即可,维护两个最小值和三个最大值

看到这个题目,想起一道面试题,一个无法载入内存的数组,从中选n个最大的数
解法,建立一个小根堆,限定元素个数为n,每次插入一个新元素后弹出堆顶元素,扫描一次,堆中的n个元素即为n个最大的数。

这道题思路可以借鉴这种方式,顺便回顾一下调整堆的方法
维护n = 3的小根堆 和 n = 2 的大根堆
最后将小根堆使用大根堆调整,这样最大的元素就是堆顶元素

详见代码

代码

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

class Solution {
public:
    void print(vector<int> nums) {
        for (int num : nums)
            cout << num << ' ';
        cout << endl;
    }

    int maximumProduct(vector<int>& nums) {
        vector<int> maxHeap, minHeap;
        for (int i = 0; i < nums.size(); ++i) {
            if (i < 3)
                minHeap.push_back(nums[i]);
            else {
                minHeap.push_back(nums[i]);
                adjustMinHeap(minHeap, 0);
                swap(minHeap[0], minHeap[3]);
                minHeap.pop_back();
            }
            if (i < 2)
                maxHeap.push_back(nums[i]);
            else {
                maxHeap.push_back(nums[i]);
                adjustMaxHeap(maxHeap, 0);
                swap(maxHeap[0], maxHeap[2]);
                maxHeap.pop_back();
            }
        }
        adjustMaxHeap(minHeap, 0);
        print(minHeap);
        print(maxHeap);
        return max(minHeap[0] * minHeap[1] * minHeap[2], maxHeap[0] * maxHeap[1] * minHeap[0]);
    }

    void adjustMaxHeap(vector<int>& nums, int start) {
        int leftChild = 2 * start + 1, rightChild = 2 * start + 2;
        if (leftChild < nums.size()) {
            adjustMaxHeap(nums, leftChild);
            if (nums[start] < nums[leftChild]) {
                swap(nums[start], nums[leftChild]);
                adjustMaxHeap(nums, leftChild);
            }
        }
        if (rightChild < nums.size()) {
            adjustMaxHeap(nums, rightChild);
            if (nums[start] < nums[rightChild]) {
                swap(nums[start], nums[rightChild]);
                adjustMaxHeap(nums, rightChild);
            }
        }
    }

    void adjustMinHeap(vector<int>& nums, int start) {
        int leftChild = 2 * start + 1, rightChild = 2 * start + 2;
        if (leftChild < nums.size()) {
            adjustMinHeap(nums, leftChild);
            if (nums[start] > nums[leftChild]) {
                swap(nums[start], nums[leftChild]);
                adjustMinHeap(nums, leftChild);
            }
        }
        if (rightChild < nums.size()) {
            adjustMinHeap(nums, rightChild);
            if (nums[start] > nums[rightChild]) {
                swap(nums[start], nums[rightChild]);
                adjustMinHeap(nums, rightChild);
            }
        }
    }
};

int main() {
    vector<int> nums = {9,1,5,6,7,2};
    Solution s;

    cout << s.maximumProduct(nums) << endl;

    system("pause");
    return 0;
}
9.16华为OD机试真题 新系统 - 矩阵螺旋遍历 (Java/Py/C/C++/Js/Go) 阅读详情

相关推荐

arcgis根据矢量范围统计栅格数据众数、最大值、均值、中位数、最小值、少数等

像元统计数据 根据多个栅格数据计算每个像元的统计数据。 可用的统计数据有:众数、最大值、均值、中位数、最小值、少数、范围、标准差、总和及变异度。 https://desktop.arcgis.com/zh-cn/arcmap/10.3/tools/spatial-analyst-toolbox/an-overview-of-the-local-tools.htm ...

GIS哼哈哈的博客 6万+

【leetcode】628.三个数的最大乘积

给你一个整型数组nums,在数组中找出由三个数组成的最大乘积,并输出这个乘积。

m0_46069937的博客 475

FreeRtos——10、全局看门狗设计

RTOS 里的看门狗不只是为了证明 CPU 还在动,而是为了证明每个业务逻辑都在按预期的节奏“呼吸”。

qq_40170041的博客 900

LeetCode刷题日记-数组-面试题 628. 三个数的最大乘积

题目描述 题目链接: https://leetcode-cn.com/problems/maximum-product-of-three-numbers/ 解题思路、 毫无疑问,肯定先对List进行排序,直接用sorted即可,之后List就是按升序排序后的列表。接下来考虑以下三种情况: 列表中全为正数,此时最大乘积等于最大的三个正数相乘,即List[-1]*List[-2]*List[-3](从列表尾部开始数三个数 如[1, 2, 3, 4]取2 * 3 * 4 列表中全为负数,此时最大乘积等于最大的三

湖人名宿Onebear的博客 236

分离与合体题解(区间DP)

题目: 经过在机房里数日的切磁,LYD从社神牛那里学会了分离与合体,出关前,杜神牛给了他一个测试 杜神牛造了个区域,它们紧邻着排成了一行,编号1-n。在这經个区域里都放着一把OI界 的金钥匙,每一把都有一定的价值,LYD当然想得到它们了。然而杜神牛规定LYD不可以一下子 把它们全部拿走,而是每次只可以拿一把。为了尽快地拿到所有的金钥匙,LYD自然就用上了刚学的分离与合体特技。 开始LYD可以选择从1~n-11~n-11~n-1的任何一个区域(记为K)进入,进入后LYD会在K区域发 生分离,从而分离为两个小L

cqbz_lipengcheng的博客 371

leetcode 628. Maximum Product of Three Numbers(三个数的最大乘积)

Given an integer array nums, find three numbers whose product is maximum and return the maximum product. Example 1: Input: nums = [1,2,3] Output: 6 Example 2: Input: nums = [1,2,3,4] Output: 24 给出一个数组,找出其中3个数,使乘积最大 思路: 假如有两个数,乘积最大的是两个最大正数的积或者两个最小负数的积 那么3个数

蓝羽飞鸟的博客 224

告别命令行!用这个开源的Web版Kettle工具,5分钟搞定数据集成任务编排

本文介绍了一款开源的Web版Kettle工具,通过直观的拖拽界面大幅提升数据集成效率,告别复杂的命令行操作。该工具基于Kettle核心引擎,支持跨平台协作、轻量化访问和降低学习曲线,特别适合业务分析师快速上手。实测显示,使用该工具后任务配置错误率下降62%,协作效率提升40%。

weixin_30267785的博客 435

LeetCode--628. 三个数的最大乘积(C++描述)

// Source : https://leetcode-cn.com/problems/maximum-product-of-three-numbers/submissions/ // Date : 2021-10-11 /****************************************************************************************** 给你一个整型数组 nums ,在数组中找出由三个数组成的最大乘积,并输出这个乘积。 示例 1: 输入:n

YGQ_qq_1435471912 305

RoboMaster机甲大师赛备赛:如何用STM32+CAN总线精准控制四个大疆3508电机?

本文详细介绍了在RoboMaster机甲大师赛中,如何利用STM32和CAN总线技术精准控制四台大疆3508电机。从硬件架构设计、多电机ID分配、数据解析到同步控制算法,全面解析了构建高响应、低延迟底盘驱动系统的关键技术,助力参赛队伍提升机器人运动性能。

weixin_28684825的博客 133

概念解析 | 事件相机的原理 (含仿真代码)

在我们熟悉的世界里,传统相机就像一个勤勉的绘画师,每隔固定的时间就为我们勾勒出一幅完整的画面。但是,大自然中有太多瞬息万变的精彩场景,这种"定时绘画"的方式往往难以捕捉。于是,一种全新的"观察者"应运而生 -事件相机(Event Camera)。事件相机,就像是一群极其敏感的"像素哨兵",它们不再按部就班地等待指令,而是主动感知周围的变化,只有在光线强度发生显著变化时才会发出信号。这种革命性的设计,让事件相机拥有了传统相机难以企及的优势:超高的时间分辨率、极低的延迟、宽广的动态范围,以及出色的能效比。

NLOS的博客 1422

《中英双解》leetcode Maximum Product of Three Numbers(三个数的最大乘积)

给你一个整型数组 nums ,在数组中找出由三个数组成的最大乘积,并输出这个乘积。 Given an integer array nums, find three numbers whose product is maximum and return the maximum product. 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/maximum-product-of-three-numbers 著作权归领扣网络所有。商业转载请联系官方授.

weixin_46272350的博客 466

如何用python自己复现worldquant brain中的回测结果

本文介绍了在Python中复现WorldQuant Brain平台上alpha 101因子1和4的回测框架构建要点。重点分析了Brain平台的策略设置:使用TOP 3000高流动性股票(可用中证1000/2000替代);delay=1表示t日信号在t+1日开盘调仓;详细解释了market/sector/industry/subindustry四级中性化方法;说明了decay参数控制持仓权重线性衰减;以及TRUNCATION参数限制单股仓位风险。文章还探讨了不同调仓时点的实现差异,为自主构建量化回测系统提供了

夏之微风的博客 734

[leetcode] 628. Maximum Product of Three Numbers

Description Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 Note: The ...

Learning from the mistakes 220

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

STM32F407中文手册

STM32f407中文手册 本参考手册面向应用开发人员,提供有关使用 STM32F405xx/07xx、STM32F415xx/17xx、 STM32F42xxx 和 STM32F43xxx 微控制器存储器与外设的完整信息。STM32F405xx/07xx、STM32F415xx/17xx、STM32F42xxx 和 STM32F43xxx 构成一个微 控制器系列,各产品具有不同的存储器大小、封装和外设。有关订购信息以及器件的机械与电气特性,请参见数据手册。有关 ARM Cortex™-M4F 内核的信息,请参见《Cortex™-M4F 技术参考手册》。

LeetCode 628. Maximum Product of Three Numbers

Maximum Product of Three Numbers 题目描述: Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2:

i逆天耗子 694

TMS_WebGMaps_1.9.8.0_Full_Source

TMS_WebGMaps_1.9.8.0_Full_Source 全源码 delphi cbuilder

LeetCode //C - 628. Maximum Product of Three Numbers

【代码】LeetCode //C - 628. Maximum Product of Three Numbers。

Made in Code 1195

ssm框架搭建

这个是一个在idea上成功跑起来的ssm框架项目,保存在这里方便以后使用直接下载

【Leetcode】628. Maximum Product of Three Numbers

628. Maximum Product of Three Numbers Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,...

蒋志碧的博客 445

628 https://leetcode-cn.com/problems/maximum-product-of-three-numbers/

class Solution { public: int maximumProduct(vector<int>& nums) { int max1 = INT_MIN ,max2= INT_MIN, max3 = INT_MIN, min1 = INT_MAX, min2 = INT_MAX; for (int n : nums) { ...

qq_34673038的博客 392

LeetCode628 Maximum Product of Three Numbers

Given an integer array, find three numbers whose product is maximum and output the maximum product. this problem is easy and have nothing to do with the subarray related problem. class Solution { public int maximumProduct(int[] nums) { if (nums

Tech in Pieces 349

LeetCode628 : Maximum Product of Three Numbers

LeetCode628 : Maximum Product of Three Numbers题目大意从所给数组中取三个数求其乘积并比较,返回最大的乘积 - Example1Input: [1,2,3] Output: 6 Example2 Input: [1,2,3,4] Output: 24 Note 1. The length of the given array will be in

Cecilia_whf的博客 520

leetcode 628. Maximum Product of Three Numbers

Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 Note:

淡然坊 362

LeetCode 628. Maximum Product of Three Numbers. C++

628. Maximum Product of Three Numbers Given an integer array, find three numbers whose product is maximum and output the maximum product. Input: [1,2,3] Output: 6 IInput: [1,2,3,4] Output: 24 解法1 分情况 这是我最开始想到的笨方法,三个数相乘无非就几种情况: 如果数组只有负数,结果一定是3负相乘; 如果数组

Hello 1418

【LeetCode 628】 Maximum Product of Three Numbers

题目描述 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] Output: 24 Note: The length o...

iCode_girl的博客 184

LeetCode628. Maximum Product of Three Numbers

三个数的最大乘积

grllery的博客 335

LeetCode 628: Maximum Product of Three Numbers

LeetCode 628: Maximum Product of Three Numbers 题目描述 给定一个整数数组,找到三个乘积最大的数字并输出最大乘积。 解题思路 不管数组中数的正负,三个乘积最大的数要么为三个最大的整数,要么两个最小的负数和最大的正数,所以题目的关键在于找出这三个数,最简单的思路就是排序了,不过这样时间复杂度就是O(NlogN)。有其他的更小的时间复杂度算法,我懒...

HakunaMatata的博客 163

leetcode628. Maximum Product of Three Numbers

628. Maximum Product of Three NumbersGiven an integer array, find three numbers whose product is maximum and output the maximum product.Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4] O

rxt2012kc的博客 1325
上一篇: [LeetCode]599. Minimum Index Sum of Two Lists
下一篇: [LeetCode]513. Find Bottom Left Tree Value
charon____
博客等级 码龄11年 3粉丝 202原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值