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. ...
随机推荐
- 新概念英语(1-15)Your passports please
Is there a problem wtih the Customers officer? A:Are you Swedish? B:No. We are not. We are Danish. A ...
- CentOS7为firewalld添加开放端口
运行.停止.禁用firewalld 启动:# systemctl start firewalld 查看状态:# systemctl status firewalld 或者 firewall-cmd ...
- python之集合,深浅copy
一. 集合 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. ...
- POJ-2253 Frogger---最短路变形&&最大边的最小值
题目链接: https://vjudge.net/problem/POJ-2253 题目大意: 青蛙A想访问青蛙B,必须跳着石头过去,不幸的是,B所在的石头太远了,需要借助其他的石头,求从A到B的路径 ...
- hdu1018 Big Number---N!的位数
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1018 题目大意: 求n阶乘的位数思路: N的阶乖的位数等于LOG10(N!)=LOG10(1)+.. ...
- 深度学习中Xavier初始化
"Xavier"初始化方法是一种很有效的神经网络初始化方法,方法来源于2010年的一篇论文<Understanding the difficulty of training ...
- url的解码方式
#coding:utf-8 import urllib legal_person_string = "%E6%B3%95%E5%AE%9A%E4%BB%A3%E8%A1%A8%E4%BA%B ...
- 使用 vue-i18n 切换中英文
兼容性: 支持 Vue.js 2.x 以上版本 安装方法:(此处只演示 npm) npm install vue-i18n 使用方法: 1.在 main.js 中引入 vue-i18n (前提是要先引 ...
- [JCIP笔记](四)踩在巨人的肩上
读完第三章那些繁琐的术语和细节,头疼了整整一个星期.作者简直是苦口婆心,说得我如做梦一般.然而进入第四章,难度骤然降低,仿佛坐杭州的过山公交车突然下坡,鸟鸣花香扑面而来,看到了一片西湖美景. 从开始看 ...
- Linux 定时执行shell脚本命令之crontab
crontab可以在指定的时间执行一个shell脚本以及执行一系列Linux命令 例如:服务器管理员定时备份数据库数据.日志等 详解: 常用命令: crontab –e //修改 crontab 文件 ...