有个男孩叫彼得。彼得是个挑剔的食客,不喜欢在同一天再吃同一种饼干。
彼得得到一份礼物,礼物是一盒饼干!该框包含K类型的cookie。另外,它还包含了每种类型的cookie的不同数量。
彼得是个挑剔的食客,他想在每一天吃n饼干。此外,他不想在任何一天吃超过一种相同种类的饼干。
也就是说,他在1日吃1^{st}曲奇,在2^{nd}日吃2 cookie等等。
如果没有办法让彼得在第二天吃n饼干的话,他就放弃,把盒子扔了。如果盒子变成空的,他也会把它扔掉。
找出这个盒子对彼得持续的最大天数。
K正整数,指定每种不同类型的cookie的数量。
与彼得保存盒子的天数相对应的整数。
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这是密码-高尔夫,所以最短的代码获胜。
发布于 2021-05-19 16:05:24
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)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发布于 2021-05-19 14:09:03
v{āNÌ‹R-Wds}\O注释:
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-negativehttps://codegolf.stackexchange.com/questions/225962
复制相似问题