5989. Element count
Title Description: give you an array nums. For the element X in the array, if y and Z exist in the array and meet the condition y < x < Z, then x is said to be good, and count the number of good numbers in the array.
Idea: the data range is very small, and direct violence is enough. If the data range is too large, use map and other statistics to calculate the maximum and minimum values, and the complexity can be \ (O(nlogn) \).
Time complexity: \ (O(nlogn) \)
Reference code:
class Solution { public: int countElements(vector<int>& nums) { sort(nums.begin() ,nums.end()); int res = nums.size(); if(nums[0] == nums.back()) return 0; map<int, int>mp; for(auto num : nums) mp[num]++; res -= mp[nums[0]]; res -= mp[nums.back()]; return res; } };
5991. Rearrange arrays by symbol
Title Description: give you an array of positive and negative half, rearrange the array so that the positive and negative are alternating, and do not change the relative order of the original positive and negative numbers.
Idea: simulate according to the meaning of the topic.
Time complexity: \ (O(n) \)
Reference code:
class Solution { public: vector<int> rearrangeArray(vector<int>& nums) { vector<int>neg , pos; for(auto num : nums){ if(num < 0) neg.push_back(num); else pos.push_back(num); } int n = neg.size(); vector<int>res; for(int i = 0 ; i < n ; ++i){ res.push_back(pos[i]); res.push_back(neg[i]); } return res; } };
5990. Find all the numbers in the array
Title Description: give you an array nums. If the number x in the array only appears once and X + 1 and X - 1 do not appear in the array, it is said that this number is good. Count the number of good numbers in the array.
Idea: take a map to store the number of occurrences of each number, and then scan it again to count the answers.
Time complexity: \ (O(nlogn) \)
Reference code:
class Solution { public: vector<int> findLonely(vector<int>& nums) { map<int , int>map; for(int num : nums) map[num]++; vector<int>res; for(int num : nums){ if(map[num] > 1) continue; if(map.count(num - 1) == 0 && map.count(num + 1) == 0) res.push_back(num); } return res; } };
5992. Count the best number of people based on statements
Title Description: give you a \ (n \times n \) two-dimensional array statements to represent the mutual description of \ (n \) individuals:
- statements[i][j] = 1 means I think j is a good man
- statements[i][j] = 0 means I thinks j is a bad person
- statements[i][j] = 2 means I does not describe j
Good people tell the truth, bad people may tell the truth or lie, and ask the maximum possible number of good people.
Data range: \ (2 \leq n \leq 15 \)
Idea: considering the small data range, you can violently enumerate all possible good and bad people, with complexity \ (O(2^n) \), and then check whether the current enumeration meets the meaning of the question, with complexity \ (O(n^2) \). You can pass this question.
Time complexity: \ (O(2^n \times n^2) \)
Reference code:
class Solution { public: int maximumGood(vector<vector<int>>& statements) { int n = statements.size(); vector<int> path(n , 0); int res = 0; auto check = [&]()->bool{ for(int i = 0 ; i < n ; ++i){ if(path[i] == 0) continue; for(int j = 0 ; j < n ; ++j){ if(i == j) continue; if(path[j] == 0 && statements[i][j] == 1) return false; if(path[j] == 1 && statements[i][j] == 0) return false; } } return true; }; auto dfs = [&](auto dfs , int cur, int idx)->void{ if(idx == n){ if(check()) res = max(res , cur); return ; } path[idx] = 0; dfs(dfs , cur, idx + 1); path[idx] = 1; dfs(dfs , cur + 1, idx + 1); }; dfs(dfs , 0 , 0); return res; } };