作为竞争性编程的初学者,我一直在努力解决hackerearth Problem here中的问题。我的问题只解决了第一个测试案例当我发现错误时,如果部件在特定测试中不能正常工作,则测试用例将在下面以及程序中给出
1
22
289 558 271 15 492 853 988 834 766 573 316 959 192 7 730 277 631 97 717
185 514 86
我的问题
function main(input) {
//Enter your code here
var data=input.split("\n"),templen=1,temparr=2,X,arr,len,temp,sight,min=0,ans;
var T=parseInt(data[0]);
for(var i=0;i<T;i++) {
min=0;
len=parseInt(data[templen]);
arr=data[temparr].split(" ");
arr.map(function(x) {
return parseInt(x);
});
if(len===1) {
ans=1;
}
//Looping from the first array element
for(var j=0;j<len;j++) {
temp=j-1;
X=0;
console.log("Value is " + j + " Array value is "+ arr[j]);
//A while loop to find out if the current element is greater than
previous element it goes till the beginning of the array
while(temp>=0) {
//If the element is greater will increment X value
if(arr[j]>arr[temp]) {
X=X+1;
console.log("Greater Temp arr "+ arr[temp]);
temp--;
}
//If the element is smaller it will break out the while loop
else {
X=X+1;
break;
}
}
temp=j+1;
//A while loop to find out if the current element is greater than
previous element it goes till the end of the array
while(temp<len) {
//If the element is greater will increment X value
of the array
if(arr[j]>arr[temp]) {
X=X+1;
console.log("Temp arr "+ arr[temp]);
temp++;
}
//If the element is lesser it will break out the while loop
else {
X=X+1;
break;
}
}
sight=X*(j+1);
console.log("Sight is "+ sight + " X is " + X + " J is "+ j + " array value is " + arr[j]);
if(sight>min) {
min=sight;
ans=j+1;
}
}
console.log(ans);
templen+=2;
temparr+=2;
}
}
process.stdin.resume();
process.stdin.setEncoding("utf-8");
var stdin_input = "";
process.stdin.on("data", function (input) {
stdin_input += input;
});
process.stdin.on("end", function () {
main(stdin_input);
});
如果你看一下97的arr [17],if条件是如何工作的,因为它不大于下一个元素arr [18],它是717,也不是前一个元素arr [16],它是631如何通过如果条件。任何帮助将不胜感激
答案 0 :(得分:0)
这只是一个带有两个while
循环的方法,一个用于计算前面的元素,另一个用于计算以下元素。
这个提议不是最优的,因为我想显示计数的中间结果集。
var data = [289, 558, 271, 15, 492, 853, 988, 834, 766, 573, 316, 959, 192, 7, 730, 277, 631, 97, 717, 185, 514, 86],
count = data.map(function (a, i, aa) {
var c = 0,
p = i;
// check previous elements
while (p-- && aa[p] < a) { // decrement and check index and if
c++; // value is smaller than actual item
} // then increment counter
p = i;
while (++p < aa.length && aa[p] < a) { // check following elements
c++;
}
return c;
}),
result = count.reduce(function (r, a, i, aa) {
return r && aa[r] * (r + 1) < a * (i + 1) ? r : i;
}, -1);
console.log('index:', result, 'value:', data[result]);
console.log(count);
&#13;
.as-console-wrapper { max-height: 100% !important; top: 0; }
&#13;