-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreedy-AppleStocks.swift
More file actions
81 lines (61 loc) · 2.54 KB
/
Greedy-AppleStocks.swift
File metadata and controls
81 lines (61 loc) · 2.54 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
Writing programming interview questions hasn't made me rich yet ... so I might give up and start trading Apple stocks all day instead.
First, I wanna know how much money I could have made yesterday if I'd been trading Apple stocks all day.
So I grabbed Apple's stock prices from yesterday and put them in an array called stockPrices, where:
The indices are the time (in minutes) past trade opening time, which was 9:30am local time.
The values are the price (in US dollars) of one share of Apple stock at that time.
So if the stock cost $500 at 10:30am, that means stockPrices[60] = 500.
Write an efficient function that takes stockPrices and returns the best profit I could have made from one purchase and one sale of one share of Apple stock yesterday.
For example:
let stockPrices = [10, 7, 5, 8, 11, 9]
getMaxProfit(from: stockPrices)
// returns 6 (buying for $5 and selling for $11)
*/
//Solution does not take in account some of the details mentioned by cake interview but gets close
func getMaxProfit(from stockPricesYesterday: [Int]) -> Int? {
guard stockPrices.count >= 2 else {return nil}
var minPrice:Int?
var maxPrice = 0
for num in stockPricesYesterday{
if minPrice == nil {
minPrice = num
} else {
if num < minPrice!{
minPrice = num
}
}
if num > maxPrice{
maxPrice = num
}
}
return maxPrice - minPrice!
}
let stockPrices = [10, 7, 5, 8, 11, 9]
print(getMaxProfit(from: stockPrices))
//Cake Interview Solution
unc getMaxProfit(from stockPricesYesterday: [Int]) -> Int? {
guard stockPricesYesterday.count >= 2 else {
return nil
}
// we'll greedily update minPrice and maxProfit, so we initialize
// them to the first price and the first possible profit
var minPrice = stockPricesYesterday[0]
var maxProfit = stockPricesYesterday[1] - stockPricesYesterday[0]
// start at the second (index 1) time
// we can't sell at the first time, since we must buy first,
// and we can't buy and sell at the same time!
// if we started at index 0, we'd try to buy *and* sell at time 0.
// this would give a profit of 0, which is a problem if our
// maxProfit is supposed to be *negative*--we'd return 0.
for currentPrice in stockPricesYesterday[1...] {
// see what our profit would be if we bought at the
// min price and sold at the current price
let potentialProfit = currentPrice - minPrice
// update maxProfit if we can do better
maxProfit = max(maxProfit, potentialProfit)
// update minPrice so it's always
// the lowest price we've seen so far
minPrice = min(minPrice, currentPrice)
}
return maxProfit
}