当前位置:网站首页>Week303 of leetcode
Week303 of leetcode
2022-07-25 15:27:00 【Ten thousand volt little sun】
LeetCode The first 303 Weekly match
Topic 1 6124. The first letter that appears twice
It's simple , According to the meaning , Use one map, Just check whether the array record has appeared .
Code :
class Solution {
public:
char repeatedCharacter(string s) {
map<char,int> hash;
for(auto x:s){
if(hash.count(x)) return x;
hash[x]++;
}
return 'a';
}
};
Topic two 6125. Equal row and column pairs
Enumerate each format , Then judge the line , Whether the columns are equal . Time complexity O ( n 3 ) O(n^3) O(n3)
Code :
class Solution {
public:
int equalPairs(vector<vector<int>>& g) {
int n=g.size();
int ans=0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
bool ok=true;
for(int k=0;k<n;k++){
if(g[i][k]!=g[k][j]) {
ok=false;
break;
}
}
if(ok) ans++;
}
}
return ans;
}
};
Topic three 6126. Design food scoring system
Ideas :
The title should realize the operation of checking the best value and changing , The first thing you can think of is the priority queue , But priority queue modification cannot be done . Then the topic is to find a certain way of cooking rating The highest value , So we can consider classification according to cooking methods , Use one map save , Then sort each category once , You can get the best value , When we think of sorting, we also think of using set Automatic sorting , Then according to the meaning of the topic , According to the first rating Sort , Press food Dictionary order , So I thought of pair Double keyword sort or , Structure overload sorting .
Then for each modification , Let's find this first food The way you cook , from map Of set Delete in , Rejoin a new rating and food
Code :
class FoodRatings {
public:
unordered_map<string,set<pair<int,string>>> h;
map<string,string> cu;
map<string,int> rat;
FoodRatings(vector<string>& foods, vector<string>& cuisines, vector<int>& ratings) {
int n=foods.size();
for(int i=0;i<n;i++){
h[cuisines[i]].insert({
-ratings[i],foods[i]});
cu[foods[i]]=cuisines[i];
rat[foods[i]]=ratings[i];
}
}
void changeRating(string food, int newRating) {
string cui=cu[food];
h[cui].erase({
-rat[food],food});
rat[food]=newRating;
h[cui].insert({
-newRating,food});
}
string highestRated(string cuisine) {
return h[cuisine].begin()->second;
}
};
/** * Your FoodRatings object will be instantiated and called as such: * FoodRatings* obj = new FoodRatings(foods, cuisines, ratings); * obj->changeRating(food,newRating); * string param_2 = obj->highestRated(cuisine); */
Topic four 6127. The number of high-quality pairs
Binary system , First, consider the relationship between binary bits .
We will a,b Take two numbers apart , Find out ,a|b+a&b Of 1 The number of is equal to a Of 1 The number of +b Of 1 The number of .
With this rule , Find each number 1 The number of , With arrays cnt This problem becomes to find an array , How many pairs? (i,j), bring cnt[i]+cnt[j]>=k.
take cnt After the array is sorted , There is monotony , You can use double pointer or bisection to find how many pairs .
The number of repetitions can be found not to affect the answer .
So before we do it, let's go to the heavy .
Code :
class Solution {
public:
long long countExcellentPairs(vector<int>& nums, int k) {
set<int> s;
for(auto &x:nums) s.insert(x);// duplicate removal
int n=s.size();
vector<int> cnt;
for(auto x:s){
int sum=0;
while(x) sum+=(x&1),x>>=1;// Get each number 1 The number of
cnt.push_back(sum);
}
long long ans=0;
sort(cnt.begin(),cnt.end());// Sort the number , monotonous
int pos=-1;
for(int i=0;i<n;i++){
int l=i,r=n-1;
while(l<=r){
// Find the first one that satisfies the sum of the two is greater than or equal to k The location of
int mid=l+r>>1;
if(cnt[mid]+cnt[i]>=k) r=mid-1,pos=mid;
else l=mid+1;
}
if(pos==-1) continue;
if(pos!=i) ans+=(n-pos)*2ll;// because cnt[i]+cnt[j] If meet , that (i,j) and (j,i) There are two different answers
else ans+=(n-pos)*2ll-1;// If it's the same location , Only one answer
}
return ans;
}
};
After reading other people's practice , I found a simpler way , because cnt The maximum number of arrays is only 30, So we can use a bucket to record the number 1~30 How many are there in each , Then double loop enumeration is enough .
边栏推荐
- Understanding the execution order of T-SQL query from the execution order of join on and where
- MATLAB 如何生产随机复序列
- How to finally generate a file from saveastextfile in spark
- 苹果内购和Apple Pay 的区别
- 自定义注解校验API参数电话号
- Xcode added mobileprovision certificate file error: Xcode encoded an error
- 记一次Yarn Required executor memeory is above the max threshold(8192MB) of this cluster!
- JVM-垃圾收集器详解
- Instance Tunnel 使用
- Idea remotely submits spark tasks to the yarn cluster
猜你喜欢

ML - 语音 - 传统语音模型

VMware Workstation fails to start VMware authorization service when opening virtual machine

Solve the timeout of dbeaver SQL client connection Phoenix query

ML - 语音 - 高级语音模型

理解“平均负载”

小波变换--dwt2 与wavedec2

spark分区算子partitionBy、coalesce、repartition

在win10系统下使用命令查看WiFi连接密码

Spark提交参数--files的使用

matlab 优化工具 manopt 安装
随机推荐
Hbck 修复问题
Solve the timeout of dbeaver SQL client connection Phoenix query
Debounce and throttle
Distributed principle - what is a distributed system
Recommend 10 learning websites that can be called artifact
Local cache --ehcache
Idea护眼色设置
VMware Workstation fails to start VMware authorization service when opening virtual machine
我的创作纪念日
ML - 语音 - 传统语音模型
matlab---错误使用 var 数据类型无效。第一个输入参数必须为单精度值或双精度值
在网页上实现任意格式的音视频快速播放功能的开发总结。
matlab 如何保存所有运行后的数据
ML - natural language processing - Basics
Record a redis timeout
Spark 内存管理机制 新版
How to finally generate a file from saveastextfile in spark
从 join on 和 where 执行顺序认识T-sql查询执行顺序
小波变换--dwt2 与wavedec2
MATLAB读取显示图像时数据格式转换原因