LeetCode 1996. The Number of Weak Characters in the Game

原题链接在这里:https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/

题目:

You are playing a game that contains multiple characters, and each of the characters has two main properties: attack and defense. You are given a 2D integer array properties where properties[i] = [attacki, defensei] represents the properties of the ith character in the game.

A character is said to be weak if any other character has both attack and defense levels strictly greater than this character's attack and defense levels. More formally, a character i is said to be weak if there exists another character j where attackj > attacki and defensej > defensei.

Return the number of weak characters.

Example 1:

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

Example 2:

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

Example 3:

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

Constraints:

  • 2 <= properties.length <= 105
  • properties[i].length == 2
  • 1 <= attacki, defensei <= 105

题解:

Sort the array based on attach asc first. If attach is equal, then on defend des.

From right, have the current max, if current defend is smaller, then we find one weak.

Why can't we do from left, one weak could be counted multiple times. e.g. (1, 2), (6,7), (7, 6). (1, 2) would be counted twice.

Time Complexity: O(nlogn). n = properties.length.

Space: O(1).

AC Java:

 1 class Solution {
 2     public int numberOfWeakCharacters(int[][] properties) {
 3         Arrays.sort(properties, (a, b) -> a[0] == b[0] ? b[1] - a[1] : a[0] - b[0]);
 4         int res = 0;
 5         int max = Integer.MIN_VALUE;
 6         for(int i = properties.length - 1; i >= 0; i--){
 7             if(properties[i][1] < max){
 8                 res++;
 9             }
10             
11             max = Math.max(max, properties[i][1]);
12         }
13         
14         return res;
15     }
16 }

AC C++:

 1 class Solution {
 2 public:
 3     int numberOfWeakCharacters(vector<vector<int>>& properties) {
 4         sort(properties.begin(), properties.end(),
 5             [](const vector<int>& a, vector<int>& b) -> bool {
 6                 return a[0] == b[0] ? a[1] > b[1] : a[0] < b[0];
 7             });
 8         
 9         int maxDefense = INT_MIN;
10         int res = 0;
11         for(int i = properties.size() - 1; i >= 0; i--){
12             if(properties[i][1] < maxDefense){
13                 res++;
14             }
15             
16             maxDefense = max(maxDefense, properties[i][1]);
17         }
18         
19         return res;
20     }
21 };

 

posted @ 2022-09-09 13:07  Dylan_Java_NYC  阅读(91)  评论(0编辑  收藏  举报