<p>You are playing a game that contains multiple characters, and each of the characters has <strong>two</strong> main properties: <strong>attack</strong> and <strong>defense</strong>. You are given a 2D integer array <code>properties</code> where <code>properties[i] = [attack<sub>i</sub>, defense<sub>i</sub>]</code> represents the properties of the <code>i<sup>th</sup></code> character in the game.</p>

<p>A character is said to be <strong>weak</strong> if any other character has <strong>both</strong> attack and defense levels <strong>strictly greater</strong> than this character&#39;s attack and defense levels. More formally, a character <code>i</code> is said to be <strong>weak</strong> if there exists another character <code>j</code> where <code>attack<sub>j</sub> &gt; attack<sub>i</sub></code> and <code>defense<sub>j</sub> &gt; defense<sub>i</sub></code>.</p>

<p>Return <em>the number of <strong>weak</strong> characters</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> properties = [[5,5],[6,3],[3,6]]
<strong>Output:</strong> 0
<strong>Explanation:</strong> No character has strictly greater attack and defense than the other.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> properties = [[2,2],[3,3]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The first character is weak because the second character has a strictly greater attack and defense.
</pre>

<p><strong class="example">Example 3:</strong></p>

<pre>
<strong>Input:</strong> properties = [[1,5],[10,4],[4,3]]
<strong>Output:</strong> 1
<strong>Explanation:</strong> The third character is weak because the second character has a strictly greater attack and defense.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>2 &lt;= properties.length &lt;= 10<sup>5</sup></code></li>
	<li><code>properties[i].length == 2</code></li>
	<li><code>1 &lt;= attack<sub>i</sub>, defense<sub>i</sub> &lt;= 10<sup>5</sup></code></li>
</ul>

class Solution:
    def numberOfWeakCharacters(self, properties: List[List[int]]) -> int:
        #bucket sort: store max defense for each attack:
        #iterate right to left, storing max defense > attack[i].. 
        #then for each element check if the max defense for attack > own attack
        maxa = max(a for a,d in properties)
        max_defence = [0]*(maxa+1) #[i] = max_defence greater than attack i

        for a,d in properties:
            max_defence[a] = max(max_defence[a],d)

        cur_max = 0
        for a in range(maxa,-1,-1):
            tmp = max_defence[a]
            max_defence[a] = cur_max
            
            cur_max = max(cur_max,tmp)

        res = 0
        for a,d in properties:
            if max_defence[a] > d:
                res +=1
        return res



        #sort by atack -
        # all attacks are increasing, 
        # we add an elements defense, if the defense added > prev defense added, then attack > prev attack and defense > prev defense


        res = 0
        properties.sort(key = lambda arr: (arr[0],-arr[1])) #increasing attack,but decreasing defense
        stack = [] #decreasing defense
        for a,d in properties:
            if stack and stack[-1][0] == a:
                stack.append([a,d])
                continue
            while stack and stack[-1][1] < d:
                stack.pop()
                res +=1
            stack.append([a,d])
        return res
684