Skip to content

Commit 74dbd38

Browse files
committed
Add 3 problems
1 parent 8a5131a commit 74dbd38

File tree

5 files changed

+117
-5
lines changed

5 files changed

+117
-5
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
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 585 DSA problems from the [LeetCode](https://leetcode.com/)
5+
This repository contains solutions for 588 DSA problems from the [LeetCode](https://leetcode.com/)
66
website.
77

88
Problems are separated into 20 topics:
99

1010
| | Topic | Package | Problems |
1111
|---:|:--------------------|:-----------------------------------------------|---------:|
12-
| 1 | Array | [array](src/array) | 81 |
12+
| 1 | Array | [array](src/array) | 83 |
1313
| 2 | Binary | [binary](src/binary) | 13 |
1414
| 3 | Binary Search | [binary_search](src/binary_search) | 19 |
1515
| 4 | Binary Search Tree | [binary_search_tree](src/binary_search_tree) | 16 |
@@ -21,7 +21,7 @@ Problems are separated into 20 topics:
2121
| 10 | Heap | [heap](src/heap) | 22 |
2222
| 11 | Interval | [interval](src/interval) | 12 |
2323
| 12 | Linked List | [linked_list](src/linked_list) | 34 |
24-
| 13 | Math | [math](src/math) | 22 |
24+
| 13 | Math | [math](src/math) | 23 |
2525
| 14 | Matrix | [matrix](src/matrix) | 19 |
2626
| 15 | Quad Tree | [quad_tree](src/quad_tree) | 1 |
2727
| 16 | Recursion | [recursion](src/recursion) | 24 |
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
import java.util.HashSet;
5+
import java.util.Set;
6+
7+
/**
8+
* Description: https://leetcode.com/problems/make-array-zero-by-subtracting-equal-amounts
9+
* Difficulty: Easy
10+
*/
11+
public class MakeArrayZeroBySubtractingEqualAmounts {
12+
13+
/**
14+
* Time complexity: O(n)
15+
* Space complexity: O(n)
16+
*/
17+
public int minimumOperationsViaSet(int[] nums) {
18+
Set<Integer> unique = new HashSet<>();
19+
for (int num : nums) {
20+
if (num != 0) unique.add(num);
21+
}
22+
23+
return unique.size();
24+
}
25+
26+
/**
27+
* Time complexity: O(nlog n + n)
28+
* Space complexity: O(log n)
29+
*/
30+
public int minimumOperationsViaSorting(int[] nums) {
31+
Arrays.sort(nums);
32+
33+
int current = 0;
34+
int operations = 0;
35+
int subtract = 0;
36+
while (current < nums.length) {
37+
int num = nums[current] - subtract;
38+
if (num == 0) {
39+
current++;
40+
continue;
41+
}
42+
43+
subtract += num;
44+
operations++;
45+
46+
current++;
47+
}
48+
49+
return operations;
50+
}
51+
}

src/array/MaximumUnitsOnTruck.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package array;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/maximum-units-on-a-truck
8+
* Difficulty: Easy
9+
* Time complexity: O(nlog n)
10+
* Space complexity: O(n)
11+
*/
12+
public class MaximumUnitsOnTruck {
13+
14+
public int maximumUnits(int[][] boxTypes, int truckSize) {
15+
List<Box> boxes = new ArrayList<>();
16+
for (int[] box : boxTypes) {
17+
boxes.add(new Box(box[0], box[1]));
18+
}
19+
boxes.sort((a, b) -> Integer.compare(b.units, a.units));
20+
21+
int units = 0;
22+
for (Box box : boxes) {
23+
int number = Math.min(truckSize, box.number);
24+
units += number * box.units;
25+
truckSize -= number;
26+
27+
if (truckSize == 0) break;
28+
}
29+
30+
return units;
31+
}
32+
33+
private record Box(int number, int units) {}
34+
}

src/math/ExcelSheetColumnTitle.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package math;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/excel-sheet-column-title
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class ExcelSheetColumnTitle {
10+
11+
public String convertToTitle(int col) {
12+
StringBuilder converted = new StringBuilder();
13+
while (col > 0) {
14+
col--;
15+
converted.append((char) (col % 26 + 'A'));
16+
col = col / 26;
17+
}
18+
19+
return converted.reverse().toString();
20+
}
21+
}

src/string/FindKLengthSubstringsWithNoRepeatedCharacters.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,15 @@
1313
*/
1414
public class FindKLengthSubstringsWithNoRepeatedCharacters {
1515

16+
private static final int ALPHABET_SIZE = 26;
17+
1618
public int numKLenSubstrNoRepeatsViaSet(String s, int k) {
17-
Set<Character> seen = new HashSet<>();
19+
if (k > ALPHABET_SIZE) return 0;
1820

21+
Set<Character> seen = new HashSet<>();
1922
int substrings = 0;
2023
int left = 0;
24+
2125
for (int right = 0; right < s.length(); right++) {
2226
while (seen.contains(s.charAt(right))) {
2327
seen.remove(s.charAt(left));
@@ -37,10 +41,12 @@ public int numKLenSubstrNoRepeatsViaSet(String s, int k) {
3741
}
3842

3943
public int numKLenSubstrNoRepeatsViaFreqMap(String s, int k) {
40-
Map<Character, Integer> freqMap = new HashMap<>();
44+
if (k > ALPHABET_SIZE) return 0;
4145

46+
Map<Character, Integer> freqMap = new HashMap<>();
4247
int substrings = 0;
4348
int left = 0;
49+
4450
for (int right = 0; right < s.length(); right++) {
4551
freqMap.merge(s.charAt(right), 1, Integer::sum);
4652
while (freqMap.get(s.charAt(right)) > 1) {

0 commit comments

Comments
 (0)