程式語言 - LeetCode - C++ - 901. Online Stock Span



題目:


解答:

class StockSpanner {
private:
    vector<int> s;
    int pos;

public:
    StockSpanner() : s(10000), pos(0) {
    }
    
    int next(int price) {
        int r = 0;
        this->s[this->pos++] = price;

        for (int i = this->pos - 1; i >= 0; --i) {
            if (this->s[i] <= price) {
                r += 1;
                continue;
            }
            break;
        }

        return r;
    }
};

/**
 * Your StockSpanner object will be instantiated and called as such:
 * StockSpanner* obj = new StockSpanner();
 * int param_1 = obj->next(price);
 */