Skip to content

Commit bf531a7

Browse files
committed
docs: add No198、No278题解
1 parent 3b6c65d commit bf531a7

File tree

3 files changed

+132
-1
lines changed

3 files changed

+132
-1
lines changed

leetcode刷题/README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
- [No.160 相交链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No160_get-intersection-node.md)
2626
- [No.169 多数元素](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No169_majority-element.md)
2727
- [No.189 旋转数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No189_rotate-arr.md)
28+
- [No.198 打家劫舍](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No198_rob.md)
2829
- [No.204 计数质数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No204_count-primes.md)
2930
- [No.206 反转链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No206_reverse-list.md)
3031
- [No.234 回文链表](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No234_is-palindrome.md)
@@ -90,4 +91,16 @@
9091

9192
- [No.326 3的幂](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No326_is-power-of-three.md)
9293
- [No.204 计数质数](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No204_count-primes.md)
93-
- [No.492 构造矩形](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No492_construct-rectangle.md)
94+
- [No.492 构造矩形](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No492_construct-rectangle.md)
95+
96+
97+
### 动态规划
98+
99+
- [No.70 爬楼梯](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No70_climb-stairs.md)
100+
- [No.121 买卖股票的最佳时机](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No121_max-profit.md)
101+
- [No.198 打家劫舍](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No198_rob.md)
102+
103+
### 排序和搜索
104+
105+
- [No.88 合并两个有序数组](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No88_merge.md)
106+
- [No.278 第一个错误的版本](https://github.com/Mayandev/javascript_algorithm/blob/master/leetcode%E5%88%B7%E9%A2%98/note/No278_is-bad-version.md)

leetcode刷题/note/No198_rob.md

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# No.198 打家劫舍
2+
3+
难度:`easy`
4+
5+
你是一个专业的小偷,计划偷窃沿街的房屋。每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报警。
6+
7+
给定一个代表每个房屋存放金额的非负整数数组,计算你在不触动警报装置的情况下,能够偷窃到的最高金额。
8+
9+
## 示例
10+
11+
示例 1:
12+
```
13+
输入: [1,2,3,1]
14+
输出: 4
15+
解释: 偷窃 1 号房屋 (金额 = 1) ,然后偷窃 3 号房屋 (金额 = 3)。
16+
  偷窃到的最高金额 = 1 + 3 = 4 。
17+
```
18+
示例 2:
19+
```
20+
输入: [2,7,9,3,1]
21+
输出: 12
22+
解释: 偷窃 1 号房屋 (金额 = 2), 偷窃 3 号房屋 (金额 = 9),接着偷窃 5 号房屋 (金额 = 1)。
23+
  偷窃到的最高金额 = 2 + 9 + 1 = 12 。
24+
```
25+
26+
## 解题思路
27+
28+
很经典的一道动态规划的题目,首先需要找到动态规划的状态转移方程。
29+
30+
举例来说:1 号房间可盗窃最大值为 33 即为 dp[1]=3,2 号房间可盗窃最大值为 44 即为 dp[2]=4,3 号房间自身的值为 22 即为 num=2,那么 dp[3] = MAX( dp[2], dp[1] + num ) = MAX(4, 3+2) = 5,3 号房间可盗窃最大值为 5。
31+
32+
动态规划方程:`dp[n] = MAX( dp[n-1], dp[n-2] + num )`
33+
34+
代码如下:
35+
36+
```javascript
37+
/**
38+
* @param {number[]} nums
39+
* @return {number}
40+
*/
41+
var rob = function(nums) {
42+
let n = nums.length;
43+
let preMax = 0;
44+
let curMax = 0;
45+
for (let i = 0; i < n; i++) {
46+
let temp = curMax;
47+
curMax = Math.max(preMax + nums[i], curMax);
48+
preMax = temp;
49+
}
50+
return curMax;
51+
};
52+
```
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
# No.278 第一个错误的版本
2+
3+
难度:`easy`
4+
5+
6+
你是产品经理,目前正在带领一个团队开发新的产品。不幸的是,你的产品的最新版本没有通过质量检测。由于每个版本都是基于之前的版本开发的,所以错误的版本之后的所有版本都是错的。
7+
8+
假设你有 n 个版本 [1, 2, ..., n],你想找出导致之后所有版本出错的第一个错误的版本。
9+
10+
你可以通过调用 bool isBadVersion(version) 接口来判断版本号 version 是否在单元测试中出错。实现一个函数来查找第一个错误的版本。你应该尽量减少对调用 API 的次数。
11+
12+
# 示例
13+
14+
示例:
15+
16+
给定 n = 5,并且 version = 4 是第一个错误的版本。
17+
18+
```
19+
调用 isBadVersion(3) -> false
20+
调用 isBadVersion(5) -> true
21+
调用 isBadVersion(4) -> true
22+
所以,4 是第一个错误的版本。 
23+
```
24+
25+
## 解题思路
26+
27+
使用二分查找的思路,如果中间值isBad,则向左搜索,否则向右搜索。
28+
29+
代码如下:
30+
31+
```javascript
32+
/**
33+
* Definition for isBadVersion()
34+
*
35+
* @param {integer} version number
36+
* @return {boolean} whether the version is bad
37+
* isBadVersion = function(version) {
38+
* ...
39+
* };
40+
*/
41+
42+
/**
43+
* @param {function} isBadVersion()
44+
* @return {function}
45+
*/
46+
var solution = function(isBadVersion) {
47+
/**
48+
* @param {integer} n Total versions
49+
* @return {integer} The first bad version
50+
*/
51+
return function(n) {
52+
let low = 1;
53+
let high = n;
54+
while (low < high) {
55+
let mid = Math.floor((low + high) / 2);
56+
if (isBadVersion(mid)) {
57+
high = mid;
58+
59+
} else {
60+
low = mid + 1;
61+
}
62+
}
63+
return low;
64+
};
65+
};
66+
```

0 commit comments

Comments
 (0)