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 of the given array will be in range [3,104] and all elements are in the range [-1000, 1000].
- 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的更多相关文章
- 【Leetcode_easy】628. Maximum Product of Three Numbers
problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...
- 628. Maximum Product of Three Numbers@python
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- LeetCode 628. Maximum Product of Three Numbers (最大三数乘积)
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [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. ...
- LeetCode 628. Maximum Product of Three Numbers三个数的最大乘积 (C++)
题目: Given an integer array, find three numbers whose product is maximum and output the maximum produ ...
- [LeetCode] 628. Maximum Product of Three Numbers 三个数字的最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [Array]628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...
- [LeetCode] Maximum Product of Three Numbers 三个数字的最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
随机推荐
- SpringMvc(4-1)Spring MVC 中的 forward 和 redirect
Spring MVC 中,我们在返回逻辑视图时,框架会通过 viewResolver 来解析得到具体的 View,然后向浏览器渲染.通过配置,我们配置某个 ViewResolver 如下: <b ...
- linux ubunt 安装软件的前期准备——更新源的更换
如果是高手,请翻到页面最下方,更换更新源的总结,直接操作即可 可能会优点啰嗦,但是认真看,一定能解决问题~~希望对大家有帮助~ 最近在熟悉linux环境,自己安装了一个ubuntu虚拟机. 很多朋友问 ...
- RxJava系列4(过滤操作符)
RxJava系列1(简介) RxJava系列2(基本概念及使用介绍) RxJava系列3(转换操作符) RxJava系列4(过滤操作符) RxJava系列5(组合操作符) RxJava系列6(从微观角 ...
- 高下相倾,前后相随——iterator 与 for ... of 循环
iterator 是es6新提供的一种遍历器.本质上是一个接口,为各种不同的数据结构,提供统一的访问机制. 数据只要部署了iterator接口,便是可遍历的数据,标志是具有Symbol.iterato ...
- 1114innodb的统计信息对optimizer成本预估影响实例 CARDINALITY
转自 https://www.cnblogs.com/olinux/p/5140615.html 转自 https://yq.aliyun.com/articles/174906?spm=5176 ...
- iOS之AFSecurityPolicy
AFSecurityPolicy是AFNetworking中负责对https请求进行证书验证的模块,本文主要是要搞清楚它是如何工作的. 在介绍AFSecurityPolicy之前,我们先来了解一下ht ...
- MyBatis(2)——MyBatis 深入学习
编写日志输出环境配置文件 在开发过程中,最重要的就是在控制台查看程序输出的日志信息,在这里我们选择使用 log4j 工具来输出: 准备工作: 将[MyBatis]文件夹下[lib]中的 log4j 开 ...
- [原创]手把手教你写网络爬虫(4):Scrapy入门
手把手教你写网络爬虫(4) 作者:拓海 摘要:从零开始写爬虫,初学者的速成指南! 封面: 上期我们理性的分析了为什么要学习Scrapy,理由只有一个,那就是免费,一分钱都不用花! 咦?怎么有人扔西红柿 ...
- [LeetCode] Pyramid Transition Matrix 金字塔转变矩阵
We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, like ...
- 分布式版本管理工具 git常用命令
Git global setup git config --global user.name "joey" git config --global user.email " ...