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.

这个题目就是需要注意负数的情况即可, 我们如果用排序的话, 比较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的更多相关文章

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

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

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

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

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

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

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

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

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

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

  6. [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. ...

  7. [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 ...

  8. [LeetCode] 152. Maximum Product Subarray 求最大子数组乘积

    Given an integer array nums, find the contiguous subarray within an array (containing at least one n ...

  9. 求连续最大子序列积 - leetcode. 152 Maximum Product Subarray

    题目链接:Maximum Product Subarray solutions同步在github 题目很简单,给一个数组,求一个连续的子数组,使得数组元素之积最大.这是求连续最大子序列和的加强版,我们 ...

随机推荐

  1. Qt编写百度离线版人脸识别+比对+活体检测

    在AI技术发展迅猛的今天,很多设备都希望加上人脸识别功能,好像不加上点人脸识别功能感觉不够高大上,都往人脸识别这边靠,手机刷脸解锁,刷脸支付,刷脸开门,刷脸金融,刷脸安防,是不是以后还可以刷脸匹配男女 ...

  2. Qt编写activex控件在网页中运行

    qt能够实现的东西非常多,还可以写activex控件直接在网页中运行.参照qtdemo下的例子即可. 方案一:可执行文件下载:https://pan.baidu.com/s/14ge9ix2Ny0x7 ...

  3. 原生js--cookie操作的封装

    封装cookie的操作:查询cookie个数.查询所有cookie的键.获取cookie.设置cookie.删除cookie.清除全部cookie /** * cookieStorage */func ...

  4. Archive of all Android Studio releases / Eclipse 版本大全 / OpenJDK 各版本

    一 Android Studio 版本大全 https://developer.android.com/studio/archive.html Download Archives This is an ...

  5. kafka进阶

    1. kafka整体结构图 Kafka名词解释和工作方式 Producer :消息生产者,就是向kafka broker发消息的客户端. Consumer :消息消费者,向kafka broker取消 ...

  6. sencha touch 在线实战培训 第一期 第六节

    2014.1.11晚上8点开的课 本来计划8号晚上开课的,不过那天晚上小区电路出了问题,所以没有讲成.后面两天我又有点其他的事情,所以放到了11号来讲. 本期培训一共八节,前三堂免费,后面的课程需要付 ...

  7. window7下 cocos2dx android交叉编译环境部署小结

    上周被android交叉编译搞惨了,还好最后弄好了,写个小结以后备用吧. 步骤,1.下载cygwin的devel和shells模块 2. 2.设置环境变量 a.设置NDK_ROOT b.设置Path ...

  8. 公钥基础设施体系和EJBCA的一些概念

    最近一段时间的在公司做的事情是: 1. 为公司的一些线上系统启用https(使用nginx反向代理的方式来实现,之前的应用无需做改动) 2.为符合规则的用户颁发数字证书(自建CA来实现,目前的用途是给 ...

  9. C语言位操作--判断整数的符号

    关于衡量计算操作的方法: 当为算法统计操作的数量的时候,所有的C运算符被认为是一样的操作.中间过程不被写入随机存储器(RAM)而不被计算,当然,这种操作数的计算方法,只是作为那些接近机器指令和CPU运 ...

  10. 关于Jmeter3.0,你必须要知道的5点变化

    2016.5.18日,Apache 发布了jmeter 3.0版本,本人第一时间上去查看并下载使用了,然后群里或同事都会问有什么样变化呢?正好在网上看到一遍关于3.0的文章,但是是英文的.这里翻译一下 ...