Last active
August 29, 2015 13:55
-
-
Save liyu1981/8731977 to your computer and use it in GitHub Desktop.
Hill problem
This file contains hidden or 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
/* | |
Challenge 2: Hill | |
Given an array of integer elements | |
Your task is to | |
* write a function that finds the minimum value X that makes possible the following: generate a new array that is sorted in strictly ascending order by increasing or decreasing each of the elements of the initial array with integer values in the [0, X] range. | |
Example: Having the initial array [5, 4, 3, 2, 8] the minimum value for X is 3. Decrease the first element (5) by 3, decrease the second one (4) by 1, increase the third one (3) by 1, increase the forth one (2) by 3 and do nothing to the last one (8). In the end we obtain the array [2, 3, 4, 5, 8] which is sorted in strictly ascending order. | |
* print the result X to the standard output (stdout) | |
Note that your function will receive the following arguments: | |
* v which is the array of integers | |
Data constraints | |
* numbers are in ascending order when arranged from the smallest to the largest number | |
* strictly ascending order means that each element is greater than the preceding one (e.g. [1, 2, 2, 3] is NOT strictly ascending order) | |
* the length of the array will not exceed 5000 | |
* the elements of any of the arrays are integer numbers in the [1, 2^31 -1] range | |
Efficiency constraints | |
your function is expected to print the result in less than 2 seconds | |
Example | |
Input v: 5, 4, 3, 2, 8 | |
Output: 3 | |
*/ | |
function hill(v) { | |
var state = { off: null, max: null }; | |
for (var i=0; i<v.length; i++) { | |
if (i === 0) { | |
state.off = 0; | |
state.max = v[i]; | |
} else { | |
if (v[i] > state.max) { | |
state.max = v[i]; | |
} else { | |
var o = state.max - v[i]; | |
if (o > state.off) { | |
state.off = o; | |
} else if (o === state.off) { | |
state.off = state.off + 1; | |
} else { | |
// keep same state.off | |
} | |
state.max = state.max; | |
} | |
} | |
} | |
console.log(state.off); | |
} |
hill([5,4,3,0,8])
returns 5
it is not correct.
My solution is:
function hill(v) {
for (var i = 0; i < 10; i++) {
var current = v[0] - i;
for (var k = 1; k < v.length; k++) {
current++;
if (v[k] >= current) {
if (v[k] - current > i) {
current = v[k] - i;
}
} else {
if (current - v[k] > i) break;
}
}
if (k === v.length) {
console.log(i)
return i;
}
}
}
returns 4 in this case
A different approach.
function hill(v) {
var copy = v.slice(0).sort();
var max = 0;
for(var i = 0; i < v.length; i++) {
var a = v[i];
var b = copy[i];
// Difference
if(a < b && ((b-a) > max)) {
max = b - a;
} else if(a-b > max) {
max = a - b;
}
}
return max;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output:
3
2
2