[LeetCode] 628. Maximum Product of Three Numbers_Easy
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.
这个题目就是需要注意负数的情况即可, 我们如果用排序的话, 比较max(nums[0]*nums[1]*nums[-1], nums[-1]*nums[-2]*nums[-3])即可. T: O(nlgn)
要T: O(n) 的话就需要得到min1, min2, max1, max2, max3同理比较max(min1 * min2 * max1, max1 * max2 * max3)即可.
Code
1) T: O(nlgn)
class Solution:
def maxProduct3nums(self, nums):
nums.sort()
return max(nums[0]*nums[1]*nums[-1], nums[-1]*nums[-2]*nums[-3])
2) T: O(n)
class Solution:
def maxProduct3nums(self, nums):
ans = [1001]*2 + [-1001]*3 # min1, min2, max1, max2, max3
for num in nums:
if num > ans[-1]:
ans[-1], ans[-2], ans[-3] = num, ans[-1], ans[-2]
elif num > ans[-2]:
ans[-2], ans[-3] = num, ans[-2]
elif num > ans[-3]:
ans[-3] = num
if num < ans[0]:
ans[0], ans[1] = num, ans[0]
elif num < ans[1]:
ans[1] = num
return max(ans[0]*ans[1]*ans[-1], ans[-1]*ans[-2]*ans[-3])
[LeetCode] 628. Maximum Product of Three Numbers_Easy的更多相关文章
- [LeetCode] 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 (最大三数乘积)
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_easy】628. Maximum Product of Three Numbers
problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...
- 【LeetCode】628. Maximum Product of Three Numbers 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...
- [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] 152. Maximum Product Subarray_Medium tag: Dynamic Programming
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积
Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...
- 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray
题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...
随机推荐
- Delphi之Code Explorer
Code Explorer(代码浏览器)是Delphi IDE的特性之一,它大受用户的欢迎.正如其名所表示,Code Explorer用于快速浏览源代码单元.Code Explorer通常位于Code ...
- * 和-> 优先级
(Apple *)pf->peel(); 则报错说 ct.cpp: In function ‘int main()’:ct.cpp:48: 错误:void 值未如预期地被忽略 ...
- Matlab当中size() length()等函数讲解
在Matlab中: size:获取数组的行数和列数 length:数组长度(即行数或列数中的较大值) numel:元素总数. s=size(A): 当只有一个输出参数时,返回一个行向量,该行向量的第一 ...
- Sencha Touch 实战开发培训 视频教程 第二期 第七节
2014.4.21 晚上8:20左右开课. 本节课视频耗时比较短,不过期间意外情况比较多,录制时间偏长了点. 本期培训一共八节,前两节免费,后面的课程需要付费才可以观看. 本节内容: 视频的录制播放 ...
- FAX modem和传真协议简介
FAX就是传真,传真通信是使用传真机,借助公用通信网或其他通信线路传送图片,文字等信息,并在接收方获得发送原件系统的副本的一种通信方式.传真通信是现代图像通信的重要组成部分,它是目前采用公用电话网传送 ...
- C#取得Web程序和非Web程序的根目录的N种取法
取得控制台应用程序的根目录方法方法1.Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径方法2.AppDomain.CurrentDomain.BaseDi ...
- mysql rowid实现
) a, b b表是数据表
- yii---where or该如何使用
今天调试YII项目的时候,遇到一个奇葩的事儿,在调试 where or 查询的时候:调试语句是这样: $str = static::find()->where(['or','username' ...
- thinkphp---模糊查询详解
最近做项目,在做搜索模块的时候,模糊查询肯定少不了. 今天就详细的看一下模糊查询: $where['title'] = array('like','%'.$words.'%'); $where['ti ...
- Hive FUNCTIONS函数
hive> SHOW FUNCTIONS; ! != % & * + - / < <= <=> <> = == > >= ^ abs ac ...