当前位置:网站首页>6096. Success logarithm of spells and potions
6096. Success logarithm of spells and potions
2022-07-02 15:40:00 【Laver (nori)】
utilize hash Record spells Subscript where a single number appears , Yes spells Sort by increments , Yes potions Sort in descending order , Traverse spells when , You can be sure that you have traversed potions The position of does not need to be traversed , Then the position of the last jump , Just continue to traverse , This ensures potions Only be traversed once .
class Solution {
public:
vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
int len = spells.size();
vector<int> ans(len, 0);
// key: val;
// value: val stay spells All subscripts in
map<int, vector<int>> indMap;
// key: val;
// value: The val And potions The multiplication of the elements of is greater than or equal to success The number of times
map<int, int> resMap;
// Storage spells The elements of , But each element is guaranteed to appear only once
vector<int> spe;
// Fill the above set
for(int i = 0; i < len; i++){
int num = spells[i];
auto it = resMap.find(num);
if(it == end(resMap)){
// For the first time
resMap.emplace(num, 0);
spe.push_back(num);
// Most of the val It only occurs once, and the direct initialization capacity here is 1 that will do
vector<int> vec(1, i);
indMap.emplace(num, vec);
}else{
// The same subscript appears in other positions , Append this subscript directly to the list
indMap[num].push_back(i);
}
}
// Sort by increments
sort(begin(spe), end(spe));
// Sort in descending order
sort(begin(potions), end(potions), [](int ele0, int ele1){ return ele0 > ele1;});
long long numI, numJ;
// Store the last result
int res = 0;
for(int i = 0; i < spe.size(); i++){
// At present val
numI = spe[i];
// Continue the subscript of the last stop traversal
// because spe The order of , Here you can guarantee res After that, you must be satisfied
int j = res;
for(; j < potions.size();){
numJ = potions[j];
// If you are not satisfied, just jump out
if(numI * numJ < success){
break;
}
// Record the current results
res = (++j);
}
// Record the current val The corresponding result
resMap[numI] = res;
}
// Fill in everything val The result is ans
for(auto &pair : resMap){
// key For the corresponding val
int val = pair.first;
// value For the number of successful
int times = pair.second;
// same val But there are many , Fill the corresponding position
auto &vec = indMap[val];
for(auto &ind : vec){
ans[ind] = times;
}
}
return ans;
}
};边栏推荐
- Solve the problem of frequent interruption of mobaxterm remote connection
- [leetcode] 344 reverse string
- 【LeetCode】1020-飞地的数量
- (Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
- MD5加密
- 10_ Redis_ geospatial_ command
- 基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
- [leetcode] 877 stone game
- MySQL -- Index Optimization -- order by
- Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
猜你喜欢

Bing.com網站

Pytorch 保存tensor到.mat文件

SQL stored procedure

LeetCode刷题——去除重复字母#316#Medium
![[leetcode] 1162 map analysis](/img/9a/d04bde0417d4d5232950a4e260eb91.png)
[leetcode] 1162 map analysis

Build your own semantic segmentation platform deeplabv3+

自定义异常

LeetCode刷题——统计各位数字都不同的数字个数#357#Medium

(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice

Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
随机推荐
How does the computer set up speakers to play microphone sound
[leetcode] 1020 number of enclaves
Download blender on Alibaba cloud image station
【LeetCode】344-反转字符串
[leetcode] 577 reverse word III in string
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
高考录取分数线爬虫
Pytoch saves tensor to Mat file
15_ Redis_ Redis. Conf detailed explanation
Build your own semantic segmentation platform deeplabv3+
[leetcode] 877 stone game
How to find a sense of career direction
How to avoid 7 common problems in mobile and network availability testing
04. Some thoughts on enterprise application construction after entering cloud native
Be a good gatekeeper on the road of anti epidemic -- infrared thermal imaging temperature detection system based on rk3568
Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?
高考分数线爬取
[development environment] install the Chinese language pack for the 2013 version of visual studio community (install test agents 2013 | install visual studio 2013 simplified Chinese)
JVM architecture, classloader, parental delegation mechanism
MySQL calculate n-day retention rate