Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork5.7k
feat: add houseRobber implementation#1282
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @function houseRobber | ||
* @description Given an integer array nums representing the amount of money of each house, return the maximum amount of money you can rob tonight without alerting the police. | ||
* @param {number[]} nums | ||
* @return {number} | ||
* @see [Leetcode link](https://leetcode.com/problems/house-robber/) | ||
*/ | ||
export const houseRobber = (nums) => { | ||
const length = nums.length | ||
if (length === 0) return 0 | ||
if (length === 1) return nums[0] | ||
if (length === 2) return Math.max(nums[0], nums[1]) | ||
const dp = Array(length) // last element of this array always contains biggest loot possible | ||
dp[0] = nums[0] | ||
dp[1] = Math.max(nums[0], nums[1]) | ||
for (let i = 2; i < length; i++) { | ||
dp[i] = Math.max(nums[i] + dp[i - 2], dp[i - 1]) | ||
} | ||
return dp[length - 1] | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { houseRobber } from '../HouseRobber' | ||
describe('houseRobber', () => { | ||
it('expects to return 0 when argument is empty array', () => { | ||
expect(houseRobber([])).toBe(0) | ||
}) | ||
it('expects to return element at index 0 when argument is array of length one', () => { | ||
expect(houseRobber([9])).toBe(9) | ||
}) | ||
it('expects to return greater number when argument is an array of length two', () => { | ||
expect(houseRobber([3, 6])).toBe(6) | ||
}) | ||
it('expects to return the maximum loot possible', () => { | ||
expect(houseRobber([1, 2, 3, 1])).toBe(4) | ||
expect(houseRobber([2, 7, 9, 3, 1])).toBe(12) | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.