Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Majorityelement#229 #48

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Medium/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Problem #229 (Majority Element II | Array, Counting, Hash Table, Sorting)
Medium

Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.

Example 1
Input: nums = [3,2,3] **Output:** [3]`

Example 2:
Input: nums = [1]
Output: [1]

Example 3:
Input: nums = [1,2]
Output: [1,2]

Constraints:
. 1<=nums.length<= 5*104
. -109<=nums[i]<=10^9
35 changes: 35 additions & 0 deletions Medium/solution.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> ans;
int candidate1 = 0;
int candidate2 = 1; // Any number different from candidate1
int countSoFar1 = 0; // # of candidate1 so far
int countSoFar2 = 0; // # of candidate2 so far

for (const int num : nums)
if (num == candidate1) {
++countSoFar1;
} else if (num == candidate2) {
++countSoFar2;
} else if (countSoFar1 == 0) { // Assign new candidate
candidate1 = num;
++countSoFar1;
} else if (countSoFar2 == 0) { // Assign new candidate
candidate2 = num;
++countSoFar2;
} else { // Meet a new number, so pair out previous counts
--countSoFar1;
--countSoFar2;
}

const int count1 = count(nums.begin(), nums.end(), candidate1);
const int count2 = count(nums.begin(), nums.end(), candidate2);

if (count1 > nums.size() / 3)
ans.push_back(candidate1);
if (count2 > nums.size() / 3)
ans.push_back(candidate2);
return ans;
}
};
40 changes: 40 additions & 0 deletions Medium/solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> ans = new ArrayList<>();
int candidate1 = 0;
int candidate2 = 1; // Any number different from candidate1
int countSoFar1 = 0; // # of candidate1 so far
int countSoFar2 = 0; // # of candidate2 so far

for (final int num : nums)
if (num == candidate1) {
++countSoFar1;
} else if (num == candidate2) {
++countSoFar2;
} else if (countSoFar1 == 0) { // Assign new candidate
candidate1 = num;
++countSoFar1;
} else if (countSoFar2 == 0) { // Assign new candidate
candidate2 = num;
++countSoFar2;
} else { // Meet a new number, so pair out previous counts
--countSoFar1;
--countSoFar2;
}

int count1 = 0;
int count2 = 0;

for (final int num : nums)
if (num == candidate1)
++count1;
else if (num == candidate2)
++count2;

if (count1 > nums.length / 3)
ans.add(candidate1);
if (count2 > nums.length / 3)
ans.add(candidate2);
return ans;
}
}