Skip to content

Commit

Permalink
Interview 150 questions
Browse files Browse the repository at this point in the history
  • Loading branch information
skiwee45 committed Jan 17, 2024
1 parent 312b135 commit 0f329f7
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package LeetCode_DynamicProgramming;
package LeetCode;

import java.util.Arrays;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package LeetCode_DynamicProgramming;
package LeetCode;

public class HouseRobber_Tabulation {
public int rob(int[] nums) {
Expand Down
17 changes: 17 additions & 0 deletions src/LeetCode/JumpGame.java
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;
}
}
57 changes: 57 additions & 0 deletions src/LeetCode/LongestPalindrome.java
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;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
package LeetCode_DynamicProgramming;

import java.util.Arrays;
package LeetCode;

public class MaximalSquare {
public int maximalSquare(char[][] orig) {
Expand Down
51 changes: 51 additions & 0 deletions src/LeetCode/RainWater.java
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;
}
}
8 changes: 8 additions & 0 deletions src/LeetCode/Runner.java
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}));
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package LeetCode_DynamicProgramming;
package LeetCode;

public class UniquePaths {
public int uniquePaths(int m, int n) {
Expand Down
12 changes: 0 additions & 12 deletions src/LeetCode_DynamicProgramming/Runner.java

This file was deleted.

0 comments on commit 0f329f7

Please sign in to comment.