Skip to content

Commit 80d576c

Browse files
committed
Update
1 parent f027242 commit 80d576c

File tree

5 files changed

+229
-3
lines changed

5 files changed

+229
-3
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 530 DSA problems from the [LeetCode](https://leetcode.com/)
5+
This repository contains solutions for 534 DSA problems from the [LeetCode](https://leetcode.com/)
66
website.
77

88
Problems are separated into 19 topics:
99

1010
| | Topic | Package | Problems |
1111
|---:|:--------------------|:-----------------------------------------------|---------:|
12-
| 1 | Array | [array](src/array) | 74 |
12+
| 1 | Array | [array](src/array) | 76 |
1313
| 2 | Binary | [binary](src/binary) | 13 |
1414
| 3 | Binary Search | [binary_search](src/binary_search) | 18 |
1515
| 4 | Binary Search Tree | [binary_search_tree](src/binary_search_tree) | 16 |
@@ -18,7 +18,7 @@ Problems are separated into 19 topics:
1818
| 7 | Dynamic Programming | [dynamic_programming](src/dynamic_programming) | 48 |
1919
| 8 | Graph | [graph](src/graph) | 65 |
2020
| 9 | Greedy Algorithms | [greedy](src/greedy) | 21 |
21-
| 10 | Heap | [heap](src/heap) | 19 |
21+
| 10 | Heap | [heap](src/heap) | 21 |
2222
| 11 | Interval | [interval](src/interval) | 11 |
2323
| 12 | Linked List | [linked_list](src/linked_list) | 32 |
2424
| 13 | Math | [math](src/math) | 20 |

src/array/BinarySubarraysWithSum.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package array;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/binary-subarrays-with-sum
8+
* Difficulty: Medium
9+
* Time complexity: O(n)
10+
* Space complexity: O(n)
11+
*/
12+
public class BinarySubarraysWithSum {
13+
14+
public int numSubarraysWithSum(int[] nums, int goal) {
15+
Map<Integer, Integer> prefixSum = new HashMap<>();
16+
17+
int count = 0;
18+
int currentSum = 0;
19+
for (int num : nums) {
20+
currentSum += num;
21+
22+
int currentSubarray = currentSum == goal ? 1 : 0;
23+
int prevSubarrays = prefixSum.getOrDefault(currentSum - goal, 0);
24+
25+
count += currentSubarray + prevSubarrays;
26+
prefixSum.merge(currentSum, 1, Integer::sum);
27+
}
28+
29+
return count;
30+
}
31+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package array;
2+
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/count-number-of-nice-subarrays
8+
* Difficulty: Medium
9+
*/
10+
public class CountNumberOfNiceSubarrays {
11+
12+
/**
13+
* Time complexity: O(n)
14+
* Space complexity: O(n)
15+
*/
16+
public int numberOfSubarraysViaPrefixSum(int[] nums, int k) {
17+
Map<Integer, Integer> prefixSum = new HashMap<>();
18+
19+
int count = 0;
20+
int currentSum = 0;
21+
for (int num : nums) {
22+
currentSum += (num % 2 == 0) ? 0 : 1;
23+
24+
int currentSubarray = currentSum == k ? 1 : 0;
25+
int prevSubarray = prefixSum.getOrDefault(currentSum - k, 0);
26+
27+
count += currentSubarray + prevSubarray;
28+
prefixSum.merge(currentSum, 1, Integer::sum);
29+
}
30+
31+
return count;
32+
}
33+
34+
/**
35+
* Time complexity: O(n)
36+
* Space complexity: O(1)
37+
*/
38+
public int numberOfSubarraysViaTwoPointers(int[] nums, int k) {
39+
int left = 0;
40+
int count = 0;
41+
int subarraysSoFar = 0;
42+
43+
for (int right = 0; right < nums.length; right++) {
44+
if (nums[right] % 2 != 0) {
45+
k--;
46+
subarraysSoFar = 0; // new odd found -> reset the counter
47+
}
48+
49+
while (k == 0) {
50+
k += nums[left] == 1 ? 1 : 0;
51+
subarraysSoFar++;
52+
left++;
53+
}
54+
55+
count += subarraysSoFar;
56+
}
57+
58+
return count;
59+
}
60+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package heap;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/least-number-of-unique-integers-after-k-removals
7+
* Difficulty: Medium
8+
* Time complexity: O(nlog n)
9+
* Space complexity: O(n)
10+
*/
11+
public class LeastNumberOfUniqueIntegersAfterKRemovals {
12+
13+
public int findLeastNumOfUniqueIntsViaMinHeap(int[] arr, int k) {
14+
Map<Integer, Integer> freqMap = buildFreqMap(arr);
15+
Queue<Map.Entry<Integer, Integer>> minHeap = buildHeap(freqMap);
16+
17+
while (k > 0) {
18+
k -= minHeap.poll().getValue();
19+
}
20+
21+
return k < 0 ? minHeap.size() + 1 : minHeap.size();
22+
}
23+
24+
private Queue<Map.Entry<Integer, Integer>> buildHeap(Map<Integer, Integer> freqMap) {
25+
Queue<Map.Entry<Integer, Integer>> minHeap = new PriorityQueue<>(
26+
Comparator.comparingInt(Map.Entry::getValue));
27+
for (Map.Entry<Integer, Integer> e : freqMap.entrySet()) {
28+
minHeap.offer(e);
29+
}
30+
return minHeap;
31+
}
32+
33+
public int findLeastNumOfUniqueIntsViaSorting(int[] arr, int k) {
34+
Map<Integer, Integer> freqMap = buildFreqMap(arr);
35+
List<Integer> sortedByFreq = getNumbersSortedByFreq(freqMap);
36+
37+
int pointer = 0;
38+
int removed = 0;
39+
while (k > 0 && pointer < sortedByFreq.size()) {
40+
int num = sortedByFreq.get(pointer);
41+
k -= freqMap.get(num);
42+
43+
if (k >= 0) removed++;
44+
45+
pointer++;
46+
}
47+
48+
return sortedByFreq.size() - removed;
49+
}
50+
51+
private Map<Integer, Integer> buildFreqMap(int[] arr) {
52+
Map<Integer, Integer> freqMap = new HashMap<>();
53+
for (int num : arr) {
54+
freqMap.merge(num, 1, Integer::sum);
55+
}
56+
57+
return freqMap;
58+
}
59+
60+
private List<Integer> getNumbersSortedByFreq(Map<Integer, Integer> freqMap) {
61+
return new ArrayList<>(freqMap.entrySet()).stream()
62+
.sorted(Comparator.comparing(Map.Entry::getValue))
63+
.map(Map.Entry::getKey)
64+
.toList();
65+
}
66+
}

src/heap/RewardTopKStudents.java

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package heap;
2+
3+
import java.util.*;
4+
5+
/**
6+
* Description: https://leetcode.com/problems/reward-top-k-students
7+
* Difficulty: Medium
8+
* Time complexity: O(nlog n)
9+
* Space complexity: O(n)
10+
*/
11+
public class RewardTopKStudents {
12+
13+
public List<Integer> topStudents(
14+
String[] positiveFeedback,
15+
String[] negativeFeedback,
16+
String[] reports,
17+
int[] studentIds,
18+
int k) {
19+
Set<String> positive = new HashSet<>(Arrays.asList(positiveFeedback));
20+
Set<String> negative = new HashSet<>(Arrays.asList(negativeFeedback));
21+
Queue<Student> maxHeap = buildHeap(studentIds, reports, positive, negative);
22+
23+
return getTopK(maxHeap, k);
24+
}
25+
26+
private List<Integer> getTopK(Queue<Student> maxHeap, int k) {
27+
List<Integer> topK = new ArrayList<>();
28+
while (k > 0) {
29+
topK.add(maxHeap.poll().id);
30+
k--;
31+
}
32+
33+
return topK;
34+
}
35+
36+
private Queue<Student> buildHeap(
37+
int[] studentIds,
38+
String[] reports,
39+
Set<String> positive,
40+
Set<String> negative) {
41+
Queue<Student> maxHeap = new PriorityQueue<>(
42+
Comparator.comparing(Student::points).reversed().thenComparing(Student::id));
43+
44+
for (int i = 0; i < studentIds.length; i++) {
45+
int points = countPoints(reports[i], positive, negative);
46+
maxHeap.offer(new Student(studentIds[i], points));
47+
}
48+
49+
return maxHeap;
50+
}
51+
52+
private int countPoints(String report, Set<String> positive, Set<String> negative) {
53+
String[] words = report.split(" ");
54+
55+
int points = 0;
56+
for (String word : words) {
57+
if (positive.contains(word)) {
58+
points += 3;
59+
} else if (negative.contains(word)) {
60+
points -= 1;
61+
}
62+
}
63+
64+
return points;
65+
}
66+
67+
private record Student(int id, int points) {
68+
}
69+
}

0 commit comments

Comments
 (0)