Hi there, I've been working on this problem for a few hours and I just can't seem to get 10 random test cases out of the 100 right.
If anyone could offer some help, it would be greatly appreciated. Here's the link to the problem:
Just a heads up, there might be a better way to solve the problem than what I came up with initially.
Code snippet:
function conquerIsland(map) {
let path=[];
let mar=[];
let len=0,len1=0;
for(let i=1;i<8;i++)
{
for( let j=0;j<i;j++)
{
if(map[j][i]=='u')
{
if(len1===0 || len1==i+j)
{
path.push([j,i]);
len1=i+j;
}
}
if(map[i][j]=='u')
{
if(len1===0 || len1==i+j)
{
path.push([i,j]);
len1=i+j;
}
}
if(map[j][i]=='m')
{
if(len==0 || len==i+j)
{
mar.push([j,i]);
len=i+i;
}
}
if(map[i][j]=='m')
{
if(len==0 || len==i+j){
mar.push([i,j]);
len=i+j;
}
}
}
if(map[i][i]=='m')
{
if(len==0 || len==i+i)
{
mar.push([i,i]);
len=i+i;
}
}
if(map[i][i]=='u')
{
if(len1==0 || len1==i+i)
{
path.push([i,i]);
len1=i*2;
}
}
}
if(path.length>0)
{
if(path.length==1)
{ let path1;
path1 = [].concat.apply([], path);
return path1;
}
else
{
path.sort(sortFunction);
function sortFunction(a, b) {
if (a[0] === b[0]) {
return 0;
}
else {
return (a[0] < b[0]) ? -1 : 1;
}
}
return path;
}
}
else
if(mar!=[])
{
if(mar.length==1)
{ let mar1;
mar1 = [].concat.apply([], mar);
return mar1;
}
else
{
mar.sort(sortFunction);
function sortFunction(a, b) {
if (a[0] === b[0]) {
return 0;
}
else {
return (a[0] < b[0]) ? -1 : 1;
}
}
return mar;
}
}
else {
return [];
}
}