Data Structure Array: Given an array of of size n and a number k, find all elements that appear more than n/k times
2014-04-10 04:21
776 查看
http://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-than-nk-times/
这一题如果没空间要求就没那么麻烦了
这一题如果没空间要求就没那么麻烦了
#include <iostream> #include <vector> #include <algorithm> #include <queue> #include <stack> #include <string> #include <fstream> #include <map> using namespace std; void more(int arr[], int n, int k) { map<int, int> S; for (int i = 0; i < n; i++) { if (S.find(arr[i]) != S.end()) S[arr[i]]++; else if (S.size() == k) { for (map<int, int>::iterator it = S.begin(); it != S.end(); it++) { if (it->second == 1) S.erase(it->first); else it->second--; } } else S[arr[i]] = 1; } for (map<int, int>::iterator it = S.begin(); it != S.end(); it++) { int tmp = 0; for (int i = 0; i < n; i++) { if (arr[i] == it->first) tmp++; } if (tmp > n / k) cout << it->first << " "; } } int main() { int arr[7] = {4, 5, 6, 7, 8, 4, 4}; more(arr, 7, 3); return 0; }
相关文章推荐
- [LinkedIn] Array of size n and an int k, find all elements that appear/occur more than n/k times
- Given an array of size n, find the majority element. The majority element is the element that appear
- Data Structure Array: Given an array arr[], find the maximum j – i such that arr[j] > arr[i]
- Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive
- Given an array of size n, find all the possible sub set of the array of size k
- CareerCup Finds all the elements that appear more than n/3 times in the list
- Given an array of size N in which every number is between 1 and N, determine if there are any dupli
- 1. 找出数组中的单身狗OddOccurrencesInArray Find value that occurs in odd number of elements.
- Find all max of elements of an array
- Find all max of elements of an array
- Data Structure Array: Maximum of all subarrays of size k
- Kettle解析JSON错误,We MUST have the same number of values for all paths,We can not find and data with path [$.
- given an array of integers Find the index of values that satisfy A+B = C + D
- Given constant integers x and t, write a function that takes no argument and returns true if the function has been called x number of times in last t secs.
- Given an array of integers, every element appears twice except for one. Find that single one.
- How to limit an array of similar hashes to those that have more than one of the same key:value pair
- Given an array of positive and negative integers find the first subarray with zero sum
- Given an array of integers, every element appears twice except for one. Find that single one.
- Ruby: How to find all indices of elements that match a given condition?
- find all pairs of elements in a balanced BST that sum to a certain number