1996. The Number of Weak Characters in the Game

C++ | Simple Explaination | 100% Success

class Solution {
public:
    static bool comp(vector<int> &a,vector<int> &b){
        if(a[0]==b[0]){
            return a[1]>b[1];
        }
        return a[0]<b[0];
    }
    int numberOfWeakCharacters(vector<vector<int>>& prop) {
        sort(prop.begin(),prop.end(),comp);
        int maxTillNow = INT_MIN;
        int ans=0;
        for(int i=prop.size()-1;i>=0;i--){
            if(prop[i][1]<maxTillNow){
                ans++;
            }
            maxTillNow = max(maxTillNow,prop[i][1]);
        }
        return ans;
    }
};

Comments

Popular posts from this blog

1431. Kids With the Greatest Number of Candies

125. Valid Palindrome

771. Jewels and Stones