-
Notifications
You must be signed in to change notification settings - Fork 0
/
19OnlineStockSpan.swift
41 lines (37 loc) · 947 Bytes
/
19OnlineStockSpan.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//
// ViewController.swift
// StockPrices
//
// Created by thailinh on 5/20/20.
// Copyright © 2020 thailinh. All rights reserved.
//
import UIKit
class StockSpanner {
private var listPrices : [Int]!
private var listSpans : [Int]!
init() {
listPrices = [Int]()
listSpans = [Int]()
}
func next(_ price: Int) -> Int {
guard listPrices.count > 0 else{
listPrices.append(price)
listSpans.append(1)
return 1
}
var span : Int = 1
var index : Int = listPrices.count - 1
while index >= 0{
let considerPrice = listPrices[index]
let considerSpan = listSpans[index]
if considerPrice > price{
break
}
span += considerSpan
index -= considerSpan
}
listPrices.append(price)
listSpans.append(span)
return span
}
}