MCPcopy Create free account
hub / github.com/Blankj/awesome-java-leetcode / Solution

Class Solution

src/com/blankj/easy/_0121/Solution.java:12–28  ·  view source on GitHub ↗

author: Blankj blog : http://blankj.com time : 2017/10/11 desc :

Source from the content-addressed store, hash-verified

10 * </pre>
11 */
12public class Solution {
13 public int maxProfit(int[] prices) {
14 int max = 0, minPrice = Integer.MAX_VALUE;
15 for (int i = 0; i < prices.length; ++i) {
16 if (prices[i] < minPrice) minPrice = prices[i];
17 int delta = prices[i] - minPrice;
18 if (delta > max) max = delta;
19 }
20 return max;
21 }
22
23 public static void main(String[] args) {
24 Solution solution = new Solution();
25 System.out.println(solution.maxProfit(new int[]{7, 1, 5, 3, 6, 4}));
26 System.out.println(solution.maxProfit(new int[]{7, 6, 4, 3, 1}));
27 }
28}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected