일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 코틀린
- 안드로이드개발레벨업교과서
- 고차함수
- Android
- searchview
- 재사용
- binding adapter
- adapter
- Fragment에서 Activity의 함수 사용하기
- fragment
- espresso
- LayoutManger
- viewholder
- 안드로이드13
- 테마 아이콘
- 리사이클러뷰
- Error:Execution failed for task ':app:mergeDebugResources'
- 코딜리티
- high order function
- Fragment 수동 추가
- 안드로이드스튜디오
- 구분선
- ui test
- 안드로이드
- recyclerview
- ActivityTestRule
- 뷰변경 감지
- 생명주기
- 스와이프
- IntentTestRule
- Today
- Total
룬아님의 취중코딩
Codility 23번 EquiLeader 본문
EquiLeader
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].
import java.util.*;
class Solution {
public int solution(int[] A) {
Stack<Integer> item = new Stack<>();
for(int i=0; i<A.length; i++){
if(item.isEmpty() || item.peek() == A[i]){
item.push(A[i]);
}else{
item.pop();
}
}
if(item.isEmpty()){
return 0;
}
int num = item.pop();
int[] B = new int[A.length];
int ncnt = 0;
for(int i=0; i<A.length; i++){
if(A[i] == num){
ncnt+=1;
}
B[i] = ncnt;
}
int ans = 0;
for(int i=1; i<A.length; i++){
if(B[i-1] >= i/2 + 1
&& B[A.length -1] - B[i-1] >= (A.length - i)/2 + 1){
ans++;
}
}
return ans;
}
}
'개발 > 알고리즘' 카테고리의 다른 글
Codility 27번 CountFactors (0) | 2019.09.23 |
---|---|
Codility 25번 MaxSliceSum (0) | 2019.09.22 |
Codility 24번 MaxProfit (0) | 2019.09.15 |
Codility 22번 Dominator (0) | 2019.09.15 |
Codility 20번 Nesting (0) | 2019.09.09 |