[Swift]LeetCode900. RLE 迭代器 | RLE Iterator

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:山青詠芝(shanqingyongzhi)
➤博客園地址:山青詠芝(https://www.cnblogs.com/strengthen/
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址: http://www.javashuo.com/article/p-meulpjbn-me.html 
➤若是連接不是山青詠芝的博客園地址,則多是爬取做者的文章。
➤原文已修改更新!強烈建議點擊原文地址閱讀!支持做者!支持原創!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html

Write an iterator that iterates through a run-length encoded sequence.git

The iterator is initialized by RLEIterator(int[] A), where A is a run-length encoding of some sequence.  More specifically, for all even iA[i] tells us the number of times that the non-negative integer value A[i+1] is repeated in the sequence.github

The iterator supports one function: next(int n), which exhausts the next nelements (n >= 1) and returns the last element exhausted in this way.  If there is no element left to exhaust, next returns -1 instead.微信

For example, we start with A = [3,8,0,9,2,5], which is a run-length encoding of the sequence [8,8,8,5,5].  This is because the sequence can be read as "three eights, zero nines, two fives". app

Example 1:函數

Input: ["RLEIterator","next","next","next","next"], [[[3,8,0,9,2,5]],[2],[1],[1],[2]] Output: [null,8,8,5,-1] Explanation: RLEIterator is initialized with RLEIterator([3,8,0,9,2,5]). This maps to the sequence [8,8,8,5,5]. RLEIterator.next is then called 4 times: .next(2) exhausts 2 terms of the sequence, returning 8. The remaining sequence is now [8, 5, 5]. .next(1) exhausts 1 term of the sequence, returning 8. The remaining sequence is now [5, 5]. .next(1) exhausts 1 term of the sequence, returning 5. The remaining sequence is now [5]. .next(2) exhausts 2 terms, returning -1. This is because the first term exhausted was 5, but the second term did not exist. Since the last term exhausted does not exist, we return -1. 

Note:測試

  1. 0 <= A.length <= 1000
  2. A.length is an even integer.
  3. 0 <= A[i] <= 10^9
  4. There are at most 1000 calls to RLEIterator.next(int n) per test case.
  5. Each call to RLEIterator.next(int n) will have 1 <= n <= 10^9.

編寫一個遍歷遊程編碼序列的迭代器。this

迭代器由 RLEIterator(int[] A) 初始化,其中 A 是某個序列的遊程編碼。更具體地,對於全部偶數 iA[i] 告訴咱們在序列中重複非負整數值 A[i + 1] 的次數。編碼

迭代器支持一個函數:next(int n),它耗盡接下來的  n 個元素(n >= 1)並返回以這種方式耗去的最後一個元素。若是沒有剩餘的元素可供耗盡,則  next 返回 -1 。spa

例如,咱們以 A = [3,8,0,9,2,5] 開始,這是序列 [8,8,8,5,5] 的遊程編碼。這是由於該序列能夠讀做 「三個八,零個九,兩個五」。 

示例:

輸入:["RLEIterator","next","next","next","next"], [[[3,8,0,9,2,5]],[2],[1],[1],[2]]
輸出:[null,8,8,5,-1]
解釋:
RLEIterator 由 RLEIterator([3,8,0,9,2,5]) 初始化。
這映射到序列 [8,8,8,5,5]。
而後調用 RLEIterator.next 4次。

.next(2) 耗去序列的 2 個項,返回 8。如今剩下的序列是 [8, 5, 5]。

.next(1) 耗去序列的 1 個項,返回 8。如今剩下的序列是 [5, 5]。

.next(1) 耗去序列的 1 個項,返回 5。如今剩下的序列是 [5]。

.next(2) 耗去序列的 2 個項,返回 -1。 這是因爲第一個被耗去的項是 5,
但第二個項並不存在。因爲最後一個要耗去的項不存在,咱們返回 -1。 

提示:

  1. 0 <= A.length <= 1000
  2. A.length 是偶數。
  3. 0 <= A[i] <= 10^9
  4. 每一個測試用例最多調用 1000 次 RLEIterator.next(int n)
  5. 每次調用 RLEIterator.next(int n) 都有 1 <= n <= 10^9 。

Runtime: 24 ms
Memory Usage: 19.6 MB
 1 class RLEIterator {
 2     var index:Int
 3     var A:[Int]
 4 
 5     init(_ A: [Int]) {
 6         self.A = A
 7         index = 0        
 8     }
 9     
10     func next(_ n: Int) -> Int {
11         var n = n
12         while(index < A.count && n > A[index])
13         {
14             n -= A[index]
15             index += 2
16         }
17         if index >= A.count
18         {
19             return -1
20         }
21         A[index] -= n;
22         return A[index + 1]      
23     }
24 }
25 
26 /**
27  * Your RLEIterator object will be instantiated and called as such:
28  * let obj = RLEIterator(A)
29  * let ret_1: Int = obj.next(n)
30  */
31  

24ms

 1 class RLEIterator {
 2     let array : [Int] 
 3     var currentPosition = 0
 4     var currentCount = 0
 5     
 6     init(_ A: [Int]) {
 7         array = A 
 8     }
 9     
10     func next(_ n: Int) -> Int {
11         var n = n
12         
13         while currentPosition < array.count {
14             if currentCount + n > array[currentPosition] {
15                 n = n - (array[currentPosition] - currentCount)
16                 currentCount = 0
17                 currentPosition += 2    
18             } else {
19                 currentCount += n                         
20                 return array[currentPosition + 1]
21             }
22         }       
23         return -1 
24     }
25 }

32ms

 1 class RLEIterator {
 2 
 3     init(_ A: [Int]) {
 4         var i = A.count - 2
 5         while i >= 0 {
 6             store.append(Record(A[i + 1], A[i]))
 7             i -= 2
 8         }
 9     }
10     
11     func next(_ n: Int) -> Int {
12         var n = n
13         while !store.isEmpty {
14             let k = min(store.last!.cnt, n)
15             store[store.count - 1].cnt -= k
16             n -= k
17             if n == 0 {
18                 return store.last!.val
19             }
20             if store.last!.cnt == 0 {
21                 store.removeLast()
22             }
23         }
24         return -1
25     }
26     
27     struct Record {
28         var val: Int
29         var cnt: Int
30         init(_ val: Int, _ cnt: Int) {
31             self.val = val
32             self.cnt = cnt
33         }
34     }
35     var store = [Record]()
36 }

40ms

 1 class RLEIterator {
 2     private let arr: [Int]
 3     var cur = (0, 0)
 4     
 5     init(_ A: [Int]) {
 6         arr = A
 7         cur = (0, A[0])
 8     }
 9     
10     func next(_ n: Int) -> Int {
11         // diff = 2 | 1 | 1 | 2
12         var diff = n 
13         // cur == (0, 3) | (0, 1) | (0,0) | (4,1)
14         // 3 - 2 < 0 => false, | 1 - 1 < 0 => false | 0-1 < 0 => true;0-1<0=>true;2-1<0=>false | 1 - 2 < 0 => true
15         while (cur.1 - diff) < 0 { 
16             //||1;1| 1
17             diff -= cur.1 
18             // || (2, 0);(4,0)|(6,1)
19             cur.0 += 2      
20             if cur.0 >= arr.count {
21                 return -1
22             }
23             // || (2, 0);(4,2)|(6)
24             cur.1 = arr[cur.0]  
25         }
26         // (0,3) -> (0,1) | (0, 1) -> (0, 0) | (4,2) -> (4,1)
27         cur.1 -= diff 
28         // false
29         if cur.0+1 >= arr.count { 
30             return -1
31         }
32          // 8 | 8 | 5
33         return arr[cur.0+1]
34     }
35 }
相關文章
相關標籤/搜索