Gabriel Feyer
BAN USERC++ solution, O(n) complexity.
#include <iostream>
#include <list>
#include <unordered_map>
using namespace std;
// Time complexity O(n)
pair<int,int> findMaxOccurence(list<int> sll){
// Save occurence of all numbers in the list. 1 Pass.
unordered_map<int,int> rankMap;
for(auto it : sll){
rankMap[it]++;
}
// Get maxx occurence #
int maxOcc = 0;
for(auto it: rankMap){
if(maxOcc < it.second)
maxOcc = it.second;
}
//Find first pair that has the max. occurences and return it.
for(auto it : sll){
if(rankMap[it] == maxOcc)
return make_pair(it, rankMap[it]);
}
}
int main()
{
list<int> list1 = {1,2,3,4,2,3,2};
pair<int,int> maxOcc = findMaxOccurence(list1);
cout << "Element " << maxOcc.first << " occurs " << maxOcc.second << " time(s)" << endl;
list1 = {4,3,5,3,4,5};
maxOcc = findMaxOccurence(list1);
cout << "Element " << maxOcc.first << " occurs " << maxOcc.second << " time(s)" << endl;
}
Output:
Element 2 occurs 3 time(s)
Element 4 occurs 2 time(s)
Process returned 0 (0x0) execution time : 0.015 s
Press any key to continue.
#include <iostream>
#include <map>
using namespace std;
void insertTask(map<char,int>& m, char lowTask, char highTask){
m[highTask] += m[lowTask]+1;
}
map<int,char> toRankedMap(map<char,int> m){
map<int,char> rankedMap;
for(auto it : m){
rankedMap[it.second] = it.first;
}
return rankedMap;
}
int main()
{
map<char,int> tempMap;
insertTask(tempMap,'A','B'); //A <- B
insertTask(tempMap,'A','C'); //A <- C
insertTask(tempMap,'B','C'); //B <- C
insertTask(tempMap,'B','D'); //B <- D
insertTask(tempMap, 'D', 'C'); //D <- C
auto rankedMap = toRankedMap(tempMap);
for(map<int,char>::reverse_iterator rit = rankedMap.rbegin(); rit != rankedMap.rend(); ++rit){
cout << rit->second << " < ";
}
return 0;
}
Output:
C < D < B < A <
Process returned 0 (0x0) execution time : 0.015 s
Press any key to continue.
#include <iostream>
#include <string>
using namespace std;
typedef string::iterator iter;
bool reg(iter is, iter ie, iter rs, iter re){
//cout << *is << " " << *rs << endl;
if(is == ie && rs == re)
return true;
if(is == ie && rs != re){
if(*rs == '+') return reg(is,ie,++rs,re);
if(*rs == '*') return reg(is,ie,++rs,re);
return false;
}
if(is != ie && rs == re)
return false;
if(*rs == '+')
return reg(++is,ie,++rs,re);
if(*rs == '*'){
bool r;
rs++;
while(is != ie){
r |= reg(++is,ie,rs,re);
}
return r;
}
if(*is == *rs)
return reg(++is,ie,++rs,re);
if(*is != *rs)
return false;
}
//wrapper
bool isMatch(string s, string regx){
cout << "Checking " << s << " against " << regx << " ... ";
if(reg(s.begin(), s.end(), regx.begin(), regx.end())){
cout << "MATCH !" << endl;
return true;
}else{
cout << "no match found." <<endl;
return false;
}
}
int main(){
string text = "baaabab";
isMatch(text, "ba*a++");
isMatch(text, "ba*a+");
isMatch(text, "a*ab");
isMatch("","+");
}
Output:
Checking baaabab against ba*a++ ... MATCH !
Checking baaabab against ba*a+ ... MATCH !
Checking baaabab against a*ab ... no match found.
Checking against + ... MATCH !
...Program finished with exit code 0
Press ENTER to exit console.
P.S. Sorry for the spam above, couldn't tell if the post worked or not...dang it.
Repsheilaahollins, Blockchain Developer at ASAPInfosystemsPvtLtd
Hello, I am working as a mineralogist studying rocks, gems, and other minerals, including their chemical and crystalline structures. I ...
Reparlenekraft8, How to book a seat on Southwest airlines flight at Absolute Softech Ltd
I am a coding specialist from MD,USA.I’m indifferent to most items on the planet.Participated in creating ...
Repmarierlewis2, AT&T Customer service email at ASAPInfosystemsPvtLtd
I am Marie and I live in Los Angeles USA, I have a strong base in marketing and I believe ...
Repclairetsmith49, AT&T Customer service email at ADP
Hello, I am Claire and work as a Human resources and I am responsible for recruiting, screening, interviewing and how ...
Repjohnlevans657, Principal Software Engineer at Ask.com
Hi, I am John, from Taxes USA, I am a dedicated, extremely organized, and highly competent Administrative Specialist seeking a ...
C++ Solution.
Assumption: int m (level) is also the limit length for the key. If this assumption is not agreeable, it's easy to increase the level and filter strings that have more than m of the same character....
OUTPUT:
- Gabriel Feyer May 23, 2018Constructed Ids: [
3 2 1 33 32 31 23 22 21 13 12 11 333 332 331 323 322 321 313 312 311 233 232 231 223 222 221 213 212 211 133 132 131 123 122 121 113 112 111 ]
next id: 3
next id: 2
next id: 1
next id: 33
next id: 32
next id: 31
next id: 23
next id: 22
next id: 21
next id: 13
next id: 12
next id: 11
next id: 333
next id: 332
next id: 331
next id: 323
next id: 322
next id: 321
next id: 313
next id: 312
next id: 311
next id: 233
next id: 232
next id: 231
next id: 223
next id: 222
next id: 221
next id: 213
next id: 212
next id: 211
next id: 133
next id: 132
next id: 131
next id: 123
next id: 122
next id: 121
next id: 113
next id: 112
next id: 111
All IDs have been used.
Process returned 0 (0x0) execution time : 0.047 s
Press any key to continue.