mirror of
https://github.com/youngyangyang04/leetcode-master.git
synced 2025-07-08 08:50:15 +08:00
Merge pull request #1307 from xiaofei-2020/dp30
添加(0213.打家劫舍II.md):增加typescript版本
This commit is contained in:
@ -165,7 +165,30 @@ const robRange = (nums, start, end) => {
|
|||||||
return dp[end]
|
return dp[end]
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
TypeScript:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
function rob(nums: number[]): number {
|
||||||
|
const length: number = nums.length;
|
||||||
|
if (length === 0) return 0;
|
||||||
|
if (length === 1) return nums[0];
|
||||||
|
return Math.max(robRange(nums, 0, length - 2),
|
||||||
|
robRange(nums, 1, length - 1));
|
||||||
|
};
|
||||||
|
function robRange(nums: number[], start: number, end: number): number {
|
||||||
|
if (start === end) return nums[start];
|
||||||
|
const dp: number[] = [];
|
||||||
|
dp[start] = nums[start];
|
||||||
|
dp[start + 1] = Math.max(nums[start], nums[start + 1]);
|
||||||
|
for (let i = start + 2; i <= end; i++) {
|
||||||
|
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
|
||||||
|
}
|
||||||
|
return dp[end];
|
||||||
|
}
|
||||||
|
```
|
||||||
|
|
||||||
Go:
|
Go:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
// 打家劫舍Ⅱ 动态规划
|
// 打家劫舍Ⅱ 动态规划
|
||||||
// 时间复杂度O(n) 空间复杂度O(n)
|
// 时间复杂度O(n) 空间复杂度O(n)
|
||||||
|
Reference in New Issue
Block a user