Skip to content

Commit fd08e46

Browse files
committed
🎨 Format files (🛠️ from Github Actions)
1 parent 5c2b18e commit fd08e46

File tree

142 files changed

+2163
-1991
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

142 files changed

+2163
-1991
lines changed
+20-21
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,30 @@
11
class Solution {
22

33
public boolean isMatch(String s, String p) {
4+
boolean[][] cache = new boolean[s.length() + 1][p.length() + 1];
45

5-
boolean[][] cache = new boolean[s.length() + 1][p.length() + 1];
6-
7-
return dfs(cache , s, p, 0 , 0);
8-
}
6+
return dfs(cache, s, p, 0, 0);
7+
}
98

10-
private boolean dfs(boolean[][] cache, String s, String p , int i, int j) {
11-
12-
if(cache[i][j] != false)
13-
return cache[i][j];
9+
private boolean dfs(boolean[][] cache, String s, String p, int i, int j) {
10+
if (cache[i][j] != false) return cache[i][j];
1411

15-
if( i >= s.length() && j>= p.length())
16-
return true;
12+
if (i >= s.length() && j >= p.length()) return true;
1713

18-
if(j >= p.length())
19-
return false;
14+
if (j >= p.length()) return false;
2015

21-
boolean match = i < s.length() && (s.charAt(i) == p.charAt(j) || p.charAt(j) == '.');
16+
boolean match =
17+
i < s.length() &&
18+
(s.charAt(i) == p.charAt(j) || p.charAt(j) == '.');
2219

23-
if(j + 1 < p.length() && p.charAt(j+1)== '*' ) {
24-
cache[i][j] = dfs(cache, s, p, i, j+2) || (match && dfs(cache, s, p, i+1, j));
25-
} else {
26-
cache[i][j] = match && dfs(cache, s, p, i+1, j+1);
27-
}
20+
if (j + 1 < p.length() && p.charAt(j + 1) == '*') {
21+
cache[i][j] =
22+
dfs(cache, s, p, i, j + 2) ||
23+
(match && dfs(cache, s, p, i + 1, j));
24+
} else {
25+
cache[i][j] = match && dfs(cache, s, p, i + 1, j + 1);
26+
}
2827

29-
return cache[i][j];
30-
}
31-
}
28+
return cache[i][j];
29+
}
30+
}

java/1143-Longest-Common-Subsequence.java

+1-4
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ public int LCS(String s1, String s2, int i, int j, int[][] dp) {
2020
}
2121
}
2222

23-
2423
// Iterative version
2524
class Solution {
25+
2626
public int longestCommonSubsequence(String text1, String text2) {
2727
//O(n*m)/O(n*m) time/memory
2828
if (text1.isEmpty() || text2.isEmpty()) {
@@ -43,15 +43,12 @@ public int longestCommonSubsequence(String text1, String text2) {
4343
for (int j = 1; j <= text2.length(); j++) {
4444
if (text1.charAt(i - 1) == text2.charAt(j - 1)) {
4545
dp[i][j] = 1 + dp[i - 1][j - 1];
46-
4746
} else {
4847
dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
4948
}
50-
5149
}
5250
}
5351

5452
return dp[text1.length()][text2.length()];
5553
}
5654
}
57-

java/115-Distinct-Subsequences.java

+20-23
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,40 @@
11
// Dynammic Programming - Memoization
22
// Time Complexity O(s * t) | Space Complexity O(s * t)
33
class Solution {
4+
45
public int numDistinct(String s, String t) {
5-
66
int n = s.length() + 1;
77
int m = t.length() + 1;
88
int[][] memo = new int[n][m];
9-
10-
for (int[] row : memo){
11-
Arrays.fill(row, -1);
9+
10+
for (int[] row : memo) {
11+
Arrays.fill(row, -1);
1212
}
13-
13+
1414
return recursion(s, t, 0, 0, memo);
15-
1615
}
17-
18-
19-
public int recursion(String s, String t, int sIdx, int tIdx, int[][] memo){
20-
21-
if (memo[sIdx][tIdx] != -1){
16+
17+
public int recursion(String s, String t, int sIdx, int tIdx, int[][] memo) {
18+
if (memo[sIdx][tIdx] != -1) {
2219
return memo[sIdx][tIdx];
2320
}
24-
25-
if (tIdx >= t.length()){
21+
22+
if (tIdx >= t.length()) {
2623
return 1;
2724
}
28-
29-
if (sIdx >= s.length()){
25+
26+
if (sIdx >= s.length()) {
3027
return 0;
3128
}
32-
33-
if (t.charAt(tIdx) == s.charAt(sIdx)){
34-
memo[sIdx][tIdx] = recursion(s, t, sIdx + 1, tIdx + 1, memo) + recursion(s, t, sIdx + 1, tIdx, memo);
35-
return memo[sIdx][tIdx];
29+
30+
if (t.charAt(tIdx) == s.charAt(sIdx)) {
31+
memo[sIdx][tIdx] =
32+
recursion(s, t, sIdx + 1, tIdx + 1, memo) +
33+
recursion(s, t, sIdx + 1, tIdx, memo);
34+
return memo[sIdx][tIdx];
3635
}
37-
36+
3837
memo[sIdx][tIdx] = recursion(s, t, sIdx + 1, tIdx, memo);
3938
return memo[sIdx][tIdx];
40-
4139
}
42-
43-
}
40+
}
+12-12
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
class Solution {
22

33
public int maxProfit(int[] prices) {
4-
int left = 0;
5-
int right = 1;
6-
int maxProfit = 0;
7-
while (right < prices.length) {
8-
if (prices[left] < prices[right]) {
9-
maxProfit = Math.max(maxProfit, prices[right] - prices[left]);
10-
right++;
11-
} else {
12-
left = right;
13-
right++;
4+
int left = 0;
5+
int right = 1;
6+
int maxProfit = 0;
7+
while (right < prices.length) {
8+
if (prices[left] < prices[right]) {
9+
maxProfit = Math.max(maxProfit, prices[right] - prices[left]);
10+
right++;
11+
} else {
12+
left = right;
13+
right++;
14+
}
1415
}
15-
}
16-
return maxProfit;
16+
return maxProfit;
1717
}
1818
}

java/124-Binary-Tree-Maximum-Path-Sum.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,20 @@
1414
* }
1515
*/
1616
class Solution {
17+
1718
public int maxPathSum(TreeNode root) {
1819
int[] res = { Integer.MIN_VALUE };
1920
maxPathSum(root, res);
2021
return res[0];
2122
}
2223

2324
public int maxPathSum(TreeNode root, int[] res) {
24-
if (root == null)
25-
return 0;
25+
if (root == null) return 0;
2626

2727
int left = Math.max(0, maxPathSum(root.left, res));
2828
int right = Math.max(0, maxPathSum(root.right, res));
2929
res[0] = Math.max(res[0], root.val + left + right);
3030

3131
return root.val + Math.max(left, right);
3232
}
33-
}
33+
}

java/127-Word-Ladder.java

+29-20
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,57 @@
11
package java;
2+
23
class Solution {
3-
public int ladderLength(String beginWord, String endWord, List<String> wordList) {
4+
5+
public int ladderLength(
6+
String beginWord,
7+
String endWord,
8+
List<String> wordList
9+
) {
410
Map<String, List<String>> adjlist = new HashMap<>();
511
wordList.add(beginWord);
6-
for(String word:wordList){
7-
StringBuilder string=null;
8-
for(int i=0;i<word.length();i++){
9-
string = new StringBuilder(word);
10-
string.setCharAt(i, '*');
11-
List<String> wordlist = adjlist.getOrDefault(string.toString(), new ArrayList<String>());
12-
wordlist.add(word);
13-
adjlist.put(string.toString(), wordlist);
12+
for (String word : wordList) {
13+
StringBuilder string = null;
14+
for (int i = 0; i < word.length(); i++) {
15+
string = new StringBuilder(word);
16+
string.setCharAt(i, '*');
17+
List<String> wordlist = adjlist.getOrDefault(
18+
string.toString(),
19+
new ArrayList<String>()
20+
);
21+
wordlist.add(word);
22+
adjlist.put(string.toString(), wordlist);
1423
}
1524
}
16-
25+
1726
Queue<String> queue = new LinkedList<>();
1827
queue.offer(beginWord);
1928
Set<String> visited = new HashSet<>();
2029
visited.add(beginWord);
2130
int step = 1;
22-
StringBuilder string=null;
23-
while(!queue.isEmpty()){
24-
step++;
31+
StringBuilder string = null;
32+
while (!queue.isEmpty()) {
33+
step++;
2534
int size = queue.size();
26-
for(int j=0;j<size;j++){
35+
for (int j = 0; j < size; j++) {
2736
String str = queue.poll();
2837

29-
for(int i=0;i<str.length();i++){
38+
for (int i = 0; i < str.length(); i++) {
3039
string = new StringBuilder(str);
3140
string.setCharAt(i, '*');
32-
for(String pat:adjlist.get(string.toString())){
33-
if(pat.equals(endWord)){
41+
for (String pat : adjlist.get(string.toString())) {
42+
if (pat.equals(endWord)) {
3443
return step;
3544
}
36-
if(visited.contains(pat)){
45+
if (visited.contains(pat)) {
3746
continue;
3847
}
3948
queue.offer(pat);
4049
visited.add(pat);
4150
}
42-
}
51+
}
4352
}
4453
// step++;
4554
}
4655
return 0;
4756
}
48-
}
57+
}
+18-17
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
class Solution {
2+
23
// 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
34
// Space Complexity: O(N^2)
45
public int minCostConnectPoints(int[][] points) {
5-
PriorityQueue<int[]> pq = new PriorityQueue<>((a,b) -> a[0] - b[0]); // edge weight, the index of next node
6-
pq.offer(new int[]{0,0});
6+
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> a[0] - b[0]); // edge weight, the index of next node
7+
pq.offer(new int[] { 0, 0 });
78
int len = points.length;
89
Set<Integer> visited = new HashSet<>();
910
int cost = 0;
10-
11-
// When visited.size() == points.len meaning that all the nodes has been connected.
12-
while(visited.size() < len){
11+
12+
// When visited.size() == points.len meaning that all the nodes has been connected.
13+
while (visited.size() < len) {
1314
int[] arr = pq.poll();
14-
15+
1516
int weight = arr[0];
1617
int currNode = arr[1];
17-
18-
if(visited.contains(currNode))
19-
continue;
20-
18+
19+
if (visited.contains(currNode)) continue;
20+
2121
visited.add(currNode);
2222
cost += weight;
23-
24-
for(int nextNode = 0; nextNode < len; nextNode++){
25-
if(!visited.contains(nextNode)){
26-
int nextWeight = Math.abs(points[nextNode][0] - points[currNode][0]) + Math.abs(points[nextNode][1] - points[currNode][1]);
27-
pq.offer(new int[]{nextWeight, nextNode});
23+
24+
for (int nextNode = 0; nextNode < len; nextNode++) {
25+
if (!visited.contains(nextNode)) {
26+
int nextWeight =
27+
Math.abs(points[nextNode][0] - points[currNode][0]) +
28+
Math.abs(points[nextNode][1] - points[currNode][1]);
29+
pq.offer(new int[] { nextWeight, nextNode });
2830
}
2931
}
3032
}
31-
32-
33+
3334
return cost;
3435
}
3536
}

0 commit comments

Comments
 (0)