File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change 35
35
67|[ Add Binary] ( ./0067-add-binary.js ) |Easy|
36
36
73|[ Set Matrix Zeroes] ( ./0073-set-matrix-zeroes.js ) |Medium|
37
37
88|[ Merge Sorted Array] ( ./0088-merge-sorted-array.js ) |Easy|
38
+ 121|[ Best Time to Buy and Sell Stock] ( ./0121-best-time-to-buy-and-sell-stock.js ) |Easy|
38
39
151|[ Reverse Words in a String] ( ./0151-reverse-words-in-a-string.js ) |Medium|
39
40
152|[ Maximum Product Subarray] ( ./0152-maximum-product-subarray.js ) |Medium|
40
41
217|[ Contains Duplicate] ( ./0217-contains-duplicate.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 121. Best Time to Buy and Sell Stock
3
+ * https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
4
+ * Difficulty: Easy
5
+ *
6
+ * You are given an array prices where prices[i] is the price of a given stock
7
+ * on the ith day.
8
+ *
9
+ * You want to maximize your profit by choosing a single day to buy one stock
10
+ * and choosing a different day in the future to sell that stock.
11
+ *
12
+ * Return the maximum profit you can achieve from this transaction. If you
13
+ * cannot achieve any profit, return 0.
14
+ */
15
+
16
+ /**
17
+ * @param {number[] } prices
18
+ * @return {number }
19
+ */
20
+ var maxProfit = function ( prices ) {
21
+ let max = 0 ;
22
+
23
+ for ( let i = 0 , min = prices [ 0 ] ; i < prices . length ; i ++ ) {
24
+ min = Math . min ( min , prices [ i ] ) ;
25
+ max = Math . max ( max , prices [ i ] - min ) ;
26
+ }
27
+
28
+ return max ;
29
+ } ;
You can’t perform that action at this time.
0 commit comments