Skip to content

Commit 1c33e21

Browse files
committed
Update
1 parent c030fc4 commit 1c33e21

File tree

5 files changed

+97
-5
lines changed

5 files changed

+97
-5
lines changed

README.md

Lines changed: 2 additions & 2 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 541 DSA problems from the [LeetCode](https://leetcode.com/)
5+
This repository contains solutions for 544 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) | 76 |
12+
| 1 | Array | [array](src/array) | 79 |
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 |
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package array;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
import java.util.List;
6+
7+
/**
8+
* Description: https://leetcode.com/problems/add-to-array-form-of-integer
9+
* Difficulty: Easy
10+
* Time complexity: O(n)
11+
* Space complexity: O(n)
12+
*/
13+
public class AddToArrayFormOfInteger {
14+
15+
public List<Integer> addToArrayForm(int[] num, int k) {
16+
List<Integer> result = new ArrayList<>();
17+
int carry = k;
18+
19+
for (int i = num.length - 1; i >= 0; i--) {
20+
int sum = num[i] + carry;
21+
result.add(sum % 10);
22+
carry = sum / 10;
23+
}
24+
25+
while (carry != 0) {
26+
result.add(carry % 10);
27+
carry = carry / 10;
28+
}
29+
30+
Collections.reverse(result);
31+
return result;
32+
}
33+
}

src/array/DegreeOfArray.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package array;
2+
3+
import java.util.Collections;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
7+
/**
8+
* Description: https://leetcode.com/problems/degree-of-an-array
9+
* Difficulty: Easy
10+
* Time complexity: O(n)
11+
* Space complexity: O(n)
12+
*/
13+
public class DegreeOfArray {
14+
15+
public int findShortestSubArray(int[] nums) {
16+
Map<Integer, Integer> first = new HashMap<>();
17+
Map<Integer, Integer> last = new HashMap<>();
18+
Map<Integer, Integer> freqMap = new HashMap<>();
19+
20+
for (int i = 0; i < nums.length; i++) {
21+
first.putIfAbsent(nums[i], i);
22+
last.put(nums[i], i);
23+
freqMap.merge(nums[i], 1, Integer::sum);
24+
}
25+
26+
int arrayDegree = Collections.max(freqMap.values());
27+
int shortest = Integer.MAX_VALUE;
28+
for (int num : freqMap.keySet()) {
29+
if (freqMap.get(num) == arrayDegree) {
30+
shortest = Math.min(shortest, last.get(num) - first.get(num) + 1);
31+
}
32+
}
33+
34+
return shortest;
35+
}
36+
}

src/array/RichestCustomerWealth.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package array;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/richest-customer-wealth
5+
* Difficulty: Easy
6+
* Time complexity: O(n * m)
7+
* Space complexity: O(1)
8+
*/
9+
public class RichestCustomerWealth {
10+
11+
public int maximumWealth(int[][] accounts) {
12+
int max = 0;
13+
for (int[] customer : accounts) {
14+
int wealth = 0;
15+
for (int account : customer) {
16+
wealth += account;
17+
}
18+
19+
max = Math.max(max, wealth);
20+
}
21+
22+
return max;
23+
}
24+
}

src/linked_list/PlusOneLinkedList.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import java.util.LinkedList;
55

66
/**
7-
* Description: https://leetcode.com/problems/plus-one-linked-list
7+
* Description: https://leetcode.com/problems/plus-one-lignked-list
88
* Difficulty: Medium
99
*/
1010
public class PlusOneLinkedList {
@@ -42,7 +42,7 @@ public ListNode plusOneViaStack(ListNode head) {
4242

4343
Deque<ListNode> stack = buildStack(dummy);
4444

45-
int carry = 0;
45+
int carry = 1;
4646
while (!stack.isEmpty()) {
4747
ListNode current = stack.pop();
4848
int sum = current.val + carry;
@@ -59,7 +59,6 @@ private Deque<ListNode> buildStack(ListNode dummy) {
5959
ListNode current = dummy;
6060
while (current != null) {
6161
stack.push(current);
62-
if (current.next == null) current.val += 1;
6362
current = current.next;
6463
}
6564

0 commit comments

Comments
 (0)