Codility [ Lesson 8 | Leader ] Dominator - JavaScript
5293 ワード
質問する
An array A consisting of N integers is given. The dominator of array A is the value that occurs in more than half of the elements of A.
For example, consider array A such that
A[0] = 3 A[1] = 4 A[2] = 3
A[3] = 2 A[4] = 3 A[5] = -1
A[6] = 3 A[7] = 3
The dominator of A is 3 because it occurs in 5 out of 8 elements of A (namely in those with indices 0, 2, 4, 6 and 7) and 5 is more than a half of 8.
Write a function
function solution(A);
that, given an array A consisting of N integers, returns index of any element of array A in which the dominator of A occurs. The function should return −1 if array A does not have a dominator.
For example, given array A such that
A[0] = 3 A[1] = 4 A[2] = 3
A[3] = 2 A[4] = 3 A[5] = -1
A[6] = 3 A[7] = 3
the function may return 0, 2, 4, 6 or 7, as explained above.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [0..100,000];
each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
もんだいぶんせき
与えられた配列要素のうち、重複値が最も多く、配列長の半分より大きい値はleader値です.値が存在する場合は、値を返します.最大値がない場合、または最大値がアレイの長さの半分未満の場合は、-1を返します.
問題を解く
まず、配列内の各要素をカウントするオブジェクトを作成します.キーは配列内の要素として指定され、valueは要素の数としてカウントされます.
次に、オブジェクトの周囲を回転するときに配列自体の長さの半分以上の値がある場合は、キー値が返され、ない場合は-1が返されます.
コード#コード#
ソース
https://app.codility.com/programmers/lessons/8-leader/
An array A consisting of N integers is given. The dominator of array A is the value that occurs in more than half of the elements of A.
For example, consider array A such that
A[0] = 3 A[1] = 4 A[2] = 3
A[3] = 2 A[4] = 3 A[5] = -1
A[6] = 3 A[7] = 3
The dominator of A is 3 because it occurs in 5 out of 8 elements of A (namely in those with indices 0, 2, 4, 6 and 7) and 5 is more than a half of 8.
Write a function
function solution(A);
that, given an array A consisting of N integers, returns index of any element of array A in which the dominator of A occurs. The function should return −1 if array A does not have a dominator.
For example, given array A such that
A[0] = 3 A[1] = 4 A[2] = 3
A[3] = 2 A[4] = 3 A[5] = -1
A[6] = 3 A[7] = 3
the function may return 0, 2, 4, 6 or 7, as explained above.
Write an efficient algorithm for the following assumptions:
N is an integer within the range [0..100,000];
each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
もんだいぶんせき
与えられた配列要素のうち、重複値が最も多く、配列長の半分より大きい値はleader値です.値が存在する場合は、値を返します.最大値がない場合、または最大値がアレイの長さの半分未満の場合は、-1を返します.
問題を解く
まず、配列内の各要素をカウントするオブジェクトを作成します.キーは配列内の要素として指定され、valueは要素の数としてカウントされます.
次に、オブジェクトの周囲を回転するときに配列自体の長さの半分以上の値がある場合は、キー値が返され、ない場合は-1が返されます.
コード#コード#
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
let answer = -1;
let tmp = {};
for(let i = 0; i<A.length; i++){
tmp[A[i]] = (tmp[A[i]] || 0) + 1
}
for(const [key, value] of Object.entries(tmp)){
if(tmp[key] > A.length/2){
answer = A.indexOf(Number(key))
}
}
return answer
}
最終結果ソース
https://app.codility.com/programmers/lessons/8-leader/
Reference
この問題について(Codility [ Lesson 8 | Leader ] Dominator - JavaScript), 我々は、より多くの情報をここで見つけました https://velog.io/@sohyeonbak_oly/Codility-Lesson-8-Leader-Dominator-JavaScriptテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol