Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Solution 364149

Submitted on 4 Dec 2013 by Alfonso Nieto-Castanon

Correct

177Size
Leading solution size is 108.
This solution is locked. To view this solution, you need to provide a solution of the same size or smaller.

Test Suite

Test
Code Input and Output
1
Pass
 
%%
%Games Feb 2014 #1
vr=[0 0 1 0 5]'; %1
vL=[0 4 4 0 0]';
vd=[2 2 0 1 3];
vu=[3 0 0 2 0];
tic
m=solve_skyscrapers(vr,vL,vd,vu)
toc
nr=length(vr);
nrsum=nr*(nr+1)/2;
assert(nr*nrsum==sum(m(:)))
assert(nr==size(m,1));
assert(nr==size(m,2));
assert(all(sum(m)==nrsum));
assert(all(sum(m,2)==nrsum));
mt=m; % view right check
vz=vr;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(k,1);
 for z=2:nr
  if mt(k,z)>shi
   shi=mt(k,z);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
vz=vd; % view down check
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(1,k);
 for z=2:nr
  if mt(z,k)>shi
   shi=mt(z,k);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
mt=fliplr(m); % view Left check
vz=vL;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(k,1);
 for z=2:nr
  if mt(k,z)>shi
   shi=mt(k,z);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)); % Assert check of valid count
 end % if
end % k
mt=flipud(m); % view Up check
vz=vu;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(1,k);
 for z=2:nr
  if mt(z,k)>shi
   shi=mt(z,k);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
m =
     4     1     2     5     3
     3     5     4     2     1
     5     4     1     3     2
     2     3     5     1     4
     1     2     3     4     5
Elapsed time is 5.177687 seconds.
2
Pass
 
%%
%Games Feb 2014 #2
vr=[0 4 0 2 0]'; %2
vL=[5 1 0 0 0]';
vd=[0 0 3 0 0];
vu=[4 1 2 0 2];
tic
m=solve_skyscrapers(vr,vL,vd,vu)
toc
nr=length(vr);
nrsum=nr*(nr+1)/2;
assert(nr*nrsum==sum(m(:)))
assert(nr==size(m,1));
assert(nr==size(m,2));
assert(all(sum(m)==nrsum));
assert(all(sum(m,2)==nrsum));
mt=m; % view right check
vz=vr;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(k,1);
 for z=2:nr
  if mt(k,z)>shi
   shi=mt(k,z);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
vz=vd;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(1,k);
 for z=2:nr
  if mt(z,k)>shi
   shi=mt(z,k);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
mt=fliplr(m); % view right check
vz=vL;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(k,1);
 for z=2:nr
  if mt(k,z)>shi
   shi=mt(k,z);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)); % Assert check of valid count
 end % if
end % k
mt=flipud(m);
vz=vu;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(1,k);
 for z=2:nr
  if mt(z,k)>shi
   shi=mt(z,k);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
m =
     5     4     3     2     1
     2     3     4     1     5
     4     2     1     5     3
     3     1     5     4     2
     1     5     2     3     4
Elapsed time is 6.264220 seconds.
3
Pass
 
%%
%Games Feb 2014 #3
vr=[5 2 2 0 0]'; %3
vL=[0 3 0 3 4]';
vd=[5 0 0 0 0];
vu=[0 2 0 0 0];
tic
m=solve_skyscrapers(vr,vL,vd,vu)
toc
nr=length(vr);
nrsum=nr*(nr+1)/2;
assert(nr*nrsum==sum(m(:)))
assert(nr==size(m,1));
assert(nr==size(m,2));
assert(all(sum(m)==nrsum));
assert(all(sum(m,2)==nrsum));
mt=m; % view right check
vz=vr;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(k,1);
 for z=2:nr
  if mt(k,z)>shi
   shi=mt(k,z);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
vz=vd;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(1,k);
 for z=2:nr
  if mt(z,k)>shi
   shi=mt(z,k);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
mt=fliplr(m); % view right check
vz=vL;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(k,1);
 for z=2:nr
  if mt(k,z)>shi
   shi=mt(k,z);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)); % Assert check of valid count
 end % if
end % k
mt=flipud(m);
vz=vu;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(1,k);
 for z=2:nr
  if mt(z,k)>shi
   shi=mt(z,k);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
m =
     1     2     3     4     5
     2     5     4     1     3
     3     1     2     5     4
     4     3     5     2     1
     5     4     1     3     2
Elapsed time is 0.089569 seconds.
4
Pass
 
%%
%Games Feb 2014 #4
vr=[0 0 4 5 0]'; %4
vL=[0 0 0 0 0]';
vd=[2 0 2 3 0];
vu=[0 0 0 0 0];
tic
m=solve_skyscrapers(vr,vL,vd,vu)
toc
nr=length(vr);
nrsum=nr*(nr+1)/2;
assert(nr*nrsum==sum(m(:)))
assert(nr==size(m,1));
assert(nr==size(m,2));
assert(all(sum(m)==nrsum));
assert(all(sum(m,2)==nrsum));
mt=m; % view right check
vz=vr;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(k,1);
 for z=2:nr
  if mt(k,z)>shi
   shi=mt(k,z);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
vz=vd;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(1,k);
 for z=2:nr
  if mt(z,k)>shi
   shi=mt(z,k);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
mt=fliplr(m); % view right check
vz=vL;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(k,1);
 for z=2:nr
  if mt(k,z)>shi
   shi=mt(k,z);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)); % Assert check of valid count
 end % if
end % k
mt=flipud(m);
vz=vu;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(1,k);
 for z=2:nr
  if mt(z,k)>shi
   shi=mt(z,k);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
m =
     4     5     2     1     3
     3     1     5     2     4
     2     3     4     5     1
     1     2     3     4     5
     5     4     1     3     2
Elapsed time is 5.459626 seconds.
5
Pass
 
%%
%Games Feb 2014 #5
vr=[3 5 0 0 0]'; %5
vL=[0 0 4 0 0]';
vd=[0 0 0 0 0];
vu=[2 0 1 0 2];
tic
m=solve_skyscrapers(vr,vL,vd,vu)
toc
nr=length(vr);
nrsum=nr*(nr+1)/2;
assert(nr*nrsum==sum(m(:)))
assert(nr==size(m,1));
assert(nr==size(m,2));
assert(all(sum(m)==nrsum));
assert(all(sum(m,2)==nrsum));
mt=m; % view right check
vz=vr;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(k,1);
 for z=2:nr
  if mt(k,z)>shi
   shi=mt(k,z);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
vz=vd;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(1,k);
 for z=2:nr
  if mt(z,k)>shi
   shi=mt(z,k);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
mt=fliplr(m); % view right check
vz=vL;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(k,1);
 for z=2:nr
  if mt(k,z)>shi
   shi=mt(k,z);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)); % Assert check of valid count
 end % if
end % k
mt=flipud(m);
vz=vu;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(1,k);
 for z=2:nr
  if mt(z,k)>shi
   shi=mt(z,k);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
m =
     2     3     1     5     4
     1     2     3     4     5
     5     4     2     3     1
     3     5     4     1     2
     4     1     5     2     3
Elapsed time is 1.848349 seconds.
6
Pass
 
%%
vr=[0 0 3 0 0]'; %Games Feb 2014 #6
vL=[3 0 0 1 0]';
vd=[0 0 0 0 0];
vu=[5 2 0 0 0];
tic
m=solve_skyscrapers(vr,vL,vd,vu)
toc
nr=length(vr);
nrsum=nr*(nr+1)/2;
assert(nr*nrsum==sum(m(:)))
assert(nr==size(m,1));
assert(nr==size(m,2));
assert(all(sum(m)==nrsum));
assert(all(sum(m,2)==nrsum));
mt=m; % view right check
vz=vr;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(k,1);
 for z=2:nr
  if mt(k,z)>shi
   shi=mt(k,z);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
vz=vd;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(1,k);
 for z=2:nr
  if mt(z,k)>shi
   shi=mt(z,k);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
mt=fliplr(m); % view right check
vz=vL;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(k,1);
 for z=2:nr
  if mt(k,z)>shi
   shi=mt(k,z);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)); % Assert check of valid count
 end % if
end % k
mt=flipud(m);
vz=vu;
for k=1:nr
 if vz(k)>0
 c=1;
 shi=mt(1,k);
 for z=2:nr
  if mt(z,k)>shi
   shi=mt(z,k);
   c=c+1;
  end
 end % z
 c;
 vz(k);
 assert(c==vz(k)) % Assert check of valid count
 end % if
end % k
m =
     5     4     2     1     3
     4     5     1     3     2
     3     2     4     5     1
     2     1     3     4     5
     1     3     5     2     4
Elapsed time is 6.381944 seconds.