Skip to content

Commit eca146b

Browse files
committed
Update
1 parent cd3a03d commit eca146b

File tree

6 files changed

+205
-29
lines changed

6 files changed

+205
-29
lines changed

README.md

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,37 +2,38 @@
22

33
![DSA Learning](https://img.shields.io/badge/DSA-Learning-blue?style=for-the-badge&logo=leetcode)
44

5-
This repository contains solutions for a couple hundred DSA problems from the [LeetCode](https://leetcode.com/)
5+
This repository contains solutions for 511 DSA problems from the [LeetCode](https://leetcode.com/)
66
website.
77

88
Problems are separated into 19 topics:
99

10-
| | Topic | Package |
11-
|---:|:--------------------|:-----------------------------------------------|
12-
| 1 | Array | [array](src/array) |
13-
| 2 | Binary | [binary](src/binary) |
14-
| 3 | Binary Search | [binary_search](src/binary_search) |
15-
| 4 | Binary Search Tree | [binary_search_tree](src/binary_search_tree) |
16-
| 5 | Binary Tree | [binary_tree](src/binary_tree) |
17-
| 6 | Design | [design](src/design) |
18-
| 7 | Dynamic Programming | [dynamic_programming](src/dynamic_programming) |
19-
| 8 | Graph | [graph](src/graph) |
20-
| 9 | Greedy Algorithms | [greedy](src/greedy) |
21-
| 10 | Heap | [heap](src/heap) |
22-
| 11 | Interval | [interval](src/interval) |
23-
| 12 | Linked List | [linked_list](src/linked_list) |
24-
| 13 | Math | [math](src/math) |
25-
| 14 | Matrix | [matrix](src/matrix) |
26-
| 15 | Quad Tree | [quad_tree](src/quad_tree) |
27-
| 16 | Recursion | [recursion](src/recursion) |
28-
| 17 | Stack | [stack](src/stack) |
29-
| 18 | String | [string](src/string) |
30-
| 19 | Trie | [trie](src/trie) |
10+
| | Topic | Package | Problems |
11+
|---:|:--------------------|:-----------------------------------------------|---------:|
12+
| 1 | Array | [array](src/array) | 69 |
13+
| 2 | Binary | [binary](src/binary) | 12 |
14+
| 3 | Binary Search | [binary_search](src/binary_search) | 18 |
15+
| 4 | Binary Search Tree | [binary_search_tree](src/binary_search_tree) | 16 |
16+
| 5 | Binary Tree | [binary_tree](src/binary_tree) | 45 |
17+
| 6 | Design | [design](src/design) | 17 |
18+
| 7 | Dynamic Programming | [dynamic_programming](src/dynamic_programming) | 45 |
19+
| 8 | Graph | [graph](src/graph) | 63 |
20+
| 9 | Greedy Algorithms | [greedy](src/greedy) | 20 |
21+
| 10 | Heap | [heap](src/heap) | 18 |
22+
| 11 | Interval | [interval](src/interval) | 11 |
23+
| 12 | Linked List | [linked_list](src/linked_list) | 31 |
24+
| 13 | Math | [math](src/math) | 19 |
25+
| 14 | Matrix | [matrix](src/matrix) | 18 |
26+
| 15 | Quad Tree | [quad_tree](src/quad_tree) | 1 |
27+
| 16 | Recursion | [recursion](src/recursion) | 24 |
28+
| 17 | Stack | [stack](src/stack) | 25 |
29+
| 18 | String | [string](src/string) | 54 |
30+
| 19 | Trie | [trie](src/trie) | 4 |
3131

3232
## Good places to start your own LeetCode journey
3333

3434
- [NeetCode](https://neetcode.io/) – organized roadmap, code solutions in 14 languages and video explanations
35-
- [Grind 75](https://www.techinterviewhandbook.org/grind75) – customizable study plan, algorithms cheetsheet and interview preparation guide
35+
- [Grind 75](https://www.techinterviewhandbook.org/grind75) – customizable study plan, algorithms cheatsheet and
36+
interview preparation guide
3637

3738
## My LeetCode profile
3839

src/dynamic_programming/MaximumSubarray.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,7 @@ public int maxSubArrayViaKadaneAlgo(int[] nums) {
1212
int max = Integer.MIN_VALUE;
1313
int sum = 0;
1414
for (int num : nums) {
15-
sum += num;
16-
if (sum < num) {
17-
sum = num;
18-
}
19-
15+
sum = Math.max(sum + num, num);
2016
max = Math.max(sum, max);
2117
}
2218

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package dynamic_programming;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/maximum-sum-circular-subarray
5+
* Difficulty: Medium
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class MaximumSumCircularSubarray {
10+
11+
public int maxSubarraySumCircularViaModifiedKadaneAlgo(int[] nums) {
12+
// we keep track of both minimum sum subarray and maximum size subarray
13+
int minSum = 0;
14+
int maxSum = 0;
15+
int totalSum = 0;
16+
17+
int min = Integer.MAX_VALUE;
18+
int max = Integer.MIN_VALUE;
19+
20+
for (int num : nums) {
21+
minSum = Math.min(minSum + num, num);
22+
maxSum = Math.max(maxSum + num, num);
23+
totalSum += num;
24+
25+
min = Math.min(min, minSum);
26+
max = Math.max(max, maxSum);
27+
}
28+
29+
// maximum sum circular subarray can either be in the middle of a regular array -> [min] [max] [min] -> max
30+
// or on both ends of it -> [max] [min] [max] -> totalSum - min
31+
return max > 0 ? Math.max(max, totalSum - min) : max;
32+
}
33+
}

src/string/OneEditDistance.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ public class OneEditDistance {
1111
public boolean isOneEditDistance(String first, String second) {
1212
if (first.length() > second.length()) return isOneEditDistance(second, first);
1313

14-
if (second.length() - first.length() > 2) return false;
14+
if (second.length() - first.length() > 1
15+
|| first.equals(second)) {
16+
return false;
17+
}
1518

1619
boolean hasEdit = false;
1720
int i = 0;

src/string/StringJustification.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package string;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/text-justification
8+
* Difficulty: Hard
9+
* Time complexity: O(n)
10+
* Space complexity: O(n)
11+
*/
12+
public class StringJustification {
13+
14+
public List<String> fullJustify(String[] words, int maxWidth) {
15+
List<String> justified = new ArrayList<>();
16+
17+
List<String> currentLine = new ArrayList<>();
18+
int currentLineLength = 0;
19+
for (String word : words) {
20+
if (word.length() + currentLineLength + currentLine.size() <= maxWidth) {
21+
currentLine.add(word);
22+
currentLineLength += word.length();
23+
} else {
24+
justified.add(buildLine(currentLine, currentLineLength, false, maxWidth));
25+
currentLine = new ArrayList<>();
26+
currentLine.add(word);
27+
currentLineLength = word.length();
28+
}
29+
}
30+
31+
justified.add(buildLine(currentLine, currentLineLength, true, maxWidth));
32+
33+
return justified;
34+
}
35+
36+
private String buildLine(List<String> words, int length, boolean isLastLine, int maxLength) {
37+
if (isLastLine || words.size() == 1) {
38+
return buildLeftJustifiedLine(words, length, maxLength);
39+
}
40+
41+
return buildMidJustifiedLine(words, length, maxLength);
42+
}
43+
44+
private String buildLeftJustifiedLine(List<String> words, int length, int maxLength) {
45+
StringBuilder line = new StringBuilder();
46+
for (int i = 0; i < words.size(); i++) {
47+
line.append(words.get(i));
48+
if (i < words.size() - 1) line.append(" ");
49+
}
50+
51+
int extraSpaces = maxLength - length - (words.size() - 1);
52+
for (int i = 0; i < extraSpaces; i++) {
53+
line.append(" ");
54+
}
55+
56+
return line.toString();
57+
}
58+
59+
private String buildMidJustifiedLine(List<String> words, int length, int maxLength) {
60+
int spaces = maxLength - length;
61+
int evenSpaces = spaces / (words.size() - 1);
62+
int extraSpaces = spaces % (words.size() - 1);
63+
64+
StringBuilder line = new StringBuilder();
65+
for (int i = 0; i < words.size(); i++) {
66+
line.append(words.get(i));
67+
if (i == words.size() - 1) break;
68+
69+
for (int j = 0; j < evenSpaces; j++) {
70+
line.append(" ");
71+
}
72+
73+
if (extraSpaces > 0) {
74+
line.append(" ");
75+
extraSpaces--;
76+
}
77+
78+
}
79+
80+
return line.toString();
81+
}
82+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package string;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
/**
9+
* Description: https://leetcode.com/problems/substring-with-concatenation-of-all-words
10+
* Difficulty: Hard
11+
* Time complexity: O(w + (s - w * wl) * (w + w * wl))
12+
* Space complexity: O(w + wl)
13+
*/
14+
public class SubstringWithConcatenationOfAllWords {
15+
16+
public List<Integer> findSubstring(String s, String[] words) {
17+
int wordLength = words[0].length();
18+
Map<String, Integer> freqMap = buildFreqMap(words);
19+
20+
List<Integer> result = new ArrayList<>();
21+
for (int start = 0; start <= s.length() - words.length * wordLength; start++) {
22+
if (isMatch(s, words, wordLength, freqMap, start)) {
23+
result.add(start);
24+
}
25+
}
26+
27+
return result;
28+
}
29+
30+
private boolean isMatch(
31+
String s,
32+
String[] words,
33+
int wordLength,
34+
Map<String, Integer> freqMap,
35+
int start) {
36+
Map<String, Integer> freqMapCopy = new HashMap<>(freqMap);
37+
int found = 0;
38+
39+
for (int i = 0; i < words.length; i++) {
40+
String word = s.substring(start + i * wordLength, start + i * wordLength + wordLength);
41+
int count = freqMapCopy.getOrDefault(word, 0);
42+
if (count <= 0) return false;
43+
44+
freqMapCopy.merge(word, -1, Integer::sum);
45+
found += 1;
46+
47+
if (found == words.length) return true;
48+
}
49+
50+
return false;
51+
}
52+
53+
private Map<String, Integer> buildFreqMap(String[] words) {
54+
Map<String, Integer> freqMap = new HashMap<>();
55+
for (String word : words) {
56+
freqMap.merge(word, 1, Integer::sum);
57+
}
58+
59+
return freqMap;
60+
}
61+
}

0 commit comments

Comments
 (0)