aboutsummaryrefslogtreecommitdiff
path: root/other/online_stock_span.rs
diff options
context:
space:
mode:
authorJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
committerJack Sangdahl <[email protected]>2025-03-10 00:36:13 -0600
commit7c60dee83b436e8e8a667bf9f191beced8de4fc3 (patch)
treeeb7a24ee2067f92245c57bb118e85a1baf15df50 /other/online_stock_span.rs
parent5d7cdc215e4ba1ec4cbb24c084cccb5e5e641468 (diff)
downloadleetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.tar.gz
leetcode-7c60dee83b436e8e8a667bf9f191beced8de4fc3.zip
chore: move cpp and rust code to directory
Diffstat (limited to 'other/online_stock_span.rs')
-rw-r--r--other/online_stock_span.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/other/online_stock_span.rs b/other/online_stock_span.rs
new file mode 100644
index 0000000..a967028
--- /dev/null
+++ b/other/online_stock_span.rs
@@ -0,0 +1,51 @@
+// 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. 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.
+ */
+
+struct Span {
+ price: i32,
+ days: i32,
+}
+
+struct StockSpanner {
+ spans: Vec<Span>,
+}
+
+impl StockSpanner {
+ fn new() -> Self {
+ StockSpanner { spans: Vec::new() }
+ }
+ fn next(&mut self, price: i32) -> i32 {
+ let mut days = 1;
+ while let Some(span) = self.spans.last() {
+ if span.price <= price {
+ days += span.days;
+ self.spans.pop();
+ } else {
+ break;
+ }
+ }
+ self.spans.push(Span { price, days });
+ days
+ }
+}
+
+fn main() {
+ let mut obj = StockSpanner::new();
+ println!("{}", obj.next(100));
+ println!("{}", obj.next(80));
+ println!("{}", obj.next(60));
+ println!("{}", obj.next(70));
+ println!("{}", obj.next(60));
+ println!("{}", obj.next(75));
+ println!("{}", obj.next(85));
+}