You
are
given
the
prices
of
a
stock
for
each
of
the
next
n
days.
You
can
buy
once
and
sell
once
and
you
want
to
maximize
your
profit.
Example
Day
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Price
70
90
40
27
69
80
13
50
35
75
51
53
56
10
15
41
Divide
and
Conquer
Algorithm:
One
of
the
following
three
must
happen:
•
You
buy
and
sell
in
the
first
n/2
days.
•
You
buy
and
sell
in
the
second
n/2
days.
•
You
buy
in
the
first
n/2
days
and
sell
in
the
second
n/2
days.
ColumbiaX
|
Machine
Learning
for
Data
Science
and
AnalyTcs:
Algorithms
Divide
and
Conquer
Algorithm
One
of
the
following
three
must
happen:
•
You
buy
and
sell
in
the
first
n/2
days.
Conquer
•
You
buy
and
sell
in
the
second
n/2
days.
Conquer
•
You
buy
in
the
first
n/2
days
and
sell
in
the
second
n/2
days.
Combine
• In
this
case,
we
find
the
minimum
of
the
first
half
and
the
maximum
in
• the
second
half
(buy
low,
sell
high).
ColumbiaX
|
Machine
Learning
for
Data
Science
and
AnalyTcs:
Algorithms
Analysis
Analysis
(similar
to
mergesort)
•
At
each
step,
we
split
our
problem
into
2
problems
of
roughly
equal
size.
• We
do
a
linear
amount
of
extra
work
to
handle
the
non-‐recursive
case
(line
9)
•
These
two
facts
imply
an
O(n
log
n)
Tme
algorithm.
ColumbiaX
|
Machine
Learning
for
Data
Science
and
AnalyTcs:
Algorithms