[LeetCode]628. Maximum Product of Three Numbers

树莓派安装snowboy 1、树莓派换源 参考链接:https://blog.csdn.net/qq_43556844/article/details/113615915 2、更新pip3 参考链接:https://blog.csdn.net/qq_43556844/article/details/113616214 3、存取麦克风 我们使用PortAudio作为音频输入/输出的跨平台支持。我们还使用sox作为快速实用程序来检查麦克风设置是否正确。 sudo apt-get install python-pyaudio python 阅读详情

[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;
}
SpringSecurity(15)——OAuth2密码模式 SpringSecurity Oauth2密码模式 阅读详情

相关推荐

Unity六边形网格A*寻路实战:从坐标转换到算法优化

寻路算法是游戏开发与AI领域的核心技术,它决定了智能体在虚拟空间中的移动决策能力。A*算法作为启发式搜索的经典实现,通过结合实际代价与预估代价,能在复杂环境中高效找到最优路径。其技术价值在于平衡了搜索效率与结果最优性,广泛应用于策略游戏、机器人导航和自动化规划等场景。在游戏开发中,六边形网格因其移动自然、邻接均衡的特性,常被用于《文明》等经典策略游戏的地图设计。本文聚焦于Unity环境下,针对尖头朝上六边形网格的A*寻路实现,深入解析了轴向坐标系的选型、邻居计算的优化策略以及地形代价的融合方法,为战棋类或策

weixin_29017445的博客 302

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

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

m0_46069937的博客 475

风控IV、AUC、KS python计算代码

df_temp:数据集,Y:是否逾期标签,feas_list:需要计算IV的 变量。2、 按月和不同Y 下的 IV计算。

暮雨听轩的专栏 1627

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

openEuler在虚拟机的网络配置

ethernet0.virtualDev 可以配置成其他类型的虚拟网络适配器。常见的选项包括:vlance - 用于模拟 AMD PCnet32 LANCE 网络适配器,支持10Mbps。e1000 - 用于模拟 Intel E1000 千兆网络适配器,支持1Gbps。e1000e - 用于模拟更现代的 Intel E1000e 千兆网络适配器。vmxnet - 用于 VMware 的自定义网络适配器,性能更好但需要安装 VMware Tools。

FHY1221的博客 2941

用PyTorch手把手搭建液态神经网络(LNN):从微分方程到无人机避障实战

本文详细介绍了如何使用PyTorch搭建液态神经网络(LNN),从微分方程基础到无人机避障实战应用。LNN通过动态时序建模和微分方程实现高效计算,特别适合多传感器数据融合场景。文章包含完整的代码实现、训练技巧及性能优化方案,帮助开发者快速掌握这一前沿技术。

uber9的博客 454

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个数

蓝羽飞鸟的博客 227

SORT算法在遮挡下总跟丢?深入OC-SORT源码,拆解其三大创新点如何解决卡尔曼滤波的顽疾

本文深入解析OC-SORT算法如何通过三大创新点解决多目标跟踪中的卡尔曼滤波顽疾。OC-SORT引入在线平滑虚拟轨迹技术(OOS)、观测中心动量匹配机制(OCM)和基于最后观测的轨迹恢复(OCR),显著提升了遮挡场景下的跟踪性能。实验数据显示,相比传统SORT算法,OC-SORT在MOT17数据集上MOTA提升5.7%,遮挡恢复成功率提高64.2%,为Multi-Object Tracking领域带来突破性进展。

weixin_42547431的博客 248

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

MCGSPro动画部件实战:用文本分段实现智能报警提示(附工程文件)

本文详细介绍了在MCGSPro组态软件中,如何利用动画部件的文本分段功能,高效实现触摸屏上的智能报警提示。通过绑定整数变量作为索引,一个部件即可动态切换多种状态信息,解决了传统方法画面臃肿、维护繁琐的痛点。文章提供了从变量定义、部件配置到逻辑联动的完整实战教程,并分享了工程文件复用与性能优化技巧。

weixin_28347369的博客 184

时间序列预测 | 决策树时间序列预测建模,单步、多步(Python)

时间序列预测 | 决策树时间序列预测建模,单步、多步(Python)

天天酷科研的博客 748

《中英双解》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的博客 470

机器翻译的评价标准:BLEU、ROUGE等指标说明及代码实现

机器翻译领域常用的自动评价指标主要包括BLEU、ROUGE、METEOR和COMET四大类。BLEU基于n-gram精确率和短句惩罚,ROUGE关注召回率,METEOR结合精确率和召回率并考虑同义词,COMET则利用预训练模型计算语义相似度。这些指标各有特点:BLEU适用于机器翻译,ROUGE对长句友好,METEOR更接近人工评分,COMET无需参考译文。代码实现上,sacrebleu、rouge-score等库提供了便捷的计算方式。新兴趋势包括基于BERT等模型的语义评估和大语言模型评估器,但传统指标仍占

数据知道的博客 9429

[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 222

怀旧版电脑GBC模拟器

再现原版GBC掌机的界面,电脑运行连游戏界面都一样大小,很怀旧,很真实,(英文版)强烈推荐,很小的文件,很难才找到的。

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

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逆天耗子 695

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

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

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

Made in Code 1197

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

【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,...

蒋志碧的博客 447

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的博客 393

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 351

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的博客 521

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 1419

【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的博客 186

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的博客 164

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的博客 1327
上一篇: [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、付费专栏及课程。

余额充值