> For the complete documentation index, see [llms.txt](https://emmaguo100.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://emmaguo100.gitbook.io/leetcode/02-02-2022-121.md).

# 02/02/2022 121

Brutal force

O(n^2)

class Solution {&#x20;

public int maxProfit(int\[] prices) {&#x20;

int max = 0;&#x20;

for (int i = 0; i < prices.length; i++){&#x20;

for(int j = i+1; j < prices.length; j++){&#x20;

int profit = prices\[j] - prices\[i];&#x20;

if(profit > max)&#x20;

max = profit;&#x20;

}&#x20;

} return max;&#x20;

}&#x20;

}

class Solution {&#x20;

public int maxProfit(int\[] prices) {&#x20;

int minPrice = Integer.MAX\_VALUE;&#x20;

int max = 0;&#x20;

for(int i = 0; i < prices.length; i++){&#x20;

if(prices\[i] < minPrice) minPrice = prices\[i];&#x20;

max = Math.max(max, prices\[i] - minPrice);&#x20;

} return max;&#x20;

}&#x20;

}
