动态规划(14)
来源:互联网 发布:手机淘宝怎么取消退款 编辑:程序博客网 时间:2024/06/05 07:54
原题:
/** * Created by gouthamvidyapradhan on 02/04/2017. * Find the contiguous subarray within an array (containing at least one number) which has the largest product. * <p> * For example, given the array [2,3,-2,4], * the contiguous subarray [2,3] has the largest product = 6. */
答案:
public class MaximumProductSubarray { /** * Main method * * @param args * @throws Exception */ public static void main(String[] args) throws Exception { int[] A = {2, 3, -2, 4}; System.out.println(new MaximumProductSubarray().maxProduct(A)); } public int maxProduct(int[] nums) { if (nums.length == 1) return nums[0]; int min = nums[0]; int max = nums[0]; int result = max; for (int i = 1; i < nums.length; i++) { int prevMin = min, prevMax = max; min = Math.min(nums[i], Math.min(nums[i] * prevMin, nums[i] * prevMax)); max = Math.max(nums[i], Math.max(nums[i] * prevMin, nums[i] * prevMax)); result = Math.max(result, max); } return result; }}
阅读全文
0 0
- 动态规划(14)
- 【动态规划(一)】动态规划基础
- 动态规划14
- 动态规划 14题
- 动态规划14
- 动态规划(1)
- 动态规划(2)
- 动态规划(3)
- 动态规划(4)
- HDOJ2046(动态规划)
- 动态规划(one)
- 动态规划(读书笔记)
- Pearls(动态规划)
- skiing(动态规划)
- HDU4001(动态规划)
- 作业题 (动态规划)
- Zipper(动态规划)
- 滑雪(动态规划)
- Unable to open debugger port (127.0.0.1:63777): java.net.BindException "Address already in use: JVM
- 如何把一个texture2d显示在umg上
- 动态规划(13)
- 在线安装eclipse中html/jsp/xml editor插件 eclipseeditor
- 如何在Android Studio 使用git 上传项目到github
- 动态规划(14)
- 转载:作为一个新人,怎样学习嵌入式Linux
- L4 详解centos7 emergency模式,rescue模式,linux相互登录,克隆
- vue里面ref和生命周期的理解
- 在JSP中,使用get提交方式出现乱码时,为什么要使用new String(s.getBytes("iso-8859-1"),"utf-8");?
- LWIP调试心得一----内存管理
- pyhton与pycharm安装使用!
- vtkUnicodeString::from_utf8(): not a valid UTF-8 string.
- 《阿里巴巴Java开发规约》插件使用详细指南