堆积块


15

给定输入要在某些点放下的块的列表,请输出结果“塔”的高度。

解释此挑战的最佳方法是通过示例。输入将是2n个代表n个块的整数的列表。第一个整数是块的x位置(从0开始),第二个整数是块的宽度。例如,输入2 4代表该块(下面标有x坐标):

  ####
0123456789

现在,假设输入为2 4 4 6。也就是说,x = 2处的一个块的宽度为4,x = 4处的一个块的宽度为6:

    ######
  ####

请注意,a。)块始终从塔的顶部“掉落”,b。)块永远不会“掉落”(即,它们始终保持平衡)。因此,输入的 2 4 4 6 12 1代表:

    ######
  ####      #

请注意,最后一块已经完全落到了“地面”上。

您的最终输出应该是每个x值处塔的最大高度,直至最大。因此,输入2 4 4 6 12 1应产生输出 0011222222001

    ######
  ####      #
0011222222001

输入可以以空格/逗号分隔的字符串,整数数组或函数/命令行参数的形式给出。块位置(x值)将始终为0或更大的整数,宽度将始终为1或更大的整数,并且始终至少有一个块。

输出可以是由非数字字符(例如"0, 0, 1, ...")分隔的单个字符串,列出所有数字的单个字符串(例如 "001..."-保证最大高度为9或更小)或整数数组。

由于这是,因此以字节为单位的最短代码将获胜。

测试用例:

In                                   Out
---------------------------------------------------------
2 4 4 6 12 1                         0011222222001
0 5 9 1 6 4 2 5                      1133333222
0 5 9 1 2 5 6 4                      1122223333
0 5 2 5 6 4 9 1                      1122223334
20 1 20 1 20 1                       00000000000000000003
5 5                                  000011111
0 2 1 2 2 2 3 2 4 2 5 2 6 2 7 2 8 4  123456789999

我们可以将输入作为2元组的数组吗?
lirtosiast

@ThomasKwa不,输入必须是一维数组。
Doorknob

Answers:


2

CJam,34个 30字节

Lq~2/{eeWf%e~_2$:).*:e>f*.e>}/

输入为CJam样式的数组,输出为数字字符串。

运行所有测试用例。

这是另一个构想的两个变体,但目前多了2个字节:

Lq~2/{_:\3$><0+:e>)aeez.*e_.e>}/
LQ~2/{_:\3$><0+:e>)aeez.+e~.e>}/

6

Python 3、89

def f(a):
 h=[]
 while a:x,w,*a=a;h[:x+w]=(h+[0]*x)[:x]+[max(h[x:x+w]+[0])+1]*w
 return h

在线尝试

该函数接受并返回一个整数列表。

def f(a):                       # input as list of integers
  h=[]                          # list of heights
  while a:                      # while there's input left
    x,w,*a=a;                   # pop first 2 integers as x and w

    h[:x+w]=                    # change the heights between 0 and x+w
      (h+[0]*x)[:x]+            # left of x -> unchanged but padded with zeros
      [max(h[x:x+w]+[0])+1]*w   # between x and x+w -> set to the previous max + 1

  return h                      # return the list of heights

2

红宝石, 88 87字节

f=->i{o=[]
(s,l,*i=i
r=s...s+l
o[r]=[([*o[r]]+[0]).max+1]*l
o.map! &:to_i)while i[0]
o}

在线尝试。

受grc的答案启发,但使用的语言不同,但略短。

说明:

f=->i                        # lambda with parameter i, expects array of ints
{
    o=[]                     # output
    (
        s,l,*i=i             # pop start and length
        r = s...s+l          # range is used twice, so shorten it to 1 char
        o[r] =
            [(
                    [*o[r]]  # o[r] returns nil if out of bounds, so splat it into another array
                    +[0]     # max doesn't like an empty array, so give it at least a 0
            ).max+1]*l       # repeat max+1 to fill length
        o.map! &:to_i        # replace nil values with 0
    ) while i[0]             # i[0] returns nil when i is empty, which is falsy
    o                        # return o
}

1

APL,79个字节

{⊃{o←(z←(≢⍵)⌈a←+/⍺)↑⍵⋄e←(z↑(-a)↑⍺[1]⍴1)⋄o+0⌈o-⍨e×e⌈.+e×o}/⌽(⊂⍬),↓(⌽2,0.5×≢⍵)⍴⍵}

输入为APL数组,输出为数字APL数组。


{⊃{o←⍵↑⍨z←(≢⍵)⌈a←+/⍺⋄e←z↑(-a)↑⍺[1]⍴1⋄o+0⌈o-⍨e×e⌈.+e×o}/⌽(⊂⍬),↓⍵⍴⍨⌽2,.5×≢⍵}(我的上帝,学会使用权)
扎卡里

请小心你的话......你似乎并不知道其中的差别之间,并1↑正因为如此,你给建议,原因更新的程序给错误的结果,但我不光顾你。
lstefano

是的,有时候当我看到一堆东西可以打高尔夫球时,我就会那样。但是,其他高尔夫球应该仍然适用。
扎卡里

他们都做。而且,我已将您的建议整合到了一起,希望能得到适当的赞誉。
lstefano

--是吗?- -0.5
扎卡里

0

的Java 1.8,351个 329字节

第一次尝试并不感到兴奋-我确信双循环,所有这些Integer.valueOf都可以打更多。

interface B{static void main(String[]x){Byte b=1;int i=0,s,c,m=0,h,a=x.length,t[];for(;i<a;){s=b.valueOf(x[i++]);c=b.valueOf(x[i++]);m=m>s+c?m:s+c;}t=new int[m];for(i=0;i<a;){h=0;s=b.valueOf(x[i++]);c=b.valueOf(x[i++]);for(m=s;m<s+c;m++)if(t[m]>=h)h=t[m]+1;for(m=s;m<s+c;)t[m++]=h;}for(i=0;i<t.length;)System.out.print(t[i++]);}}

不打高尔夫球

interface B {
static void main(String[]x){
    int start, count, maxWidth=0, height, args=x.length, totals[];
    Byte b=1;
    for (int i=0; i<args;){
        start = b.valueOf(x[i++]);
        count = b.valueOf(x[i++]);
        maxWidth = maxWidth>start+count ? maxWidth : start+count; 
    }
    totals=new int[maxWidth];
    for (int i=0; i<args;){
        height=0;
        start = b.valueOf(x[i++]);
        count = b.valueOf(x[i++]);
        for (int j = start; j<start+count; j++) {
            if (totals[j]>=height) {
                height=totals[j]+1;
            }
        }
        for (int j = start; j<start+count; j++) {
            totals[j] = height;
        }
    }
    for (int i=0;i<totals.length; i++){
        System.out.print(totals[i]);
    }
}
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.