Java DP beats 90%,3ms. you can find something.


  • 0
    J
    public class Solution {
       
    	public static int maxProduct(int[] nums) {
    		int l = nums.length;
    		int max[] = new int[l];
    		int min[] = new int[l];
    		max[0] = nums[0];
    		min[0] = nums[0];
    		int product = max[0];
    		for (int i = 1; i < l; i++) {
    			max[i] = MaxinThree(nums[i], nums[i] * max[i - 1], nums[i] * min[i - 1]); // max in three
    			min[i] = MininThree(nums[i], nums[i] * max[i - 1], nums[i] * min[i - 1]); // min in three
    
    			if (max[i] > product)
    				product = max[i];
    		}
    		return product;
    	}
    
    	static int MaxinThree(int a, int b, int c) {
    		return (((a > b) ? a : b) > c) ? (a > b ? a : b) : c;
    	}
    
    	static int MininThree(int a, int b, int c) {
    		return (((a < b) ? a : b) < c) ? (a < b ? a : b) : c;
    	}
    }
    

  • 0
    T

    time limit exceeded


  • 0
    D

    @justcodeit you can write MaxinThree and MininThree in a much elegant/clear way. Like:

    max(max(a,b), c)
    min(min(a,b), c)
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.