我遇到了Ghost Leg挑战
,程序应找到通向最左侧路径的垂直线号(例如,在下图中,程序应返回3,因为它可以按照“鬼腿法则”到达最左侧) ,遇到碰到的地方应该沿着水平线)
参数传递如下:
7 4 5 // lines[1] refers to length of each vertical line, the number of vertical lines,the number of horizontal lines
1 3 1 // from lines[2~L-1] each line refers to the a_i, b_i, c_i length of a horizontal line(pls check the picture)
3 2 2
2 3 5
3 4 4
1 6 6
以下是我的解决方案:
let lines = [ '5 5 8',
'3 3 4',
'1 3 2',
'4 2 2',
'2 1 2',
'2 4 4',
'3 1 1',
'1 4 3',
'4 3 4' ];
// convert the stdin to an two Dimentional array
let newLines = [];
let log = console.log.bind(console)
for(var i=1; i<lines.length; i++) {
let eachLine = []
let eachEle = lines[i].split(' ')
for(var j in eachEle) {
eachLine.push(parseInt(eachEle[j]))
}
newLines.push(eachLine)
}
// my way try to do it is the opposite way, like start from the vertical Line 1, and move back to the top following the rule, and return the line number it reaches
let cur_index = 1
let last_c_i = 7
let nodefound = [1]
while(nodefound !== []) {
const filteredarr = newLines.filter(
function(each) {
return (each[0] === cur_index && each[1] < last_c_i) || (each[0] === cur_index - 1 && each[2] < last_c_i)})
if (filteredarr.length !== 0) {
nodefound = filteredarr.reduce(function (prev, current) {
return (prev[1] > current[1]) ? prev : current
})
last_c_i = nodefound[2]
} else {
return console.log(cur_index);
}
log('nodefound', nodefound)
if (nodefound[0] === cur_index.toString() || nodefound[0] === cur_index) {
cur_index++
} else {
cur_index--
}
}
该解决方案通过了一些案例,但失败了一些,我无法检查所有测试用例,因此无法弄清楚问题出在哪里。 任何帮助将不胜感激,谢谢!
答案 0 :(得分:0)
我修改了代码,并通过了所有测试。 我认为这里有很多肮脏的代码。稍后将尝试对其进行优化... 如果对此有任何建议也将有所帮助
process.stdin.resume();
process.stdin.setEncoding('utf8');
// the challenge is read form stdin
var lines = [];
var reader = require('readline').createInterface({
input: process.stdin,
output: process.stdout
});
reader.on('line', (line) => {
lines.push(line);
});
reader.on('close', () => {
let newLines = []
let verLines = parseInt(lines[0].split(' ')[1])
let lstIndex = verLines + 1
for(var i=1; i<lines.length; i++) {
let eachLine = []
let eachEle = lines[i].split(' ')
for(var j in eachEle) {
eachLine.push(parseInt(eachEle[j]))
}
newLines.push(eachLine)
}
let cur_index = 0
let last_c_i = 0
let cur_result = 0
let nodefound = []
let lstReturn
function smallestOfFour(r) {
var smallest= Math.min.apply(null, r.join(",").split(",")); //merge arrays then find largest number in flat
return r.find(/./.test, RegExp("\\b" + smallest + "\\b")); // find by matching element of sub-array to biggest flat array value
}
for(var j = 1; j <= verLines; j++) {
cur_index = j
while (nodefound !== []) {
// filtered array in the line
let filteredarr_cur = newLines.filter(
function (each) {
return (each[0] === cur_index && each[1] >= last_c_i && each !== nodefound)
})
let filteredarr_last = newLines.filter(
function (each) {
return (each[0] === cur_index - 1 && each[2] > last_c_i && each !== nodefound)
}
)
if (filteredarr_cur.length !== 0) {
var current_cur = filteredarr_cur.reduce((prev, current) => {
return prev[1] < current[1] ? prev : current
})
tmp = current_cur
last_c_i = tmp[2]
} else {
//
}
if (filteredarr_last.length !== 0) {
var last_cur = filteredarr_last.reduce((prev, current) => {
return prev[2] < current[2] ? prev : current
})
tmp = last_cur
last_c_i = tmp[1]
}
if (filteredarr_cur.length !== 0 && filteredarr_last.length !== 0) {
if (current_cur[1] < last_cur[2]) {
tmp = current_cur
last_c_i = tmp[2]
} else {
tmp = last_cur
last_c_i = tmp[1]
}
}
if (filteredarr_cur.length == 0 && filteredarr_last.length == 0) {
tmp = []
last_c_i = 0
nodefound = []
break
}
nodefound = tmp
if (nodefound[0] === cur_index) {
cur_index++
} else {
cur_index--
}
}
if (cur_index < lstIndex) {
lstIndex = cur_index
lstReturn = j
}
}
log(lstReturn)
});