当前位置:网站首页>LeetCode Week 303
LeetCode Week 303
2022-08-01 16:32:00 【William_Tao (Siege Lion)】
LeetCode第 303 场周赛
第 303 场周赛
自己选择的路,Go on happily
2351. 第一个出现两次的字母
第一个出现两次的字母
循环扫描字符串s,For each character encountered, the number of corresponding characters is incremented by one
If the first character counts 2That means the element appears first,直接返回.复杂度o(n^3)
class Solution {
public:
char repeatedCharacter(string s){
char res;
map<char,int> m;
for(auto& c:s){
++m[c];
if(m[c]==2){
return c;}
}
return ' ';
}
};
2352. 相等行列对
2352. 相等行列对
思路:
暴力枚举
- n行n列的矩阵,Loop through the elements of each row and column,If the corresponding elements of the row and column are not equal then it cannot be the answer.
- If the elements of the corresponding row and column are all equal,则答案加一.
class Solution {
public:
int equalPairs(vector<vector<int>>& grid) {
int row = grid[0].size();
int col = grid.size();
int cnt=0;
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
bool same = true;
for(int k =0;k<col &&same;k++){
if(grid[i][k]!=grid[k][j]){
same = false;
}
}
if(same){cnt++;}
}
}
return cnt;
}
};
思路二:
Transpose the matrix.
Compare the values of the corresponding elements to see if they are equal
复杂度o(n^2)
class Solution {
public:
int equalPairs(vector<vector<int>>& grid) {
int row = grid[0].size();
int col = grid.size();
int cnt=0;
bool same ;
vector<vector<int>> Revsgrid(row,vector<int>(col));
for(int i=0;i<row;i++){
for(int j=0;j<col;j++){
Revsgrid[i][j] =grid[j][i];
}
}
//In fact, it is to know whether the corresponding positions are equal when comparing rows
for(auto t:grid){
for(auto p:Revsgrid){
if(t==p) cnt++;
}
}
return cnt;
}
};
方法三:
hashmap
Store each row element of the matrix as a string(Because row elements can be many,不方便存储),Then the elements of the matrix are followed“,”划分.Store each line of string in hashmap作为key,同时value加一
Transposes are stored in columns,If it is found that there is a corresponding value stored by row before,则将mp中的valueTake out and add to the result(mp中的key可能重复,So it can't just add one)
class Solution {
public:
int equalPairs(vector<vector<int>>& grid) {
int row = grid[0].size();
int col = grid.size();
int cnt=0;
unordered_map<string, int> mp;
bool same ;
for(int i=0;i<row;i++){
string s = "";
for(int j=0;j<col;j++){
s+=to_string(grid[i][j]);
s+=",";
}
//Remove the last character,
s.pop_back();
mp[s]++;
}
//
for(int i=0;i<row;i++){
string s = "";
for(int j=0;j<col;j++){
s+=to_string(grid[j][i]);
s+=",";
}
//Remove the last character,
s.pop_back();
if(mp.count(s)){
cnt+=mp[s];
}
}
return cnt;
}
};
2353. 设计食物评分系统

思路:
平衡树(有序集合)
我们可以用一个哈希表 \textit{fs}fs Record the food rating and cooking method for each food name,Another hash table sets a balanced tree cs Record the food score and food name set corresponding to each cooking method.
对于 changeRating 操作,先从cs[fs[food].cuisine]delete old data,然后将 newRating}newRating 和 food 记录到 cs 和fs 中.
这里pairUse negative for ratings in , 这里放入“negative rating”和字符串,这样“negative rating”小的,That is, the food with the highest rating will be ranked first
作者:endlesscheng
链接:https://leetcode.cn/problems/design-a-food-rating-system/solution/ha-xi-biao-tao-ping-heng-shu-by-endlessc-hzct/
来源:力扣(LeetCode)
著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.
class FoodRatings {
//set中pair这样设计是为了使得pairFood ratings are compared first by default,Then compare the following food name dictionary sorting
unordered_map<string,set<pair<int,string>>> cs;;// 烹饪方式----> (食物评分,食物名字)
unordered_map<string,pair<int,string>> fs;//食物 ---->(食物评分,烹饪方式)
public:
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
for(int i = 0;i<foods.size();i++){
auto& f = foods[i],&c = cuisines[i];
//cout<<f<<":"<<c<<endl;
int r = ratings[i];
fs[f] = {r,c};
//Creates an element before the element pointed to by the iterator,and returns an iterator of newly added elements;
cs[c].emplace(-r,f);//Make sure the smallest corresponds to the highest rating
}
}
void changeRating(string food, int newRating) {
auto &[r,c] = fs[food];
auto &s = cs[c];
//Remove old elements
s.erase({-r,food});
//把新的添加进来
s.emplace(-newRating,food);
//Modify the rating(下一次再调用changeRatigThe value is guaranteed to be the value after the change)
r = newRating;
}
string highestRated(string cuisine) {
return cs[cuisine].begin()->second;
}
};
2354. 优质数对的数目
思路:等价转换+脑筋急转弯
https://leetcode.cn/problems/number-of-excellent-pairs/solution/deng-jie-zhuan-huan-pythonjavacgo-by-end-2qzs/
class Solution {
public:
long long countExcellentPairs(vector<int>& nums, int k) {
long res= 0L;
unordered_map<int,int> mp;
for(int x:unordered_set<int>(nums.begin(),nums.end())){
mp[__builtin_popcount(x)]++;//A statistic will be performed on how many binary numbers are counted
}
for(auto&[cx,ccx]:mp){
for(auto&[cy,ccy]:mp){
if(cx+cy>=k){
res+=(long)ccx*ccy;
}
}
}
return res;
}
};
总结:
This weekly competition is done through practice,并没有参加.
T1:属于一个hashmap存储,Priority is stored to2is the first letter that appears(不算难)
T2:The main underlying core is the information that can put the ranks in the question,Transpose the matrix and then compare the elements at the corresponding positions,主要有直接暴力枚举,矩阵转置,hashmap.But I feel that matrix transpose is better,hashmapThe idea is similar to matrix transpose,And don't even think about it,还有点麻烦.(个人感觉)
T3:The main thing is to set up a good data structure,be able to meet the needs of the final result.The difficulty lies in the design of the data structure.
T4:It belongs to the content related to brain teasers and collections,有点吃力.(Still don't seem to understand!)At the same time look at the analysis time,学到了__builtin_popcount(x)这一个函数.用于计算一个 32 位无符号整数有多少个位为1
The following process will continue to summarize the topics of the previous weekly competition,Do a consolidation.At the same time keep learningC++中stlSome commonly used knowledge points.
边栏推荐
猜你喜欢

软件测试谈薪技巧:同为测试人员,为什么有人5K,有人 20K?

ESP8266-Arduino编程实例-GA1A12S202对数刻度模拟光传感器

Break the limit of file locks and use storage power to help enterprises grow new momentum

直播app开发,是优化直播体验不得不关注的两大指标

百图生科卓越开发者计划全面升级暨《计算免疫问题白皮书》发布

指针进阶(二)

Use Canvas to implement mobile phone signature

8年软件测试工程师感悟 —— 写给还在迷茫中的朋友

便携烙铁开源系统IronOS,支持多款便携DC, QC, PD供电烙铁,支持所有智能烙铁标准功能

IronOS, an open source system for portable soldering irons, supports a variety of portable DC, QC, PD powered soldering irons, and supports all standard functions of smart soldering irons
随机推荐
全新升级!《云原生架构白皮书 2022 版》重磅发布
HashCode technology insider interview must ask
Convert tensor to image in pytorch
pytorch中tensor转成图片保存
Go unit tests
03 gp 集群搭建
年化收益高的理财产品
第一次改开源中间件keycloak总个结
清华教授发文劝退读博:我见过太多博士生精神崩溃、心态失衡、身体垮掉、一事无成!...
How to Efficiently Develop Jmix Extension Components
04 flink 集群搭建
A full review of mainstream timed task solutions
27英寸横置大屏+实体按键,全新探险者才是安全而合理的做法
【硬核拆解】50块2个的2022年夏季款智能节电器到底能不能省电?
重庆银河证券股票开户安全吗,是正规的证券公司吗
OneFlow源码解析:Op、Kernel与解释器
便携烙铁开源系统IronOS,支持多款便携DC, QC, PD供电烙铁,支持所有智能烙铁标准功能
ODrive开发 #1 ODrive固件开发指南[通俗易懂]
兆骑科创平台招才引智,海内外高层次人才引进平台
06 redis 集群搭建
