力扣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;
      }
  };
相关推荐
追随者永远是胜利者1 小时前
(LeetCode-Hot100)253. 会议室 II
java·算法·leetcode·go
Jason_Honey22 小时前
【平安Agent算法岗面试-二面】
人工智能·算法·面试
程序员酥皮蛋2 小时前
hot 100 第三十五题 35.二叉树的中序遍历
数据结构·算法·leetcode
追随者永远是胜利者2 小时前
(LeetCode-Hot100)207. 课程表
java·算法·leetcode·go
仰泳的熊猫3 小时前
题目1535:蓝桥杯算法提高VIP-最小乘积(提高型)
数据结构·c++·算法·蓝桥杯
那起舞的日子3 小时前
动态规划-Dynamic Programing-DP
算法·动态规划
闻缺陷则喜何志丹3 小时前
【前后缀分解】P9255 [PA 2022] Podwyżki|普及+
数据结构·c++·算法·前后缀分解
每天吃饭的羊3 小时前
时间复杂度
数据结构·算法·排序算法
yzx9910134 小时前
Python数据结构入门指南:从基础到实践
开发语言·数据结构·python
ValhallaCoder5 小时前
hot100-堆
数据结构·python·算法·