Topic
main element III
Given an array of integers, the main element is found, and its occurrence in the array is strictly 1/kthan the number of elements in the array.
Sample Example
to the array [3,1,2,3,2,3,3,4,4,4] , and k = 3, returns 3
Note
Only the main elements in the array are unique
challenges
Requires a time complexity of O (n) and a space complexity of O (k)
Solving
The previous question just introduced the method used, but this is really very complex
First use HashMap to achieve
Public classSolution {/** * @paramnums:a List of integers *@paramK:as described *@return: The majority number*/ Public intMajoritynumber (arraylist<integer> nums,intk) {//Write your code if(Nums = =NULL) return0; HashMap<Integer,Integer> map =NewHashmap<integer,integer>(); intMajority = 0; for(inti = 0;i<nums.size (); i++){ intnum =Nums.get (i); if(Map.containskey (num)) {map.put (Num,map.get (num)-W); }Else{map.put (num,1); } if(Map.get (num) *k >nums.size ()) {Majority=num; Break; } } returnmajority; }}
Java Code
Total time: 1655 Ms
classSolution:"""@param nums:a List of integers @param k:as described @return: the majority number""" defMajoritynumber (self, nums, K):#Write your code here ifNums = =None:return0 D={} majority=0 forNuminchNums:ifNuminchD:d[num]+ = 1Else: D[num]= 1ifD[num]*k >Len (nums): Majority=Num Break returnMajority
Python Code
Total time: 307 MS
Advanced 2: The idea is that if there are k different numbers, it will be offset. It is necessary to use ingenious data structures to record candidates and make the following actions O (1):
1. Add a candidate/to a candidate number of occurrences +1
2. Is there a number in the candidates
3. Number of occurrences in candidates-1
4. Remove candidates with a number of 0 occurrences
For 1, 22 operations, we can naturally think of using a hash table to complete. For the first 42 operations, we want to be able to have the least number of candidate information, but it is not the time complexity of O (1) If the heap is used. Note that each time you add a candidate, Count is 1, and each time you change the number of occurrences of a candidate, it only involves adding 1 operations. Therefore, if we can maintain the order of candidates, we can solve this problem easily. The method is to use LinkedList. Unlike ordinary linkedlist, we put all occurrences of the same candidate in a bucket, the candidate in the bucket is linked with the doubly Linked list, and the buckets are also doubly. Linked list link up. So for the +1 operation, we only need to find the corresponding candidate through the hash table, and move the candidate from the current bucket to the next bucket (the bucket with +1 occurrences). In addition, for all operations of the number 1, we record a global base, each 1 operation, then base+1. If the candidates in the first bucket in base and buckets occur the same number of occurrences, the first bucket is deleted entirely. Finally, we'll get the maximum k-1 candidates, iterate over the entire array, and use the hash of O (k) to record the number of occurrences of this k-1 candidates, and you can verify who the real main element is.
PublicclassSolution {/** *@param nums:a List of integers*@param K:as described* @return: The majority number*/public int Majoritynumber (ArrayList<Integer>nums, int k) { //Write your code HashMap<Integer,Integer> counters = new hashmap<integer,integer>(); for(Integer num:nums) {if(!counters.containskey (num)) {counters.put (num,1); }Else{counters.put (num,counters.get (num)+ 1); } if(Counters.size () >=k) {RemoveKey (counters);//Empty}} if(counters.size () = =0) {returnInteger.min_value; } for(Integer i:counters.keyset ()) {counters.put (i,0); } for(Integer i:nums) {if(Counters.containskey (i)) {Counters.put (I,counters.get (i)+ 1); }} int Maxcounter=0; int Maxkey=0; for(Integer i:counters.keyset ()) {if(Counters.Get (i) >maxcounter) {Maxcounter=Counters.Get (i); Maxkey=i; } } returnMaxkey; } private void RemoveKey (HashMap<integer, integer>counters) {Set<Integer> KeySet =Counters.keyset (); List<Integer> removelist = new arraylist<>(); for(Integer key:keyset) {counters.put (key, Counters.Get (key)-1); if(Counters.Get (key) = =0) {removelist.add (key); } } for(Integer key:removelist) {counters.remove (key); } }}
Python Code
Total time: 1725 Ms
The expression was not understood thoroughly
Lintcode Main element: Majority Number III main element III