Skip to content

Commit 0d3d2bd

Browse files
committed
Add 3 problems
1 parent 18faeee commit 0d3d2bd

File tree

4 files changed

+123
-4
lines changed

4 files changed

+123
-4
lines changed

README.md

Lines changed: 4 additions & 4 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 590 DSA problems from the [LeetCode](https://leetcode.com/)
5+
This repository contains solutions for 593 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) | 83 |
12+
| 1 | Array | [array](src/array) | 84 |
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 |
@@ -20,13 +20,13 @@ Problems are separated into 20 topics:
2020
| 9 | Greedy Algorithms | [greedy](src/greedy) | 21 |
2121
| 10 | Heap | [heap](src/heap) | 22 |
2222
| 11 | Interval | [interval](src/interval) | 12 |
23-
| 12 | Linked List | [linked_list](src/linked_list) | 34 |
23+
| 12 | Linked List | [linked_list](src/linked_list) | 35 |
2424
| 13 | Math | [math](src/math) | 23 |
2525
| 14 | Matrix | [matrix](src/matrix) | 20 |
2626
| 15 | Quad Tree | [quad_tree](src/quad_tree) | 1 |
2727
| 16 | Recursion | [recursion](src/recursion) | 24 |
2828
| 17 | SQL | [sql](src/sql) | 28 |
29-
| 18 | Stack | [stack](src/stack) | 25 |
29+
| 18 | Stack | [stack](src/stack) | 26 |
3030
| 19 | String | [string](src/string) | 62 |
3131
| 20 | Trie | [trie](src/trie) | 4 |
3232

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package array;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/rearrange-array-elements-by-sign
5+
* Difficulty: Medium
6+
* Time complexity: O(n(
7+
* Space complexity: O(n)
8+
*/
9+
public class RearrangeArrayElementsBySign {
10+
11+
public int[] rearrangeArray(int[] nums) {
12+
int[] rearranged = new int[nums.length];
13+
int positivePointer = 0;
14+
int negativePointer = 1;
15+
16+
for (int num : nums) {
17+
if (num > 0) {
18+
rearranged[positivePointer] = num;
19+
positivePointer += 2;
20+
} else {
21+
rearranged[negativePointer] = num;
22+
negativePointer += 2;
23+
}
24+
}
25+
26+
return rearranged;
27+
}
28+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package linked_list;
2+
3+
/**
4+
* Description: https://www.baeldung.com/java-greatest-common-divisor
5+
* Difficulty: Medium
6+
* Time complexity: O(n * log v)
7+
* Space complexity: O(1)
8+
*/
9+
public class FindGreatestCommonDivisorsInLinkedList {
10+
11+
public ListNode insertGreatestCommonDivisors(ListNode head) {
12+
ListNode current = head;
13+
while (current != null && current.next != null) {
14+
int gcd = findGCDViaEuclidAlgo(current.val, current.next.val);
15+
16+
ListNode next = current.next;
17+
current.next = new ListNode(gcd, next);
18+
19+
current = next;
20+
}
21+
22+
return head;
23+
}
24+
25+
private int findGCDViaEuclidAlgo(int a, int b) {
26+
if (b == 0) return a;
27+
return findGCDViaEuclidAlgo(b, a % b);
28+
}
29+
30+
private static class ListNode {
31+
int val;
32+
ListNode next;
33+
34+
public ListNode(int val, ListNode next) {
35+
this.val = val;
36+
this.next = next;
37+
}
38+
}
39+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package stack;
2+
3+
import java.util.LinkedList;
4+
import java.util.Queue;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/implement-stack-using-queues
8+
* Difficulty: Easy
9+
* Time complexity: O(1) for push() and peek(), O(n) for pop()
10+
* Space complexity: O(n)
11+
*/
12+
public class ImplementStackUsingQueues {
13+
14+
private static class MyStack {
15+
16+
private Queue<Integer> main;
17+
private Queue<Integer> tmp;
18+
private int top;
19+
20+
public MyStack() {
21+
this.main = new LinkedList<>();
22+
this.tmp = new LinkedList<>();
23+
this.top = -1;
24+
}
25+
26+
public void push(int x) {
27+
top = x;
28+
main.offer(x);
29+
}
30+
31+
public int pop() {
32+
while (main.size() > 1) {
33+
top = main.poll();
34+
tmp.offer(top);
35+
}
36+
37+
Queue<Integer> swap = main;
38+
main = tmp;
39+
tmp = swap;
40+
41+
return tmp.poll();
42+
}
43+
44+
public int top() {
45+
return top;
46+
}
47+
48+
public boolean empty() {
49+
return main.isEmpty();
50+
}
51+
}
52+
}

0 commit comments

Comments
 (0)