Skip to content

Commit 37f04db

Browse files
committed
add 169, 2554
1 parent a5d5ad0 commit 37f04db

2 files changed

+47
-0
lines changed

0169-majority-element.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
169. Majority Element
3+
4+
Submitted: December 5, 2024
5+
6+
Runtime: 0 ms (beats 100.00%)
7+
Memory: 28.15 MB (beats 20.01%)
8+
*/
9+
10+
class Solution {
11+
public:
12+
int majorityElement(vector<int>& nums) {
13+
unordered_map<int, int> map;
14+
for ( const int num : nums ) {
15+
map[num]++;
16+
}
17+
for ( auto& kv : map ) {
18+
// problem statement says that floor(n / 2) is majority
19+
if (kv.second > (nums.size() / 2)) return kv.first;
20+
}
21+
return -1;
22+
}
23+
};
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
/*
2+
2554. Maximum Number of Integers to Choose From a Range I
3+
4+
Submitted: December 5, 2024
5+
6+
Runtime: 200 ms (beats 46.95%)
7+
Memory: 177.84 MB (beats 60.98)
8+
*/
9+
10+
class Solution {
11+
public:
12+
int maxCount(vector<int>& banned, int n, int maxSum) {
13+
// set for O(1) operations
14+
unordered_set<int> banned_set(banned.begin(), banned.end());
15+
int sum(0);
16+
int res(0);
17+
for (int i = 1; i != n + 1 && sum + i <= maxSum; ++i) {
18+
if (banned_set.count(i)) continue; // ignore banned numbers
19+
++res;
20+
sum += (i);
21+
}
22+
return res;
23+
}
24+
};

0 commit comments

Comments
 (0)