★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公众号:为敢(WeiGanTechnologies)
➤我的域名:https://www.zengqiang.org
➤GitHub地址:https://github.com/strengthen/LeetCode
➤原文地址:http://www.javashuo.com/article/p-yxejonan-dg.html
➤若是连接不是山青咏芝的博客园地址,则多是爬取做者的文章。
➤原文已修改更新!强烈建议点击原文地址阅读!支持做者!支持原创!
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★html
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.git
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).github
Example 1:数组
Input: nums = [2,2,1,1,5,3,3,5]
Output: 7
Explanation: For the subarray [2,2,1,1,5,3,3] of length 7, if we remove nums[4]=5, we will get [2,2,1,1,3,3], so that each number will appear exactly twice.
Example 2:微信
Input: nums = [1,1,1,2,2,2,3,3,3,4,4,4,5]
Output: 13
Example 3:app
Input: nums = [1,1,1,2,2,2]
Output: 5
Example 4:ide
Input: nums = [10,2,8,9,3,8,1,5,2,3,7,6]
Output: 8this
Constraints:spa
2 <= nums.length <= 10^5
1 <= nums[i] <= 10^5code
给出一个正整数数组 nums,请你帮忙从该数组中找出能知足下面要求的 最长 前缀,并返回其长度:
从前缀中 删除一个 元素后,使得所剩下的每一个数字的出现次数相同。
若是删除这个元素后没有剩余元素存在,仍可认为每一个数字都具备相同的出现次数(也就是 0 次)。
示例 1:
输入:nums = [2,2,1,1,5,3,3,5]
输出:7
解释:对于长度为 7 的子数组 [2,2,1,1,5,3,3],若是咱们从中删去 nums[4]=5,就能够获得 [2,2,1,1,3,3],里面每一个数字都出现了两次。
示例 2:
输入:nums = [1,1,1,2,2,2,3,3,3,4,4,4,5]
输出:13
示例 3:
输入:nums = [1,1,1,2,2,2]
输出:5
示例 4:
输入:nums = [10,2,8,9,3,8,1,5,2,3,7,6]
输出:8
提示:
2 <= nums.length <= 10^5
1 <= nums[i] <= 10^5
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 }