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] = 2we 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] = 2the 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].
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if (A.length == 1) {
return 0;
}
// 배열 전체 갯수 중 반+1 이상이 되어야 equi leader 존재
int conditionCnt = (A.length/2)+1;
int leaderValue = 0; // 기준이 되는 leader 값
int equiLeaderCnt = 0;
// 요소 갯수 세기
HashMap<Integer, Integer> hMap = new HashMap<Integer, Integer>();
for (int n : A) {
if (hMap.containsKey(n)) {
hMap.put(n, hMap.get(n)+1);
} else {
hMap.put(n, 1);
}
}
// 기준이 되는 leader값 추출
for (Map.Entry<Integer, Integer> entry : hMap.entrySet()) {
if (conditionCnt == 0 || entry.getValue().compareTo(conditionCnt) >= 0) {
leaderValue = entry.getKey();
}
}
int[] tmpA = new int[A.length];
for (int i=0; i<A.length; i++) {
if (A[i] == leaderValue) {
tmpA[i] = 1;
} else {
tmpA[i] = 0;
}
}
// equiLeader 갯수
for (int i=1; i<tmpA.length+1; i++) {
if (tmpA[i-1] == 1) {
int hOne = 0;
int hTwo = 0;
for (int j=0; j<i; j++) {
hOne += tmpA[j];
}
for (int k=i; k<tmpA.length; k++) {
hTwo += tmpA[k];
}
//int conOne = (int) Math.ceil((double)i/2);
//int conTwo = (int) Math.ceil((double)(tmpA.length-i)/2);
int conOne = (i/2)+1;
int conTwo = ((tmpA.length-i)/2)+1;
if (hOne >= conOne && hTwo >= conTwo) {
equiLeaderCnt++;
}
}
}
// System.out.println(leaderValue); // 4
//System.out.println(hMap); // {2=1, 3=1, 4=4}
return equiLeaderCnt;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
import java.util.Map;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if (A.length == 1) {
return 0;
}
// 배열 전체 갯수 중 반+1 이상이 되어야 equi leader 존재
int conditionCnt = (A.length/2)+1;
int leaderValue = 0; // 기준이 되는 leader 값
int equiLeaderCnt = 0;
// 요소 갯수 세기
HashMap<Integer, Integer> hMap = new HashMap<Integer, Integer>();
for (int n : A) {
if (hMap.containsKey(n)) {
hMap.put(n, hMap.get(n)+1);
} else {
hMap.put(n, 1);
}
}
// 기준이 되는 leader값 추출
for (Map.Entry<Integer, Integer> entry : hMap.entrySet()) {
if (conditionCnt == 0 || entry.getValue().compareTo(conditionCnt) >= 0) {
leaderValue = entry.getKey();
}
}
int[] tmpA = new int[A.length];
for (int i=0; i<A.length; i++) {
if (A[i] == leaderValue) {
tmpA[i] = 1;
} else {
tmpA[i] = 0;
}
}
// equiLeader 갯수
for (int i=1; i<tmpA.length+1; i++) {
if (tmpA[i-1] == 1) {
int hOne = 0;
int hTwo = 0;
for (int j=0; j<i; j++) {
hOne += tmpA[j];
}
for (int k=i; k<tmpA.length; k++) {
hTwo += tmpA[k];
}
//int conOne = (int) Math.ceil((double)i/2);
//int conTwo = (int) Math.ceil((double)(tmpA.length-i)/2);
int conOne = (i/2)+1;
int conTwo = ((tmpA.length-i)/2)+1;
if (hOne >= conOne && hTwo >= conTwo) {
equiLeaderCnt++;
}
}
}
// System.out.println(leaderValue); // 4
//System.out.println(hMap); // {2=1, 3=1, 4=4}
return equiLeaderCnt;
}
}
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
import java.util.Map;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if (A.length == 1) {
return 0;
}
// 배열 전체 갯수 중 반+1 이상이 되어야 equi leader 존재
int conditionCnt = (A.length/2)+1;
int leaderValue = 0; // 기준이 되는 leader 값
int equiLeaderCnt = 0;
// 요소 갯수 세기
HashMap<Integer, Integer> hMap = new HashMap<Integer, Integer>();
for (int n : A) {
if (hMap.containsKey(n)) {
hMap.put(n, hMap.get(n)+1);
} else {
hMap.put(n, 1);
}
}
// 기준이 되는 leader값 추출
for (Map.Entry<Integer, Integer> entry : hMap.entrySet()) {
if (conditionCnt == 0 || entry.getValue().compareTo(conditionCnt) >= 0) {
leaderValue = entry.getKey();
}
}
int[] tmpA = new int[A.length];
for (int i=0; i<A.length; i++) {
if (A[i] == leaderValue) {
tmpA[i] = 1;
} else {
tmpA[i] = 0;
}
}
// equiLeader 갯수
for (int i=1; i<tmpA.length+1; i++) {
if (tmpA[i-1] == 1) {
int hOne = 0;
int hTwo = 0;
for (int j=0; j<i; j++) {
hOne += tmpA[j];
}
for (int k=i; k<tmpA.length; k++) {
hTwo += tmpA[k];
}
//int conOne = (int) Math.ceil((double)i/2);
//int conTwo = (int) Math.ceil((double)(tmpA.length-i)/2);
int conOne = (i/2)+1;
int conTwo = ((tmpA.length-i)/2)+1;
if (hOne >= conOne && hTwo >= conTwo) {
equiLeaderCnt++;
}
}
}
// System.out.println(leaderValue); // 4
//System.out.println(hMap); // {2=1, 3=1, 4=4}
return equiLeaderCnt;
}
}
[4, 3, 4, 4, 4, 2]
[4, 3, 3, 4, 3, 3, 4, 4]
[1]
[1, 1]
[1, 1, 1]
[1, 1, 1, 1]
[4, 3, 3, 4, 4, 3]
[4, 3, 3, 4, 4, 3, 3]
[4, 3, 3, 4, 4, 3, 3]
[4, 3, 3, 4, 4, 3, 3, 3]
function result: 2
function result: 0
function result: 0
function result: 1
function result: 2
function result: 3
function result: 0
function result: 0
function result: 0
function result: 2
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
import java.util.Map;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if (A.length == 1) {
return 0;
}
// 배열 전체 갯수 중 반+1 이상이 되어야 equi leader 존재
int conditionCnt = (A.length/2)+1;
int leaderValue = 0; // 기준이 되는 leader 값
int equiLeaderCnt = 0;
// 요소 갯수 세기
HashMap<Integer, Integer> hMap = new HashMap<Integer, Integer>();
for (int n : A) {
if (hMap.containsKey(n)) {
hMap.put(n, hMap.get(n)+1);
} else {
hMap.put(n, 1);
}
}
// 기준이 되는 leader값 추출
for (Map.Entry<Integer, Integer> entry : hMap.entrySet()) {
if (conditionCnt == 0 || entry.getValue().compareTo(conditionCnt) >= 0) {
leaderValue = entry.getKey();
}
}
int[] tmpA = new int[A.length];
for (int i=0; i<A.length; i++) {
if (A[i] == leaderValue) {
tmpA[i] = 1;
} else {
tmpA[i] = 0;
}
}
// equiLeader 갯수
for (int i=1; i<tmpA.length+1; i++) {
if (tmpA[i-1] == 1) {
int hOne = 0;
int hTwo = 0;
for (int j=0; j<i; j++) {
hOne += tmpA[j];
}
for (int k=i; k<tmpA.length; k++) {
hTwo += tmpA[k];
}
//int conOne = (int) Math.ceil((double)i/2);
//int conTwo = (int) Math.ceil((double)(tmpA.length-i)/2);
int conOne = (i/2)+1;
int conTwo = ((tmpA.length-i)/2)+1;
if (hOne >= conOne && hTwo >= conTwo) {
equiLeaderCnt++;
}
}
}
// System.out.println(leaderValue); // 4
//System.out.println(hMap); // {2=1, 3=1, 4=4}
return equiLeaderCnt;
}
}
[4, 3, 4, 4, 4, 2]
[4, 3, 3, 4, 3, 3, 4, 4]
[1]
[1, 1]
[1, 1, 1]
[1, 1, 1, 1]
[4, 3, 3, 4, 4, 3]
[4, 3, 3, 4, 4, 3, 3]
[4, 3, 3, 4, 4, 3, 3]
[4, 3, 3, 4, 4, 3, 3, 3]
function result: 2
function result: 0
function result: 0
function result: 1
function result: 2
function result: 3
function result: 0
function result: 0
function result: 0
function result: 2
// you can also use imports, for example:
// import java.util.*;
import java.util.HashMap;
import java.util.Map;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(int[] A) {
// write your code in Java SE 8
if (A.length == 1) {
return 0;
}
// 배열 전체 갯수 중 반+1 이상이 되어야 equi leader 존재
int conditionCnt = (A.length/2)+1;
int leaderValue = 0; // 기준이 되는 leader 값
int equiLeaderCnt = 0;
// 요소 갯수 세기
HashMap<Integer, Integer> hMap = new HashMap<Integer, Integer>();
for (int n : A) {
if (hMap.containsKey(n)) {
hMap.put(n, hMap.get(n)+1);
} else {
hMap.put(n, 1);
}
}
// 기준이 되는 leader값 추출
for (Map.Entry<Integer, Integer> entry : hMap.entrySet()) {
if (conditionCnt == 0 || entry.getValue().compareTo(conditionCnt) >= 0) {
leaderValue = entry.getKey();
}
}
int[] tmpA = new int[A.length];
for (int i=0; i<A.length; i++) {
if (A[i] == leaderValue) {
tmpA[i] = 1;
} else {
tmpA[i] = 0;
}
}
// equiLeader 갯수
for (int i=1; i<tmpA.length+1; i++) {
if (tmpA[i-1] == 1) {
int hOne = 0;
int hTwo = 0;
for (int j=0; j<i; j++) {
hOne += tmpA[j];
}
for (int k=i; k<tmpA.length; k++) {
hTwo += tmpA[k];
}
//int conOne = (int) Math.ceil((double)i/2);
//int conTwo = (int) Math.ceil((double)(tmpA.length-i)/2);
int conOne = (i/2)+1;
int conTwo = ((tmpA.length-i)/2)+1;
if (hOne >= conOne && hTwo >= conTwo) {
equiLeaderCnt++;
}
}
}
// System.out.println(leaderValue); // 4
//System.out.println(hMap); // {2=1, 3=1, 4=4}
return equiLeaderCnt;
}
}
The following issues have been detected: wrong answers, timeout errors.
For example, for the input [4, 4, 2, 5, 3, 4, 4, 4] the solution returned a wrong answer (got 2 expected 3).
large random test with two values, length = ~50,000
Killed. Hard limit reached: 6.000 sec.
random(0,1) + 50000 * [0] + random(0, 1), length = ~100,000
Killed. Hard limit reached: 6.000 sec.