Skip to content

Instantly share code, notes, and snippets.

@khpatel4991
Created March 23, 2018 01:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khpatel4991/b8933d7812bcee22816970d43ab5dd75 to your computer and use it in GitHub Desktop.
Save khpatel4991/b8933d7812bcee22816970d43ab5dd75 to your computer and use it in GitHub Desktop.
const prices = [10, 7, 5, 12, 1, 89];
const initialState = {
maxProfit: 0,
bestBuyPrice: prices[0],
};
const bestProfit$ = Rx
.Observable
.from(prices)
.scan(({ maxProfit, bestBuyPrice }, currentPrice) => {
const potentialProfit = currentPrice - bestBuyPrice;
return {
maxProfit: Math.max(maxProfit, potentialProfit),
bestBuyPrice: Math.min(currentPrice, bestBuyPrice);
}
}, initialState)
.flatMap(obj => Rx.Observable.of(obj))
bestProfit$.subscribe(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment