티스토리 뷰
Problem
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 <= 10^5
- properties[i].length == 2
- 1 <= attacki, defensei <= 10^5
Solution
공격과 방어 두 개의 메인 속성을 가진 캐릭터로 게임을하는데 이 때 캐릭터는 [공격, 방어] 형태로 주어집니다.
공격과 방어가 다른 캐릭터에 비해 둘 다 낮은 캐릭터를 weak 캐릭터라고 할 때 weak 캐릭터가 모두 몇 개인지 구하는 문제입니다.
package io.lcalmsky.leetcode.the_number_of_week_characters_in_the_game;
import java.util.Arrays;
public class Solution {
public int numberOfWeakCharacters(int[][] properties) {
Arrays.sort(properties, (o1, o2) -> o1[0] == o2[0] ? o1[1] - o2[1] : o2[0] - o1[0]);
int maxDefence = 0;
int count = 0;
for (int[] property : properties) {
if (property[1] < maxDefence) {
count++;
continue;
}
maxDefence = property[1];
}
return count;
}
}
캐릭터들을 정렬할 때 공격력이 같으면 방어력을 오름차순으로, 그렇지 않으면 공격력을 내림 차순으로 정렬한 뒤, 최대 방어력을 갱신하면서 현재 방어력이 더 낮은 경우 카운트 개수를 증가시키면 됩니다.
Test
package io.lcalmsky.leetcode.the_number_of_week_characters_in_the_game;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
class SolutionTest {
@Test
void testAll() {
assertAll(
() -> test(new int[][]{{5, 5}, {6, 3}, {3, 6}}, 0),
() -> test(new int[][]{{2, 2}, {3, 3}}, 1),
() -> test(new int[][]{{1, 5}, {10, 4}, {4, 3}}, 1)
);
}
private void test(int[][] given, int expected) {
// when
Solution solution = new Solution();
int actual = solution.numberOfWeakCharacters(given);
// then
assertEquals(expected, actual);
}
}
'Algorithm > LeetCode' 카테고리의 다른 글
[LeetCode] 1027. Longest Arithmetic Subsequence (0) | 2023.06.23 |
---|---|
188. Best Time to Buy and Sell Stock IV (1) | 2022.09.13 |
94. Binary Tree Inorder Traversal (0) | 2022.09.10 |
144. Binary Tree Preorder Traversal (0) | 2022.09.09 |
814. Binary Tree Pruning (0) | 2022.09.08 |
- Total
- Today
- Yesterday
- @ManyToOne
- Java
- Linux
- intellij
- spring boot app
- leetcode
- 스프링 부트 튜토리얼
- 스프링부트
- 함께 자라기 후기
- Spring Data JPA
- Spring Boot
- 알고리즘
- 스프링 부트
- 헥사고날 아키텍처
- 스프링 부트 애플리케이션
- JPA
- QueryDSL
- spring boot application
- 스프링 부트 회원 가입
- proto3
- JSON
- 스프링 데이터 jpa
- Jackson
- r
- Spring Boot Tutorial
- 함께 자라기
- Spring Boot JPA
- gRPC
- 클린 아키텍처
- spring boot jwt
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |