Skip to content

Commit 1463061

Browse files
author
lucifer
committed
2 parents 123a0ba + 50c9a89 commit 1463061

File tree

4 files changed

+91
-97
lines changed

4 files changed

+91
-97
lines changed

daily/2019-07-26.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
这是数据压缩问题中的一种。
1616

17-
类似的问题有, 如果将 IP 地址用 4 个字节来表示等等。
17+
类似的问题有, 如何将 IP 地址用 4 个字节来表示等等。
1818

1919
这道题的思路,如果我们不考虑用一个字节去存储的话,我们通过观察
2020
坐标,发现“坐标和 3 取余的结果相同的就是同一列”,因此我们可以根据

problems/1011.capacity-to-ship-packages-within-d-days.md

Lines changed: 58 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,16 @@ https://leetcode-cn.com/problems/capacity-to-ship-packages-within-d-days
6666
```python
6767
def canShip(opacity):
6868
# 指定船的容量是否可以在D天运完
69-
lo = 0
70-
hi = total
71-
while lo < hi:
72-
mid = (lo + hi) // 2
73-
if canShip(mid):
74-
hi = mid
75-
else:
76-
lo = mid + 1
77-
78-
return lo
79-
69+
lo = 0
70+
hi = total
71+
while lo < hi:
72+
mid = (lo + hi) // 2
73+
if canShip(mid):
74+
hi = mid
75+
else:
76+
lo = mid + 1
77+
78+
return lo
8079
```
8180

8281
## 关键点解析
@@ -85,6 +84,10 @@ return lo
8584

8685
## 代码
8786

87+
* 语言支持:`JS``Python`
88+
89+
`python`:
90+
8891
```python
8992
class Solution:
9093
def shipWithinDays(self, weights: List[int], D: int) -> int:
@@ -115,6 +118,50 @@ class Solution:
115118
return lo
116119
```
117120

121+
`js`:
122+
123+
```js
124+
/**
125+
* @param {number[]} weights
126+
* @param {number} D
127+
* @return {number}
128+
*/
129+
var shipWithinDays = function(weights, D) {
130+
let high = weights.reduce((acc, cur) => acc + cur)
131+
let low = 0
132+
133+
while(low < high) {
134+
let mid = Math.floor((high + low) / 2)
135+
if (canShip(mid)) {
136+
high = mid
137+
} else {
138+
low = mid + 1
139+
}
140+
}
141+
142+
return low
143+
144+
function canShip(opacity) {
145+
let remain = opacity
146+
let count = 1
147+
for (let weight of weights) {
148+
if (weight > opacity) {
149+
return false
150+
}
151+
remain -= weight
152+
if (remain < 0) {
153+
count++
154+
remain = opacity - weight
155+
}
156+
if (count > D) {
157+
return false
158+
}
159+
}
160+
return count <= D
161+
}
162+
};
163+
```
164+
118165
## 扩展
119166

120167
## 参考

problems/887.super-egg-drop.md

Lines changed: 1 addition & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -108,88 +108,8 @@ Note:
108108

109109
## 代码
110110

111-
```js
112111

113-
/*
114-
* @lc app=leetcode id=887 lang=javascript
115-
*
116-
* [887] Super Egg Drop
117-
*
118-
* https://leetcode.com/problems/super-egg-drop/description/
119-
*
120-
* algorithms
121-
* Hard (24.64%)
122-
* Total Accepted: 6.2K
123-
* Total Submissions: 24.9K
124-
* Testcase Example: '1\n2'
125-
*
126-
* You are given K eggs, and you have access to a building with N floors from 1
127-
* to N.
128-
*
129-
* Each egg is identical in function, and if an egg breaks, you cannot drop it
130-
* again.
131-
*
132-
* You know that there exists a floor F with 0 <= F <= N such that any egg
133-
* dropped at a floor higher than F will break, and any egg dropped at or below
134-
* floor F will not break.
135-
*
136-
* Each move, you may take an egg (if you have an unbroken one) and drop it
137-
* from any floor X (with 1 <= X <= N).
138-
*
139-
* Your goal is to know with certainty what the value of F is.
140-
*
141-
* What is the minimum number of moves that you need to know with certainty
142-
* what F is, regardless of the initial value of F?
143-
*
144-
*
145-
*
146-
*
147-
*
148-
*
149-
*
150-
* Example 1:
151-
*
152-
*
153-
* Input: K = 1, N = 2
154-
* Output: 2
155-
* Explanation:
156-
* Drop the egg from floor 1. If it breaks, we know with certainty that F = 0.
157-
* Otherwise, drop the egg from floor 2. If it breaks, we know with certainty
158-
* that F = 1.
159-
* If it didn't break, then we know with certainty F = 2.
160-
* Hence, we needed 2 moves in the worst case to know what F is with
161-
* certainty.
162-
*
163-
*
164-
*
165-
* Example 2:
166-
*
167-
*
168-
* Input: K = 2, N = 6
169-
* Output: 3
170-
*
171-
*
172-
*
173-
* Example 3:
174-
*
175-
*
176-
* Input: K = 3, N = 14
177-
* Output: 4
178-
*
179-
*
180-
*
181-
*
182-
* Note:
183-
*
184-
*
185-
* 1 <= K <= 100
186-
* 1 <= N <= 10000
187-
*
188-
*
189-
*
190-
*
191-
*
192-
*/
112+
```js
193113
/**
194114
* @param {number} K
195115
* @param {number} N
@@ -205,7 +125,6 @@ var superEggDrop = function(K, N) {
205125
for (let k = 1; k <= K; ++k)
206126
dp[m][k] = dp[m - 1][k - 1] + 1 + dp[m - 1][k];
207127
}
208-
console.log(dp);
209128
return m;
210129
};
211130
```

problems/98.validate-binary-search-tree.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ Explanation: The root node's value is 5 but its right child's value is 4.
4141
我们只需要中序遍历,然后两两判断是否有逆序的元素对即可,如果有,则不是BST,否则即为一个BST。
4242

4343
### 定义法
44-
根据定义,一个结点若是在根的左子树上,那它应该小于根结点的值而大于左子树最大值;若是在根的右子树上,那它应该大于根结点的值而小于右子树最小值。也就是说,每一个结点必须落在某个取值范围:
44+
根据定义,一个结点若是在根的左子树上,那它应该小于根结点的值而大于左子树最小值;若是在根的右子树上,那它应该大于根结点的值而小于右子树最大值。也就是说,每一个结点必须落在某个取值范围:
4545
1. 根结点的取值范围为(考虑某个结点为最大或最小整数的情况):(long_min, long_max)
4646
2. 左子树的取值范围为:(current_min, root.value)
4747
3. 右子树的取值范围为:(root.value, current_max)
@@ -151,7 +151,7 @@ public:
151151
};
152152
```
153153
154-
Java Implementation
154+
Java Code:
155155
156156
```java
157157
/**
@@ -185,7 +185,7 @@ class Solution {
185185

186186
### 定义法
187187

188-
* 语言支持:C++,Python3, Java
188+
* 语言支持:C++,Python3, Java, JS
189189

190190
C++ Code:
191191

@@ -311,6 +311,34 @@ class Solution {
311311
}
312312
```
313313

314+
JavaScript Code:
315+
316+
```javascript
317+
/**
318+
* Definition for a binary tree node.
319+
* function TreeNode(val) {
320+
* this.val = val;
321+
* this.left = this.right = null;
322+
* }
323+
*/
324+
/**
325+
* @param {TreeNode} root
326+
* @return {boolean}
327+
*/
328+
var isValidBST = function (root) {
329+
if (!root) return true;
330+
return valid(root);
331+
};
332+
333+
function valid(root, min = -Infinity, max = Infinity) {
334+
if (!root) return true;
335+
const val = root.val;
336+
if (val <= min) return false;
337+
if (val >= max) return false;
338+
return valid(root.left, min, val) && valid(root.right, val, max)
339+
}
340+
```
341+
314342
## 相关题目
315343

316344
[230.kth-smallest-element-in-a-bst](./230.kth-smallest-element-in-a-bst.md)

0 commit comments

Comments
 (0)