LeetCode //C - 901. Online Stock Span

901. Online Stock Span

Design an algorithm that collects daily price quotes for some stock and returns the span of that stock's price for the current day.

The span of the stock's price in one day is the maximum number of consecutive days (starting from that day and going backward) for which the stock price was less than or equal to the price of that day.

  • For example, if the prices of the stock in the last four days is 7,2,1,2 and the price of the stock today is 2, then the span of today is 4 because starting from today, the price of the stock was less than or equal 2 for 4 consecutive days.
  • Also, if the prices of the stock in the last four days is 7,34,1,2 and the price of the stock today is 8, then the span of today is 3 because starting from today, the price of the stock was less than or equal 8 for 3 consecutive days.

Implement the StockSpanner class:

  • StockSpanner() Initializes the object of the class.
  • int next(int price) Returns the span of the stock's price given that today's price is price.
Example 1:

Input:

"StockSpanner", "next", "next", "next", "next", "next", "next", "next"

\[\], \[100\], \[80\], \[60\], \[70\], \[60\], \[75\], \[85\]

Output:

null, 1, 1, 1, 2, 1, 4, 6

Explanation

StockSpanner stockSpanner = new StockSpanner();

stockSpanner.next(100); // return 1

stockSpanner.next(80); // return 1

stockSpanner.next(60); // return 1

stockSpanner.next(70); // return 2

stockSpanner.next(60); // return 1

stockSpanner.next(75); // return 4, because the last 4 prices (including today's price of 75) were less than or equal to today's price.

stockSpanner.next(85); // return 6

Constraints:
  • 1 < = p r i c e < = 1 0 5 1 <= price <= 10^5 1<=price<=105
  • At most 1 0 4 10^4 104 calls will be made to next.

From: LeetCode

Link: 901. Online Stock Span


Solution:

Ideas:

This code defines a StockSpanner struct with three fields: prices and spans are dynamically allocated arrays to store the price and span of each stock, and top is used to keep track of the stack's top element index. The stockSpannerCreate function initializes a StockSpanner object, stockSpannerNext processes the next stock price and calculates its span, and stockSpannerFree cleans up the allocated memory to prevent memory leaks.

Caode:
c 复制代码
typedef struct {
    int* prices;
    int* spans;
    int top;
} StockSpanner;

StockSpanner* stockSpannerCreate() {
    StockSpanner* spanner = (StockSpanner*)malloc(sizeof(StockSpanner));
    spanner->prices = (int*)malloc(sizeof(int) * 10000); // Assuming at most 10^4 calls.
    spanner->spans = (int*)malloc(sizeof(int) * 10000);  // Same assumption as above.
    spanner->top = -1; // Initialize stack top as -1 indicating empty stack.
    return spanner;
}

int stockSpannerNext(StockSpanner* obj, int price) {
    int span = 1;
    while (obj->top >= 0 && obj->prices[obj->top] <= price) {
        span += obj->spans[obj->top]; // Add the span of elements that are less than or equal to the current price.
        obj->top--; // Pop the elements that are less than or equal to the current price.
    }
    obj->top++;
    obj->prices[obj->top] = price; // Push the current price onto the stack.
    obj->spans[obj->top] = span; // Push the current span onto the stack.
    return span;
}

void stockSpannerFree(StockSpanner* obj) {
    free(obj->prices); // Free the allocated memory for prices.
    free(obj->spans); // Free the allocated memory for spans.
    free(obj); // Finally, free the object itself.
}

/**
 * Your StockSpanner struct will be instantiated and called as such:
 * StockSpanner* obj = stockSpannerCreate();
 * int param_1 = stockSpannerNext(obj, price);
 * stockSpannerFree(obj);
 */
相关推荐
Logic10113 分钟前
C语言/数据结构位运算题解:异或XOR找出蛋糕订单中的“VIP独特口味“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
土司大王17 分钟前
LeetCode hot100——35.搜索插入位置:Java 二分模板、左闭右开区间与插入点分析
java·算法·leetcode
微三云生态系统架构师-彭丹34 分钟前
远方好物S2B2C系统架构:一级分销与保证金托管的合规技术实现
人工智能·算法
土司大王39 分钟前
LeetCode hot100——34.在排序数组中查找元素的第一个和最后一个位置:Java 二分模板、边界分析
java·算法·leetcode
Logic1011 小时前
C语言/数据结构位运算题解:异或XOR找出飞行棋中的“独特颜色棋子“——只出现一次的数字
c语言·数据结构·数组·位运算·时间复杂度·算法题·异或性质
罗西的思考2 小时前
机器人模型(WM / WAM / VLA)综合分析与对比:从「看」到「想」再到「做」
人工智能·算法·机器学习
Logic1013 小时前
C语言/数据动态规划题解:翻转一次子数组后的最大子数组和——四状态DP(O(n)时间O(1)空间)
c语言·数据结构·动态规划·时间复杂度·算法题·状态机dp·最大子数组和
小哈里3 小时前
【知识】从学科到实践:自然・工程・社科・人文|算法・研发・产品・品牌设计
程序人生·算法·产品·设计·工程
小π军4 小时前
最大重叠区间数量
数据结构·算法
镜像视界(浙江)科技有限公司4 小时前
《视频孪生之上:二维展示终结,三维空间计算重构城市逻辑》——跨摄像连续表达 × 三角测量厘米级定位 × 动态轨迹建模,构建新一代城市空间
大数据·人工智能·算法·矩阵·音视频·空间计算