mirror of
https://github.com/20kaushik02/leetcode-gulag.git
synced 2025-12-06 07:34:06 +00:00
day 4: 169-majority-element
This commit is contained in:
parent
688a352dbe
commit
ae9f1fe46f
11
169-majority-element/driver.cpp
Normal file
11
169-majority-element/driver.cpp
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
vector<int> nums = {2, 2, 1, 1, 1, 2, 2};
|
||||||
|
int answer = 2;
|
||||||
|
Solution soln;
|
||||||
|
cout << "Majority element is correct? " << soln.test(nums, answer) << endl;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
17
169-majority-element/soln.cpp
Normal file
17
169-majority-element/soln.cpp
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
#include "soln.hpp"
|
||||||
|
|
||||||
|
int Solution::majorityElement(vector<int> &nums)
|
||||||
|
{
|
||||||
|
map<int, int> counts;
|
||||||
|
for (auto &it : nums)
|
||||||
|
{
|
||||||
|
counts[it]++;
|
||||||
|
if (counts[it] > (nums.size() / 2))
|
||||||
|
return it;
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool Solution::test(vector<int>& nums, int answer) {
|
||||||
|
return majorityElement(nums) == answer;
|
||||||
|
}
|
||||||
9
169-majority-element/soln.hpp
Normal file
9
169-majority-element/soln.hpp
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#include <bits/stdc++.h>
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
class Solution
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
int majorityElement(vector<int> &nums);
|
||||||
|
bool test(vector<int> &nums, int answer);
|
||||||
|
};
|
||||||
Loading…
x
Reference in New Issue
Block a user