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 42ffea3

Browse files
✨ Day 14; Week 2 ✔️
1 parent 6a6db1c commit 42ffea3

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

‎README.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@
1414
6. [Insert into a Binary Search Tree](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3485/) ➡️ [CPP Solution](Week1/insertIntoBST.cpp)
1515
7. [Rotate List](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/559/week-1-october-1st-october-7th/3486/) ➡️ [CPP Solution](Week1/rotateRight.cpp)
1616

17-
## Week 2 🚧
17+
## Week 2
1818

1919
1. [Binary Search](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/560/week-2-october-8th-october-14th/3488/) ➡️ [CPP Solution](Week2/search.cpp)
2020
2. [Serialize and Deserialize BST](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/560/week-2-october-8th-october-14th/3489/) ➡️ [CPP Solution](Week2/Codec.cpp)
2121
3. [Minimum Number of Arrows to Burst Balloons](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/560/week-2-october-8th-october-14th/3490/) ➡️ [CPP Solution](Week2/findMinArrowShots.cpp)
2222
4. [Remove Duplicate Letters](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/560/week-2-october-8th-october-14th/3491/) ➡️ [CPP Solution](Week2/removeDuplicateLetters.cpp)
2323
5. [Buddy Strings](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/560/week-2-october-8th-october-14th/3492/) ➡️ [CPP Solution](Week2/buddyStrings.cpp)
2424
6. [Sort List](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/560/week-2-october-8th-october-14th/3493/) ➡️ [CPP Solution](Week2/sortList.cpp)
25+
7. [House Robber II](https://leetcode.com/explore/challenge/card/october-leetcoding-challenge/560/week-2-october-8th-october-14th/3494/) ➡️ [CPP Solution](Week2/rob.cpp)
2526

2627
## Week 3 🚧
2728

‎Week2/rob.cpp‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
class Solution {
2+
private:
3+
int rob(vector<int>& nums, int start, int end) {
4+
int prevTwo = 0, prevOne = 0, maxVal = 0;
5+
6+
for(int i = start; i < end; ++i) {
7+
maxVal = max(prevTwo + nums[i], prevOne);
8+
prevTwo = prevOne;
9+
prevOne = maxVal;
10+
}
11+
12+
return prevOne;
13+
}
14+
public:
15+
int rob(vector<int>& nums) {
16+
int n = nums.size();
17+
if(n == 1) return nums[0];
18+
19+
int max1 = rob(nums, 0, n - 1);
20+
int max2 = rob(nums, 1, n);
21+
22+
return max(max1, max2);
23+
}
24+
};

0 commit comments

Comments
(0)

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