Skip to content

Commit f7bb65b

Browse files
committed
Update
1 parent 1c33e21 commit f7bb65b

30 files changed

+275
-6
lines changed

README.md

Lines changed: 7 additions & 6 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 544 DSA problems from the [LeetCode](https://leetcode.com/)
5+
This repository contains solutions for 573 DSA problems from the [LeetCode](https://leetcode.com/)
66
website.
77

8-
Problems are separated into 19 topics:
8+
Problems are separated into 20 topics:
99

1010
| | Topic | Package | Problems |
1111
|---:|:--------------------|:-----------------------------------------------|---------:|
12-
| 1 | Array | [array](src/array) | 79 |
12+
| 1 | Array | [array](src/array) | 80 |
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 |
@@ -25,9 +25,10 @@ Problems are separated into 19 topics:
2525
| 14 | Matrix | [matrix](src/matrix) | 18 |
2626
| 15 | Quad Tree | [quad_tree](src/quad_tree) | 1 |
2727
| 16 | Recursion | [recursion](src/recursion) | 24 |
28-
| 17 | Stack | [stack](src/stack) | 25 |
29-
| 18 | String | [string](src/string) | 56 |
30-
| 19 | Trie | [trie](src/trie) | 4 |
28+
| 17 | SQL | [sql](src/sql) | 28 |
29+
| 18 | Stack | [stack](src/stack) | 25 |
30+
| 19 | String | [string](src/string) | 56 |
31+
| 20 | Trie | [trie](src/trie) | 4 |
3132

3233
## Good places to start your own LeetCode journey
3334

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package array;
2+
3+
import java.util.Arrays;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* Description: https://leetcode.com/problems/sort-array-by-increasing-frequency
9+
* Difficulty: Easy
10+
* Time complexity: O(n)
11+
* Space complexity: O(n)
12+
*/
13+
public class SortArrayByIncreasingFrequency {
14+
15+
public int[] frequencySort(int[] nums) {
16+
Map<Integer, Integer> freqMap = buildFreqMap(nums);
17+
return Arrays.stream(nums)
18+
.boxed()
19+
.sorted((a, b) -> compare(a, b, freqMap))
20+
.mapToInt(v -> v)
21+
.toArray();
22+
}
23+
24+
private Map<Integer, Integer> buildFreqMap(int[] nums) {
25+
Map<Integer, Integer> freqMap = new HashMap<>();
26+
for (int num : nums) {
27+
freqMap.merge(num, 1, Integer::sum);
28+
}
29+
30+
return freqMap;
31+
}
32+
33+
private int compare(int a, int b, Map<Integer, Integer> freqMap) {
34+
int result = Integer.compare(freqMap.get(a), freqMap.get(b));
35+
return result != 0 ? result : Integer.compare(b, a);
36+
}
37+
}

src/sql/ArticleViews.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Description: https://leetcode.com/problems/article-views-i
2+
-- Difficulty: Easy
3+
4+
SELECT DISTINCT author_id AS id
5+
FROM Views
6+
WHERE author_id = viewer_id
7+
ORDER BY author_id;

src/sql/AverageSellingPrice.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- Description: https://leetcode.com/problems/average-selling-price
2+
-- Difficulty: Easy
3+
4+
SELECT s.product_id,
5+
ROUND(SUM(s.units * p.price) / SUM(s.units), 2) AS average_price
6+
FROM UnitsSold AS s
7+
LEFT JOIN Prices AS p
8+
ON s.product_id = p.product_id
9+
AND s.purchase_date BETWEEN p.start_date AND p.end_date
10+
GROUP BY s.product_id;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
-- Description: https://leetcode.com/problems/average-time-of-process-per-machine
2+
-- Difficulty: Easy
3+
4+
SELECT s.machine_id, ROUND(AVG(e.timestamp - s.timestamp), 3) AS processing_time
5+
FROM Activity AS s
6+
INNER JOIN Activity AS e
7+
ON s.machine_id = e.machine_id
8+
AND s.process_id = e.process_id
9+
AND s.activity_type = 'start'
10+
AND e.activity_type = 'end'
11+
GROUP BY s.machine_id;

src/sql/BigCountries.sql

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Description: https://leetcode.com/problems/big-countries
2+
-- Difficulty: Easy
3+
4+
SELECT name, population, area
5+
FROM World
6+
WHERE area >= 3000000
7+
OR population >= 25000000;

src/sql/BiggestSingleNumber.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
-- Description: https://leetcode.com/problems/biggest-single-number
2+
-- Difficulty: Easy
3+
4+
SELECT MAX(num) AS num
5+
FROM (SELECT num
6+
FROM MyNumbers
7+
GROUP BY num
8+
HAVING COUNT(num) = 1) AS tmp;
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Description: https://leetcode.com/problems/classes-more-than-5-students
2+
-- Difficulty: Easy
3+
4+
SELECT class
5+
FROM Courses
6+
GROUP BY class
7+
HAVING COUNT(DISTINCT student) >= 5;

src/sql/ConfirmationRate.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Description: https://leetcode.com/problems/confirmation-rate
2+
-- Difficulty: Medium
3+
4+
SELECT s.user_id,
5+
ROUND(AVG(CASE WHEN c.action = 'confirmed' THEN 1.0 ELSE 0.0 END), 2) AS confirmation_rate
6+
FROM Signups AS s
7+
LEFT JOIN Confirmations AS c
8+
ON s.user_id = c.user_id
9+
GROUP BY s.user_id
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-- Description: https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions
2+
-- Difficulty: Easy
3+
4+
SELECT v.customer_id, COUNT(v.visit_id) AS count_no_trans
5+
FROM Visits AS v
6+
LEFT JOIN Transactions AS t
7+
ON v.visit_id = t.visit_id
8+
WHERE t.transaction_id IS NULL
9+
GROUP BY v.customer_id;

0 commit comments

Comments
 (0)