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:

  1. The length of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
  2. Multiplication of any three numbers in the input won't exceed the range of 32-bit signed integer.

理很明白,逻辑没弄清.

想法是:找出 top3 最大, top2 最小, 一趟找出, 关键是逻辑.

形象一些讲就是: 孩子们长身体,老大穿不了的衣服传给老二,老二穿不了的传给老三,老三找到合适衣服直接穿上.

人家逻辑:

\(O(n)\) time, \(O(1)\) extra space.

int maximumProduct(vector<int>& A) {
int m1 = INT_MIN, m2 = INT_MIN;
int m3 = INT_MIN, s1 = INT_MAX, s2 = INT_MAX, res, i, v; //孩子们长身体,老大穿不了的衣服传给老二,老二穿不了的传给老三.
for (i = 0; i < A.size(); i++) {
v = A[i];
if (v > m1) {m3 = m2; m2 = m1; m1 = v;}
else if (v > m2) {m3 = m2; m2 = v;}
else if (v > m3) {m3 = v;} if (v < s1) {s2 = s1; s1 = v;}
else if (v < s2) {s2 = v;}
}
res = m1 * m2 * m3 > m1 * s1 * s2 ? m1 * m2 * m3 : m1 * s1 * s2;
return res;
}

628. Maximum Product of Three Numbers的更多相关文章

  1. 【Leetcode_easy】628. Maximum Product of Three Numbers

    problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...

  2. 628. Maximum Product of Three Numbers@python

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  3. LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  4. [LeetCode&Python] Problem 628. Maximum Product of Three Numbers

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  5. LeetCode 628. Maximum Product of Three Numbers三个数的最大乘积 (C++)

    题目: Given an integer array, find three numbers whose product is maximum and output the maximum produ ...

  6. [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  7. [Array]628. Maximum Product of Three Numbers

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

  8. 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)

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

  9. [LeetCode] Maximum Product of Three Numbers 三个数字的最大乘积

    Given an integer array, find three numbers whose product is maximum and output the maximum product. ...

随机推荐

  1. RSA的公钥、私钥

    一.举个例子 1.发消息 用对方的公钥给对方发消息 2.发公告 发公告的时候,用自己的私钥形成签名! 二.加密和签名 RSA的公钥.私钥是互相对应的,RSA会生成两个密钥,你可以把任何一个用于公钥,然 ...

  2. TCP/IP和HTTP协议代理

    TCP/IP协议族 TCP/IP(传输控制协议/网际协议)是用于计算机通信的一个协议族. TCP/IP协议族包括诸如Internet协议(IP).地址解析协议(ARP).互联网控制信息协议(ICMP) ...

  3. 配置ssh无密钥登陆

    ssh 无密码登录要使用公钥与私钥. linux下可以用用ssh-keygen生成公钥/私钥对,下面以CentOS为例. 有机器LxfN1(192.168.136.128),LxfN2(192.168 ...

  4. OAuth2.0学习(1-9)新浪开放平台微博认证-web应用授权(授权码方式)

    1. 引导需要授权的用户到如下地址: URL 1 https://api.weibo.com/oauth2/authorize?client_id=YOUR_CLIENT_ID&respons ...

  5. css3中的动画 @keyframes animation

    动画的运用比较重要.接下来我希望针对我自己学习遇到的问题,再总结一下这个属性的使用方法. 创建一个动画: @keyframes 动画名 {样式} 引用自己创建的动画: animation:动画名  时 ...

  6. 用js来实现那些数据结构(数组篇03)

    终于,这是有关于数组的最后一篇,下一篇会真真切切给大家带来数据结构在js中的实现方式.那么这篇文章还是得啰嗦一下数组的相关知识,因为数组真的太重要了!不要怀疑数组在JS中的重要性与实用性.这篇文章分为 ...

  7. 工作笔记 | Visual Studio 调用 Web Service

    引言 最近笔者负责ERP财务系统跟中粮集团财务公司的财务系统做对接,鉴于ERP系统中应付结算单结算量比较大,而且管理相对集中,ERP系统与中粮财务公司的支付平台系统对接,实现银企直联,将网银录入的环节 ...

  8. 告知服务器意图的http方法

    1.GET 用来获取资源,返回已有的结果 2.POST 传输实体主体,返回处理过后的结果 3.PUT 向服务器传输文件,返回是否成功的状态码 4.DELETE 删除服务器文件,返回是否成功的状态码 5 ...

  9. 【SQL.基础构建-第二节(2/4)】

    --      Tips:查询基础 --一.SELECT 语句基础-- 1.查询指定列:SELECT 关键字--语法:--SELECT <列名>, ...     -- 希望查询列的名称- ...

  10. Kotlin技术入门以及和Java对比.md

    一.Kotlin基础环境搭建 Android studio的版本大于3.0 直接支持Kotlin语法,直接创建即可; Android studio的版本小于3.0,步骤如下: 需要下载插件 插件搜索 ...