-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBest_time_to_buy_Stocks_DSA_Problem_17_Array.cpp
More file actions
54 lines (48 loc) · 1.1 KB
/
Best_time_to_buy_Stocks_DSA_Problem_17_Array.cpp
File metadata and controls
54 lines (48 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
//https://leetcode.com/problems/best-time-to-buy-and-sell-stock/
#include <bits/stdc++.h>
using namespace std;
// brute force Approch
int max_profit(int arr[], int n)
{
int min_diff = INT_MIN;
for (int i = 0; i < n; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[j] - arr[i] > min_diff)
min_diff = arr[j] - arr[i];
}
}
if (min_diff < 0)
min_diff = 0;
return min_diff;
}
// Optimal Approch O(n)
int max_profit__(int prices[], int n)
{
int max_profit = INT_MIN;
int buy_day = INT_MAX;
int sold_that_day = INT_MIN;
for (int i = 0; i < n; i++)
{
if (prices[i] < buy_day)
buy_day = prices[i];
// profit if sold that day
sold_that_day = prices[i] - buy_day;
if (sold_that_day > max_profit)
{
max_profit = sold_that_day;
}
}
if (max_profit < 0)
return 0;
else
return max_profit;
}
int main()
{
int arr[] = {7, 1, 5, 3, 6, 4};
int n = sizeof(arr) / sizeof(arr[0]);
cout << max_profit__(arr, n);
return 0;
}