未验证 提交 50c9a891 编写于 作者: F feikerwu 提交者: GitHub

feat(js): add js solution for problem 1011 (#332)

* feat(js): add js solution for problem 1011

* Update 1011.capacity-to-ship-packages-within-d-days.md
Co-authored-by: Nlucifer <azl397985856@gmail.com>
上级 e77854d8
......@@ -66,17 +66,16 @@ https://leetcode-cn.com/problems/capacity-to-ship-packages-within-d-days
```python
def canShip(opacity):
# 指定船的容量是否可以在D天运完
lo = 0
hi = total
while lo < hi:
mid = (lo + hi) // 2
if canShip(mid):
hi = mid
else:
lo = mid + 1
return lo
lo = 0
hi = total
while lo < hi:
mid = (lo + hi) // 2
if canShip(mid):
hi = mid
else:
lo = mid + 1
return lo
```
## 关键点解析
......@@ -85,6 +84,10 @@ return lo
## 代码
* 语言支持:`JS``Python`
`python`:
```python
class Solution:
def shipWithinDays(self, weights: List[int], D: int) -> int:
......@@ -115,6 +118,50 @@ class Solution:
return lo
```
`js`:
```js
/**
* @param {number[]} weights
* @param {number} D
* @return {number}
*/
var shipWithinDays = function(weights, D) {
let high = weights.reduce((acc, cur) => acc + cur)
let low = 0
while(low < high) {
let mid = Math.floor((high + low) / 2)
if (canShip(mid)) {
high = mid
} else {
low = mid + 1
}
}
return low
function canShip(opacity) {
let remain = opacity
let count = 1
for (let weight of weights) {
if (weight > opacity) {
return false
}
remain -= weight
if (remain < 0) {
count++
remain = opacity - weight
}
if (count > D) {
return false
}
}
return count <= D
}
};
```
## 扩展
## 参考
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册