[Swift]LeetCode1157. 子數組中佔絕大多數的元素 | Online Majority Element In Subarray

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

Implementing the class MajorityChecker, which has the following API:git

  • MajorityChecker(int[] arr) constructs an instance of MajorityChecker with the given array arr;
  • int query(int left, int right, int threshold) has arguments such that:
    • 0 <= left <= right < arr.length representing a subarray of arr;
    • 2 * threshold > right - left + 1, ie. the threshold is always a strict majority of the length of the subarray

Each query(...) returns the element in arr[left], arr[left+1], ..., arr[right] that occurs at least threshold times, or -1 if no such element exists.github

 

Example:數組

MajorityChecker majorityChecker = new MajorityChecker([1,1,2,2,1,1]);
majorityChecker.query(0,5,4); // returns 1
majorityChecker.query(0,3,3); // returns -1
majorityChecker.query(2,3,2); // returns 2

 

Constraints:微信

  • 1 <= arr.length <= 20000
  • 1 <= arr[i] <= 20000
  • For each query, 0 <= left <= right < len(arr)
  • For each query, 2 * threshold > right - left + 1
  • The number of queries is at most 10000

實現一個 MajorityChecker 的類,它應該具備下述幾個 API:spa

  • MajorityChecker(int[] arr) 會用給定的數組 arr 來構造一個 MajorityChecker 的實例。
  • int query(int left, int right, int threshold) 有這麼幾個參數:
    • 0 <= left <= right < arr.length 表示數組 arr 的子數組的長度。
    • 2 * threshold > right - left + 1,也就是說閥值 threshold 始終比子序列長度的一半還要大。

每次查詢 query(...) 會返回在 arr[left], arr[left+1], ..., arr[right] 中至少出現閥值次數 threshold 的元素,若是不存在這樣的元素,就返回 -1code

 

示例:htm

MajorityChecker majorityChecker = new MajorityChecker([1,1,2,2,1,1]);
majorityChecker.query(0,5,4); // 返回 1
majorityChecker.query(0,3,3); // 返回 -1
majorityChecker.query(2,3,2); // 返回 2

 

提示:blog

  • 1 <= arr.length <= 20000
  • 1 <= arr[i] <= 20000
  • 對於每次查詢,0 <= left <= right < len(arr)
  • 對於每次查詢,2 * threshold > right - left + 1
  • 查詢次數最多爲 10000

2304mselement

 1 class MajorityChecker {
 2     var arr:[Int] = [Int]()
 3 
 4     init(_ arr: [Int]) {
 5         self.arr = arr        
 6     }
 7     
 8     func query(_ left: Int, _ right: Int, _ threshold: Int) -> Int {
 9         var count:[Int] = [Int](repeating:0,count:200000)
10         if left <= right
11         {
12             for i in left...right
13             {
14                 if ++count[arr[i] - 1] == threshold
15                 {
16                     return arr[i]
17                 }
18             }
19         }        
20         return -1
21     }
22 }
23 
24 /*擴展Int類,實現自增++、自減--運算符*/
25 extension Int{
26     //++前綴:先自增再執行表達示
27     static prefix func ++(num:inout Int) -> Int {
28         //輸入輸出參數num
29         num += 1
30         //返回加1後的數值
31         return num
32     }
33 }
34 
35 /**
36  * Your MajorityChecker object will be instantiated and called as such:
37  * let obj = MajorityChecker(arr)
38  * let ret_1: Int = obj.query(left, right, threshold)
39  */

 1 class MajorityChecker {
 2     var a:[Int] = [Int]()
 3     var big:[Int] = [Int]()
 4     var fs:[[Int]] = [[Int]]()
 5     var B:Int = 0
 6 
 7     init(_ arr: [Int]) {
 8         self.a = arr
 9         let n:Int = a.count
10         self.B = Int(sqrt(Double(n)))
11         var f:[Int] = [Int](repeating:0,count:20001)
12         for v in a
13         {
14             f[v] += 1
15         }
16         var bigger:[Int] = [Int](repeating:0,count:n)
17         var p:Int = 0
18         for i in 1...20000
19         {
20             if f[i] >= B
21             {                
22                 bigger[p] = i
23                 p += 1
24             }
25         }
26         big = bigger[0..<p] 
27         fs = [[Int]](repeating:[Int](repeating:0,count:n+1),count:p)
28         for i in 0..<p
29         {
30             for j in 0..<n
31             {
32                 if a[j] == big[i]
33                 {
34                     fs[i][j+1] = fs[i][j] + 1
35                 }
36                 else
37                 {
38                     fs[i][j+1] = fs[i][j]
39                 }
40             }
41         }
42     }
43     
44     func query(_ left: Int, _ right: Int, _ threshold: Int) -> Int {
45         if right - left + 1 <= 2*B
46         {
47             var best:Int = -1
48             var f:Int = 0
49             for i in  left...right
50             {
51                 if best == a[i]
52                 {
53                     f += 1
54 
55                 }
56                 else if f == 0
57                 {
58                     best = a[i]
59                 }
60                 else
61                 {
62                     f -= 1
63                 }
64             }
65             var ct:Int = 0
66             for i in  left...right
67             {
68                 if a[i] == best
69                 {
70                     ct += 1
71                 }
72             }
73             return ct >= threshold ? best : -1
74         }
75         else
76         {
77             for i in 0..<big.count
78             {
79                 var ff:Int = fs[i][right+1] - fs[i][left]
80                 if ff >= threshold
81                 {
82                     return big[i]
83                 }                
84             }
85             return -1
86         }
87     }
88 }
89 
90 /**
91  * Your MajorityChecker object will be instantiated and called as such:
92  * let obj = MajorityChecker(arr)
93  * let ret_1: Int = obj.query(left, right, threshold)
94  */
相關文章
相關標籤/搜索