File tree 2 files changed +36
-0
lines changed
2 files changed +36
-0
lines changed Original file line number Diff line number Diff line change 213
213
1880|[ Check if Word Equals Summation of Two Words] ( ./1880-check-if-word-equals-summation-of-two-words.js ) |Easy|
214
214
1929|[ Concatenation of Array] ( ./1929-concatenation-of-array.js ) |Easy|
215
215
1935|[ Maximum Number of Words You Can Type] ( ./1935-maximum-number-of-words-you-can-type.js ) |Easy|
216
+ 1996|[ The Number of Weak Characters in the Game] ( ./1996-the-number-of-weak-characters-in-the-game.js ) |Medium|
216
217
2000|[ Reverse Prefix of Word] ( ./2000-reverse-prefix-of-word.js ) |Easy|
217
218
2011|[ Final Value of Variable After Performing Operations] ( ./2011-final-value-of-variable-after-performing-operations.js ) |Easy|
218
219
2016|[ Maximum Difference Between Increasing Elements] ( ./2016-maximum-difference-between-increasing-elements.js ) |Easy|
Original file line number Diff line number Diff line change
1
+ /**
2
+ * 1996. The Number of Weak Characters in the Game
3
+ * https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/
4
+ * Difficulty: Medium
5
+ *
6
+ * You are playing a game that contains multiple characters, and each of the
7
+ * characters has two main properties: attack and defense. You are given a 2D
8
+ * integer array properties where properties[i] = [attacki, defensei] represents
9
+ * the properties of the ith character in the game.
10
+ *
11
+ * A character is said to be weak if any other character has both attack and
12
+ * defense levels strictly greater than this character's attack and defense levels.
13
+ * More formally, a character i is said to be weak if there exists another character
14
+ * j where attackj > attacki and defensej > defensei.
15
+ *
16
+ * Return the number of weak characters.
17
+ */
18
+
19
+ /**
20
+ * @param {number[][] } properties
21
+ * @return {number }
22
+ */
23
+ var numberOfWeakCharacters = function ( properties ) {
24
+ let result = 0 ;
25
+ let max = 0 ;
26
+
27
+ properties . sort ( ( a , b ) => ( b [ 0 ] === a [ 0 ] ) ? a [ 1 ] - b [ 1 ] : b [ 0 ] - a [ 0 ] ) ;
28
+
29
+ for ( let i = 0 ; i < properties . length ; i ++ ) {
30
+ if ( properties [ i ] [ 1 ] < max ) result ++ ;
31
+ max = Math . max ( max , properties [ i ] [ 1 ] ) ;
32
+ }
33
+
34
+ return result ;
35
+ } ;
You can’t perform that action at this time.
0 commit comments