力扣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;
      }
  };
相关推荐
GalaxyPokemon1 小时前
归并排序:分治思想的高效排序
数据结构·算法·排序算法
ThreeYear_s1 小时前
基于FPGA的PID算法学习———实现PI比例控制算法
学习·算法·fpga开发
Coding小公仔4 小时前
LeetCode 240 搜索二维矩阵 II
算法·leetcode·矩阵
C++chaofan4 小时前
74. 搜索二维矩阵
java·算法·leetcode·矩阵
青小莫4 小时前
数据结构-C语言-链表OJ
c语言·数据结构·链表
Studying 开龙wu5 小时前
机器学习监督学习实战五:六种算法对声呐回波信号进行分类
学习·算法·机器学习
Mi Manchi265 小时前
力扣热题100之二叉树的层序遍历
python·算法·leetcode
wu~9705 小时前
leetcode:42. 接雨水(秒变简单题)
算法·leetcode·职场和发展
zhurui_xiaozhuzaizai6 小时前
模型训练-关于token【低概率token, 高熵token】
人工智能·算法·自然语言处理