力扣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;
      }
  };
相关推荐
想吃火锅10051 分钟前
【leetcode】165.比较版本号js
javascript·算法·leetcode
San813_LDD6 分钟前
[量化]《浮点数比较的艺术:从内存布局到极致性能优化》
网络·算法
ysu_03149 分钟前
leetcode数据结构与算法1~4
c语言·数据结构·学习·算法·leetcode
小欣加油11 分钟前
leetcode2574 左右元素和的差值
数据结构·c++·算法·leetcode·职场和发展
PH = 721 分钟前
动态规划-求最优解-自底向上
算法·动态规划
用户4978630507327 分钟前
前缀和与差分
算法
weixin_4617694030 分钟前
通过数组和队列构造二叉树方法(用于算法测试),C++ vector不能直接使用null
数据结构·c++·算法·vector·nullptr·null
千寻girling41 分钟前
一周没跑步了 ,今日跑步 5KM , 哑铃+健身 20min , 俯卧撑 30 个 ;
数据结构·c++·python·算法·leetcode·职场和发展·线性回归
专注API从业者44 分钟前
电商选品效率翻倍!基于 Open Claw + 淘宝商品 API 实现自动化监控选品(附完整可运行代码)
大数据·运维·数据结构·数据库·自动化
CQU_JIAKE1 小时前
6.5aaaaa
算法·深度优先