-
4-1.MissingIntegerCodility/Javascript 2017. 4. 6. 11:07Task description
Write a function:
function solution(A);
that, given a non-empty zero-indexed array A of N integers, returns the minimal positive integer (greater than 0) that does not occur in A.
For example, given:
A[0] = 1 A[1] = 3 A[2] = 6 A[3] = 4 A[4] = 1 A[5] = 2the function should return 5.
Assume that:
- N is an integer within the range [1..100,000];
- each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
Complexity:
- expected worst-case time complexity is O(N);
- expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).
Elements of input arrays can be modified.
Copyright 2009–2017 by Codility Limited. All Rights Reserved. Unauthorized copying, publication or disclosure prohibited.123456789101112131415161718192021222324252627282930313233// Correctness 100%// Performance 100%// Task Score 100%function solution(A) {// write your code in JavaScript (Node.js 6.4.0)var o = {};for( a of A ){if( a > 0 ) {o[a] = true;}}var keyArr = Object.keys(o).map((a) => parseInt(a)).sort((a,b) => a-b);if(keyArr.length == 0){return 1;} else if(keyArr.length == 1 && keyArr[0] == 1){return 2;}for( var i = 0 ; i < keyArr.length ; i++){if(i+1 != keyArr[i]){return i+1;} else if ( i+1 == keyArr.length ){return keyArr.length+1;}}}cs 'Codility > Javascript' 카테고리의 다른 글
4-3.FrogRiverOne (0) 2017.04.06 4-2.PermCheck (0) 2017.04.06 3-3.TapeEquilibrium (0) 2017.04.05 3-2.FrogJmp (0) 2017.04.05 3-1.PermMissingElem (0) 2017.04.04