当前位置:网站首页>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;
}
};边栏推荐
- Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
- Thoroughly understand browser strong cache and negotiation cache
- [leetcode] 877 stone game
- LeetCode刷题——去除重复字母#316#Medium
- College entrance examination admission score line crawler
- 搭建自己的语义分割平台deeplabV3+
- [leetcode] 876 intermediate node of linked list
- Party History Documentary theme public welfare digital cultural and creative products officially launched
- 6095. 强密码检验器 II
- 【LeetCode】977-有序数组的平方
猜你喜欢

SQL transaction

Yolov5 code reproduction and server operation

【LeetCode】1162-地图分析

Solve the problem of frequent interruption of mobaxterm remote connection

4. Jctree related knowledge learning

Redux - detailed explanation

搭载TI AM62x处理器,飞凌FET6254-C核心板首发上市!

5. Practice: jctree implements the annotation processor at compile time

百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香

彻底弄懂浏览器强缓存和协商缓存
随机推荐
[leetcode] 1254 - count the number of closed Islands
【LeetCode】1140-石子游戏II
工程师评测 | RK3568开发板上手测试
【LeetCode】577-反转字符串中的单词 III
夏季高考文化成绩一分一段表
2303. 计算应缴税款总额
Infra11199 database system
[leetcode] 189 rotation array
提前批院校名称
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
Case introduction and problem analysis of microservice
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
【LeetCode】877-石子游戏
PTA ladder game exercise set l2-001 inter city emergency rescue
4. Jctree related knowledge learning
【LeetCode】1162-地图分析
面对“缺芯”挑战,飞凌如何为客户产能提供稳定强大的保障?
04. Some thoughts on enterprise application construction after entering cloud native
终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
NBA player analysis