Skip to content

Commit 7a19142

Browse files
committed
leetcode
1 parent 5d384f8 commit 7a19142

File tree

4 files changed

+185
-0
lines changed

4 files changed

+185
-0
lines changed

Diff for: HouseRobber/house_robber.dart

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
3+
-* 198. House Robber *-
4+
5+
6+
7+
You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed, the only constraint stopping you from robbing each of them is that adjacent houses have security systems connected and it will automatically contact the police if two adjacent houses were broken into on the same night.
8+
9+
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.
10+
11+
12+
13+
Example 1:
14+
15+
Input: nums = [1,2,3,1]
16+
Output: 4
17+
Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
18+
Total amount you can rob = 1 + 3 = 4.
19+
Example 2:
20+
21+
Input: nums = [2,7,9,3,1]
22+
Output: 12
23+
Explanation: Rob house 1 (money = 2), rob house 3 (money = 9) and rob house 5 (money = 1).
24+
Total amount you can rob = 2 + 9 + 1 = 12.
25+
26+
27+
Constraints:
28+
29+
1 <= nums.length <= 100
30+
0 <= nums[i] <= 400
31+
32+
*/
33+
34+
import 'dart:collection';
35+
import 'dart:math';
36+
37+
class A {
38+
int rob(List<int> nums) {
39+
int previous = 0;
40+
int last = 0;
41+
for (int current in nums) {
42+
last = max(previous + current, previous = last);
43+
}
44+
return last;
45+
}
46+
}
47+
48+
class B {
49+
int rob(List<int> nums) {
50+
//max money can get if rob current house
51+
int rob = 0;
52+
//max money can get if not rob current house
53+
int notRob = 0;
54+
for (int i = 0; i < nums.length; i++) {
55+
//if rob current value, previous house must not be robbed
56+
int curRob = notRob + nums[i];
57+
//if not rob ith house, take the max value of robbed (i-1)th house and not rob (i-1)th house
58+
notRob = max(notRob, rob);
59+
rob = curRob;
60+
}
61+
return max(rob, notRob);
62+
}
63+
}
64+
65+
class C {
66+
HashMap<int, int> map = HashMap();
67+
int rob(List<int> nums) {
68+
if (nums.length == 0) return 0;
69+
map[nums.length] = 0;
70+
return tryRob(nums, 0);
71+
}
72+
73+
int tryRob(List<int> nums, int idx) {
74+
if (map.containsKey(idx)) return map[idx] ?? 0;
75+
76+
late int maxi;
77+
if (nums.length - idx == 1) {
78+
maxi = nums[nums.length - 1];
79+
map[idx] = maxi;
80+
} else if (nums.length - idx == 2) {
81+
maxi = max(nums[nums.length - 1], nums[nums.length - 2]);
82+
map[idx] = maxi;
83+
} else {
84+
maxi = max(nums[idx] + tryRob(nums, idx + 2),
85+
nums[idx + 1] + tryRob(nums, idx + 3));
86+
map[idx] = maxi;
87+
}
88+
return maxi;
89+
}
90+
}

Diff for: HouseRobber/house_robber.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
func rob(nums []int) int {
4+
//max money can get if rob current house
5+
var previous int = 0
6+
//max money can get if not rob current house
7+
var last int = 0
8+
//if rob current value, previous house must not be robbed
9+
for i := 0; i < len(nums); i++ {
10+
var current int = last + nums[i]
11+
//if not rob ith house, take the max value of robbed (i-1)th house and not rob (i-1)th house
12+
last = max(last, previous)
13+
previous = current
14+
}
15+
return max(previous, last)
16+
}
17+
18+
func max(a int, b int) int {
19+
if a > b {
20+
return a
21+
} else {
22+
return b
23+
}
24+
}

Diff for: HouseRobber/house_robber.md

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 🔥 House Robber 🔥 || 3 Approaches || Simple Fast and Easy || with Explanation
2+
3+
## Solution - 1
4+
5+
```dart
6+
class Solution {
7+
int rob(List<int> nums) {
8+
//max money can get if rob current house
9+
int rob = 0;
10+
//max money can get if not rob current house
11+
int notRob = 0;
12+
for (int i = 0; i < nums.length; i++) {
13+
//if rob current value, previous house must not be robbed
14+
int curRob = notRob + nums[i];
15+
//if not rob ith house, take the max value of robbed (i-1)th house and not rob (i-1)th house
16+
notRob = max(notRob, rob);
17+
rob = curRob;
18+
}
19+
return max(rob, notRob);
20+
}
21+
}
22+
```
23+
24+
## Solution - 2
25+
26+
```dart
27+
class Solution {
28+
int rob(List<int> nums) {
29+
int previous = 0;
30+
int last = 0;
31+
for (int current in nums) {
32+
last = max(previous + current, previous = last);
33+
}
34+
return last;
35+
}
36+
}
37+
```
38+
39+
## Solution - 3
40+
41+
```dart
42+
import 'dart:collection';
43+
44+
class Solution {
45+
HashMap<int, int> map = HashMap();
46+
int rob(List<int> nums) {
47+
if (nums.length == 0) return 0;
48+
map[nums.length] = 0;
49+
return tryRob(nums, 0);
50+
}
51+
52+
int tryRob(List<int> nums, int idx) {
53+
if (map.containsKey(idx)) return map[idx] ?? 0;
54+
55+
late int maxi;
56+
if (nums.length - idx == 1) {
57+
maxi = nums[nums.length - 1];
58+
map[idx] = maxi;
59+
} else if (nums.length - idx == 2) {
60+
maxi = max(nums[nums.length - 1], nums[nums.length - 2]);
61+
map[idx] = maxi;
62+
} else {
63+
maxi = max(nums[idx] + tryRob(nums, idx + 2),
64+
nums[idx + 1] + tryRob(nums, idx + 3));
65+
map[idx] = maxi;
66+
}
67+
return maxi;
68+
}
69+
}
70+
```

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
166166
- [**1339.** Maximum Product of Splitted Binary Tree](MaximumProductOfSplittedBinaryTree/maximum_product_of_splitted_binary_tree.dart)
167167
- [**124.** Binary Tree Maximum Path Sum](BinaryTreeMaximumPathSum/binary_tree_maximum_path_sum.dart)
168168
- [**931.** Minimum Falling Path Sum](MinimumFallingPathSum/minimum_falling_path_sum.dart)
169+
- [**198.** House Robber](HouseRobber/house_robber.dart)
169170

170171
## Reach me via
171172

0 commit comments

Comments
 (0)