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

Example 1:

  1. Input: [1,2,3]
  2. Output: 6

Example 2:

  1. Input: [1,2,3,4]
  2. 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)

  1. class Solution:
  2. def maxProduct3nums(self, nums):
  3. nums.sort()
  4. return max(nums[0]*nums[1]*nums[-1], nums[-1]*nums[-2]*nums[-3])

2) T: O(n)

  1. class Solution:
  2. def maxProduct3nums(self, nums):
  3. ans = [1001]*2 + [-1001]*3 # min1, min2, max1, max2, max3
  4. for num in nums:
  5. if num > ans[-1]:
  6. ans[-1], ans[-2], ans[-3] = num, ans[-1], ans[-2]
  7. elif num > ans[-2]:
  8. ans[-2], ans[-3] = num, ans[-2]
  9. elif num > ans[-3]:
  10. ans[-3] = num
  11. if num < ans[0]:
  12. ans[0], ans[1] = num, ans[0]
  13. elif num < ans[1]:
  14. ans[1] = num
  15. 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. JavaScript 浮点数陷阱及解法

    众所周知,JavaScript 浮点数运算时经常遇到会 0.000000001 和 0.999999999 这样奇怪的结果,如 0.1+0.2=0.30000000000000004.1-0.9=0. ...

  2. Excel中用countif和countifs统计符合条件的个数 good

    countif单条件统计个数   1 就以下表为例,统计总分大于(包含等于)400的人数. 2 在J2单元格输入公式=COUNTIF(I2:I22,">=400") 3 回车 ...

  3. flex常用兼容写法

    一般放在common.css中: .flex{ display: -webkit-box; display: -webkit-flex; display: -moz-box; display: -ms ...

  4. [转]13 Hours: The Secret Soldiers of Benghazi

    转:http://www.imfdb.org/wiki/13_Hours:_The_Secret_Soldiers_of_Benghazi The following weapons were use ...

  5. [BeiJing2011]元素[贪心+线性基]

    2460: [BeiJing2011]元素 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1245  Solved: 652[Submit][Stat ...

  6. Unity3D protobuf-net使用方式

    1.下载protobuf-net 2.创建Unity工程,创建一个Plugins文件夹,将protobuf-net解压把里面得protobuf-net放到Plugins 3.创建一个名为mcs的文本文 ...

  7. 在 NHibernate 中一切必须是 Virtual 的吗?

    原文地址:Must Everything Be Virtual With NHibernate? 老赵在博文中 我对NHibernate的感受(2):何必到处都virtual 提到这篇文章,顺便翻译一 ...

  8. iOS - ipa安装包大小优化

    在App Store上显示的下载大小和实际下载下来的大小,我们通过下表做一个对比: iPhone型号 系统 AppStore 显示大小 下载到设备大小 iPhone6 10.2.1 91.5MB 88 ...

  9. mvc 实现超时弹窗后跳转

    为了实现保持登录状态,可以用cookie来解决这一问题 假设过期时间为30分钟,校验发生在服务器,借助过滤器,可以这样写 public class PowerFilter : AuthorizeAtt ...

  10. mysql多列索引优化

    “把Where条件里面的列都建上索引”,这种说法其实是非常错误的! 这样一个查询,假设actor_id与film_id都单独建立索引 SELECT film_id , actor_id FROM sa ...