티스토리 뷰
Problem
In a town, there are n people labeled from 1 to n. There is a rumor that one of these people is secretly the town judge.
If the town judge exists, then:
The town judge trusts nobody.
Everybody (except for the town judge) trusts the town judge.
There is exactly one person that satisfies properties 1 and 2.
You are given an array trust where trust[i] = [ai, bi] representing that the person labeled ai trusts the person labeled bi.
Return the label of the town judge if the town judge exists and can be identified, or return -1 otherwise.
Example 1:
Input: n = 2, trust = [[1,2]]
Output: 2
Example 2:
Input: n = 3, trust = [[1,3],[2,3]]
Output: 3
Example 3:
Input: n = 3, trust = [[1,3],[2,3],[3,1]]
Output: -1
Constraints:
- 1 <= n <= 1000
- 0 <= trust.length <= 10^4
- trust[i].length == 2
- All the pairs of trust are unique.
- ai != bi
- 1 <= ai, bi <= n
Solution
1부터 n까지 숫자로 표현되는 사람 n명이 마을에 존재하고 그 중 한 명이 비밀리에 업무를 수행하는 마을 판사입니다. (우리나라로 따지면 암행어사인듯!)
만약에 마을 판사가 존재한다고하면, 마을 판사는 아무도 믿지 않고, 마을 판사를 제외한 모든 사람은 마을 판사를 믿고, 앞의 두 조건을 만족하는 한 사람이 존재합니다.
마을 사람의 수와 믿음을 나타내는 배열이 주어질 때 마을 판사가 존재하면 마을 판사의 숫자를, 그렇지 않으면 -1을 반환하는 문제입니다.
모든 사람이 믿고있는 한 사람이 다른 사람을 아무도 믿지 않을 경우 마을 판사가 존재함을 알 수 있습니다.
public class Solution {
public int findJudge(int n, int[][] trust) {
int[] count = new int[n + 1];
for (int[] t : trust) {
count[t[0]]--;
count[t[1]]++;
}
for (int i = 1; i <= n; i++) {
if (count[i] == n - 1) {
return i;
}
}
return -1;
}
}
신뢰 받는 쪽은 더해주고 신뢰를 하는 쪽은 빼준 뒤 마지막에 n-1만큼 신뢰받은 인덱스를 반환하면 됩니다.
Test
package io.lcalmsky.leetcode.find_the_town_judge;
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(2, new int[][]{{1, 2}}, 2),
() -> test(3, new int[][]{{1, 3}, {2, 3}}, 3),
() -> test(3, new int[][]{{1, 3}, {2, 3}, {3, 1}}, -1)
);
}
private void test(int n, int[][] trust, int expected) {
// when
Solution solution = new Solution();
int actual = solution.findJudge(n, trust);
// then
assertEquals(expected, actual);
}
}
'Algorithm > LeetCode Daily Challenge' 카테고리의 다른 글
131. Palindrome Partitioning (0) | 2022.01.06 |
---|---|
1009. Complement of Base 10 Integer (0) | 2022.01.05 |
1010. Pairs of Songs With Total Durations Divisible by 60 (0) | 2022.01.03 |
1026. Maximum Difference Between Node and Ancestor (0) | 2022.01.02 |
[LeetCode - Daily Challenge] 876. Middle of the Linked List (0) | 2021.12.31 |
- Total
- 202,075
- Today
- 1
- Yesterday
- 511
- 스프링부트
- QueryDSL
- 스프링 부트 튜토리얼
- @ManyToOne
- leetcode
- 함께 자라기
- spring boot jwt
- intellij
- Spring Boot
- JSON
- Jackson
- 함께 자라기 후기
- proto3
- 알고리즘
- gRPC
- Spring Boot Tutorial
- 헥사고날 아키텍처
- Java
- r
- 클린 아키텍처
- Spring Boot JPA
- Spring Data JPA
- JPA
- 스프링 데이터 jpa
- spring boot application
- 스프링 부트
- spring boot app
- Linux
- 스프링 부트 회원 가입
- 스프링 부트 애플리케이션