Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
LeetCode 121 - Best time to buy and sell stock - O(n) solution
//'main' method must be in a class 'Rextester'. //openjdk version '11.0.5' import java.util.*; import java.lang.*; //LeetCode 121 - Best time to Buy and Sell Stock //input [7,1,5,3,6,2] //output - 5 (max profit) class Rextester { public static void main(String args[]) { System.out.println("Max profit is: "); int[] stock = new int[]{7,1,5,3,6,2}; System.out.println(maxProfit(stock)); } public static int maxProfit(int[] prices) { int minValue=Integer.MAX_VALUE; int maxProfit=0; for(int price:prices){ if(price>minValue) maxProfit = Math.max(maxProfit,(price-minValue)); if(price<minValue) minValue = price; } return maxProfit; } }
run
|
edit
|
history
|
help
0
練習問題
Problem_OnOff
MP8 game
kkk
Problem: on_off
2.C
Sieve of Eratosthenes Revisited
jb11.0 threads tick tock
Reverse Prime
JAVA İLE CELCIUS HESAPLAMA - Fahrenheit to Celsius with Java