Weekly Contest 168

5291. Find Numbers with Even Number of Digits

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public:
int findNumbers(vector<int>& nums) {
int count = 0;
for(auto num: nums){
int t = num;
int rem = 0;
if(t == 0) continue;
while(t){
t /= 10;
rem++;
}
if(rem % 2 == 0) count++;
}
return count;
}
};

5292. Divide Array in Sets of K Consecutive Numbers

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
bool isPossibleDivide(vector<int>& nums, int k) {
unordered_map<int, int> mp;
for(auto i: nums) mp[i]++;
sort(nums.begin(), nums.end());
for(int i=0; i<nums.size(); i++){
int cur = nums[i];
if(mp.find(cur) == mp.end()) continue;
for(int j=cur; j<cur+k; j++){
if(mp.find(j) == mp.end() || mp[j] == 0) return false;
mp[j]--;
if(mp[j] == 0) mp.erase(j);
}
}
return mp.size() == 0;
}
};

5293. Maximum Number of Occurrences of a Substring

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Solution {
public:
int maxFreq(string s, int maxLetters, int minSize, int maxSize) {
unordered_map<string, int> subs{};
int result = 0;
for(int i=0; i<=s.size()-minSize; i++){
string sub = s.substr(i, minSize);
set<char> chars{};
for(auto c: sub) chars.insert(c);
if(chars.size() <= maxLetters) result = max(result, ++subs[sub]);
}
return result;
}
};

5294. Maximum Candies You Can Get from Boxes

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class Solution {
public:
int maxCandies(vector<int>& status, vector<int>& candies, vector<vector<int>>& keys, vector<vector<int>>& containedBoxes, vector<int>& initialBoxes) {
int result=0; //total number of candies
vector<int> boxes(initialBoxes.begin(),initialBoxes.end()); //the boxes we have (including both open or not open)
unordered_set<int> v; //the boxes that we have already visited
while(true)
{
int flag=0; //check if the process ends
for(int i=(int)boxes.size()-1;i>-1;i--)
{
if(status[boxes[i]]==1&&v.find(boxes[i])==v.end())//is open and not visited yet
{
flag=1; //the process doesn't end
for(int j=0;j<keys[boxes[i]].size();j++) status[keys[boxes[i]][j]]=1; //we use the key to open the boxes
for(int j=0;j<containedBoxes[boxes[i]].size();j++) boxes.push_back(containedBoxes[boxes[i]][j]); //we get more boxes
v.insert(boxes[i]); //the box has been visited
result+=candies[boxes[i]]; //we get the candies
boxes.erase(boxes.begin()+i); //the box has been visited, we removed it from the BFS process
}
}
if(flag==0) break; //if the process ends
}
return result;
}
};
Donate? comment?