forked from neetcode-gh/leetcode
-
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.
🎨 Format files (🛠️ from Github Actions)
- Loading branch information
Showing
142 changed files
with
2,163 additions
and
1,991 deletions.
There are no files selected for viewing
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,31 +1,30 @@ | ||
class Solution { | ||
|
||
public boolean isMatch(String s, String p) { | ||
boolean[][] cache = new boolean[s.length() + 1][p.length() + 1]; | ||
|
||
boolean[][] cache = new boolean[s.length() + 1][p.length() + 1]; | ||
|
||
return dfs(cache , s, p, 0 , 0); | ||
} | ||
return dfs(cache, s, p, 0, 0); | ||
} | ||
|
||
private boolean dfs(boolean[][] cache, String s, String p , int i, int j) { | ||
|
||
if(cache[i][j] != false) | ||
return cache[i][j]; | ||
private boolean dfs(boolean[][] cache, String s, String p, int i, int j) { | ||
if (cache[i][j] != false) return cache[i][j]; | ||
|
||
if( i >= s.length() && j>= p.length()) | ||
return true; | ||
if (i >= s.length() && j >= p.length()) return true; | ||
|
||
if(j >= p.length()) | ||
return false; | ||
if (j >= p.length()) return false; | ||
|
||
boolean match = i < s.length() && (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.'); | ||
boolean match = | ||
i < s.length() && | ||
(s.charAt(i) == p.charAt(j) || p.charAt(j) == '.'); | ||
|
||
if(j + 1 < p.length() && p.charAt(j+1)== '*' ) { | ||
cache[i][j] = dfs(cache, s, p, i, j+2) || (match && dfs(cache, s, p, i+1, j)); | ||
} else { | ||
cache[i][j] = match && dfs(cache, s, p, i+1, j+1); | ||
} | ||
if (j + 1 < p.length() && p.charAt(j + 1) == '*') { | ||
cache[i][j] = | ||
dfs(cache, s, p, i, j + 2) || | ||
(match && dfs(cache, s, p, i + 1, j)); | ||
} else { | ||
cache[i][j] = match && dfs(cache, s, p, i + 1, j + 1); | ||
} | ||
|
||
return cache[i][j]; | ||
} | ||
} | ||
return cache[i][j]; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,43 +1,40 @@ | ||
// Dynammic Programming - Memoization | ||
// Time Complexity O(s * t) | Space Complexity O(s * t) | ||
class Solution { | ||
|
||
public int numDistinct(String s, String t) { | ||
|
||
int n = s.length() + 1; | ||
int m = t.length() + 1; | ||
int[][] memo = new int[n][m]; | ||
for (int[] row : memo){ | ||
Arrays.fill(row, -1); | ||
|
||
for (int[] row : memo) { | ||
Arrays.fill(row, -1); | ||
} | ||
|
||
return recursion(s, t, 0, 0, memo); | ||
|
||
} | ||
|
||
|
||
public int recursion(String s, String t, int sIdx, int tIdx, int[][] memo){ | ||
|
||
if (memo[sIdx][tIdx] != -1){ | ||
|
||
public int recursion(String s, String t, int sIdx, int tIdx, int[][] memo) { | ||
if (memo[sIdx][tIdx] != -1) { | ||
return memo[sIdx][tIdx]; | ||
} | ||
if (tIdx >= t.length()){ | ||
|
||
if (tIdx >= t.length()) { | ||
return 1; | ||
} | ||
if (sIdx >= s.length()){ | ||
|
||
if (sIdx >= s.length()) { | ||
return 0; | ||
} | ||
|
||
if (t.charAt(tIdx) == s.charAt(sIdx)){ | ||
memo[sIdx][tIdx] = recursion(s, t, sIdx + 1, tIdx + 1, memo) + recursion(s, t, sIdx + 1, tIdx, memo); | ||
return memo[sIdx][tIdx]; | ||
|
||
if (t.charAt(tIdx) == s.charAt(sIdx)) { | ||
memo[sIdx][tIdx] = | ||
recursion(s, t, sIdx + 1, tIdx + 1, memo) + | ||
recursion(s, t, sIdx + 1, tIdx, memo); | ||
return memo[sIdx][tIdx]; | ||
} | ||
|
||
memo[sIdx][tIdx] = recursion(s, t, sIdx + 1, tIdx, memo); | ||
return memo[sIdx][tIdx]; | ||
|
||
} | ||
|
||
} | ||
} |
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,18 +1,18 @@ | ||
class Solution { | ||
|
||
public int maxProfit(int[] prices) { | ||
int left = 0; | ||
int right = 1; | ||
int maxProfit = 0; | ||
while (right < prices.length) { | ||
if (prices[left] < prices[right]) { | ||
maxProfit = Math.max(maxProfit, prices[right] - prices[left]); | ||
right++; | ||
} else { | ||
left = right; | ||
right++; | ||
int left = 0; | ||
int right = 1; | ||
int maxProfit = 0; | ||
while (right < prices.length) { | ||
if (prices[left] < prices[right]) { | ||
maxProfit = Math.max(maxProfit, prices[right] - prices[left]); | ||
right++; | ||
} else { | ||
left = right; | ||
right++; | ||
} | ||
} | ||
} | ||
return maxProfit; | ||
return maxProfit; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,48 +1,57 @@ | ||
package java; | ||
|
||
class Solution { | ||
public int ladderLength(String beginWord, String endWord, List<String> wordList) { | ||
|
||
public int ladderLength( | ||
String beginWord, | ||
String endWord, | ||
List<String> wordList | ||
) { | ||
Map<String, List<String>> adjlist = new HashMap<>(); | ||
wordList.add(beginWord); | ||
for(String word:wordList){ | ||
StringBuilder string=null; | ||
for(int i=0;i<word.length();i++){ | ||
string = new StringBuilder(word); | ||
string.setCharAt(i, '*'); | ||
List<String> wordlist = adjlist.getOrDefault(string.toString(), new ArrayList<String>()); | ||
wordlist.add(word); | ||
adjlist.put(string.toString(), wordlist); | ||
for (String word : wordList) { | ||
StringBuilder string = null; | ||
for (int i = 0; i < word.length(); i++) { | ||
string = new StringBuilder(word); | ||
string.setCharAt(i, '*'); | ||
List<String> wordlist = adjlist.getOrDefault( | ||
string.toString(), | ||
new ArrayList<String>() | ||
); | ||
wordlist.add(word); | ||
adjlist.put(string.toString(), wordlist); | ||
} | ||
} | ||
|
||
Queue<String> queue = new LinkedList<>(); | ||
queue.offer(beginWord); | ||
Set<String> visited = new HashSet<>(); | ||
visited.add(beginWord); | ||
int step = 1; | ||
StringBuilder string=null; | ||
while(!queue.isEmpty()){ | ||
step++; | ||
StringBuilder string = null; | ||
while (!queue.isEmpty()) { | ||
step++; | ||
int size = queue.size(); | ||
for(int j=0;j<size;j++){ | ||
for (int j = 0; j < size; j++) { | ||
String str = queue.poll(); | ||
|
||
for(int i=0;i<str.length();i++){ | ||
for (int i = 0; i < str.length(); i++) { | ||
string = new StringBuilder(str); | ||
string.setCharAt(i, '*'); | ||
for(String pat:adjlist.get(string.toString())){ | ||
if(pat.equals(endWord)){ | ||
for (String pat : adjlist.get(string.toString())) { | ||
if (pat.equals(endWord)) { | ||
return step; | ||
} | ||
if(visited.contains(pat)){ | ||
if (visited.contains(pat)) { | ||
continue; | ||
} | ||
queue.offer(pat); | ||
visited.add(pat); | ||
} | ||
} | ||
} | ||
} | ||
// step++; | ||
} | ||
return 0; | ||
} | ||
} | ||
} |
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,35 +1,36 @@ | ||
class Solution { | ||
|
||
// Time Complexity: O(N^2 log(N)) where N is the length of points. N^2 comes from the fact we need to find the distance between a currNode and every other node to pick the shortest distance. log(N) comes from Priority Queue | ||
// Space Complexity: O(N^2) | ||
public int minCostConnectPoints(int[][] points) { | ||
PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> a[0] - b[0]); // edge weight, the index of next node | ||
pq.offer(new int[]{0,0}); | ||
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); // edge weight, the index of next node | ||
pq.offer(new int[] { 0, 0 }); | ||
int len = points.length; | ||
Set<Integer> visited = new HashSet<>(); | ||
int cost = 0; | ||
// When visited.size() == points.len meaning that all the nodes has been connected. | ||
while(visited.size() < len){ | ||
|
||
// When visited.size() == points.len meaning that all the nodes has been connected. | ||
while (visited.size() < len) { | ||
int[] arr = pq.poll(); | ||
|
||
int weight = arr[0]; | ||
int currNode = arr[1]; | ||
|
||
if(visited.contains(currNode)) | ||
continue; | ||
|
||
|
||
if (visited.contains(currNode)) continue; | ||
|
||
visited.add(currNode); | ||
cost += weight; | ||
|
||
for(int nextNode = 0; nextNode < len; nextNode++){ | ||
if(!visited.contains(nextNode)){ | ||
int nextWeight = Math.abs(points[nextNode][0] - points[currNode][0]) + Math.abs(points[nextNode][1] - points[currNode][1]); | ||
pq.offer(new int[]{nextWeight, nextNode}); | ||
|
||
for (int nextNode = 0; nextNode < len; nextNode++) { | ||
if (!visited.contains(nextNode)) { | ||
int nextWeight = | ||
Math.abs(points[nextNode][0] - points[currNode][0]) + | ||
Math.abs(points[nextNode][1] - points[currNode][1]); | ||
pq.offer(new int[] { nextWeight, nextNode }); | ||
} | ||
} | ||
} | ||
|
||
|
||
|
||
return cost; | ||
} | ||
} |
Oops, something went wrong.