Sunday, March 5, 2023

[LeetCode] 2300. Successful Pairs of Spells and Potions


Problem: 2300. Successful Pairs of Spells and Potions
Category: Add Hoc
Algorithm: Sorting
class Solution {
public:
    vector<int> successfulPairs(vector<int>& spells, vector<int>& potions, long long success) {
        int tspells = spells.size();
        vector<pair<int, int>> arr;
        for (int i = 0; i < tspells; i++) {
            arr.push_back({spells[i], i});
        }
        sort(arr.begin(), arr.end());
        vector<int> ans(tspells);
        sort(potions.begin(), potions.end());
        int cnt = 0;
        for (auto it : arr) {
            while (potions.size() and 1LL * potions.back() * it.first >= success) {
                potions.pop_back();
                cnt++;
            }
            ans[it.second] = cnt;
        }
        return ans;
    } 

}; 

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.