aboutsummaryrefslogtreecommitdiff
path: root/online_stock_span.rs
diff options
context:
space:
mode:
authorjack <[email protected]>2024-06-14 13:13:15 -0600
committerjack <[email protected]>2024-06-14 13:13:15 -0600
commitd343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa (patch)
tree96df922dae2a5073d9a50ea70c7aae6aa37e3ebe /online_stock_span.rs
downloadleetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.tar.gz
leetcode-d343a3b18dbaca9297d4f7b2280c4a16f0a5c8aa.zip
initial
Diffstat (limited to 'online_stock_span.rs')
-rw-r--r--online_stock_span.rs34
1 files changed, 34 insertions, 0 deletions
diff --git a/online_stock_span.rs b/online_stock_span.rs
new file mode 100644
index 0000000..a1818ff
--- /dev/null
+++ b/online_stock_span.rs
@@ -0,0 +1,34 @@
+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 obj = StockSpanner::new();
+ let ret_1: i32 = obj.next(price);
+}