【LeetCode】628. Maximum Product of Three Numbers
来源:互联网 发布:航信开票软件 编辑:程序博客网 时间:2024/06/05 19:39
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
题目是求三个数的最大乘积。
如果直接暴力是O(n3)时间复杂度,不可取。
仔细思考可以发现,如果数组都是正数,那么最大的乘积肯定是最大的三个数相乘。
如果考虑负数的情况,两个负数相乘为正,那么最大的值就是最小的两个负数加上最大的正数。
如果都是负数的话,最大乘积也是最大的三个数相乘。
class Solution {public: int maximumProduct(vector<int>& nums) { sort(nums.begin(),nums.end()); int n=nums.size(); int maxx=max(nums[n-1]*nums[n-2]*nums[n-3],nums[0]*nums[1]*nums[n-1]); return maxx; }};
阅读全文
0 0
- [leetcode]628. Maximum Product of Three Numbers
- leetcode#628. Maximum Product of Three Numbers
- leetcode: 628. Maximum Product of Three Numbers
- leetcode 628. Maximum Product of Three Numbers
- [LeetCode] 628. Maximum Product of Three Numbers
- [leetcode]628. Maximum Product of Three Numbers
- LeetCode 628. Maximum Product of Three Numbers
- leetcode -- 628. Maximum Product of Three Numbers
- [LeetCode]628. Maximum Product of Three Numbers
- [LeetCode]628. Maximum Product of Three Numbers
- 【LeetCode】628. Maximum Product of Three Numbers
- LeetCode 628. Maximum Product of Three Numbers
- Leetcode:628. Maximum Product of Three Numbers
- LeetCode 628. Maximum Product of Three Numbers
- 【LeetCode】628. Maximum Product of Three Numbers
- LeetCode-628. Maximum Product of Three Numbers
- leetcode 628. Maximum Product of Three Numbers
- leetcode 628:Maximum Product of Three Numbers
- 实践
- <jsp:include page="/header.jsp"></jsp:include> 引发的bug
- 快速排序
- Python浅拷贝
- Python-WSGI协议
- 【LeetCode】628. Maximum Product of Three Numbers
- java项目写yml配置文件时候需要注意的几个地方
- 联想拯救者安装固态硬盘重做系统
- 炉石兄弟免费版/破解版 0.3.1406.392
- 静态双向链表的多数组实现
- 二叉树
- Final有什么特点?
- Java图书管理系统
- 用户注册与异步刷新