力扣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;
      }
  };
相关推荐
MicroTech202537 分钟前
微算法科技(MLGO)研发突破性低复杂度CFG算法,成功缓解边缘分裂学习中的掉队者问题
科技·学习·算法
墨染点香1 小时前
LeetCode 刷题【126. 单词接龙 II】
算法·leetcode·职场和发展
aloha_7892 小时前
力扣hot100做题整理91-100
数据结构·算法·leetcode
Tiny番茄2 小时前
31.下一个排列
数据结构·python·算法·leetcode
挂科是不可能出现的2 小时前
最长连续序列
数据结构·c++·算法
_Aaron___2 小时前
List.subList() 返回值为什么不能强转成 ArrayList
数据结构·windows·list
前端小L3 小时前
动态规划的“数学之魂”:从DP推演到质因数分解——巧解「只有两个键的键盘」
算法·动态规划
RTC老炮3 小时前
webrtc弱网-ReceiveSideCongestionController类源码分析及算法原理
网络·算法·webrtc
21号 13 小时前
9.Redis 集群(重在理解)
数据库·redis·算法
码农多耕地呗4 小时前
力扣146.LRU缓存(哈希表缓存.映射+双向链表数据结构手搓.维护使用状况顺序)(java)
数据结构·leetcode·缓存