-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
137 additions
and
18 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
...tCode_DynamicProgramming/HouseRobber.java → src/LeetCode/HouseRobber.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
package LeetCode_DynamicProgramming; | ||
package LeetCode; | ||
|
||
import java.util.Arrays; | ||
|
||
|
2 changes: 1 addition & 1 deletion
2
...icProgramming/HouseRobber_Tabulation.java → src/LeetCode/HouseRobber_Tabulation.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package LeetCode; | ||
|
||
public class JumpGame { | ||
public boolean canJump(int[] nums) { | ||
// at each iteration store max jump | ||
int maxJump = 0; | ||
int i; | ||
for (i = 0; i < nums.length - 1; i++) { | ||
if (nums[i] > maxJump) | ||
maxJump = nums[i]; | ||
if (maxJump <= 0) | ||
break; | ||
maxJump--; | ||
} | ||
return i == nums.length - 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package LeetCode; | ||
|
||
public class LongestPalindrome { | ||
public String longestPalindrome(String s) { | ||
Palindrome longestPalindrome = new Palindrome("", 0); | ||
char[] charArray = s.toCharArray(); | ||
for (int i = 0; i < s.length(); i++) { | ||
Palindrome palindrome = longestPalindromeFromCenter(charArray, i); | ||
if (palindrome.length > longestPalindrome.length) { | ||
longestPalindrome = palindrome; | ||
} | ||
} | ||
|
||
for (int i = 0; i < s.length(); i++) { | ||
Palindrome palindrome = longestPalindromeFromCursor(charArray, i); | ||
if (palindrome.length > longestPalindrome.length) { | ||
longestPalindrome = palindrome; | ||
} | ||
} | ||
|
||
return longestPalindrome.value; | ||
} | ||
|
||
private Palindrome longestPalindromeFromCenter(char[] s, int center) { | ||
int i = 1; | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
stringBuilder.append(s[center]); | ||
while (center - i >= 0 && center + i < s.length && s[center - i] == s[center + i]) { | ||
stringBuilder.append(s[center + i]); | ||
stringBuilder.insert(0, s[center - i]); | ||
i++; | ||
} | ||
return new Palindrome(stringBuilder.toString(), i * 2 - 1); | ||
} | ||
|
||
private Palindrome longestPalindromeFromCursor(char[] s, int cursor) { | ||
// cursor is the left more index of the pair | ||
int i = 0; | ||
StringBuilder stringBuilder = new StringBuilder(); | ||
while (cursor - i >= 0 && cursor + i + 1 < s.length && s[cursor - i] == s[cursor + i + 1]) { | ||
stringBuilder.append(s[cursor + i + 1]); | ||
stringBuilder.insert(0, s[cursor - i]); | ||
i++; | ||
} | ||
return new Palindrome(stringBuilder.toString(), i * 2); | ||
} | ||
|
||
private class Palindrome { | ||
public String value; | ||
public int length; | ||
|
||
public Palindrome(String v, int l) { | ||
value = v; | ||
length = l; | ||
} | ||
} | ||
} |
4 changes: 1 addition & 3 deletions
4
...ode_DynamicProgramming/MaximalSquare.java → src/LeetCode/MaximalSquare.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package LeetCode; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
public class RainWater { | ||
public int trap(int[] height) { | ||
//track maximums one way and prefix sum | ||
//then go the other way | ||
//make sure to track when 2 peaks are equal to max too | ||
//but when going the other way, don't track equal peaks | ||
int n = height.length; | ||
|
||
//create prefix sum | ||
int[] prefixSum = new int[n]; | ||
prefixSum[0] = height[0]; | ||
for (int i = 1; i < n; i++) { | ||
prefixSum[i] = prefixSum[i - 1] + height[i]; | ||
} | ||
System.out.println(Arrays.toString(prefixSum)); | ||
|
||
|
||
//go left to right | ||
List<Integer> maxIndexes = new ArrayList<>(); | ||
int max = 0; | ||
for (int i = 0; i < n; i++) { | ||
if (height[i] > 0 && height[i] >= max) { | ||
max = height[i]; | ||
maxIndexes.add(i); | ||
} | ||
} | ||
|
||
System.out.println(maxIndexes); | ||
|
||
//calculate answer | ||
int water = 0; | ||
for (int i = 0; i < maxIndexes.size() - 1; i++) { | ||
int leftIndex = maxIndexes.get(i); | ||
int rightIndex = maxIndexes.get(i + 1); | ||
//we know leftHeight is <= to rightHeight, so no need for rightHeight | ||
int leftHeight = height[leftIndex]; | ||
int blocksInBetween = prefixSum[rightIndex - 1] - prefixSum[leftIndex]; | ||
int areaInBetween = (rightIndex - leftIndex - 1) * leftHeight; | ||
int waterInBetween = areaInBetween - blocksInBetween; | ||
water += waterInBetween; | ||
} | ||
|
||
return water; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package LeetCode; | ||
|
||
public class Runner { | ||
public static void main(String[] args) { | ||
RainWater test = new RainWater(); | ||
System.out.println(test.trap(new int[] {0,1,0,2,1,0,1,3,2,1})); | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
...tCode_DynamicProgramming/UniquePaths.java → src/LeetCode/UniquePaths.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.