Skip to content

Commit

Permalink
update: 打家劫舍
Browse files Browse the repository at this point in the history
  • Loading branch information
yi-ge committed Sep 16, 2023
1 parent d884fde commit 6983b1e
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 49 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,10 @@ TypeScript / JavaScript 基础算法、数据结构练习,包含 LeetCode 或

### 数组/队列/集合/映射

- [打家劫舍](src/array/house-robber.ts) [数组, 动态规划]

- LeetCode 198. 打家劫舍 <https://leetcode.cn/problems/house-robber>

- [水果成篮](src/array/fruit-into-baskets.ts) [数组, 哈希表, 滑动窗口]

- LeetCode 904. 水果成篮 <https://leetcode.cn/problems/fruit-into-baskets>
Expand Down
Binary file added images/array/house-robber.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,6 @@
]
},
"dependencies": {
"leetcode-test-helper": "^0.4.4"
"leetcode-test-helper": "^0.4.7"
}
}
11 changes: 6 additions & 5 deletions src/array/house-robber.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
/**
* @param {number[]} nums
* @return {number}
*/
// 打家劫舍
// https://leetcode.cn/problems/house-robber
// INLINE ../../images/array/house-robber.jpeg

export const rob = function (nums?: number[]): number {
if (!nums) return 0
const len = nums.length
if (len === 0) return 0
if (len === 1) return nums[0]

let first = nums[0]; let second = Math.max(nums[0], nums[1])
let first = nums[0]
let second = Math.max(nums[0], nums[1])
for (let i = 2; i < len; i++) {
const temp = second
second = Math.max(first + nums[i], second)
Expand Down
12 changes: 11 additions & 1 deletion test/array/house-robber.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,15 @@ test('打家劫舍', () => {
expect(rob([])).toBe(0)
expect(rob([1, 2, 3, 1])).toBe(4)
expect(rob([2, 7, 9, 3, 1])).toBe(12)
expect(rob([828, 125, 740, 724, 983, 321, 773, 678, 841, 842, 875, 377, 674, 144, 340, 467, 625, 916, 463, 922, 255, 662, 692, 123, 778, 766, 254, 559, 480, 483, 904, 60, 305, 966, 872, 935, 626, 691, 832, 998, 508, 657, 215, 162, 858, 179, 869, 674, 452, 158, 520, 138, 847, 452, 764, 995, 600, 568, 92, 496, 533, 404, 186, 345, 304, 420, 181, 73, 547, 281, 374, 376, 454, 438, 553, 929, 140, 298, 451, 674, 91, 531, 685, 862, 446, 262, 477, 573, 627, 624, 814, 103, 294, 388])).toBe(29123)
expect(
rob([
828, 125, 740, 724, 983, 321, 773, 678, 841, 842, 875, 377, 674, 144, 340,
467, 625, 916, 463, 922, 255, 662, 692, 123, 778, 766, 254, 559, 480, 483,
904, 60, 305, 966, 872, 935, 626, 691, 832, 998, 508, 657, 215, 162, 858,
179, 869, 674, 452, 158, 520, 138, 847, 452, 764, 995, 600, 568, 92, 496,
533, 404, 186, 345, 304, 420, 181, 73, 547, 281, 374, 376, 454, 438, 553,
929, 140, 298, 451, 674, 91, 531, 685, 862, 446, 262, 477, 573, 627, 624,
814, 103, 294, 388,
])
).toBe(29123)
})
86 changes: 44 additions & 42 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1137,7 +1137,7 @@ fs.realpath@^1.0.0:
resolved "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz"
integrity sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==

fsevents@^2.3.2:
fsevents@2.3.2, fsevents@^2.3.2:
version "2.3.2"
resolved "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz"
integrity sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==
Expand Down Expand Up @@ -1778,14 +1778,14 @@ lazy-cache@^1.0.3:
resolved "https://registry.yarnpkg.com/lazy-cache/-/lazy-cache-1.0.4.tgz#a1d78fc3a50474cb80845d3b3b6e1da49a446e8e"
integrity sha512-RE2g0b5VGZsOCFOCgP7omTRYFqydmZkBwl5oNnQ1lDYC57uyO9KqNnNVxT7COSHTxrRCWVcAVOcbjk+tvh/rgQ==

leetcode-test-helper@^0.4.4:
version "0.4.4"
resolved "https://registry.yarnpkg.com/leetcode-test-helper/-/leetcode-test-helper-0.4.4.tgz#fcfe1dd6c1762f75d7618745aa8782908068507f"
integrity sha512-7Zuhk/J7IEo03C17oUwZw98md2321Cb5G4e9AUrXMGlna+hIdjhOKaoshDFxWMsxl5n/NjXJifJ/zVCi5olYlg==
leetcode-test-helper@^0.4.7:
version "0.4.7"
resolved "https://registry.yarnpkg.com/leetcode-test-helper/-/leetcode-test-helper-0.4.7.tgz#7798cdf1c322f1ed452b8b26d56b769a5c732aed"
integrity sha512-ULRnWlvIMhduzAvUdxAh1moyrraOLsm5CmA0chYXZYo7OSfpnp176B6JDca+lFGvrPvRLicDzB+3rmi1uHz0xg==
dependencies:
playwright "^1.26.0"
playwright-extra "^4.3.5"
puppeteer-extra-plugin-stealth "^2.11.1"
playwright "^1.38.0"
playwright-extra "^4.3.6"
puppeteer-extra-plugin-stealth "^2.11.2"

leven@^3.1.0:
version "3.1.0"
Expand Down Expand Up @@ -2006,24 +2006,26 @@ pkg-dir@^4.2.0:
dependencies:
find-up "^4.0.0"

playwright-core@1.26.0:
version "1.26.0"
resolved "https://registry.yarnpkg.com/playwright-core/-/playwright-core-1.26.0.tgz#850228f0638d410a5cdd69800d552f60e4d295cd"
integrity sha512-p8huU8eU4gD3VkJd3DA1nA7R3XA6rFvFL+1RYS96cSljCF2yJE9CWEHTPF4LqX8KN9MoWCrAfVKP5381X3CZqg==
playwright-core@1.38.0:
version "1.38.0"
resolved "https://registry.yarnpkg.com/playwright-core/-/playwright-core-1.38.0.tgz#cb8e135da1c0b1918b070642372040ed9aa7009a"
integrity sha512-f8z1y8J9zvmHoEhKgspmCvOExF2XdcxMW8jNRuX4vkQFrzV4MlZ55iwb5QeyiFQgOFCUolXiRHgpjSEnqvO48g==

playwright-extra@^4.3.5:
version "4.3.5"
resolved "https://registry.yarnpkg.com/playwright-extra/-/playwright-extra-4.3.5.tgz#696512cc2c0750a5e2d420522152b8dd46ba9fbd"
integrity sha512-YPYsoJFK46h2FqNpD8//ucutSf3c84C0KVYX19gUr0rErajrWZcnfoNfm/okzc8z6/EfTWS3xWb76jDbYI8Qew==
playwright-extra@^4.3.6:
version "4.3.6"
resolved "https://registry.yarnpkg.com/playwright-extra/-/playwright-extra-4.3.6.tgz#3e132e88b88fd5e046b8a73f724b3f2712fb2c3f"
integrity sha512-q2rVtcE8V8K3vPVF1zny4pvwZveHLH8KBuVU2MoE3Jw4OKVoBWsHI9CH9zPydovHHOCDxjGN2Vg+2m644q3ijA==
dependencies:
debug "^4.3.4"

playwright@^1.26.0:
version "1.26.0"
resolved "https://registry.yarnpkg.com/playwright/-/playwright-1.26.0.tgz#b351990ff03a7e422bf585233eb53651fc363896"
integrity sha512-XxTVlvFEYHdatxUkh1KiPq9BclNtFKMi3BgQnl/aactmhN4G9AkZUXwt0ck6NDAOrDFlfibhbM7A1kZwQJKSBw==
playwright@^1.38.0:
version "1.38.0"
resolved "https://registry.yarnpkg.com/playwright/-/playwright-1.38.0.tgz#0ee19d38512b7b1f961c0eb44008a6fed373d206"
integrity sha512-fJGw+HO0YY+fU/F1N57DMO+TmXHTrmr905J05zwAQE9xkuwP/QLDk63rVhmyxh03dYnEhnRbsdbH9B0UVVRB3A==
dependencies:
playwright-core "1.26.0"
playwright-core "1.38.0"
optionalDependencies:
fsevents "2.3.2"

pretty-format@^29.0.0, pretty-format@^29.0.3:
version "29.0.3"
Expand All @@ -2042,39 +2044,39 @@ prompts@^2.0.1:
kleur "^3.0.3"
sisteransi "^1.0.5"

puppeteer-extra-plugin-stealth@^2.11.1:
version "2.11.1"
resolved "https://registry.yarnpkg.com/puppeteer-extra-plugin-stealth/-/puppeteer-extra-plugin-stealth-2.11.1.tgz#7d56a27a986cb5eb69dca3c65695ad6444f4e822"
integrity sha512-n0wdC0Ilc9tk5L6FWLyd0P2gT8b2fp+2NuB+KB0oTSw3wXaZ0D6WNakjJsayJ4waGzIJFCUHkmK9zgx5NKMoFw==
puppeteer-extra-plugin-stealth@^2.11.2:
version "2.11.2"
resolved "https://registry.yarnpkg.com/puppeteer-extra-plugin-stealth/-/puppeteer-extra-plugin-stealth-2.11.2.tgz#bd3f5a1781cac8a98c983d148086585a84fcc8f1"
integrity sha512-bUemM5XmTj9i2ZerBzsk2AN5is0wHMNE6K0hXBzBXOzP5m5G3Wl0RHhiqKeHToe/uIH8AoZiGhc1tCkLZQPKTQ==
dependencies:
debug "^4.1.1"
puppeteer-extra-plugin "^3.2.2"
puppeteer-extra-plugin-user-preferences "^2.4.0"
puppeteer-extra-plugin "^3.2.3"
puppeteer-extra-plugin-user-preferences "^2.4.1"

puppeteer-extra-plugin-user-data-dir@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/puppeteer-extra-plugin-user-data-dir/-/puppeteer-extra-plugin-user-data-dir-2.4.0.tgz#20e87582482b61e497abd96fec452f63bd2d9123"
integrity sha512-qrhYPTGIqzL2hpeJ5DXjf8xMy5rt1UvcqSgpGTTOUOjIMz1ROWnKHjBoE9fNBJ4+ToRZbP8MzIDXWlEk/e1zJA==
puppeteer-extra-plugin-user-data-dir@^2.4.1:
version "2.4.1"
resolved "https://registry.yarnpkg.com/puppeteer-extra-plugin-user-data-dir/-/puppeteer-extra-plugin-user-data-dir-2.4.1.tgz#4ea9d56d20455672a54fe086309a102a5126411c"
integrity sha512-kH1GnCcqEDoBXO7epAse4TBPJh9tEpVEK/vkedKfjOVOhZAvLkHGc9swMs5ChrJbRnf8Hdpug6TJlEuimXNQ+g==
dependencies:
debug "^4.1.1"
fs-extra "^10.0.0"
puppeteer-extra-plugin "^3.2.2"
puppeteer-extra-plugin "^3.2.3"
rimraf "^3.0.2"

puppeteer-extra-plugin-user-preferences@^2.4.0:
version "2.4.0"
resolved "https://registry.yarnpkg.com/puppeteer-extra-plugin-user-preferences/-/puppeteer-extra-plugin-user-preferences-2.4.0.tgz#8b75bc39c3de9913e236ae1d982a24711d84ba6f"
integrity sha512-4XxMhMkJ+qqLsPY9ULF90qS9Bj1Qrwwgp1TY9zTdp1dJuy7QSgYE7xlyamq3cKrRuzg3QUOqygJo52sVeXSg5A==
puppeteer-extra-plugin-user-preferences@^2.4.1:
version "2.4.1"
resolved "https://registry.yarnpkg.com/puppeteer-extra-plugin-user-preferences/-/puppeteer-extra-plugin-user-preferences-2.4.1.tgz#db8ec63c04a6a10a8f8997e15fdffdf13272161d"
integrity sha512-i1oAZxRbc1bk8MZufKCruCEC3CCafO9RKMkkodZltI4OqibLFXF3tj6HZ4LZ9C5vCXZjYcDWazgtY69mnmrQ9A==
dependencies:
debug "^4.1.1"
deepmerge "^4.2.2"
puppeteer-extra-plugin "^3.2.2"
puppeteer-extra-plugin-user-data-dir "^2.4.0"
puppeteer-extra-plugin "^3.2.3"
puppeteer-extra-plugin-user-data-dir "^2.4.1"

puppeteer-extra-plugin@^3.2.2:
version "3.2.2"
resolved "https://registry.yarnpkg.com/puppeteer-extra-plugin/-/puppeteer-extra-plugin-3.2.2.tgz#3c02c0a10f8eadf32e7debb7ee24105a53afc17b"
integrity sha512-0uatQxzuVn8yegbrEwSk03wvwpMB5jNs7uTTnermylLZzoT+1rmAQaJXwlS3+vADUbw6ELNgNEHC7Skm0RqHbQ==
puppeteer-extra-plugin@^3.2.3:
version "3.2.3"
resolved "https://registry.yarnpkg.com/puppeteer-extra-plugin/-/puppeteer-extra-plugin-3.2.3.tgz#50c9f0749c005bbc7b8b208bcd00a9d46a15b585"
integrity sha512-6RNy0e6pH8vaS3akPIKGg28xcryKscczt4wIl0ePciZENGE2yoaQJNd17UiEbdmh5/6WW6dPcfRWT9lxBwCi2Q==
dependencies:
"@types/debug" "^4.1.0"
debug "^4.1.1"
Expand Down

0 comments on commit 6983b1e

Please sign in to comment.