time complexity – Why is the maximum single sell profit by divide and conquer O(n log n)

The single sell profit problem is:

Given a list of prices on each day, find the maximum profit that could have been made by buying on one of the days and selling on a later day.

There is a solution with a single scan that is easy to implement and runs in $O(n)$. This question is not about that. It’s about the divide-and-conquer solution. Here’s my Python implementation:

def best_trade(prices: List(int)) -> int:
    def f(prices: List(int)) -> Tuple(int, int, int):
        if len(prices) < 2:
            return 0, min(prices), max(prices)
        else:
            best_left, min_left, max_left = f(prices(: len(prices) // 2))
            best_right, min_right, max_right = f(prices(len(prices) // 2 :))
            return (
                max(best_left, best_right, max_right - min_left),
                min(min_left, min_right),
                max(max_left, max_right),
            )

    return f(prices)(0) if prices else 0  # else 0 for the corner case where prices = ()

The inner recursive function returns the maximum profit, the minimum price, and the maximum price.

This recurses by breaking the problem into two subproblems, each half the size of the original problem. As far as I can tell the combination step outside the recursion is $O(1)$. I’m using max and min builtins for readability, but it just compares 3 numbers, 2 numbers and 2 numbers.

If that’s correct, the running time is $T(n) < 2 T(n/2) + O(1)$. By the master method, this implies the algorithm has time complexity $O(n)$. That’s because $a = 2$, $b = 2$ and $d = 0$, so we’re in the case where $a > b^d$, so $O(n^{log_b a}) = O(n)$.

I can’t see any flaw in the above, but I’ve read twice that this algorithm is $O(n log n)$: once when this problem is discussed in Elements of Programming Interviews in Python by Aziz, Lee and Prakash (it’s problem 5.6), and once in these PDF lecture notes by Kevin Zatloukal of UW (page 23). Both these sources say the divide and conquer solution is slower than a simple scan.

Reading between the lines in both cases, they appear to be describing a divide-and-conquer implementation without an inner function that returns the best trade directly, which requires them to call min and max on the subarrays, i.e.

def best_trade_2(prices: List(int)) -> int:
    if len(prices) < 2:
        return 0
    else:
        best_left = best_trade_2(prices(: len(prices) // 2))
        best_right = best_trade_2(prices(len(prices) // 2 :))
        return max(
            best_left,
            best_right,
            max(prices(len(prices) // 2 :)) - min(prices(: len(prices) // 2)),
        )

I can see here that the work done outside the recursion is $O(n)$ because of the max/min calls on the sublists, which implies the algorithm is $O(n log n)$.

My implementation seems fundamentally the same algorithm but it has different complexity. I feel like I’m getting work for free simply by bundling the min/max calculations into my recursion. What am I missing? Is my implementation actually different (and better)? Or is my analysis of my implementation wrong?

#14% Profit On #ardr At (binance.com)

Free Crypto Signals – https://t.me/btctradingclub

Largest Crypto Signals group on telegram in 2021 get daily 3-4 high quality Crypto signals for Binance, Bitmex, Bittrex, Binance Futures, Bybit & many other exchanges around 80-90% accuracy. All signals are provided by our Experts Team with proper take profit and proper stop loss. We also provide free Crypto trading basic training learn when buy/sell in market & Free automated Crypto trading BOT which copied all your signals in account for more join above given Telegram group

Signals result – https://t.me/premiumclubresults

#35% Profit On #arpa At (binance.com) Free Crypto Signals On Mobile App & Automated Crypto Bot

Free Crypto BOT – https:/t.me/cryptosignalalert

Largest Crypto trading group on telegram which provide Free Crypto BOT which trades automatically on your account & make daily profit. We have made good profit just by trading on BTC & ETH from our Binance BOT scalping Trades

Join our FREE Telegram channel where we provide signals with more than 90% accuracy.. We do all the hard work for you. Simply copy the signals provided by our professional traders. Follow our signals and trade. With our unbeatable accuracy, you will be able to trade like a pro. Make consistently huge profit and highly impressive returns with low-risk investments

#20% Profit On #iotx At (binance.com)

Free Crypto Signals – https://t.me/btctradingclub

FREE Automated Crypto trading BOT which copied all signals in your account with consistent profit & learn Crypto trading basic training learn when buy/sell in market make daily profit in your account for more join above given telegram group

Crypto signals on telegram for Binance, Bittrex, Binance Futures, Bybit, Bitmex & many other exchanges get daily 4-5 signals around 80-90% accuracy. All signals are provided by our Experts who are in Crypto Industry for more than 5 years. The signals are given with proper take profit and proper stop loss

Signals result – https://t.me/premiumclubresults

#15% Profit On #stpt At (binance.com)

Free Crypto Signals – https://t.me/btctradingclub

Daily 3-4 high quality Crypto signals for Binance, Bitmex, Bittrex, Binance Futures, Bybit & many other exchanges around 80-90% accuracy. All signals are provided by our Experts Team with proper take profit and proper stop loss. We also provide free Crypto trading basic training learn when buy/sell in market & Free automated Crypto trading BOT which copied all signals in your account for more join above given Telegram group

Signals result – https://t.me/premiumclubresults

90% Profit On #aion At (binance.com)

Free Crypto Signals – https://t.me/btctradingclub

BEST Crypto signals on Telegram providing signals for Binance, Bitmex, Bittrex, Binance Futures, Bybit around 80-90% accuracy. All signals are provided by our Experts Team with proper take profit and proper stop loss. We also provide free Crypto trading basic training learn when buy/sell in market & Free automated Crypto trading BOT which copied all signals in your account with consistent profit

Signals result – https://t.me/premiumclubresults

trading – Kraken. Stop Loss. Take Profit

I’m a long time day trader in other markets like Forex and futures. In the usual platforms (metatrader, ninjatrader,etc), we take for granted been able to place a market order, and at the same time a Stop Loss and a Take Profit. If market hit’s either, all positions are closed/cancelled.

I was trying to achieve the same thing in Kraken Web Trader, so:

  • I open a Market Buy Order of 2 BTC, and Set a conditional Stop loss
  • I then open 2 take profit orders, one 10 points above entry price and the other one 20 points above entry price. 1 BTC each

Now what happened is that my Stop Loss got hit, but the Take profit orders were not cancelled. In stead, when price reached those levels, it triggered a sell order for 1 BTC

I think market order + Stop Loss + Take Profit, all linked (so they cancel each other) is a must in any trading platform.

Isn’t there any way to achieve this in Kraken? How am I supossed to open an order and forget about it? Can’t be monitoring the trades all the time

#70% Profit On #snm At (binance.com)

Free Crypto BOT – https://t.me/cryptosignalalert

Daily 3-4 high quality Crypto Signals for Binance, Bittrex, Binance Futures, Kraken, Bitseven, Bitmex & other exchanges around 80-90% accuracy with consistent profit

We also provide Free Crypto Bot known as the best Crypto Scanner. It almost scans the entire market to fetch the best coin which can yield great profit consistently

All the Premium Signals are being traded by our Binance Crypto BOT automatically on Binance account with the quantity and limit set by trader. We also provide Crypto signals on your mobile app now get daily on mobile app & start trading for more join above given telegram group

50% Profit On #troy At (binance.com)

Free Crypto signals – https://t.me/btctradingclub

We are the Number# 1 Community in Telegram provide Free Crypto signals for Binance, Bittrex, Binance Futures, Bybit, Bitmex & many other exchanges around 80-90% accuracy. All signals are provided by our Experts who are in Crypto Industry for more than 5 years. The signals are given with proper take profit and proper stop loss, automated Crypto Trading Bot execute trade automatically in your account with consistent profit & Learn Crypto trading free training will be provided

Signals result – https://t.me/premiumclubresults

Smart Bit Profit – Smartbitprofit.com

I am not the admin or the owner of the project, I don’t know the admin!

Smart Bit Profit

Reduced Size Image

View / Sign-Up

PLANS:
3.3% – 15 % Daily For 30 Days

PAYMENT PROCESSORS:
Perfect Money, Bitcoin, Ethereum, Litecoin, Dogecoin, Bitcoin Cash

PROGRAM DETAILS:

Minimum Investment:
US$ 20
Maximum Investment:
US$ 9999999

Referral:
5% – 3% – 1%

Type of Script:
licensed

Start of Monitoring:
01.02.2021

View / Sign-Up