nikita.groshin
BAN USERHere is two solution - iterative and hashtable in javascript
var min = 1,
max = 100,
i,
arr = [];
for (i = min; i < max; i++)
{
arr.push(i);
}
//Yes I know it is already sorted - it just indicate that fact that sorting is required
arr.sort(function(a, b) {
return a - b;
});
function isPairSumIterative(arr, k) {
var start = 0,
end = arr.length - 1;
while (start < end) {
var sum = arr[start] + arr[end];
if (sum === k) {
return true;
} else if (sum > k) {
end--;
} else {
start++;
}
}
return false;
}
function isPairSumHashTable(arr, k) {
var hash = {}, found = false;
arr.some(function(item){
var diff = k - item;
if (diff >= 0 && hash[diff]) {
found = true;
return true;
} else {
hash[item] = 0;
}
});
return found || hash[k] !== undefined;
}
console.log(isPairSumIterative(arr, 92));
console.log(isPairSumHashTable(arr, 92));
Javascript solution
//Prepare an array
var min = 0,
max = 1000,
i,
arr = [];
for (i = min; i < max; i++)
{
arr.push(i);
}
arr.sort(function(a, b) {
return Math.random() > 0.5;
});
//Solution itself
var currentMin = Number.MAX_VALUE,
arrayOfMinIndexs = [];
arr.forEach(function(item, index){
if (item < currentMin) {
currentMin = item;
arrayOfMinIndexs = [index];
} else if(item === currentMin) {
arrayOfMinIndexs.push(index);
}
});
console.log('Min value: ' + currentMin);
console.log(arrayOfMinIndexs);
Here is simple js solution
I'm nor sure is it correct, but I choose beginning of coordinates as [0, 0] in left bottom side
In this case robot goes out of screen with directions from example
/**
* Abstraction of grid
*
* @param {Number} width
* @param {Number} height
*
* @constructor
*/
var Grid = function(width, height) {
/**
* Grid width
*
* @type {Number}
*/
this.width = width;
/**
* Grid height
*
* @type {Number}
*/
this.height = height;
/**
*
* @type {null|Robot}
*/
this.robot = null;
};
/**
* Add robot to grid
*
* @param {Robot} robot
*/
Grid.prototype.addRobot = function(robot) {
this.robot = robot;
if (!this._isFit()) {
throw new Error('Robot not fit');
}
};
/**
*
*
*/
Grid.prototype.update = function() {
if (!this._isFit()) {
throw new Error('Robot not fit');
}
};
/**
* Checks is robot fit in grid
*
* @returns {boolean}
* @private
*/
Grid.prototype._isFit = function() {
return this.robot.x>=0
&& this.robot.y >=0
&& this.robot.x < this.width
&& this.robot.y < this.height;
};
/**
*
* Abstraction of robot
*
* @param {Number} x
* @param {Number} y
* @param {string} direction
* @constructor
*/
var Robot = function(x, y, direction) {
/**
* X coordinate
*
* @type {Number}
*/
this.x = x;
/**
* X coordinate
*
* @type {Number}
*/
this.y = y;
/**
* Current direction
*
* @type {string}
*/
this.direction = direction || 'N';
/**
* Available directions
*
* @type {string[]}
* @private
*/
this._directions = [
'N',
'S',
'E',
'W'
];
};
/**
* Make step - move or change direction
*
* @param step
*/
Robot.prototype.step = function (step) {
if (this._isDirection(step)) {
this.direction = step;
} else {
this._move();
}
};
/**
* Check is step direction
*
* @param step
* @returns {boolean}
* @private
*/
Robot.prototype._isDirection = function (step) {
return this._directions.indexOf(step) !== -1;
};
/**
* Move robot to direction
*
* @private
*/
Robot.prototype._move = function () {
switch(this.direction){
case 'N':
this.y++;
break;
case 'S':
this.y--;
case 'W':
this.x++;
break;
case 'E':
this.x--;
};
};
var grid = new Grid(100, 500);
var robot = new Robot(5, 3);
grid.addRobot(robot);
['N','S', 'M', 'M', 'E', 'W', 'E', 'S' , 'M', 'S', 'M'].forEach(function(step){
robot.step(step);
grid.update();
});
console.log(robot);
You guys overcomplicate it
Here is simple solution in js
//Prepare array
var min = 0,
max = 1000,
i,
arr = [];
for (i = min; i < max; i++)
{
arr.push(i);
}
arr.sort(function(a, b) {
return Math.random() > 0.5;
});
//Create simple kinda 'Heap'
var Heap = function(size){
this.size = size;
this.data = [];
};
Heap.prototype.push = function(item) {
if (this.data.length < this.size) {
this.data.push(item);
} else {
for (var i = 0; i < this.size; i++) {
if (this.data[i] < item) {
this.data[i] = item;
return;
}
}
}
};
Heap.prototype.print = function() {
console.log(this.data);
};
///Then iterate over array and push items in heap
var heap = new Heap(10);
arr.forEach(function(item) {
heap.push(item);
});
heap.print();
In js it is trivial
- nikita.groshin July 24, 2015