LeetCode: Jump Game Total 解题报告

Maximum Product Subarray

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.
Array Dynamic Programming

SOLUTION 1

使用DP来做:

因为有正负值好几种情况。所以我们计算当前节点最大值,最小值时,应该考虑前一位置的最大值,最大值几种情况。(例如:如果当前为-2, 前一个位置最小值为-6,最大值为8,那么当前最大值应该是-2*-6 = 12)

对于以index位置结尾的连续子串来说,计算最大,最小值可以三种选择:

1. 当前值* 前一位置的最大值。

2. 当前值* 前一位置的最小值。

3. 丢弃前一伴置的所有的值

我们对这三项取最大值,最小值,就可以得到当前的最大乘积,最小乘积。

 1 package Algorithms.array;
 2
 3 public class MaxProduct {
 4     public static int maxProduct(int[] A) {
 5         if (A == null || A.length == 0) {
 6             return 0;
 7         }
 8
 9         // record the max value in the last node.
10         int DMax = A[0];
11
12         // record the min value in the last node.
13         int DMin = A[0];
14
15         // This is very important, should recode the A[0] as the initial value.
16         int max = A[0];
17
18         for (int i = 1; i < A.length; i++) {
19             int n1 = DMax * A[i];
20             int n2 = DMin * A[i];
21
22             // we can select the former nodes, or just discade them.
23             DMax = Math.max(A[i], Math.max(n1, n2));
24             max = Math.max(max, DMax);
25
26             // we can select the former nodes, or just discade them.
27             DMin = Math.min(A[i], Math.min(n1, n2));
28         }
29
30         return max;
31     }
32
33     /*
34      * 作法是找到连续的正数,不断相乘即可。
35      * */
36     public static void main(String[] strs) {
37         int[] A = {2, 3, -2, 4};
38
39         System.out.println(maxProduct(A));
40     }
41 }

时间: 2024-10-27 19:59:32

LeetCode: Jump Game Total 解题报告的相关文章

LeetCode: Jump Game II 解题报告

Jump Game II Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum nu

LeetCode: Pascal&#39;s Triangle 解题报告

Pascal's Triangle Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Return [ [1], [1,1], [1,2,1], [1,3,3,1], [1,4,6,4,1] ] SOLUTION 1:很easy的题.注意记得把List加到ret中.比较简单,每一行的每一个元素有这个规律:1. 左右2边的是1.i, j 表示行,列坐标.2.

[LeetCode]Longest Valid Parentheses, 解题报告

题目 Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. For "(()", the longest valid parentheses substring is "()", which has length = 2. Another example i

【LeetCode】Insert Interval 解题报告

[题目] Given a set of non-overlapping intervals, insert a new interval into the intervals (merge if necessary). You may assume that the intervals were initially sorted according to their start times. Example 1: Given intervals [1,3],[6,9], insert and m

【LeetCode】Jump Game II 解题报告

[题目] Given an array of non-negative integers, you are initially positioned at the first index of the array. Each element in the array represents your maximum jump length at that position. Your goal is to reach the last index in the minimum number of

LeetCode: Unique Paths II 解题报告

Unique Paths II Total Accepted: 31019 Total Submissions: 110866My Submissions Question Solution Follow up for "Unique Paths": Now consider if some obstacles are added to the grids. How many unique paths would there be? An obstacle and empty spac

【LeetCode】Symmetric Tree 解题报告

Symmetric Tree 解题报告 [LeetCode] https://leetcode.com/problems/symmetric-tree/ Total Accepted: 106639 Total Submissions: 313969 Difficulty: Easy Question Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For

Leetcode 115 Distinct Subsequences 解题报告

Distinct Subsequences Total Accepted: 38466 Total Submissions: 143567My Submissions Question Solution Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence of a string is a new string which is formed from

【LeetCode】Next Permutation 解题报告

[题目] Implement next permutation, which rearranges numbers into the lexicographically next greater permutation of numbers. If such arrangement is not possible, it must rearrange it as the lowest possible order (ie, sorted in ascending order). The repl