GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Leetcode-Algorithms/src/leetcode/HouseRobber.java at master · edyluisrey/Leetcode-Algorithms

Expand file tree

/

HouseRobber.java

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
24 lines (19 loc) · 643 Bytes
/

HouseRobber.java

Copy path

File metadata and controls

24 lines (19 loc) · 643 Bytes
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
package leetcode;
public class HouseRobber {
public static void main(String[] args) {
HouseRobber test = new HouseRobber();
System.out.println(test.rob(new int[]{2,15,5,16}));
}
public int rob(int[] nums) {
if(nums == null || nums.length ==0) return 0;
int[] max = new int[nums.length];
if(nums.length==1) return nums[0];
if(nums.length==2) return Math.max(nums[0], nums[1]);
max[0]= nums[0];
max[1]= Math.max(nums[0], nums[1]);
for(int i = 2; i< nums.length; i++){
max[i]= Math.max(max[i-2]+nums[i], max[i-1]);
}
return max[nums.length-1];
}
}
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.