在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Implementing the class
Each
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:
实现一个
每次查询
示例: 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
提示:
2304ms 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 */
|
请发表评论