Skip to content

Commit d07ac58

Browse files
committedJan 2, 2022
Add solution #1996
1 parent 1988fbb commit d07ac58

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed
 

‎README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@
213213
1880|[Check if Word Equals Summation of Two Words](./1880-check-if-word-equals-summation-of-two-words.js)|Easy|
214214
1929|[Concatenation of Array](./1929-concatenation-of-array.js)|Easy|
215215
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|
216217
2000|[Reverse Prefix of Word](./2000-reverse-prefix-of-word.js)|Easy|
217218
2011|[Final Value of Variable After Performing Operations](./2011-final-value-of-variable-after-performing-operations.js)|Easy|
218219
2016|[Maximum Difference Between Increasing Elements](./2016-maximum-difference-between-increasing-elements.js)|Easy|
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
};

0 commit comments

Comments
 (0)
Please sign in to comment.