Last active
February 13, 2020 17:35
-
-
Save adamcaron/dddd8834241ada3e64ce6663bb1fb686 to your computer and use it in GitHub Desktop.
First crack at Codility demo test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// This is a demo task. | |
// Write a function: | |
// function solution(A); | |
// that, given an array A of N integers, returns the smallest positive integer (greater than 0) that does not occur in A. | |
// For example, given A = [1, 3, 6, 4, 1, 2], the function should return 5. | |
// Given A = [1, 2, 3], the function should return 4. | |
// Given A = [−1, −3], the function should return 1. | |
// 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..1,000,000]. | |
// you can write to stdout for debugging purposes, e.g. | |
// console.log('this is a debug message'); | |
/* | |
* 1st attempt | |
*/ | |
function solution(A) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
let smallest = 1 | |
// possible to simplify A | |
// by removing negatives | |
// and duplicates | |
if (!A.find(e => e === smallest)) { | |
return smallest | |
} | |
for (let i = 1; i < 1000000; i++) { | |
if (!A.find(e => e === i)) { | |
return i | |
} | |
} | |
} | |
// Detected time complexity: | |
// O(N**2) | |
// generate array, and text file with output. | |
// var myFunc = Array(1000000).fill(1).map((x, y) => x + y) | |
// node => var fs = require('fs); fs.writeFile('million.txt', myFunc(), () => {}) | |
/* | |
* 2nd attempt | |
*/ | |
function solution(A) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
let smallest = 1 | |
// possible to simplify A | |
// by removing negatives | |
const deDuped = A.reduce((acc, x) => { | |
if (!acc.includes(x)) { | |
acc.push(x) | |
} | |
return acc | |
}, []) | |
if (!deDuped.includes(smallest)) { | |
return smallest | |
} | |
for (let i = 1; i < 1000000; i++) { | |
if (!A.includes(i)) { | |
return i | |
} | |
} | |
} | |
// Detected time complexity: | |
// O(N**2) | |
/* | |
* 3rd Attempt | |
*/ | |
function solution(A) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
let smallest = 1 | |
if (!A.includes(smallest)) { | |
return smallest | |
} | |
// possible to simplify A | |
// and duplicates | |
// remove negatives | |
const sorted = A.sort() | |
const idx = sorted.findIndex((e) => 0 < e) | |
const positives = sorted.splice(idx) | |
for (let i = 1; i < 1000000; i++) { | |
if (!positives.includes(i)) { | |
return i | |
} | |
} | |
} | |
//Detected time complexity: | |
//O(N**2) | |
//Task Score | |
//66% | |
//Correctness | |
//100% | |
//Performance | |
//25% |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment