problem

628. Maximum Product of Three Numbers

题意:三个数乘积的最大值;

solution1:

如果全是负数,三个负数相乘还是负数,为了让负数最大,那么其绝对值就该最小,而负数排序后绝对值小的都在末尾,所以是末尾三个数字相乘,这个跟全是正数的情况一样。那么重点在于前半段是负数,后半段是正数,那么最好的情况肯定是两个最小的负数相乘得到一个正数,然后跟一个最大的正数相乘,这样得到的肯定是最大的数,所以我们让前两个数相乘,再和数组的最后一个数字相乘,就可以得到这种情况下的最大的乘积.

class Solution {
public:
int maximumProduct(vector<int>& nums) {
//positive-negtive;
sort(nums.begin(), nums.end());
int n = nums.size();
int tmp = nums[n-]*nums[n-]*nums[n-];
return max(tmp, nums[]*nums[]*nums[n-]);
}
};

solution2:找出三个最大的或者一个最大的两个最小的,比较他们的乘积,而且是O(n)的时间复杂度

注意,求解数值时候的逻辑顺序。

class Solution {
public:
int maximumProduct(vector<int>& nums) {
//positive-negtive;
int max1 = INT_MIN, max2 = INT_MIN, max3 = INT_MIN;
int min1 = INT_MAX, min2 = INT_MAX;
for (auto num : nums)
{
if(num>max1)
{
max3 = max2; max2 = max1; max1 = num;
}
else if(num>max2)
{
max3 = max2; max2 = num;
}
else if(num>max3) max3 = num; if(num<min1)
{
min2 = min1; min1 = num;
}
else if(num<min2) min2 = num;
}
return max(max1*max2*max3, max1*min1*min2);
}
};

参考

1. Leetcode_easy_628. Maximum Product of Three Numbers;

2. Grandyang;

【Leetcode_easy】628. Maximum Product of Three Numbers的更多相关文章

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

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

  2. 628. Maximum Product of Three Numbers@python

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

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

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

  4. 628. Maximum Product of Three Numbers

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

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

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

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

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

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

  8. [Array]628. Maximum Product of Three Numbers

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

  9. 【LeetCode】152. Maximum Product Subarray

    题目: Find the contiguous subarray within an array (containing at least one number) which has the larg ...

随机推荐

  1. layui table 跨页记忆选择

    layui 表格功能目前默认不支持跨页记忆选择 下面来实现layui table跨页记忆选择实现 基于layui版本 1.4.5 表格跨页通用方法 //表格分页复选框 layui.define(['j ...

  2. python中的_xx, __xx, __xx__

    一.从模块分析 ########  bb.py (一个用来导入的模块) ########## var = 0_var = 1__var = 2__var__ = 3 1. from module im ...

  3. The 2019 China Collegiate Programming Contest Harbin Site I. Interesting Permutation

    链接: https://codeforces.com/gym/102394/problem/I 题意: DreamGrid has an interesting permutation of 1,2, ...

  4. [Javascript] Run asynchronous functions in sequence using reduce

    This can be handy if you have a rate limit on API requests or if you need to pass the result of each ...

  5. CSS绝对定位详解

    设置为绝对定位的元素框从文档流完全删除,并相对于其包含块定位,包含块可能是文档中的另一个元素或者是初始包含块.直线电机生产厂家 元素原先在正常文档流中所占的空间会关闭,就好像该元素原来不存在一样.元素 ...

  6. 十八.搭建Nginx服务器、配置网页认证、基于域名的虚拟主机、ssl虚拟主机

    配置要求: client:192.168.4.10 proxy:192.168.4.5(eth0) 192.168.2.5(eth1) web1:192.168.2.100 web2:192.168. ...

  7. SQL SERVER PIVOT使用

    参照这个网址介绍 http://www.cnblogs.com/lwhkdash/archive/2012/06/26/2562979.html 一般SQL Server的函数都会识别为紫色,可是PI ...

  8. PAT TOP 1005 Programming Pattern (35 分)哈希做法

    1005 Programming Pattern (35 分) Programmers often have a preference among program constructs. For ex ...

  9. (WAWAWAWAWAWAW) G. Periodic RMQ Problem

    没有联通门 : Codeforces G. Periodic RMQ Problem /* Codeforces G. Periodic RMQ Problem MMP 什么动态开点线段树啊 ... ...

  10. 【算法】变邻域搜索算法(Variable Neighborhood Search,VNS)超详细一看就懂的解析

    更多精彩尽在微信公众号[程序猿声] 变邻域搜索算法(Variable Neighborhood Search,VNS)一看就懂的解析 00 目录 局部搜索再次科普 变邻域搜索 造轮子写代码 01 局部 ...