LeetCode 152 Maximum Product Subarray (思维)
来源:互联网 发布:蒙牛微销售打卡软件 编辑:程序博客网 时间:2024/05/20 14:26
Find the contiguous subarray within an array (containing at least one number) which has the largest product.
For example, given the array [2,3,-2,4]
,
the contiguous subarray [2,3]
has the largest product = 6
.
题目链接:https://leetcode.com/problems/maximum-product-subarray/
题目分析:维护当前的最大和最小,若当前为负数,最小值乘完会变大
public class Solution { public int maxProduct(int[] nums) { int n = nums.length; int ma = nums[0], mi = nums[0], ans = nums[0]; for (int i = 1; i < n; i ++) { if (nums[i] < 0) { int tmp = mi; mi = ma; ma = tmp; } ma = Math.max(nums[i], ma * nums[i]); mi = Math.min(nums[i], mi * nums[i]); ans = Math.max(ans, ma); } return ans; }}
阅读全文
0 0
- LeetCode 152 Maximum Product Subarray (思维)
- Leetcode(152) Maximum Product Subarray
- leetcode 152: Maximum Product Subarray
- [leetcode 152] Maximum Product Subarray
- [leetcode] 152 Maximum Product Subarray
- leetcode-152 Maximum Product Subarray
- Maximum Product Subarray - LeetCode 152
- LeetCode---(152)Maximum Product Subarray
- Leetcode 152 Maximum Product Subarray
- LeetCode(152) Maximum Product Subarray
- leetcode 152: Maximum Product Subarray
- leetcode 152 Maximum Product Subarray
- [leetcode] 152 Maximum Product Subarray
- LeetCode 152: Maximum Product Subarray
- LeetCode 152 Maximum Product Subarray
- LeetCode 152 Maximum Product Subarray
- LeetCode(152) Maximum Product Subarray
- Leetcode 152 Maximum Product Subarray
- Android 自定义控件零基础(1)
- TCP/IP四层模型和OSI七层模型的概念
- Spring的理解
- Java集合框架
- 分块初学(1)
- LeetCode 152 Maximum Product Subarray (思维)
- svn客户端创建
- PCManFTP v2.0(CVE-2013-4730)漏洞分析报告
- java+mysql开发的系统,在系统中数据库备份还原的思路与实现
- Java入门--main
- 自定义控件之绘图篇:drawText()详解
- 凡
- 程序员职业生涯全攻略,附神级跳槽攻略图
- APUE第九章 进程关系