엔지니어 게시판
LeetCode 솔루션 분류

[9/9] 1996. 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
Accepted
73,713
Submissions
169,691
태그

관련자료

댓글 1

학부유학생님의 댓글

  • 익명
  • 작성일
Runtime: 2730 ms, faster than 80.70% of Python3 online submissions for The Number of Weak Characters in the Game.
Memory Usage: 70.9 MB, less than 7.38% of Python3 online submissions for The Number of Weak Characters in the Game.
from collections import defaultdict
class Solution:
    def numberOfWeakCharacters(self, properties: List[List[int]]) -> int:
        a_to_d = defaultdict(list)
        res = 0
        max_d = -1
        
        for a, d in properties:
            a_to_d[a].append(d)
        
        for key in sorted(list(a_to_d.keys()), reverse=True):
            for d in a_to_d[key]:
                if d < max_d: res += 1
            
            max_d = max(max_d, max(a_to_d[key]))
        
        return res
전체 410 / 1 페이지
번호
제목
이름

최근글


인기글


새댓글


Stats


  • 현재 접속자 603 명
  • 오늘 방문자 7,431 명
  • 어제 방문자 9,517 명
  • 최대 방문자 14,831 명
  • 전체 회원수 1,599 명
알림 0