class RLEIterator {
private var ar = [Int]()
private var num = [Int]()
private var current = 0
private var index = 0
init(_ A: [Int]) {
for i in stride(from: 0, to: A.count, by: 2) {
if i == 0 {
ar.append(A[i])
} else {
ar.append(A[i] + ar.last!)
}
num.append(A[i+1])
}
}
func next(_ n: Int) -> Int {
current += n
for i in stride(from: index, to: ar.count, by: 1) {
if ar[i] >= current {
index = i
return num[i]
}
}
return -1
}
}
More than 5 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme