forked from code-clash/buy-and-sell-once
optimized array solution
parent
44c919f873
commit
bbe033c582
@ -1,20 +1,27 @@
|
|||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.io.*;
|
|
||||||
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 prices = new Scanner( System.in ).tokens()
|
||||||
|
.mapToInt( Integer::valueOf )
|
||||||
|
.toArray();
|
||||||
|
|
||||||
// code your solution here
|
// code your solution here
|
||||||
|
int maxProfit = 0;
|
||||||
|
for ( int index = 0; index < prices.length; index++ ) {
|
||||||
|
var currentPrice = prices[index];
|
||||||
|
|
||||||
|
for ( int rem = index + 1; rem < prices.length; rem++ ) {
|
||||||
|
var futurePrice = prices[rem];
|
||||||
|
var profit = futurePrice - currentPrice;
|
||||||
|
maxProfit = Math.max( profit, maxProfit );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Write result with System.out.println()
|
// Write result with System.out.println()
|
||||||
System.out.println( "value" );
|
System.out.println( maxProfit );
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue