Compare commits
	
		
			1 Commits
		
	
	
		
	
	| Author | SHA1 | Date | |
|---|---|---|---|
| e271940ba9 | 
@@ -1,20 +1,56 @@
 | 
				
			|||||||
import java.util.*;
 | 
					import java.util.*;
 | 
				
			||||||
import java.io.*;
 | 
					import java.util.stream.IntStream;
 | 
				
			||||||
import java.math.*;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
/**
 | 
					 | 
				
			||||||
 * Template code to help you parse the standard input 
 | 
					 | 
				
			||||||
 * according to the problem statement.
 | 
					 | 
				
			||||||
 **/
 | 
					 | 
				
			||||||
class Solution {
 | 
					class Solution {
 | 
				
			||||||
	
 | 
						
 | 
				
			||||||
	public static void main( String[] args ) {
 | 
						public static void main( String[] args ) {
 | 
				
			||||||
		Scanner in = new Scanner( System.in );
 | 
					 | 
				
			||||||
		// read values with in.next...() methods
 | 
							// read values with in.next...() methods
 | 
				
			||||||
		
 | 
							var scanner = new Scanner( System.in );
 | 
				
			||||||
		// code your solution here
 | 
							var sellCount = scanner.nextInt(); 
 | 
				
			||||||
 | 
							var prices = scanner.tokens()
 | 
				
			||||||
 | 
									.mapToInt( Integer::valueOf )
 | 
				
			||||||
 | 
									.toArray();
 | 
				
			||||||
		
 | 
							
 | 
				
			||||||
		// Write result with System.out.println()
 | 
							// Write result with System.out.println()
 | 
				
			||||||
		System.out.println( "value" );
 | 
							System.out.println( maxProfits( sellCount, prices ));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						// code your solution here
 | 
				
			||||||
 | 
						// 0 - 1 | ... | x-sellCount - x  -->  0 - x-1 | ... | x-1 - x
 | 
				
			||||||
 | 
						private static int maxProfits( int sellCount, int[] prices ) {
 | 
				
			||||||
 | 
							var maxProfit = 0;
 | 
				
			||||||
 | 
							for ( int sale = 0; sale < sellCount; sale++ ) {
 | 
				
			||||||
 | 
								int max = 0;
 | 
				
			||||||
 | 
								// recursion end condition
 | 
				
			||||||
 | 
								if ( sellCount == 1 ) {  
 | 
				
			||||||
 | 
									max = maxProfit( prices );
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								else {
 | 
				
			||||||
 | 
									for ( int index = 2; index < prices.length; index++ ) {
 | 
				
			||||||
 | 
										var firstRange = Arrays.copyOfRange( prices, 0, index );
 | 
				
			||||||
 | 
										final int max1 = maxProfit( firstRange );
 | 
				
			||||||
 | 
										
 | 
				
			||||||
 | 
										var secondRange = Arrays.copyOfRange( prices, index, prices.length );
 | 
				
			||||||
 | 
										// reduce range sizes recursively
 | 
				
			||||||
 | 
										final int max2 = maxProfits( sellCount - 1, secondRange );
 | 
				
			||||||
 | 
										
 | 
				
			||||||
 | 
										max = Math.max( max, max1 + max2 );
 | 
				
			||||||
 | 
									}
 | 
				
			||||||
 | 
								}
 | 
				
			||||||
 | 
								maxProfit = Math.max( maxProfit, max );
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return maxProfit;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						
 | 
				
			||||||
 | 
						private static int maxProfit( int[] prices ) {
 | 
				
			||||||
 | 
							var maxProfit = 0;
 | 
				
			||||||
 | 
							for ( int index = 0; index < prices.length; index++ ) {
 | 
				
			||||||
 | 
								var range = Arrays.copyOfRange( prices, Math.min( index + 1, prices.length - 1 ), prices.length );
 | 
				
			||||||
 | 
								int max = IntStream.of( range ).max().orElse( 0 );
 | 
				
			||||||
 | 
								// difference between current price and future maximum
 | 
				
			||||||
 | 
								maxProfit = Math.max( maxProfit, max - prices[index] );
 | 
				
			||||||
 | 
							}
 | 
				
			||||||
 | 
							return maxProfit;
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user