首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >彼得是个挑剔的食客!

彼得是个挑剔的食客!
EN

Code Golf用户
提问于 2021-05-19 12:34:14
回答 12查看 3.4K关注 0票数 37

有个男孩叫彼得。彼得是个挑剔的食客,不喜欢在同一天再吃同一种饼干。

彼得得到一份礼物,礼物是一盒饼干!该框包含K类型的cookie。另外,它还包含了每种类型的cookie的不同数量。

彼得是个挑剔的食客,他想在每一天吃n饼干。此外,他不想在任何一天吃超过一种相同种类的饼干。

也就是说,他在1日吃1^{st}曲奇,在2^{nd}日吃2 cookie等等。

如果没有办法让彼得在第二天吃n饼干的话,他就放弃,把盒子扔了。如果盒子变成空的,他也会把它扔掉。

找出这个盒子对彼得持续的最大天数。

输入

K正整数,指定每种不同类型的cookie的数量。

输出

与彼得保存盒子的天数相对应的整数。

测试用例

代码语言:javascript
复制
INPUT -> OUTPUT
1 2 -> 2
1 2 3 -> 3
1 2 3 4 4 -> 4
1 2 3 3 -> 3
2 2 2 -> 3
1 1 1 1 -> 2
11 22 33 44 55 66 77 88 99 -> 9
10 20 30 40 50 60 70 80 -> 8
3 3 3 3 3 3 3 3 3 3 3 -> 7
3 3 3 3 3 3 3 3 3 -> 6
9 5 6 8 7 -> 5
1 1 1 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 2 2 2 2 2 2 3 3 3 3 3 3 3 3 3 3 3 3 4 4 4 4 4 4 4 4 4 4 4 4 5 5 5 5 5 5 5 5 5 5 5 5 6 6 6 6 6 6 6 6 6 6 6 6 7 7 7 7 7 7 7 7 7 7 7 7 8 8 8 8 8 8 8 8 8 8 8 8 9 9 9 9 9 9 9 9 9 9 9 9 10 10 10 10 10 10 10 10 10 10 10 10 -> 35
1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 1 2 3 4 5 6 7 8 9 10 -> 32

测试用例要点

这是密码-高尔夫,所以最短的代码获胜。

EN

回答 12

Code Golf用户

发布于 2021-05-19 16:05:24

JavaScript (ES6),70字节

代码语言:javascript
复制
f=(a,c=d=0)=>c?d-1:f(a.sort((a,b)=>b-a).map(x=>c*x?x-!!c--:x,c=++d),c)

在网上试试!

评论

代码语言:javascript
复制
f = (                  // f is a recursive function taking:
  a,                   //   a[] = list of cookies
  c =                  //   c   = cookies that were not eaten the day before
  d = 0                //   d   = day counter
) =>                   //
  c ?                  // if we failed to eat all the cookies the day before:
    d - 1              //   stop and return d - 1
  :                    // else:
    f(                 //   do a recursive call:
      a.sort((a, b) => //     sort the list of cookies ...
        b - a          //       ... from highest to lowest
      )                //
      .map(x =>        //     for each value x in the sorted list:
        c * x ?        //       if both c and x are not equal to 0:
          x - !!c--    //         decrement x and c
        :              //       else:
          x,           //         leave x unchanged
        c = ++d        //       start by incrementing d and setting c to d
      ),               //     end of map()
      c                //     pass c
    )                  //   end of recursive call
票数 5
EN

Code Golf用户

发布于 2021-05-19 14:09:03

05AB1E,14字节

代码语言:javascript
复制
v{āNÌ‹R-Wds}\O

在网上试试!试试看所有的案子!

注释:

代码语言:javascript
复制
v          }     # for N in range(len(input))
 {               # sort the current cookie list (initially the input)
  ā              # push the range [1..len(list)]
   NÌ            # push N+2
     ‹           # is each value in the range less than N+2?
                 # this yields a list with N+1 leading ones
      R          # reverse the list of 1's and 0's
       -         # subtract this from the current cookie list
        Wds      # push min(list) >= 0 under the current list

            \    # after the loop: discard the final cookie list
             O   # sum the stack: this counts the number of times
                 #                the cookie counts were non-negative
票数 4
EN

Code Golf用户

发布于 2021-05-19 21:46:57

K (ngn/k),32字节

代码语言:javascript
复制
{#(|/'0>)_x{x[y#>x]-:1;x}\1+!#x}

在网上试试!

  • x{...}\1+!#x设置一个扫描,种子为x (每种类型的cookie数),运行在1..(number of types of cookies + 1) (变量y) 上。
    • y#>x标识要吃的n cookie(即包含最大值的y索引)
    • {x[...]-:1;x}吃了这些cookie,将结果输入下一次扫描。

  • (|/'0>)_...删除任何负值的扫描迭代结果
  • #...返回过滤结果的计数
票数 3
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/225962

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档