[Swift]LeetCode1224. 最大相等頻率 | Maximum Equal Frequency

★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★
➤微信公衆號:爲敢(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


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 }
相關文章
相關標籤/搜索