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. [ Educational Codeforces Round 65 (Rated for Div. 2)][二分]

    https://codeforc.es/contest/1167/problem/E E. Range Deleting time limit per test 2 seconds memory li ...

  2. [Dart] splitMapJoin

    var str3 = '''Multi Line String'''; print( str3.splitMapJoin( RegExp(r'^', multiLine: true), // Matc ...

  3. hive on spark (spark2.0.0 hive2.3.3)

    hive on spark真的很折腾人啊!!!!!!! 一.软件准备阶段 maven3.3.9 spark2.0.0 hive2.3.3 hadoop2.7.6 二.下载源码spark2.0.0,编译 ...

  4. /etc/sudoers

    Defaults !visiblepw Defaults always_set_home Defaults match_group_by_gid Defaults always_query_group ...

  5. vim命令整理

    最近使用vim比较多,整理一下!

  6. P3119 [USACO15JAN]草鉴定

    约翰有n块草场,编号1到n,这些草场由若干条单行道相连.奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草. 贝西总是从1号草场出发,最后回到1号草场.她想经过尽可能多的草场,贝西在通一个草 ...

  7. windows7上启动jmeter报错,寻求解决办法?

    背景: 已安装jdk 12,已配置环境变量,点击jmeter.bat 或者进入cmd启动jmter都无法启动 如图: 情况1.在cmd模式下报错 情况2: 打开运行,输入“powershell ise ...

  8. SCHED_FIFO与SCHED_OTHER调度机制

    疑问 两个线程分别有不同的调度策略,一个SCHED_FIFO,一个SCHED_OTHER,按照之前的理解,SCHED_FIFO实时线程一定会占用CPU一直运行,导致SCHED_OTHER的普通线程得不 ...

  9. Thingsboard学习之一CentOS安装系统更新

    首先安装好系统,查询到系统的IP地址后,使用Putty登入系统 更新系统 yum update 安装git yum install git 动图演示

  10. java JSON的使用和解析

    There is no royal road to learning. 博主:JavaPanda https://www.cnblogs.com/LearnAndGet/p/10009646.html ...