- Friends of appropriate age
People send friend requests to each other. Now given an array containing their age, ages[i] indicates the age of the ith person. When either of the following conditions is met, A cannot send friend requests to B (A, B are not the same person):
age[B] <= 0.5 * age[A] + 7
age[B] > age[A]
age[B] > 100 && age[A] < 100
Otherwise, A can send a friend request to B.
Note that if A makes a request to B, it doesn't equal B. Also, people don't send themselves friend requests.
Ask how many Friend Requests will be sent in total?
After sorting the barrels, you can just iterate through them by age.
class Solution { public: int numFriendRequests(vector<int>& ages) { int cnt[121]; memset(cnt, 0 , sizeof(cnt)); for (int age : ages) { ++cnt[age]; } int res = 0; for (int a = 0; a <= 120; ++a) { int cntA = cnt[a]; if (cntA == 0) { continue; } for (int b = 0; b <= 120; ++b) { int cntB = cnt[b]; if (cntB == 0) { continue; } if ((a*0.5+7 >= b) || (b > a) || (a < 100 && 100 < b)) { continue; } { // cout << cntA << " " << cntB << " " << a << " vs " << b << endl; res += cntA * cntB; if (a == b) { res -= cntA; } } } } return res; } };
- Arrange work to maximize benefits
There are some jobs: difficulty[i] indicates the difficulty of the first job and profit[i] indicates the benefit of the second job.
Now we have some workers. The worker[i] is the ability of the first worker, that is, the worker can only complete jobs that are less difficult than or equal to the worker[i].
Each worker can only schedule at most one job, but a job can be completed multiple times.
For example, if three workers try to do the same job for a reward of 1, the total benefit is $3. If a worker cannot do any work, his benefit is $0.
What is the maximum benefit we can get?
The key point of using hash table to store the relationship between difficulty and benefit is to use prefix and save maximum benefit as well as get maximum benefit when different work difficulties are the same
class Solution { public: int maxProfitAssignment(vector<int>& difficulty, vector<int>& profit, vector<int>& worker) { int ret = 0; int nPos = 0; int nSize = difficulty.size(); std::unordered_map<int, int> ProfitMap; if (nSize == 0) { return ret; } for (int i = 0; i < nSize; i++) { int nBefore = 0; if (ProfitMap.find(difficulty[i]) != ProfitMap.end()) { nBefore = ProfitMap[difficulty[i]]; } ProfitMap[difficulty[i]] = max(profit[i], nBefore); } sort(difficulty.begin(), difficulty.end()); sort(worker.begin(), worker.end()); for (int i = 1; i < nSize; i++) { ProfitMap[difficulty[i]] = max(ProfitMap[difficulty[i]], ProfitMap[difficulty[i - 1]]); } for (int i = 0; i < worker.size(); i++) { while (difficulty[nPos] <= worker[i] && nPos < nSize) { nPos++; } nPos--; if (nPos < 0) { nPos = 0; continue; } ret += ProfitMap[difficulty[nPos]]; } return ret; } };
- Maximum Artificial Island
You are given a binary matrix grid of size n x n. You can only change a grid of 0 to 1 at most. When you return to this operation, what is the maximum island area in the grid? Islands are formed from a group of 1 connected in the upper, lower, left, and right directions.
DFS traverses each island, then DFS traverses the ocean for queries
#define maxn 250005 int getIdx(int i,int j,int m){ return i*m+j; } class UF{ public: int fa[maxn],rank[maxn],size[maxn]; void init(vector<vector<int>>&grid){ int n=grid.size(),m=grid[0].size(); for(int i=0;i<n*m;i++){ fa[i]=i; rank[i]=1; } for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ int idx=getIdx(i,j,m); if(grid[i][j]==1)size[idx]=1; else size[idx]=0; } } } int find(int x){ return x==fa[x]?x:(fa[x]=find(fa[x])); } void uni(int i,int j){ int x=find(i),y=find(j); if(x==y)return; if(rank[x]<rank[y]){ fa[x]=y; size[y]+=size[x]; }else{ fa[y]=x; size[x]+=size[y]; } if(rank[x]==rank[y]&&x!=y)rank[y]++; } int getSize(int i){ int root=find(i); return size[root]; } }; class Solution { public: int largestIsland(vector<vector<int>>& grid) { int n=grid.size(),m=grid[0].size(); UF uf; uf.init(grid); for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(grid[i][j]==1){ if(i-1>=0&&grid[i-1][j]==1){ uf.uni(getIdx(i,j,m),getIdx(i-1,j,m)); } if(j-1>=0&&grid[i][j-1]==1){ uf.uni(getIdx(i,j,m),getIdx(i,j-1,m)); } } } } int ans=0; for(int i=0;i<n;i++){ for(int j=0;j<m;j++){ if(grid[i][j]==0){ vector<int>root; if(i-1>=0&&grid[i-1][j]==1){ root.push_back(uf.find(getIdx(i-1,j,m))); } if(i+1<n&&grid[i+1][j]==1){ root.push_back(uf.find(getIdx(i+1,j,m))); } if(j-1>=0&&grid[i][j-1]==1){ root.push_back(uf.find(getIdx(i,j-1,m))); } if(j+1<m&&grid[i][j+1]==1){ root.push_back(uf.find(getIdx(i,j+1,m))); } sort(root.begin(),root.end()); root.erase(unique(root.begin(),root.end()),root.end()); int tmpAns=1; for(int i=0;i<root.size();i++){ tmpAns+=uf.getSize(root[i]); } ans=max(ans,tmpAns); } } } if(ans==0){ if(grid[0][0]==0)return 0; if(grid[0][0]==1)return n*m; } return ans; } };
- Unique character in statistics substring
We define a function, countUniqueChars(s), to count the unique characters in string s and return the number of unique characters.
For example: s = LEETCODE, where "L", "T", "C", "O", "D" are unique characters because they only appear once, countUniqueChars(s) = 5.
This question will give you a string s, and we need to return the sum of countUniqueChars(t), where t is the substring of S. Note that some substrings may be duplicated, but you must also count these duplicate substrings (that is, you must count the unique characters in all the substrings of s).
Since the answer may be very large, please return to the result mod 10 ^ 9 + 7.
26 characters, next to each statistical interval
class Solution { public: int uniqueLetterString(string s) { int len=s.length(); vector<int> left(len,-1); vector<int> right(len,-1); //Find Left Endpoint vector<int> prev(26,-1); for(int i=0;i<len;i++){ left[i]=prev[s[i]-'A']; prev[s[i]-'A']=i; } //Find Right Endpoint for(int i=0;i<26;i++){ prev[i]=len; } for(int i=len-1;i>=0;i--){ right[i]=prev[s[i]-'A']; prev[s[i]-'A']=i; } //Calculate contribution of characters based on intervals long long int ans=0; for(int i=0;i<len;i++){ ans=(ans+(i-left[i])*(right[i]-i))%1000000007;//Calculation } return ans; } };
- Sum of Continuous Integers
Given a positive integer N, how many consecutive sets of positive integers satisfy the sum of all numbers to be N?
Mathematical problems: List the formula N = x + (x + 1) +...+ (x + k), merge to find the law, because K <= 2N, calculate x to meet the non-negative integer will get a solution. In addition, by mining conditions, you can only find K < root 2N
class Solution { public: int consecutiveNumbersSum(int n) { while ((n & 1) == 0) n >>= 1; int ans = 1, d = 3; while (d * d <= n) { int e = 0; while (n % d == 0) { n /= d; e++; } ans *= e + 1; d += 2; } if (n > 1) ans <<= 1; return ans; } };
830.Location of larger groups
In a string s that consists of lowercase letters, there is a grouping of consecutive identical characters.
For example, in the string s = "abbxxxxzyy", there are groupings such a s "a", "bb", "xxxx", "z" and "yy".
Grouping can be expressed as an interval [start, end], where start and end represent the subscripts to the starting and ending positions of the grouping, respectively. In the example above,'xxx'grouping is represented as [3,6] by an interval.
We call all groups that contain three or more consecutive characters larger than or equal to a larger group.
Find the intervals for each large group, and return the results after sorting them in ascending order by the subscript at the starting position.
Traverse the record grouping length and judge
class Solution { public: vector<vector<int>> largeGroupPositions(string s) { vector<vector<int>> ret; int n = s.size(); int num = 1; for (int i = 0; i < n; i++) { if (i == n - 1 || s[i] != s[i + 1]) { if (num >= 3) { ret.push_back({i - num + 1, i}); } num = 1; } else { num++; } } return ret; } };
- Hide personal information
Give you a personal information string S, which may be a mailbox address or a series of phone numbers. The information provided is properly hidden and returned.
Simple use of strings
class Solution { public: string maskPII(string s) { if(s.find('@')==string::npos)//Telephone { for(int i=0;i<s.size();++i)//Delete all non-numeric characters { if(isdigit(s[i]))continue; s.erase(s.begin()+i); --i; } s.replace(s.begin(),s.end()-4,s.end()-s.begin()-4,'*');//Replace (start, end, new string length, new character) s.insert(s.end()-4,'-'); s.insert(s.end()-8,'-'); if(s.size()>12) { s.insert(s.end()-12,'-'); s.insert(s.begin(),'+'); } } else//mailbox { transform(s.begin(),s.end(),s.begin(),::tolower);//All lowercase /*Another way for(char& c : s) tolower(c); */ s.replace(1,s.find('@')-2,"*****");//Replace (start position, string length, new string) } return s; } };