当前位置:网站首页>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;
}
};
边栏推荐
- Semantic segmentation learning notes (1)
- [leetcode] 577 reverse word III in string
- Solution of Queen n problem
- Infra11199 database system
- MySQL calculate n-day retention rate
- LeetCode_ Sliding window_ Medium_ 395. Longest substring with at least k repeated characters
- 14_ Redis_ Optimistic lock
- 密码学基础知识
- (4) Flink's table API and SQL table schema
- Leetcode skimming -- count the number of numbers with different numbers 357 medium
猜你喜欢
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
SQL transaction
17_ Redis_ Redis publish subscription
yolo格式数据集处理(xml转txt)
【LeetCode】1905-统计子岛屿
10_Redis_geospatial_命令
让您的HMI更具优势,FET-G2LD-C核心板是个好选择
Markdown tutorial
Guangzhou Emergency Management Bureau issued a high temperature and high humidity chemical safety reminder in July
随机推荐
Folium, diagnosis and close contact trajectory above
Leetcode question brushing - parity linked list 328 medium
【LeetCode】577-反转字符串中的单词 III
LeetCode刷题——验证二叉树的前序序列化#331#Medium
LeetCode刷题——两整数之和#371#Medium
For the problem that Folium map cannot be displayed, the temporary solution is as follows
[leetcode] 695 - maximum area of the island
Leetcode skimming - remove duplicate letters 316 medium
FPGA - clock-03-clock management module (CMT) of internal structure of 7 Series FPGA
【LeetCode】1905-统计子岛屿
Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
[leetcode] 167 - sum of two numbers II - enter an ordered array
Thoroughly understand browser strong cache and negotiation cache
Basic knowledge of cryptography
Loss function and positive and negative sample allocation: Yolo series
[leetcode] 417 - Pacific Atlantic current problem
13_ Redis_ affair
. Net again! Happy 20th birthday
Libcurl Lesson 13 static library introduces OpenSSL compilation dependency
6.12 企业内部upp平台(Unified Process Platform)的关键一刻