Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit bb32a41

Browse files
Merge pull request youngyangyang04#1306 from xiaofei-2020/dp29
添加(0198.打家劫舍.md):增加typescript版本
2 parents f115be0 + c363e9d commit bb32a41

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed

‎problems/0198.打家劫舍.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,29 @@ const rob = nums => {
189189
};
190190
```
191191

192+
TypeScript:
193+
194+
```typescript
195+
function rob(nums: number[]): number {
196+
/**
197+
dp[i]: 前i个房屋能偷到的最大金额
198+
dp[0]: nums[0];
199+
dp[1]: max(nums[0], nums[1]);
200+
...
201+
dp[i]: max(dp[i-1], dp[i-2]+nums[i]);
202+
*/
203+
const length: number = nums.length;
204+
if (length === 1) return nums[0];
205+
const dp: number[] = [];
206+
dp[0] = nums[0];
207+
dp[1] = Math.max(nums[0], nums[1]);
208+
for (let i = 2; i < length; i++) {
209+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
210+
}
211+
return dp[length - 1];
212+
};
213+
```
214+
192215

193216

194217

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /