File tree Expand file tree Collapse file tree 5 files changed +97
-5
lines changed Expand file tree Collapse file tree 5 files changed +97
-5
lines changed Original file line number Diff line number Diff line change 2
2
3
3
![ DSA Learning] ( https://img.shields.io/badge/DSA-Learning-blue?style=for-the-badge&logo=leetcode )
4
4
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/ )
6
6
website.
7
7
8
8
Problems are separated into 19 topics:
9
9
10
10
| | Topic | Package | Problems |
11
11
| ---:| :--------------------| :-----------------------------------------------| ---------:|
12
- | 1 | Array | [ array] ( src/array ) | 76 |
12
+ | 1 | Array | [ array] ( src/array ) | 79 |
13
13
| 2 | Binary | [ binary] ( src/binary ) | 13 |
14
14
| 3 | Binary Search | [ binary_search] ( src/binary_search ) | 19 |
15
15
| 4 | Binary Search Tree | [ binary_search_tree] ( src/binary_search_tree ) | 16 |
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change 4
4
import java .util .LinkedList ;
5
5
6
6
/**
7
- * Description: https://leetcode.com/problems/plus-one-linked -list
7
+ * Description: https://leetcode.com/problems/plus-one-lignked -list
8
8
* Difficulty: Medium
9
9
*/
10
10
public class PlusOneLinkedList {
@@ -42,7 +42,7 @@ public ListNode plusOneViaStack(ListNode head) {
42
42
43
43
Deque <ListNode > stack = buildStack (dummy );
44
44
45
- int carry = 0 ;
45
+ int carry = 1 ;
46
46
while (!stack .isEmpty ()) {
47
47
ListNode current = stack .pop ();
48
48
int sum = current .val + carry ;
@@ -59,7 +59,6 @@ private Deque<ListNode> buildStack(ListNode dummy) {
59
59
ListNode current = dummy ;
60
60
while (current != null ) {
61
61
stack .push (current );
62
- if (current .next == null ) current .val += 1 ;
63
62
current = current .next ;
64
63
}
65
64
You can’t perform that action at this time.
0 commit comments