1. 程式人生 > >[Swift]LeetCode904. 水果成籃 | Fruit Into Baskets

[Swift]LeetCode904. 水果成籃 | Fruit Into Baskets

重復 star int 返回 distance += dice mpm last

In a row of trees, the i-th tree produces fruit with type tree[i].

You start at any tree of your choice, then repeatedly perform the following steps:

  1. Add one piece of fruit from this tree to your baskets. If you cannot, stop.
  2. Move to the next tree to the right of the current tree. If there is no tree to the right, stop.

Note that you do not have any choice after the initial choice of starting tree: you must perform step 1, then step 2, then back to step 1, then step 2, and so on until you stop.

You have two baskets, and each basket can carry any quantity of fruit, but you want each basket to only carry one type of fruit each.

What is the total amount of fruit you can collect with this procedure?

Example 1:

Input: [1,2,1]
Output: 3
Explanation: We can collect [1,2,1].

Example 2:

Input: [0,1,2,2]
Output: 3
Explanation: We can collect [1,2,2].
If we started at the first tree, we would only collect [0, 1].

Example 3:

Input: [1,2,3,2,2]
Output: 4
Explanation: We can collect [2,3,2,2].
If we started at the first tree, we would only collect [1, 2].

Example 4:

Input: [3,3,3,1,2,1,1,2,3,3,4]
Output: 5
Explanation: We can collect [1,2,1,1,2].
If we started at the first tree or the eighth tree, we would only collect 4 fruits. 

Note:

  1. 1 <= tree.length <= 40000
  2. 0 <= tree[i] < tree.length

在一排樹中,第 i 棵樹產生 tree[i] 型的水果。
你可以從你選擇的任何樹開始,然後重復執行以下步驟:

  1. 把這棵樹上的水果放進你的籃子裏。如果你做不到,就停下來。
  2. 移動到當前樹右側的下一棵樹。如果右邊沒有樹,就停下來。

請註意,在選擇一顆樹後,你沒有任何選擇:你必須執行步驟 1,然後執行步驟 2,然後返回步驟 1,然後執行步驟 2,依此類推,直至停止。

你有兩個籃子,每個籃子可以攜帶任何數量的水果,但你希望每個籃子只攜帶一種類型的水果。
用這個程序你能收集的水果總量是多少?

示例 1:

輸入:[1,2,1]
輸出:3
解釋:我們可以收集 [1,2,1]。

示例 2:

輸入:[0,1,2,2]
輸出:3
解釋:我們可以收集 [1,2,2].
如果我們從第一棵樹開始,我們將只能收集到 [0, 1]。

示例 3:

輸入:[1,2,3,2,2]
輸出:4
解釋:我們可以收集 [2,3,2,2].
如果我們從第一棵樹開始,我們將只能收集到 [1, 2]。

示例 4:

輸入:[3,3,3,1,2,1,1,2,3,3,4]
輸出:5
解釋:我們可以收集 [1,2,1,1,2].
如果我們從第一棵樹或第八棵樹開始,我們將只能收集到 4 個水果。 

提示:

  1. 1 <= tree.length <= 40000
  2. 0 <= tree[i] < tree.length

720ms

 1 class Solution {
 2     func totalFruit(_ tree: [Int]) -> Int {
 3         var one = Int.min
 4         var two = Int.min
 5         var lastIndex = Int.min
 6         var last = Int.min
 7         var maxv = 0
 8         var cur = 0
 9         for i in tree.indices {
10             if one == Int.min {
11                 one = tree[i]
12                 lastIndex = i
13                 last = one
14                 cur = 1
15                 continue
16             }
17             if tree[i] == one {
18                 if last != one {
19                     last = one
20                     lastIndex = i
21                 }
22                 cur += 1
23                 continue
24             }
25 
26             if two == Int.min {
27                 two = tree[i]
28                 last = two
29                 lastIndex = i
30                 cur += 1
31                 continue
32             }
33 
34             if two == tree[i] {
35                 if last != two {
36                     last = two
37                     lastIndex = i
38                 }
39                 cur += 1
40                 continue
41             }
42 
43             maxv = max(maxv, cur)
44             cur = i - lastIndex + 1
45             if last == one {
46                 two = tree[i]
47             }
48             else {
49                 one = tree[i]
50             }
51             lastIndex = i
52             last = tree[i]
53 
54         }
55 
56         return max(maxv, cur)
57     }
58 }

728ms

 1 class Solution {
 2     func totalFruit(_ tree: [Int]) -> Int {
 3         guard tree.count >= 3 else {
 4             return tree.count
 5         }
 6         
 7         var minIndex = 0
 8         var maxIndex = minIndex
 9         var maxValue = 0
10         
11         for i in 0..<tree.count where !(tree[i] == tree[minIndex] || tree[i] == tree[maxIndex]) {
12             maxValue = max(maxValue, i - minIndex)
13             minIndex = i - 1
14             
15             while minIndex > 0 && tree[minIndex - 1] == tree[minIndex] {
16                 minIndex -= 1
17             }
18             
19             maxIndex = i
20         }
21         
22         return max(maxValue, tree.count - minIndex)
23     }
24 }

Runtime: 744 ms Memory Usage: 19.3 MB
 1 class Solution {
 2     func totalFruit(_ tree: [Int]) -> Int {
 3         var box1 = 0
 4         var box2 = 0
 5         var sum = 0
 6         var count = 0
 7         var start = 0
 8         while start < tree.count - sum {
 9             box1 = tree[start]
10             box2 = -1
11             count = 0
12             count += 1
13             for i in (start + 1)..<tree.count {
14                 if tree[i] == box1 {
15                     count += 1
16                     continue
17                 }
18                 if box2 < 0 {
19                     box2 = tree[i]
20                     count += 1
21                     start = i
22                     continue
23                 } else if box2 == tree[i] {
24                     count += 1
25                     continue
26                 }
27                 break
28             }
29             sum = max(sum, count)
30         }
31         return sum
32     }
33 }
Runtime: 752 ms Memory Usage: 19.4 MB
 1 class Solution {
 2     func totalFruit(_ tree: [Int]) -> Int {
 3         var res:Int = 0
 4         var cur:Int = 0
 5         var count_b:Int = 0
 6         var a:Int = 0
 7         var b:Int = 0
 8         for c in tree
 9         {
10             cur = c == a || c == b ? cur + 1 : count_b + 1
11             count_b = c == b ? count_b + 1 : 1
12             if b != c
13             {
14                 a = b
15                 b = c
16             }
17             res = max(res, cur)
18         }
19         return res       
20     }
21 }

768ms

 1 class Solution {
 2     func totalFruit(_ tree: [Int]) -> Int {
 3         var tempMax = 0
 4         var left = 0, right = 0, lastA = 0, lastB = 0
 5         let count = tree.count
 6         guard count > 0 else {
 7             return 0
 8         }
 9         var typeA = tree[0]
10         var typeB:Int?  
11         while right < count {
12             let type = tree[right]
13             if type == typeA {
14                 lastA = right
15                 tempMax = max(right - left + 1, tempMax)
16                 right += 1
17             }else if let _ = typeB {
18                 if typeB == type {
19                     lastB = right
20                     tempMax = max(right - left + 1, tempMax)
21                     right += 1
22                 }else{
23                     if typeA == tree[right - 1] {
24                         typeB = type
25                         left = lastB + 1
26                     }else{
27                         typeA = type
28                         left = lastA + 1
29                     }
30                 }
31             }else{
32                 typeB = type
33                 lastB = right
34                 tempMax = max(right - left + 1, tempMax)
35                 right += 1
36             }
37         }  
38         return tempMax
39     }
40 }

776ms

 1 class Solution {
 2     func totalFruit(_ tree: [Int]) -> Int {
 3         var s = Set<Int>()
 4         var ans = 0
 5         var pre = 0
 6         for idx in 0..<tree.endIndex {
 7             s.insert(tree[idx])
 8             if s.count > 2 {
 9                 pre = idx - 1
10                 while pre > 0, tree[pre - 1] == tree[pre] {
11                     pre -= 1
12                 }
13                 s.remove(tree[pre - 1])
14             } else {
15                 ans = max(ans, idx - pre + 1)
16             }
17         }
18         return ans 
19     }
20 }

788ms

 1 class Solution {
 2     func totalFruit(_ tree: [Int]) -> Int {
 3         var prev = [-1, 0]
 4         var curr = [-1, 0]
 5         var maxFruits = 0
 6         
 7         for f in tree {
 8             if f == curr[0] {
 9                 curr[1] += 1
10             } else {
11                 (curr, prev) = (prev, curr)
12                 if f == curr[0] {
13                     prev[1] += curr[1]
14                 } else {
15                     maxFruits = max(maxFruits, prev[1] + curr[1])
16                 }
17                 curr = [f, 1]
18             }
19         }
20         return max(maxFruits, prev[1] + curr[1])        
21     }
22 }

792ms

 1 class Solution {
 2     func totalFruit(_ tree: [Int]) -> Int {
 3         if tree.count <= 1 {
 4             return tree.count
 5         }
 6         var distance = [Int]()
 7         distance.append(1)
 8         for i in 1..<tree.count {
 9             if tree[i] == tree[i - 1] {
10                 distance.append(distance[i - 1] + 1)
11             } else {
12                 distance.append(1)
13             }
14         }
15         var result = 1
16         var pair: Set<Int> = [tree[0]]
17         var maxCollect = Int.min
18         for i in 1..<tree.count {
19             pair.insert(tree[i])
20             if pair.count <= 2 {
21                 result += 1
22             } else {
23                 result = distance[i - 1] + 1
24                 pair = [tree[i], tree[i - 1]]
25             }
26             maxCollect = max(maxCollect, result)
27         }
28         return maxCollect
29     }
30 }

[Swift]LeetCode904. 水果成籃 | Fruit Into Baskets