├── .gitignore ├── Lesson 1 – Time Complexity ├── FrogJmp.js ├── FrogJmp.spec.js ├── PermMissingElem.spec.js ├── TapeEquilibrium.spec.js ├── PermMissingElem.js └── TapeEquilibrium.js ├── Lesson 3 – Prefix Sums ├── PassingCars.spec.js └── PassingCars.js ├── Lesson 5 - Stacks and Queues ├── StoneWall.spec.js ├── Nesting.spec.js ├── Nesting.js ├── Fish.spec.js ├── Brackets.js ├── Brackets.spec.js ├── StoneWall.js └── Fish.js ├── Lesson 4 – Sorting ├── MaxProductOfThree.spec.js ├── Distinct.spec.js ├── Triangle.js ├── Distinct.js ├── MaxProductOfThree.js └── Triangle.spec.js ├── Lesson 2 – Counting Elements ├── PermCheck.spec.js ├── MaxCounters.spec.js ├── PermCheck.js ├── FrogRiverOne.spec.js ├── FrogRiverOne.js └── MaxCounters.js ├── Lesson 7 - Maximum slice problem ├── MaxProfit.js ├── MaxProfit.spec.js ├── MaxSliceSum.js └── MaxSliceSum.spec.js ├── Lesson 6 - Leader ├── Dominator.spec.js ├── EquiLeader.spec.js ├── Dominator.js └── EquiLeader.js ├── package.json ├── LICENSE └── README.md /.gitignore: -------------------------------------------------------------------------------- 1 | .idea/ 2 | /node_modules/ 3 | *.log -------------------------------------------------------------------------------- /Lesson 1 – Time Complexity/FrogJmp.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function solution(X, Y, D) { 4 | return Math.ceil((Y - X) / D); 5 | }; -------------------------------------------------------------------------------- /Lesson 1 – Time Complexity/FrogJmp.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var solution = require('./FrogJmp'); 4 | 5 | it('should return the minimal number of jumps', function() { 6 | assert(solution(10, 85, 30) === 3); 7 | }); -------------------------------------------------------------------------------- /Lesson 3 – Prefix Sums/PassingCars.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var solution = require('./PassingCars'); 4 | 5 | it('should return the number of passing cars', function() { 6 | assert.deepEqual(solution([0,1,0,1,1]), 5); 7 | }); -------------------------------------------------------------------------------- /Lesson 1 – Time Complexity/PermMissingElem.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var solution = require('./PermMissingElem'); 4 | 5 | it('should return the value of the missing element', function() { 6 | assert(solution([2,3,1,5]) === 4); 7 | }); -------------------------------------------------------------------------------- /Lesson 5 - Stacks and Queues/StoneWall.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var solution = require('./StoneWall'); 4 | 5 | it('should return the minimum number of blocks', function() { 6 | assert.deepEqual(solution([8,8,5,7,9,8,7,4,8]), 7); 7 | }); 8 | 9 | -------------------------------------------------------------------------------- /Lesson 1 – Time Complexity/TapeEquilibrium.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var solution = require('./TapeEquilibrium'); 4 | 5 | it('should return the minimal difference that can be achieved', function() { 6 | assert(solution([3, 1, 2, 4, 3]) === 1); 7 | }); -------------------------------------------------------------------------------- /Lesson 4 – Sorting/MaxProductOfThree.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var solution = require('./MaxProductOfThree'); 4 | 5 | it('should return the value of the maximal product of any triplet', function() { 6 | assert.deepEqual(solution([-3,1,2,-2,5,6]), 60); 7 | }); -------------------------------------------------------------------------------- /Lesson 2 – Counting Elements/PermCheck.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var solution = require('./PermCheck'); 4 | 5 | it('should check whether array A is a permutation', function() { 6 | assert(solution([4,1,3,2]) === 1); 7 | assert(solution([4,1,3]) === 0); 8 | }); -------------------------------------------------------------------------------- /Lesson 1 – Time Complexity/PermMissingElem.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function solution(A) { 4 | var asum = 0; 5 | var n = A.length + 1; 6 | var sum = n * (n + 1) / 2; 7 | 8 | for (var i=0; i A.length){ 9 | return 0; 10 | } 11 | 12 | P[A[i]] = true; 13 | } 14 | 15 | return Object.keys(P).length === A.length ? 1 : 0; 16 | }; -------------------------------------------------------------------------------- /Lesson 4 – Sorting/MaxProductOfThree.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function solution(A) { 4 | A.sort(function(a,b){return b-a;}); 5 | 6 | return Math.max( 7 | A[0] * A[1] * A[2], 8 | A[0] * A[1] * A[A.length-1], 9 | A[0] * A[A.length-1] * A[A.length-2], 10 | A[A.length-1] * A[A.length-2] * A[A.length-3] 11 | ); 12 | }; -------------------------------------------------------------------------------- /Lesson 3 – Prefix Sums/PassingCars.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function solution(A) { 4 | var zeros = 0; 5 | var sum = 0; 6 | 7 | for (var i=0; i 1000000000) ? -1 : sum; 17 | }; -------------------------------------------------------------------------------- /Lesson 2 – Counting Elements/FrogRiverOne.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var solution = require('./FrogRiverOne'); 4 | 5 | it('should return the earliest time when the frog can jump to the other side of the river', function() { 6 | assert(solution(5, [1,3,1,4,2,3,5,4]) === 6); 7 | assert(solution(5, [1,2,4,5,2]) === -1); 8 | assert(solution(1, [1]) === 0); 9 | }); -------------------------------------------------------------------------------- /Lesson 4 – Sorting/Triangle.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var solution = require('./Triangle'); 4 | 5 | it('should exists a triangular triplet for an array', function() { 6 | assert.deepEqual(solution([10,2,5,1,8,20]), 1); 7 | }); 8 | 9 | it('should not exists a triangular triplet for an array', function() { 10 | assert.deepEqual(solution([10,50,5,1]), 0); 11 | }); -------------------------------------------------------------------------------- /Lesson 2 – Counting Elements/FrogRiverOne.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function solution(X, A) { 4 | 5 | var P = {}; 6 | var size = 0; 7 | 8 | for (var i=0; i=0.10.0" 14 | }, 15 | "scripts": { 16 | "test": "mocha" 17 | }, 18 | "keywords": [ 19 | "codility", 20 | "training" 21 | ], 22 | "devDependencies": { 23 | "mocha": "*" 24 | } 25 | } -------------------------------------------------------------------------------- /Lesson 7 - Maximum slice problem/MaxSliceSum.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function solution(A) { 4 | 5 | var max = null; 6 | var maxEnding = 0; 7 | var maxSlice = 0; 8 | 9 | for (var i=0; i max ? A[i] : max; 14 | maxEnding = maxEnding + A[i] > 0 ? maxEnding + A[i] : 0; 15 | maxSlice = maxSlice > maxEnding ? maxSlice : maxEnding; 16 | } 17 | 18 | return maxSlice === 0 ? max : maxSlice; 19 | }; -------------------------------------------------------------------------------- /Lesson 5 - Stacks and Queues/Fish.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var solution = require('./Fish'); 4 | 5 | it('should return the number of fish that will stay alive', function() { 6 | assert.deepEqual(solution([4,3,2,1,5], [0,1,0,0,0]), 2); 7 | }); 8 | 9 | it('should return the number of fish that will stay alive', function() { 10 | assert.deepEqual(solution([4,3,2,1,5], [0,1,0,1,0]), 2); 11 | }); 12 | 13 | it('should return the number of fish that will stay alive', function() { 14 | assert.deepEqual(solution([4,3,2,1,5], [0,1,0,1,1]), 4); 15 | }); 16 | 17 | -------------------------------------------------------------------------------- /Lesson 5 - Stacks and Queues/Brackets.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function solution(S) { 4 | 5 | var buffer = []; 6 | var opens = '([{'; 7 | var closes = ')]}'; 8 | 9 | for (var i = 0; i -1) { 11 | buffer.push(S[i]); 12 | continue; 13 | } 14 | if (!buffer.length) { 15 | return 0; 16 | } 17 | if (closes.indexOf(S[i]) !== opens.indexOf(buffer.pop())) { 18 | return 0; 19 | } 20 | } 21 | 22 | if (!buffer.length) { 23 | return 1; 24 | } 25 | 26 | return 0; 27 | }; -------------------------------------------------------------------------------- /Lesson 6 - Leader/EquiLeader.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var solution = require('./EquiLeader'); 4 | 5 | it('should return the number of equi leaders', function() { 6 | assert.deepEqual(solution([4,3,4,4,4,2]), 2); 7 | }); 8 | 9 | it('should return the number of equi leaders', function() { 10 | assert.deepEqual(solution([1, 1, 2, 2, 3]), 0); 11 | }); 12 | 13 | it('should return the number of equi leaders', function() { 14 | assert.deepEqual(solution([2, 2, 1]), 0); 15 | }); 16 | 17 | it('should return the number of equi leaders', function() { 18 | assert.deepEqual(solution([-3, 3, -3, -3, -3, 7]), 2); 19 | }); 20 | 21 | -------------------------------------------------------------------------------- /Lesson 7 - Maximum slice problem/MaxSliceSum.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var solution = require('./MaxSliceSum'); 4 | 5 | it('should return the maximum sum of any slice of A', function() { 6 | assert.deepEqual(solution([3, 2, -6, 4, 0]), 5); 7 | }); 8 | 9 | it('should return the maximum sum of any slice of A', function() { 10 | assert.deepEqual(solution([3, 2, -1, 4, 0]), 8); 11 | }); 12 | 13 | it('hould return the maximum sum of any slice of A', function() { 14 | assert.deepEqual(solution([-5, -4, -3, -10]), -3); 15 | }); 16 | 17 | it('hould return the maximum sum of any slice of A', function() { 18 | assert.deepEqual(solution([-1, -5, 0]), 0); 19 | }); 20 | 21 | -------------------------------------------------------------------------------- /Lesson 5 - Stacks and Queues/Brackets.spec.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | var assert = require('assert'); 3 | var solution = require('./Brackets'); 4 | 5 | it('should be properly nested', function() { 6 | assert.deepEqual(solution(''), 1); 7 | }); 8 | 9 | it('should be properly nested', function() { 10 | assert.deepEqual(solution('{[()()]}'), 1); 11 | }); 12 | 13 | it('should be properly nested', function() { 14 | assert.deepEqual(solution('()()'), 1); 15 | }); 16 | 17 | it('should be properly nested', function() { 18 | assert.deepEqual(solution('()(){}'), 1); 19 | }); 20 | 21 | it('should be properly nested', function() { 22 | assert.deepEqual(solution('{[()()]}()'), 1); 23 | }); 24 | 25 | it('should not be properly nested', function() { 26 | assert.deepEqual(solution('([)()]'), 0); 27 | }); -------------------------------------------------------------------------------- /Lesson 2 – Counting Elements/MaxCounters.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function solution(N, A) { 4 | var counters = new Array(N); 5 | for (var i=0; i= 1 && A[k] <= N){ 14 | if (counters[A[k]-1] < nextMax){ 15 | counters[A[k]-1] = nextMax; 16 | } 17 | counters[A[k]-1]++; 18 | 19 | if (max < counters[A[k]-1]) { 20 | max = counters[A[k]-1]; 21 | } 22 | } 23 | if (A[k] == N + 1) { 24 | nextMax = max; 25 | } 26 | } 27 | 28 | for (var j=0; j lastBlock) { 19 | blocks.push(lastBlock); 20 | blocks.push(H[i]); 21 | count++; 22 | continue; 23 | } 24 | 25 | while (lastBlock && H[i] < lastBlock) { 26 | if (blocks.length) { 27 | lastBlock = blocks.pop(); 28 | } 29 | else { 30 | lastBlock = null; 31 | } 32 | } 33 | 34 | if (lastBlock != null) { 35 | blocks.push(lastBlock); 36 | } 37 | 38 | if (H[i] != lastBlock) { 39 | blocks.push(H[i]); 40 | count++; 41 | } 42 | } 43 | 44 | return count; 45 | }; -------------------------------------------------------------------------------- /Lesson 5 - Stacks and Queues/Fish.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function solution(A, B) { 4 | 5 | var alive = 0; 6 | var predators = []; 7 | 8 | for (var i=0; i< A.length; i++) { 9 | 10 | if (B[i] === 1) { 11 | predators.push(A[i]); 12 | } 13 | else { 14 | if (predators.length) { 15 | var predator = predators.pop(); 16 | while (predator && predator < A[i]) { 17 | if (predators.length) { 18 | predator = predators.pop(); 19 | } 20 | else { 21 | predator = null; 22 | } 23 | } 24 | 25 | if (predator && predator > A[i]) { 26 | predators.push(predator); 27 | } 28 | else { 29 | alive++; 30 | } 31 | } 32 | else { 33 | alive++; 34 | } 35 | } 36 | } 37 | 38 | alive += predators.length; 39 | return alive; 40 | }; -------------------------------------------------------------------------------- /Lesson 6 - Leader/EquiLeader.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = function solution(A) { 4 | 5 | var size = 0; 6 | var leaderCandidate = null; 7 | var equiLeaders = 0; 8 | 9 | for (var j=0; j (i + 1) / 2 && leaderCount > (A.length - i - 1) / 2) { 42 | equiLeaders++; 43 | } 44 | } 45 | 46 | return equiLeaders; 47 | }; -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Jan Antala 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # codility 2 | 3 | JS solutions to Codility tasks: https://codility.com/train/ 4 | 5 | ## Lesson 1 – Time Complexity 6 | 7 | - [TapeEquilibrium](https://codility.com/demo/take-sample-test/tape_equilibrium) 8 | - [FrogJmp](https://codility.com/demo/take-sample-test/frog_jmp) 9 | - [PermMissingElem](https://codility.com/demo/take-sample-test/perm_missing_elem) 10 | 11 | ## Lesson 2 – Counting Elements 12 | - [PermCheck](https://codility.com/demo/take-sample-test/perm_check) 13 | - [FrogRiverOne](https://codility.com/demo/take-sample-test/frog_river_one) 14 | - [MaxCounters](https://codility.com/demo/take-sample-test/max_counters) 15 | 16 | ## Lesson 3 – Prefix Sums 17 | - [PassingCars](https://codility.com/demo/take-sample-test/passing_cars) 18 | 19 | ## Lesson 4 – Sorting 20 | - [Distinct](https://codility.com/demo/take-sample-test/distinct) 21 | - [MaxProductOfThree](https://codility.com/demo/take-sample-test/max_product_of_three) 22 | - [Triangle](https://codility.com/demo/take-sample-test/triangle) 23 | 24 | ## Lesson 5 - Stacks and Queues 25 | - [Brackets](https://codility.com/demo/take-sample-test/brackets) 26 | - [Nesting](https://codility.com/demo/take-sample-test/nesting) 27 | - [Fish](https://codility.com/demo/take-sample-test/fish) 28 | - [StoneWall](https://codility.com/demo/take-sample-test/stone_wall) 29 | 30 | ## Lesson 6 - Leader 31 | - [EquiLeader](https://codility.com/demo/take-sample-test/equi_leader) 32 | - [Dominator](https://codility.com/demo/take-sample-test/dominator) 33 | 34 | ## Lesson 7 - Maximum slice problem 35 | - [MaxProfit](https://codility.com/demo/take-sample-test/max_profit) 36 | - [MaxSliceSum](https://codility.com/demo/take-sample-test/max_slice_sum) 37 | 38 | # License 39 | 40 | The MIT License 41 | 42 | Copyright (c) 2015 [Jan Antala](http://www.janantala.com) 43 | --------------------------------------------------------------------------------