Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit c4011a4

Browse files
Max Product Problem - DP
1 parent 2943b42 commit c4011a4

File tree

1 file changed

+5
-13
lines changed

1 file changed

+5
-13
lines changed

‎src/com/leetcode/solution/MaxProduct.java‎

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.leetcode.solution;
22

3-
import java.util.Arrays;
43
import java.util.Scanner;
54

65
public class MaxProduct {
@@ -11,7 +10,7 @@ public static void main(String[] args) {
1110
int n=sc.nextInt();
1211

1312

14-
intarr[] = new int[n];
13+
int[] arr = new int[n];
1514

1615
for (int i=0;i<arr.length;i++){
1716
arr[i]=sc.nextInt();
@@ -20,27 +19,20 @@ public static void main(String[] args) {
2019

2120
MaxValueProdcut mx= new MaxValueProdcut();
2221
int result= mx.maxProduct(arr);
23-
24-
25-
System.out.println(result);
26-
27-
28-
29-
22+
System.out.println(result);
3023

3124
}
32-
3325
}
3426
class MaxValueProdcut {
3527
public int maxProduct(int[] nums) {
3628
if(nums == null || nums.length == 0){ return 0;}
3729
if(nums.length == 1) return nums[0];
3830

3931
int maxProduct = 0, currProduct = 1;
40-
for(int i = 0; i < nums.length; i++){
41-
currProduct *= nums[i];
32+
for(int num : nums) {
33+
currProduct *= num;
4234
maxProduct = Math.max(maxProduct, currProduct);
43-
if(currProduct == 0) currProduct = 1;
35+
if(currProduct == 0) currProduct = 1;
4436
}
4537

4638
currProduct = 1;

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /