中级农民
- 积分
- 105
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2020-9-20
- 最后登录
- 1970-1-1
|
我觉得最好的办法是不要自己写,用自带的库函数,算cheating吗?
https://www.1point3acres.com/bbs/thread-670713-1-1.html
昨天刷的题
LC 1011
- import java.util.AbstractList
- class Solution {
- fun shipWithinDays(weights: IntArray, D: Int): Int {
- val list = object : AbstractList<Int> () {
- override fun get(index: Int): Int {
- return index + 1
- }
- override val size: Int
- get() = weights.sum()
- }
- val index = list.binarySearch{
- var current = 0
- var count = 1
- for(i in weights){
- if((current + i) <= it) {
- current += i
- } else {
- current = i
- count += 1
- if(count > D || current > it){
- println("$it is not valid")
- return@binarySearch -1
- }
- }
- }
- println("$it is valid")
- 1
- }
- return -index
- }
- }
复制代码
|
|