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);
 */
相关推荐
路弥行至15 分钟前
C语言入门教程 | 第七讲:函数和程序结构完全指南
c语言·经验分享·笔记·其他·算法·课程设计·入门教程
Xの哲學17 分钟前
Linux ioctl 深度剖析:从原理到实践
linux·网络·算法·架构·边缘计算
隐语SecretFlow28 分钟前
隐语SecreFlow:如何全面提升MPC多方安全学习的性能?
算法
王国强20091 小时前
什么是算法复杂度?
算法
夏鹏今天学习了吗1 小时前
【LeetCode热题100(54/100)】全排列
算法·leetcode·深度优先
緈福的街口1 小时前
gps的定位图,在车的位置去寻找周围20x20的区域,怎么确定周围有多少辆车,使用什么数据结构
数据结构·算法
江塘1 小时前
机器学习-KNN算法实战及模型评估可视化(C++/Python实现)
开发语言·c++·人工智能·python·算法·机器学习
La Pulga2 小时前
【STM32】WDG看门狗
c语言·stm32·单片机·嵌入式硬件·mcu
麦麦大数据2 小时前
F039 python五种算法美食推荐可视化大数据系统vue+flask前后端分离架构
python·算法·vue·推荐算法·美食·五种算法
星空露珠3 小时前
数独解题算法lua脚本
开发语言·数据结构·算法·游戏·lua