Skip to content

Commit 2615c80

Browse files
authored
Merge pull request neetcode-gh#357 from miladra/main
Add 198, 213, 5, 647, 62, 300
2 parents 65b8987 + 37f8d89 commit 2615c80

7 files changed

+165
-0
lines changed

java/152-Maximum-Product-Subarray.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
class Solution {
22
public int maxProduct(int[] nums) {
3+
if (nums.length == 1) return nums[0];
4+
35
int res = nums[0];
46
int max = 1;
57
int min = 1;

java/198-House-Robber.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
if (nums == null || nums.length == 0) return 0;
4+
if (nums.length == 1) return nums[0];
5+
if (nums.length == 2) return Math.max(nums[0], nums[1]);
6+
7+
int robWithOutIncludingLastHouse = 0, robWithIncludingLastHouse = 0;
8+
9+
for (int n : nums) {
10+
int temp = Math.max(robWithOutIncludingLastHouse + n, robWithIncludingLastHouse);
11+
robWithOutIncludingLastHouse = robWithIncludingLastHouse;
12+
robWithIncludingLastHouse = temp;
13+
}
14+
return robWithIncludingLastHouse;
15+
}
16+
17+
public int robDP(int[] nums) {
18+
if (nums == null || nums.length == 0) return 0;
19+
if (nums.length == 1) return nums[0];
20+
if (nums.length == 2) return Math.max(nums[0], nums[1]);
21+
22+
int[] dp = new int[nums.length];
23+
dp[0] = nums[0];
24+
dp[1] = Math.max(nums[0], nums[1]);
25+
26+
for (int i = 2; i < nums.length; i++) {
27+
dp[i] = Math.max(dp[i - 1], dp[i - 2] + nums[i]);
28+
}
29+
return dp[nums.length - 1];
30+
}
31+
}

java/213-House-RobberII.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int rob(int[] nums) {
3+
4+
5+
if(nums.length == 0 ) return 0;
6+
if(nums.length == 1 ) return nums[0];
7+
8+
return Math.max(robHelper(nums,0,nums.length - 2) , robHelper(nums , 1 , nums.length - 1));
9+
}
10+
public int robHelper(int[] nums , int start , int end){
11+
int rob1 = 0;
12+
int rob2 = 0;
13+
for (int i = start; i <= end; i++) {
14+
int temp = Math.max(rob1 + nums[i], rob2);
15+
rob1 = rob2;
16+
rob2 = temp;
17+
}
18+
return rob2;
19+
}
20+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public int lengthOfLIS(int[] nums) {
3+
//O(n^2)
4+
if(nums.length == 1) return 1;
5+
6+
int[] LIS = new int[nums.length];
7+
Arrays.fill(LIS,1);
8+
int maximumSoFar = 1;
9+
10+
for (int i = nums.length - 1 ; i >= 0; i--) {
11+
for(int j= i + 1 ; j < nums.length ; j++){
12+
if (nums[i] < nums[j]) {
13+
LIS[i] = Math.max(1 + LIS[j], LIS[i]);
14+
}
15+
}
16+
maximumSoFar = Math.max(maximumSoFar, LIS[i]);
17+
}
18+
return maximumSoFar;
19+
}
20+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
class Solution {
2+
public String longestPalindrome(String s) {
3+
4+
int strLength = s.length();
5+
6+
if (strLength < 2) {
7+
return s;
8+
}
9+
10+
int resultLength = 0;
11+
String result = "";
12+
13+
for (int i = 0; i < s.length(); i++) {
14+
//Odd length
15+
int l = i, r = i;
16+
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
17+
if ((r - l + 1) > resultLength) {
18+
result = s.substring(l, r + 1);
19+
resultLength = r - l + 1;
20+
}
21+
l -= 1;
22+
r += 1;
23+
}
24+
25+
// even length
26+
l = i;
27+
r = i + 1;
28+
while (l >= 0 && r < s.length() && s.charAt(l) == s.charAt(r)) {
29+
if ((r - l + 1) > resultLength) {
30+
result = s.substring(l, r + 1);
31+
resultLength = r - l + 1;
32+
}
33+
l -= 1;
34+
r += 1;
35+
}
36+
37+
38+
}
39+
40+
return result;
41+
42+
}
43+
}

java/62-Unique-Paths.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class Solution {
2+
public static int uniquePaths(int m, int n) {
3+
int[][] dp = new int[m][n];
4+
5+
// Fill out last row
6+
for (int j = 0; j < n; j++) {
7+
dp[m-1][j] = 1;
8+
}
9+
10+
// Fill out last column
11+
for (int i = 0; i < m; i++) {
12+
dp[i][n-1] = 1;
13+
}
14+
15+
for (int i = m - 2; i >= 0; i--) {
16+
for (int j = n - 2; j >= 0; j--) {
17+
dp[i][j] = dp[i][j + 1] + dp[i + 1][j];
18+
}
19+
}
20+
return dp[0][0];
21+
}
22+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution {
2+
public int countSubstrings(String s) {
3+
4+
if (s.length() < 2) {
5+
return s.length();
6+
}
7+
int result = 0;
8+
for (int i = 0; i < s.length(); i++) {
9+
// Odd Length
10+
int left = i, right = i;
11+
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
12+
result++;
13+
left--;
14+
right++;
15+
}
16+
// Even Length
17+
left = i;
18+
right = i + 1;
19+
while (left >= 0 && right < s.length() && s.charAt(left) == s.charAt(right)) {
20+
result++;
21+
left--;
22+
right++;
23+
}
24+
}
25+
return result;
26+
}
27+
}

0 commit comments

Comments
 (0)