G
G
Glorious Cat2018-03-10 10:32:40
JavaScript
Glorious Cat, 2018-03-10 10:32:40

The task from How I Flunked a Twitter Interview, how to show filling with water?

There is a task to calculate the number of cells (blocks) that water must fill when it rains. It is necessary to draw the conclusion of the walls (I did this) and show how the gaps between the walls will be filled with water. So, how to make this most filling with water?

https://codepen.io/anon/pen/RMNQOQ

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vasily Varyukhin, 2018-03-10
@tansur

My pen test .
PS Well, further optimize.

V
ViaCom, 2018-03-11
@ViaCom

Suddenly, on tsql
, it was too lazy to write dynamic pivot, and the number of rows is equal to the number of columns, again out of laziness.
The problem, alas, was not solved by one request, since the CTE in memory is formed by a new one every time we connect with it in the from clause. If it were not for the need to generate columns randomly, it would be possible to write in one request.

declare @m int = 16
drop table if exists #n
;with z(n, c1, c2) as 
(
  select 
     n  = 1
    ,c1 = a.c
    ,c2 = a.c
  from (select replicate(N'■', abs(checksum(newid())) % @m) c ) a
  union all 
  select 
     a.n1
    ,a.c2
    ,a.c2 + isnull(replicate(N'□', len(a.c1)-len(a.c2)) ,N'')
  from 
  (
    select 
       n1 = n+1
      ,c1 = z.c2
      ,c2 = replicate(N'■', abs(checksum(newid())) % @m ) 
    from z where n < @m
  ) a
) 
select * into #n from z 
order by n desc
option (maxrecursion 100)

select left(c2,l) from #n z1
cross apply (select max(len(c1)) l from #n z2 where z2.n >= z1.n) x
order by n

And the result
5aa50aeef132c325622342.jpeg

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question