628. Maximum Product of Three Numbers@python
Given an integer array, find three numbers whose product is maximum and output the maximum product.
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.
原题地址: Maximum Product of Three Numbers
难度: Easy
题意: 找出相乘最大的三个数
思路:
因为数字有正有负,因此相乘最大的三个数分为两种情况:
(1)最大的三个正数
(2)最小的两个负数以及一个最大的正数
代码:
class Solution(object):
def maximumProduct(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
a = b = c = None
d = e = 0x7FFFFFFF
for i in range(len(nums)):
if nums[i] >= a: # 找出最大的三个数
a, b, c = nums[i], a, b
elif nums[i] >= b:
b, c = nums[i], b
elif nums[i] >= c:
c = nums[i] if nums[i] <= e: # 找出最小的两个数
d, e = e, nums[i]
elif nums[i] <= d:
d = nums[i] max_val = 0 # if a > 0 and b > 0 and c > 0:
# max_val = max(max_val, a * b * c) # if a > 0 and d < 0 and e < 0:
# max_val = max(max_val, a * d * e) # if a < 0 and b < 0 and c < 0:
# max_val = a * b * c
max_val = max(a*b*c, a*d*e)
return max_val
时间复杂度: O(n)
空间复杂度: O(1)
628. Maximum Product of Three Numbers@python的更多相关文章
- 【Leetcode_easy】628. Maximum Product of Three Numbers
problem 628. Maximum Product of Three Numbers 题意:三个数乘积的最大值: solution1: 如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其 ...
- [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 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 方法一:排序 日期 题目地址:https://lee ...
- [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. ...
- 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 ...
- [Array]628. Maximum Product of Three Numbers
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
- [LeetCode] Maximum Product of Three Numbers 三个数字的最大乘积
Given an integer array, find three numbers whose product is maximum and output the maximum product. ...
随机推荐
- MySql 长时间读数据发生超时的异常 Mysql Reader Exception TimeOut expired
mysql connector: .net var r = cmd.ExecuteReader() r.Reader() // <--长时间不停调用 Timeout expired. Th ...
- 洛谷P2473 [SCOI2008]奖励关(期望+状压)
传送门 我数学期望还是太差了…… 先考虑状压模型,设$dp[i][S]$表示第$i$轮,当前宝物状态为$S$,能获得的最大期望分数 然而这个模型有一个问题,第$i$轮不一定能达到状态$S$ 那么考虑转 ...
- IDE工具、文本编辑器的列块编辑模式
前言 有时候需要对若干列进行一样的操作,比如在前一百行数据的最前边加上一样的字符,这时候可以通过列块编辑模式来快捷地实现这个效果.在列块编辑模式下,被选定的区域内的所有字符会被替换成你之后输入的字符. ...
- [題解](最小生成樹)luogu_P1265
首先考虑最小生成树的模型,唯一不同的是第二种情形. 即“三个或三个以上的城市申请修建的公路成环” 考虑该情形,因为修路的申请是申请离它最近的城市,所以上述条件实质上为 “存在三个或三个以上的城市,他们 ...
- 应用日志获取-web系统
1 场景 应用使开发写的,但应用使部署再服务器上,而开发没有ssh登陆服务器的权限. so,开发总是请运维查日志,下载日志. so and so,运维要花很多时间帮开发去搞日志. 这是件很没意义的事, ...
- JQ Ajax 同步与异步的区别
$.ajax({ url: xml_addr, type: 'get', dataType: 'xml', timeout: 1000, //设定超时 cache: false, //禁用缓存 asy ...
- fake-useragent
在编写爬虫进行网页数据的时候,大多数情况下,需要在请求是增加请求头 python下非常好用的伪装请求头的库:fake-useragent,具体使用说明如下: 安装fake-useragent库 pip ...
- ERP实施顾问,请找准自己的定位
最近给一些实施顾问做了培训.这些实施顾问都是我们渠道伙伴中具有较高提升潜质的顾问,期待做一次集中培训,他们能够在ERP项目实施上有所突破与提升,并能够为公司的ERP项目实施工作承担更多职责,分担更多压 ...
- jsp动态图片页面基础
1. 什么是动态网页? 动态网页是指在服务器端运行的程序或者网页,它们会随不同客户.不同时间,返回不同的网页. 注意:在静态网页中插入flash ,虽然flash是在动的,但是并不是说这个网页就是动态 ...
- I/O————File对象
File文件对象 文件和文件夹都是用File代表 创建一个文件对象,(并不会有真正的文件或文件夹被创建) File f1 = new File("d:/lolfilder"); S ...