일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- Dependency
- @Scheduled
- 프로그래머스
- Spring Mail
- Spring Boot
- pom.xml
- Collections
- list
- vuejs #vue #js #프론트엔드 #nodejs #클라이언트사이드 #템플릿엔진
- GOF
- 스프링 부트
- 의존성관리
- pair
- 스프링 스케줄러
- mybatis
- thymeleaf
- C++
- 스프링
- HashMap
- 스프링부트
- Arrays
- 코딩테스트
- springboot
- 스프링 메일
- codility
- Spring
- 프로젝트 구조
- spring scheduler
- java
- maven
- Today
- Total
Rooted In Develop
[Codility] Leader - EquiLeader / Java 본문
1. 문제
A non-empty array A consisting of N integers is given.
The leader of this array is the value that occurs in more than half of the elements of A.
An equi leader is an index S such that 0 ≤ S < N − 1 and two sequences A[0], A[1], ..., A[S] and A[S + 1], A[S + 2], ..., A[N − 1] have leaders of the same value.
For example, given array A such that:
A[0] = 4 A[1] = 3 A[2] = 4 A[3] = 4 A[4] = 4 A[5] = 2
we can find two equi leaders:
- 0, because sequences: (4) and (3, 4, 4, 4, 2) have the same leader, whose value is 4.
- 2, because sequences: (4, 3, 4) and (4, 4, 2) have the same leader, whose value is 4.
The goal is to count the number of equi leaders.
Write a function:
class Solution { public int solution(int[] A); }
that, given a non-empty array A consisting of N integers, returns the number of equi leaders.
For example, given:
A[0] = 4 A[1] = 3 A[2] = 4 A[3] = 4 A[4] = 4 A[5] = 2
the function should return 2, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- each element of array A is an integer within the range [−1,000,000,000..1,000,000,000].
Copyright 2009–2020 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.
2. 코드
import java.util.*;
class Solution {
public int solution(int[] A) {
HashMap<Integer, Integer> map = new HashMap<>();
int maxCount = 0;
int leader = 0;
for(int i=0; i<A.length; i++) {
if(map.containsKey(A[i])) {
int count = map.get(A[i]) + 1;
map.put(A[i],count);
if(maxCount < count) {
maxCount = count;
leader = A[i];
}
} else {
map.put(A[i],1);
}
}
if(maxCount == 0)
return 0;
int[] Counter = new int[A.length];
for(int i=0; i<A.length; i++) {
if(A[i] == leader)
Counter[i] = 1;
else
Counter[i] = 0;
}
int result = 0;
int leftLen = 0;
int rightLen = A.length;
int leftCount = 0;
int rightCount = map.get(leader);
for(int i=0; i<A.length; i++) {
leftCount += Counter[i];
rightCount -= Counter[i];
leftLen++;
rightLen--;
if( (leftCount > leftLen/2) && (rightCount > rightLen/2) )
result++;
}
return result;
}
}
'Algorithm Test > Java' 카테고리의 다른 글
[Codility] Leader - Dominator / Java (0) | 2020.06.16 |
---|---|
[Codility] Stacks and Queues - Nesting / Java (0) | 2020.06.15 |
[Codility] Stacks and Queues - Fish / Java (0) | 2020.06.15 |
[Codility] Stacks and Queues - Brackets / Java (0) | 2020.06.15 |
[Codility] Sorting - Triangle / Java (0) | 2020.06.14 |