Skip to content

Commit

Permalink
Merge pull request #1189 from deepkrg17/stock_span
Browse files Browse the repository at this point in the history
Added Stock span python and kotlin code
  • Loading branch information
OtacilioN committed Oct 18, 2023
2 parents b50f732 + 0c52ed6 commit 9410528
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Stock Span/StockSpan.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
fun stockSpan(prices: List<Int>): MutableList<Int> {
val spans = mutableListOf<Int>()
val prevs = mutableListOf<Pair<Int, Int>>()
for (price in prices) {
var days = 1
while (!prevs.isEmpty() && (prevs.last().first <= price)) {
days += prevs.removeLast().second
}
prevs.add(Pair(price, days))
spans.add(days)
}
return spans
}

fun main() {
val a = listOf(100,80,60,70, 60,75,85)
for (x in stockSpan(a)) {
print("$x ")
}
}
14 changes: 14 additions & 0 deletions Stock Span/stock_span.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def stock_span(prices):
spans = []
prevs = []
for price in prices:
days = 1
while prevs and prevs[-1][0] <= price:
days += prevs.pop()[1]
prevs.append((price, days))
spans.append(days)
return spans

a = [100,80,60,70, 60,75,85]
for x in stock_span(a):
print(x, end=" ")

0 comments on commit 9410528

Please sign in to comment.