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. api-gateway实践(13)新服务网关 - 断路保护/熔断机制

    参考链接:SpringCloud的Hystrix(五) Hystrix机制 新需求列表 1.在线测试 根据定义,生成输入界面, 点击测试, 验证参数,发起调用,返回执行结果 2.熔断保护 两个实现类: ...

  2. 关于CheckStyle在eclipse出现的问题

    今天在公司换了一个CheckStyle xml文件.那么我尝试直接import进去新的文件. 在我Check code的时候就爆了下面的错误 o: Failed during checkstyle c ...

  3. .NET Core 2.1 Preview 2发布 - April 10, 2018

    我们今天宣布发布 .NET Core 2.1 Preview 2.这也是我们在接下来的两到三个月内接近最终发布的版本,该版本现已准备好进行广泛的测试.我们希望您有任何反馈意见. ASP.NET Cor ...

  4. FreeMarker的用法

    freemark就是一个对静态页面上的标签进行动态解析.填充数据的一个框架. 语法(转:http://zhuyuehua.iteye.com/blog/1975251):  1. freemarker ...

  5. [论文阅读]VERY DEEP CONVOLUTIONAL NETWORKS FOR LARGE-SCALE IMAGE RECOGNITION(VGGNet)

    VGGNet由牛津大学的视觉几何组(Visual Geometry Group)提出,是ILSVRC-2014中定位任务第一名和分类任务第二名.本文的主要贡献点就是使用小的卷积核(3x3)来增加网络的 ...

  6. html标记语言 --框架

    html标记语言 --框架 六.框架 1.什么是框架 框架将浏览器划分成不同的部分,每一部分加载不同的网页 实现同一浏览器窗口中加载多个页面的效果. 语法格式<frameset>..... ...

  7. Python进阶_mysql(1)

    什么是数据库? 数据库(Database)是按照数据结构来组织.存储和管理数据的仓库,每个数据库都有一个或多个不同的API用于创建,访问,管理,搜索和复制所保存的数据. 进入mysql (linux ...

  8. ASP.NET Core + Docker + Jenkins + gogs + CentOS 从零开始搭建持续集成

    为什么不用gitlab? 没有采用gitlab,因为gitlab比较吃配置,至少得2核4G的配置.采用go语言开发的gogs来代替,搭建方便(不到10分钟就能安装完成),资源消耗低,功能也比较强大,也 ...

  9. Mysql查询小作业

    数据准备drop table if exists class;create table class(    class_no int(2) unsigned zerofill primary key ...

  10. Ajax/XHR/HTTP/jQuery Ajax

    Ajax即通过XHR API使用js发起的异步网络请求,它不会导致页面刷新,因此是现代Web App的关键技术. HTTP协议是Web开发中最重要的网络协议,HTTP协议详细规定了请求和响应报文. 请 ...