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. 【大数据系列】hadoop集群的配置

    一.hadoop的配置文件分类 1.只读类型的默认文件 core-default.xml     hdfs-default.xml    mapred-default.xml   mapred-que ...

  2. 百度前端学院js课堂作业合集+分析(更新中...)

    第一课:简陋的登录框 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  3. Python pyQt4/PyQt5 学习笔记3(绝对对位,盒布局,网格布局)

    本节研究布局管理的内容. (一)绝对对位  import sys from PyQt4 import QtGui class Example(QtGui.QWidget): def __init__( ...

  4. Android O PackageInstaller 解析

    Android O 8.0 1.src\com\android\packageinstaller\permission\mode\PermissionGroups.java @Override pub ...

  5. 部署OpenStack问题汇总(七)--解决apache启动错误"httpd:Could not reliably determine..."

    今天在调试openstack的时候,重启apache,出现以下报错: [root@hctrl log]# service httpd restart 停止 httpd:[确定] 正在启动 httpd: ...

  6. CentOS 安装apache

    yum 安装apache yum –y install httpd 设置开机启动 chkconfig --levels 235 httpd on 启动 /etc/init.d/httpd start ...

  7. Linux批量杀死进程

    杀死进程在linux中使用kill命令了,我们可以下面来给各位介绍一篇关于Linux下批量杀死进程的例子,希望此例子可以对各位同学带来帮助的哦. 批量杀死包含关键字“php-fpm”的进程. kill ...

  8. Unity3D笔记 英保通四 虚拟轴应用及键盘事件

    Input: 1.使用这个类能够读取输入管理器设置的按键,以及访问移动设备的多点触控或加速感应数据.想要读取轴向使用Input.GetAxis方法获取下列默认轴: "Horizontal&q ...

  9. Spark版本发布历史,及其各版本特性

      2016年11月5日 We are proud to announce that Apache Spark won the 2016 CloudSort Benchmark (both Dayto ...

  10. Des加密(js+java结果一致)【原创】

    des加密算法,javascript版本和java版本 目录: 1.资源文件下载 2.JavaScript文件(des.js) 3.html文件(des.html) 4.java文件(des.java ...