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:
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);*/

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/696202.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/696202.shtml
英文地址,請注明出處:http://en.pswp.cn/news/696202.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

ESP8266智能家居(1)——開發環境的搭建

1.前期介紹 本次打算使用esp8266的開發板——NodeMCU&#xff0c;進行物聯網相關項目的學習。開發環境使用Arduino軟件。 NodeMCU實物圖為&#xff1a; 開發環境截圖為&#xff1a; 2.軟件下載 我使用的arduino版本為1.8.5&#xff0c;其安裝包如下&#xff1a; 【免費】ar…

vue3 #跨組件通信

//爺爺組件中 import { provide , ref } from vue const money ref (100) //定義數據 provide( money , money ) //提供數據給孫子組件 const changeMoney ( m:number ) > { //定義函數 if (money) { money.value money.value - m } } provide(&quo…

Python系列(19)—— 條件語句

一、條件語句的基本概念 條件語句&#xff0c;也稱為選擇語句&#xff0c;允許程序根據條件的結果來執行不同的代碼塊。Python中最常用的條件語句是if語句&#xff0c;其基本語法如下&#xff1a; if condition:# 當條件為真時執行的代碼塊如果條件為真&#xff08;即非零或非…

學習總結22

解題思路 簡單模擬。 代碼 #include <bits/stdc.h> using namespace std; long long g[2000000]; long long n; int main() {long long x,y,z,sum0,k0;scanf("%lld",&n);for(x1;x<n;x)scanf("%lld",&g[x]);for(x1;x<n;x){scanf(&qu…

GEE必須會教程—時間都去哪了(Date參數類型)

時間和空間是世界存在的兩種基本屬性&#xff0c;大部分的數據都有特有的通道存儲時間信息&#xff0c;用戶需要通過獲取數據存儲的信息&#xff0c;來判斷數據的可用性&#xff0c;以及數據在時間上發生的變化。在遙感上&#xff0c;空間數據集合中&#xff0c;時間信息顯得更…

django配置視圖并與模版進行數據交互

目錄 安裝django 創建一個django項目 項目結構 創建視圖層views.py 寫入視圖函數 創建對應視圖的路由 創建模版層 配置項目中的模版路徑 創建模版html文件 啟動項目 瀏覽器訪問結果 安裝django pip install django 創建一個django項目 這里最好用命令行完成&#xf…

SQL注入之DNSLog外帶注入

一、認識&#xff1a; 什么是dnslog呢&#xff1f; DNS就是域名解析服務&#xff0c;把一個域名轉換成對應的IP地址&#xff0c;轉換完成之后&#xff0c;DNS服務器就會有一個日志記錄本次轉換的時間、域名、域名對應的ip、請求方的一些信息&#xff0c;這個日志就叫DNSLog。…

漢諾塔問題—java詳解(附源碼)

來源及應用 相傳在古印度圣廟中&#xff0c;有一種被稱為漢諾塔(Hanoi)的游戲。該游戲是在一塊銅板裝置上&#xff0c;有三根桿(編號A、B、C)&#xff0c;在A桿自下而上、由大到小按順序放置64個金盤(如圖1)。游戲的目標&#xff1a;把A桿上的金盤全部移到C桿上&#xff0c;并仍…

【Nacos】構建云原生應用的動態服務發現、配置管理和服務管理平臺【企業級生產環境集群搭建應用】

基礎描述 一個更易于構建云原生應用的動態服務發現、配置管理和服務管理平臺Nacos 致力于幫助您發現、配置和管理微服務。Nacos 提供了一組簡單易用的特性集&#xff0c;幫助您快速實現動態服務發現、服務配置、服務元數據及流量管理。Nacos 幫助您更敏捷和容易地構建、交付和…

貓頭虎分享已解決Bug || Spring Error: Request method ‘POST‘ not supported

博主貓頭虎的技術世界 &#x1f31f; 歡迎來到貓頭虎的博客 — 探索技術的無限可能&#xff01; 專欄鏈接&#xff1a; &#x1f517; 精選專欄&#xff1a; 《面試題大全》 — 面試準備的寶典&#xff01;《IDEA開發秘籍》 — 提升你的IDEA技能&#xff01;《100天精通鴻蒙》 …

海思3559 yolov5 wk模型部署筆記

文章目錄 安裝3559工具鏈編譯opencv編譯項目總結 安裝3559工具鏈 將3559工具鏈copy到虛擬機上&#xff0c;并解壓得到安裝包 解壓&#xff1a; tar -zxvf aarch64-himix100-linux.tgz解壓后會得到安裝包文件夾&#xff1a; 安裝工具鏈&#xff1a; sudo ./aarch64-himix100…

代碼隨想錄算法訓練營第17天—二叉樹06 | ● *654.最大二叉樹 ● 617.合并二叉樹 ● 700.二叉搜索樹中的搜索 ● *98.驗證二叉搜索樹

*654.最大二叉樹 題目鏈接/文章講解&#xff1a;https://programmercarl.com/0654.%E6%9C%80%E5%A4%A7%E4%BA%8C%E5%8F%89%E6%A0%91.html 視頻講解&#xff1a;https://www.bilibili.com/video/BV1MG411G7ox 考點 前序遍歷構建二叉樹 我的思路 參考了力扣題目里的提示遞歸三要…

【大數據面試題】008 談一談 Flink資源如何配置

【大數據面試題】008 談一談 Flink 資源如何配置 并行度 Parallelism 概念作用Slot 概念作用如何設置TaskManager 任務管理器Flink submit 腳本 一步一個腳印&#xff0c;一天一道面試題 該文章有較多引用文章 https://zhuanlan.zhihu.com/p/572170629?utm_id0 并行度 Paralle…

Unity2023.1.19沒有PBR Graph?

Unity2023.1.19沒有PBR Graph? 關于Unity2023.1.19沒有PBR graph的說法,我沒看見管方給出的答案,百度則提到了Unity2020版之后Shader Graph的“全新更新”,之前也沒太注意版本的區別,以后項目盡量都留心一下。 之前文章說過,孿生智慧項目推薦使用URP渲染管線,以上的截…

安裝sklearn遇到ImportError: dlopen: cannot load any more object with static TLS

1.看https://blog.csdn.net/Go_ahead_forever/article/details/133755918 知不能 pip install sklearn&#xff0c;而是 pip install scikit-learn2.網上說調換import的順序就能解決。 但是我不知道調換哪個&#xff0c;索性重新開了anaconda環境&#xff0c;一個個安裝缺什么…

Stable Diffusion 繪畫入門教程(webui)-ControlNet(線稿約束)

上篇文章介紹了openpose&#xff0c;本篇文章介紹下線稿約束&#xff0c;關于線稿約束有好幾個處理器都屬于此類型&#xff0c;但是有一些區別。 包含&#xff1a; 1、Canny(硬邊緣&#xff09;&#xff1a;識別線條比較多比較細&#xff0c;一般用于更大程度得還原照片 2、ML…

在docker中運行vins-fusion

文章目錄 VINS-fusion拉取鏡像創建容器在vscode中運行代碼運行效果VINS-fusion VINS-Fusion 是一個開源的實時多傳感器狀態估計庫,主要由香港科技大學的沈邵劼教授領導的研究團隊開發。它是 VINS-Mono(單目視覺慣性系統)的擴展,支持多種傳感器組合,如雙目、立體相機和IMU…

Spring Security 認證授權安全框架

Spring Security概述 1.什么是Spring Security? Spring Security是一個Java框架&#xff0c;用于保護應用程序的安全性。它提供了一套全面的安全解決方案&#xff0c;包括身份驗證、授權、防止攻擊等功能。Spring Security基于過濾器鏈的概念&#xff0c;可以輕松地集成到任…

指針筆試題(C語言進階)

目錄 前言 1、案例一 1.1 答案 1.2 解析 2、案例二 2.1 答案 2.2 解析 3、案例三 3.1 答案 3.2 解析 4、案例四 4.1 答案 4.2 解析 5、案例五 5.1 答案 5.2 解析 總結 前言 “紙上得來終覺淺&#xff0c;絕知此事要躬行”。本篇通過對指針實際案例的分析&…

Google重磅開源!Gemma 2B/7B小模型登場,6萬億Tokens喂飽,聊天編程兩不誤,LLaMA也黯然失色?

Google又有大動作&#xff01; 近日&#xff0c;他們發布了Gemma 2B和7B兩個開源AI模型&#xff0c;與大型封閉模型不同&#xff0c;它們更適合小型任務&#xff0c;如聊天和文本摘要。 這兩個模型在訓練過程中使用了6萬億個Tokens的數據&#xff0c;包括網頁文檔、代碼和數學…