力扣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;
      }
  };
相关推荐
ShineWinsu9 小时前
对于 C++:C++14中从变量模板、泛型 Lambda 到并发与字面量的解析
c++·算法
土司大王10 小时前
LeetCode hot100——合并两个有序链表
算法·leetcode·链表
码行山野赴时序归途10 小时前
算法效率的度量(上):时间复杂度中的对数思维
c语言·数据结构·算法
算AI10 小时前
基于LLM的无人机仿真测试:新方法竞赛夺佳绩
人工智能·深度学习·算法·机器学习·ai
kaixin_啊啊11 小时前
专用优化算法LKH
算法
一木 之林12 小时前
四、STL容器与数据结构
开发语言·数据结构·c++
_Narcissus_13 小时前
二分算法笔记及例题
数据结构·c++·笔记·算法·蓝桥杯·查找·二分算法
tachibana213 小时前
RAGAS 指标解读
数据库·人工智能·算法·机器学习·架构·大模型·llm
qq_4195630914 小时前
ToT 的 BFS/DFS 有个致命缺口:蒙特卡洛树搜索(MCTS)用「随机试错+统计」让大模型想得更深,小模型 + 它竟超过 GPT-4
算法·深度优先·宽度优先
万法若空15 小时前
CSP-J/S 排序算法完整专题训练题单
数据结构·算法·排序算法