Skip to content

Commit 458bca4

Browse files
committed
new soln
1 parent 59e5eef commit 458bca4

File tree

3 files changed

+113
-0
lines changed

3 files changed

+113
-0
lines changed

198.Rob.cs

+20
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,24 @@ public int Rob(int[] nums) {
4949
}
5050
return Math.Max(nums[0],nums[1]);
5151
}
52+
}
53+
54+
public class Solution {
55+
public int Rob(int[] nums) {
56+
int[] dp = new int[nums.Length];
57+
Array.Fill(dp,-1);
58+
if(nums.Length == 1)
59+
return nums[0];
60+
if(nums.Length == 2)
61+
return Math.Max(nums[1],nums[0]);
62+
if(nums.Length == 3)
63+
return Math.Max(nums[1],nums[0]+nums[2]);
64+
dp[0] = nums[0];
65+
dp[1] = nums[1];
66+
dp[2] = nums[0]+nums[2];
67+
for(int i = 3; i<nums.Length;i++){
68+
dp[i] = nums[i] + Math.Max(dp[i-2],dp[i-3]);
69+
}
70+
return Math.Max(dp[nums.Length-1],dp[nums.Length-2]);
71+
}
5272
}

213.Rob.cs

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
// 213. House Robber II
2+
// You are a professional robber planning to rob houses along a street. Each house has a certain amount of money stashed. All houses at this place are arranged in a circle. That means the first house is the neighbor of the last one. Meanwhile, adjacent houses have a security system connected, and it will automatically contact the police if two adjacent houses were broken into on the same night.
3+
// 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.
4+
// Example 1:
5+
// Input: nums = [2,3,2]
6+
// Output: 3
7+
// Explanation: You cannot rob house 1 (money = 2) and then rob house 3 (money = 2), because they are adjacent houses.
8+
// Example 2:
9+
// Input: nums = [1,2,3,1]
10+
// Output: 4
11+
// Explanation: Rob house 1 (money = 1) and then rob house 3 (money = 3).
12+
// Total amount you can rob = 1 + 3 = 4.
13+
// Example 3:
14+
// Input: nums = [1,2,3]
15+
// Output: 3
16+
// Constraints:
17+
// 1 <= nums.length <= 100
18+
// 0 <= nums[i] <= 1000
19+
20+
21+
//Approch: same as 198, but here first and last cant be include at once.
22+
public class Solution {
23+
public int Rob(int[] nums) {
24+
int[] dp = new int[nums.Length];
25+
if(nums.Length == 1)
26+
return nums[0];
27+
if(nums.Length == 2)
28+
return Math.Max(nums[1],nums[0]);
29+
if(nums.Length == 3)
30+
return Math.Max(nums[1],Math.Max(nums[0],nums[2]));
31+
32+
//Assume array without last element
33+
dp[0] = nums[0];
34+
dp[1] = nums[1];
35+
dp[2] = nums[0]+nums[2];
36+
for(int i = 3; i<nums.Length-1;i++){
37+
dp[i] = nums[i] + Math.Max(dp[i-2],dp[i-3]);
38+
}
39+
int profi1 = Math.Max(dp[nums.Length-2],dp[nums.Length-3]);
40+
Array.Fill(dp,0);
41+
42+
//Assume array without first element
43+
dp[0] = 0;
44+
dp[1] = nums[1];
45+
dp[2] = nums[2];
46+
for(int i = 3; i<nums.Length;i++){
47+
dp[i] = nums[i] + Math.Max(dp[i-2],dp[i-3]);
48+
}
49+
int profi2 = Math.Max(dp[nums.Length-1],dp[nums.Length-2]);
50+
51+
return Math.Max(profi1,profi2);
52+
}
53+
}

452.FindMinArrowShots.cs

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// 452. Minimum Number of Arrows to Burst Balloons
2+
// There are some spherical balloons taped onto a flat wall that represents the XY-plane. The balloons are represented as a 2D integer array points where points[i] = [xstart, xend] denotes a balloon whose horizontal diameter stretches between xstart and xend. You do not know the exact y-coordinates of the balloons.
3+
// Arrows can be shot up directly vertically (in the positive y-direction) from different points along the x-axis. A balloon with xstart and xend is burst by an arrow shot at x if xstart <= x <= xend. There is no limit to the number of arrows that can be shot. A shot arrow keeps traveling up infinitely, bursting any balloons in its path.
4+
// Given the array points, return the minimum number of arrows that must be shot to burst all balloons.
5+
// Example 1:
6+
// Input: points = [[10,16],[2,8],[1,6],[7,12]]
7+
// Output: 2
8+
// Explanation: The balloons can be burst by 2 arrows:
9+
// - Shoot an arrow at x = 6, bursting the balloons [2,8] and [1,6].
10+
// - Shoot an arrow at x = 11, bursting the balloons [10,16] and [7,12].
11+
// Example 2:
12+
// Input: points = [[1,2],[3,4],[5,6],[7,8]]
13+
// Output: 4
14+
// Explanation: One arrow needs to be shot for each balloon for a total of 4 arrows.
15+
// Example 3:
16+
// Input: points = [[1,2],[2,3],[3,4],[4,5]]
17+
// Output: 2
18+
// Explanation: The balloons can be burst by 2 arrows:
19+
// - Shoot an arrow at x = 2, bursting the balloons [1,2] and [2,3].
20+
// - Shoot an arrow at x = 4, bursting the balloons [3,4] and [4,5].
21+
// Constraints:
22+
// 1 <= points.length <= 105
23+
// points[i].length == 2
24+
// -231 <= xstart < xend <= 231 - 1
25+
26+
27+
public class Solution {
28+
public int FindMinArrowShots(int[][] points) {
29+
points = points.OrderBy(x => x[1]).ThenBy(x => x[0]).ToArray();
30+
int count = 0;
31+
for(int i = 0; i < points.Length; i++){
32+
count++;
33+
int j = i + 1;
34+
while(j < points.Length && points[i][1] >= points[j][0])
35+
j++;
36+
i = j-1;
37+
}
38+
return count;
39+
}
40+
}

0 commit comments

Comments
 (0)