152. Maximum Product Subarray dynamic programming

2024-05-07 07:58

本文主要是介绍152. Maximum Product Subarray dynamic programming,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

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.

最大成绩可能是负数与负数的乘积或者正数与正数的乘积;

int maxProduct(vector<int>& nums) {if (nums.size() == 0)return 0;int posinum = nums[0];int neginum = nums[0];int sum = nums[0];for (int i = 1; i < nums.size(); i++){int tempposi = posinum;int tempnegi = neginum;posinum = max(nums[i], max(tempposi * nums[i], tempnegi * nums[i]));neginum = min(nums[i], min(tempposi * nums[i], tempnegi * nums[i]));sum = max(sum, posinum);}return sum;
}

这篇关于152. Maximum Product Subarray dynamic programming的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/966774

相关文章

ural 1014. Product of Digits贪心

1014. Product of Digits Time limit: 1.0 second Memory limit: 64 MB Your task is to find the minimal positive integer number  Q so that the product of digits of  Q is exactly equal to  N. Inpu

论文精读-Supervised Raw Video Denoising with a Benchmark Dataset on Dynamic Scenes

论文精读-Supervised Raw Video Denoising with a Benchmark Dataset on Dynamic Scenes 优势 1、构建了一个用于监督原始视频去噪的基准数据集。为了多次捕捉瞬间,我们手动为对象s创建运动。在高ISO模式下捕获每一时刻的噪声帧,并通过对多个噪声帧进行平均得到相应的干净帧。 2、有效的原始视频去噪网络(RViDeNet),通过探

leetcode#628. Maximum Product of Three Numbers

题目 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 Note: The lengt

如何将Product依赖的LibraryModule导出成jar

在Android Studio新建Module时可以选择创建的module是工程module还是Android Library。 或者可以在工程module中的build.gradle文件中将 apply plugin: 'com.android.application'改为apply plugin: 'com.android.library' 同时将applicati

Leetcode 152. 乘积最大子数组(Medium)

给你一个整数数组 nums ,请你找出数组中乘积最大的非空连续  子数组 (该子数组中至少包含一个数字),并返回该子数组所对应的乘积。 测试用例的答案是一个 32-位 整数。 示例 1: 输入: nums = [2,3,-2,4]输出: 6解释: 子数组 [2,3] 有最大乘积 6。 示例 2: 输入: nums = [-2,0,-1]输出: 0解释: 结果不能为 2,

Maximum likelihood function maximizes what thing?

最大似然函数(Maximum Likelihood Function)最大化的是数据在给定参数下出现的概率。具体来说,它最大化的是似然函数(Likelihood Function),即给定参数 ( \theta ) 下观测数据的概率。在统计学中,似然函数 ( L(\theta) ) 通常定义为所有独立观测数据点概率的乘积,对于参数 ( \theta ) 的函数。 对于一组独立同分布的观测数据

【硬刚ES】ES基础(十三)Dynamic Template和Index Template

本文是对《【硬刚大数据之学习路线篇】从零到大数据专家的学习指南(全面升级版)》的ES部分补充。

ORA-24067: exceeded maximum number of subscribers for queue ADMIN.SMS_MT_QUEUE

临时处理办法: delete from aq$_ss_MT_tab_D;delete from aq$_ss_MT_tab_g;delete from aq$_ss_MT_tab_h;delete from aq$_ss_MT_tab_i;delete from aq$_ss_MT_tab_p;delete from aq$_ss_MT_tab_s;delete from aq$

[LeetCode] 239. Sliding Window Maximum

题:https://leetcode.com/problems/sliding-window-maximum/description/ 题目 Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You

[LeetCode] 238. Product of Array Except Self

题:https://leetcode.com/problems/product-of-array-except-self/description/ 题目 Given an array nums of n integers where n > 1, return an array output such that output[i] is equal to the product of all