在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given an array nums of positive integers, return the longest possible length of an array prefix of nums, such that it is possible to remove exactly one element from this prefix so that every number that has appeared in it will have the same number of occurrences. If after removing one element there are no remaining elements, it's still considered that every appeared number has the same number of ocurrences (0). Example 1: Input: nums = [2,2,1,1,5,3,3,5] Input: nums = [1,1,1,2,2,2,3,3,3,4,4,4,5] Input: nums = [1,1,1,2,2,2] Input: nums = [10,2,8,9,3,8,1,5,2,3,7,6] Constraints: 2 <= nums.length <= 10^5 给出一个正整数数组 nums,请你帮忙从该数组中找出能满足下面要求的 最长 前缀,并返回其长度: 从前缀中 删除一个 元素后,使得所剩下的每个数字的出现次数相同。 示例 1: 输入:nums = [2,2,1,1,5,3,3,5] 输入:nums = [1,1,1,2,2,2,3,3,3,4,4,4,5] 输入:nums = [1,1,1,2,2,2] 输入:nums = [10,2,8,9,3,8,1,5,2,3,7,6] 提示: 2 <= nums.length <= 10^5 Runtime: 440 ms
Memory Usage: 23.6 MB
1 class Solution { 2 func maxEqualFreq(_ nums: [Int]) -> Int { 3 var count:[Int] = [Int](repeating:0,count:100001) 4 var freq:[Int] = [Int](repeating:0,count:100001) 5 var res:Int = 0 6 let N:Int = nums.count 7 var a:Int = 0 8 var c:Int = 0 9 var d:Int = 0 10 for n in 1...N 11 { 12 var a:Int = nums[n - 1] 13 freq[count[a]] -= 1 14 count[a] += 1 15 c = count[a] 16 freq[count[a]] += 1 17 18 if freq[c] * c == n && n < N 19 { 20 res = n + 1 21 } 22 d = n - freq[c] * c 23 if (d == c + 1 || d == 1) && freq[d] == 1 24 { 25 res = n 26 } 27 } 28 return res 29 } 30 } 444ms 1 class Solution { 2 func maxEqualFreq(_ nums: [Int]) -> Int { 3 var count = [Int](repeating: 0, count: 100001) 4 var freq = [Int](repeating: 0, count: 100001) 5 6 let N = nums.count 7 8 var res = 0 9 var a = 0 10 var c = 0 11 var d = 0 12 13 for n in 1...N { 14 a = nums[n-1] 15 freq[count[a]] -= 1 16 count[a] += 1 17 c = count[a] 18 freq[count[a]] += 1 19 20 if freq[c] * c == n && n < N { 21 res = n + 1 22 } 23 d = n - freq[c] * c 24 if (d == c + 1 || d == 1) && freq[d] == 1 { 25 res = n 26 } 27 } 28 29 return res 30 } 31 } 492ms 1 class Solution { 2 func maxEqualFreq(_ nums: [Int]) -> Int { 3 let MAX_NUM = 1000_005 4 var freq = [Int](repeating: 0, count: MAX_NUM) 5 var freq_freq = freq 6 var ans = 0 7 let N = nums.count 8 for i in nums.indices { 9 let tmp = nums[i] 10 freq_freq[freq[tmp]] -= 1 11 12 freq[tmp] += 1 13 let c = freq[tmp] 14 freq_freq[c] += 1 15 16 if freq_freq[c]*c == i+1 && i+1<N { 17 ans = i + 1 + 1 18 } 19 20 let d = i + 1 - freq_freq[c] * c 21 if (d == c + 1 || d == 1) && freq_freq[d] == 1 { 22 ans = i + 1 23 } 24 } 25 return ans 26 } 27 } 528ms 1 class Solution { 2 func maxEqualFreq(_ nums: [Int]) -> Int { 3 let numCount = nums.count 4 var countHash = [Int:Int]() 5 var freqHash = [Int:Int]() 6 var result = 0 7 8 for i in 0..<numCount { 9 let num = nums[i] 10 11 countHash[num] = (countHash[num] ?? 0) + 1 12 let freq = countHash[num]! 13 freqHash[freq] = (freqHash[freq] ?? 0) + 1 14 let freqCount = freqHash[freq]! * freq 15 16 if freqCount == i { 17 // All same frequency at i (which is 0 based), there's one we can remove 18 result = max(result, i+1) 19 } else if freqCount == i + 1, i != numCount - 1 { 20 // print(i) 21 // print(freqHash) 22 // All same frequency, and still not at the last character. Add one more 23 result = max(result, i+2) 24 } 25 } 26 27 return result 28 } 29 } 812ms 1 class Solution { 2 3 var dict = [Int: Int]() // 频率 --> 个数 4 var m = [Int: Int]() // 当前数的频率 5 6 func maxEqualFreq(_ nums: [Int]) -> Int { 7 var result = 1 8 for i in 0..<nums.count { 9 10 m[nums[i], default: 0] += 1 11 let last = m[nums[i]]! - 1 12 13 //维护前缀 14 if let counter = dict[last] { 15 dict[last] = counter - 1 16 if counter == 1 { 17 dict.removeValue(forKey: last) 18 } 19 } 20 if let freq = m[nums[i]] { 21 dict[freq, default:0] += 1 22 } 23 if check() { result = i + 1 } 24 } 25 return result 26 } 27 28 29 fileprivate func check() -> Bool { 30 31 //只出现一个数字 32 if m.count == 1 { return true } 33 34 // 出现很多数字,但是都次数都是1 35 if dict.count == 1 { return dict[1] != nil } 36 37 if dict.count != 2 { return false } 38 39 //只有一个数字出现一次,其他都出现多次,且次数相等 40 for (freq, counter) in dict { 41 if freq == 1 && counter == 1 { return true } 42 } 43 44 //所有数字都出现多次,但是大多数次数都相等,只有一个特殊多了一次 45 let keys = Array(dict.keys) 46 if let counter0 = dict[keys[0]], let counter1 = dict[keys[1]] { 47 if keys[0] - keys[1] == 1 { return counter0 == 1 } 48 if keys[1] - keys[0] == 1 { return counter1 == 1 } 49 } 50 return false 51 } 52 }
|
请发表评论