力扣2300.咒语和药水的成功对数

力扣2300.咒语和药水的成功对数

  • 排序 + 二分

    • 对于能整除的数 -1以后一起处理
    • 可以在原数组直接修改
cpp 复制代码
  class Solution {
  public:
      vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
          sort(potions.begin(),potions.end());
          for(int &x:spells)
          {
              long long target = (success-1)/x;
              if(target < potions.back()) 
                  x = potions.end() - ranges::upper_bound(potions,(int)target);
              else 
                  x = 0;
          }
          return spells;
      }
  };
  • 也可以另开一个新的
cpp 复制代码
  class Solution {
  public:
      vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
          vector<int> res;
          sort(potions.begin(),potions.end());
          for(int i=0;i<spells.size();i++)
          {
              long long target = (success-1)/spells[i];
              if(target < potions.back()) 
                  res.emplace_back((potions.end() - ranges::upper_bound(potions,(int)target)));
              else 
                  res.emplace_back(0);
          }
          return res;
      }
  };
相关推荐
TracyCoder1233 分钟前
LeetCode Hot100(19/100)——206. 反转链表
算法·leetcode
m0_715575345 分钟前
分布式任务调度系统
开发语言·c++·算法
naruto_lnq26 分钟前
泛型编程与STL设计思想
开发语言·c++·算法
踩坑记录42 分钟前
leetcode hot100 94. 二叉树的中序遍历 easy 递归 dfs
leetcode
zxsz_com_cn1 小时前
设备预测性维护算法分类及优劣势分析,选型指南来了
算法·分类·数据挖掘
m0_748708051 小时前
C++中的观察者模式实战
开发语言·c++·算法
然哥依旧1 小时前
【轴承故障诊断】基于融合鱼鹰和柯西变异的麻雀优化算法OCSSA-VMD-CNN-BILSTM轴承诊断研究【西储大学数据】(Matlab代码实现)
算法·支持向量机·matlab·cnn
qq_537562672 小时前
跨语言调用C++接口
开发语言·c++·算法
Tingjct2 小时前
【初阶数据结构-二叉树】
c语言·开发语言·数据结构·算法