当前位置:网站首页>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;
}
};边栏推荐
- [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)
- 工程师评测 | RK3568开发板上手测试
- 损失函数与正负样本分配:YOLO系列
- Let your HMI have more advantages. Fet-g2ld-c core board is a good choice
- 终于搞懂了JS中的事件循环,同步/异步,微任务/宏任务,运行机制(附笔试题)
- 【LeetCode】977-有序数组的平方
- MD5 encryption
- [leetcode] 695 - maximum area of the island
- For the problem that Folium map cannot be displayed, the temporary solution is as follows
- 04.进入云原生后的企业级应用构建的一些思考
猜你喜欢

Basic knowledge of cryptography

Facing the challenge of "lack of core", how can Feiling provide a stable and strong guarantee for customers' production capacity?

2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)

Leetcode skimming -- sum of two integers 371 medium

SQL stored procedure

MySQL -- Index Optimization -- order by

LeetCode刷题——两整数之和#371#Medium

【LeetCode】1162-地图分析

基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测

Build your own semantic segmentation platform deeplabv3+
随机推荐
LeetCode刷题——两整数之和#371#Medium
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
Build your own semantic segmentation platform deeplabv3+
Pytorch 保存tensor到.mat文件
【LeetCode】1905-统计子岛屿
Force deduction solution summarizes the lucky numbers in 1380 matrix
Bing. Site Internet
工程师评测 | RK3568开发板上手测试
Solution of Queen n problem
提前批院校名称
11_ Redis_ Hyperloglog_ command
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
基于RZ/G2L | OK-G2LD-C开发板存储读写速度与网络实测
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
YOLOV5 代码复现以及搭载服务器运行
Summary of the first three passes of sqli Labs
12_ Redis_ Bitmap_ command
LeetCode_ String_ Simple_ 412.Fizz Buzz
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
高考录取分数线爬取