Skip to content

Commit c030fc4

Browse files
committed
Update
1 parent 74a000f commit c030fc4

File tree

5 files changed

+167
-5
lines changed

5 files changed

+167
-5
lines changed

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
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 537 DSA problems from the [LeetCode](https://leetcode.com/)
5+
This repository contains solutions for 541 DSA problems from the [LeetCode](https://leetcode.com/)
66
website.
77

88
Problems are separated into 19 topics:
@@ -11,17 +11,17 @@ Problems are separated into 19 topics:
1111
|---:|:--------------------|:-----------------------------------------------|---------:|
1212
| 1 | Array | [array](src/array) | 76 |
1313
| 2 | Binary | [binary](src/binary) | 13 |
14-
| 3 | Binary Search | [binary_search](src/binary_search) | 18 |
14+
| 3 | Binary Search | [binary_search](src/binary_search) | 19 |
1515
| 4 | Binary Search Tree | [binary_search_tree](src/binary_search_tree) | 16 |
1616
| 5 | Binary Tree | [binary_tree](src/binary_tree) | 47 |
1717
| 6 | Design | [design](src/design) | 18 |
18-
| 7 | Dynamic Programming | [dynamic_programming](src/dynamic_programming) | 48 |
18+
| 7 | Dynamic Programming | [dynamic_programming](src/dynamic_programming) | 49 |
1919
| 8 | Graph | [graph](src/graph) | 66 |
2020
| 9 | Greedy Algorithms | [greedy](src/greedy) | 21 |
2121
| 10 | Heap | [heap](src/heap) | 21 |
2222
| 11 | Interval | [interval](src/interval) | 11 |
23-
| 12 | Linked List | [linked_list](src/linked_list) | 33 |
24-
| 13 | Math | [math](src/math) | 20 |
23+
| 12 | Linked List | [linked_list](src/linked_list) | 34 |
24+
| 13 | Math | [math](src/math) | 21 |
2525
| 14 | Matrix | [matrix](src/matrix) | 18 |
2626
| 15 | Quad Tree | [quad_tree](src/quad_tree) | 1 |
2727
| 16 | Recursion | [recursion](src/recursion) | 24 |
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package binary_search;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/peak-index-in-a-mountain-array
5+
* Difficulty: Medium
6+
* Time complexity: O(log n)
7+
* Space complexity: O(1)
8+
*/
9+
public class PeakIndexInMountainArray {
10+
11+
public int peakIndexInMountainArray(int[] arr) {
12+
int left = 0;
13+
int right = arr.length - 1;
14+
15+
while (left <= right) {
16+
int mid = left + (right - left) / 2;
17+
18+
if (arr[mid] < arr[mid + 1]) {
19+
left = mid + 1;
20+
} else if (arr[mid] < arr[mid - 1]) {
21+
right = mid - 1;
22+
} else {
23+
return mid;
24+
}
25+
}
26+
27+
return -1;
28+
}
29+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package dynamic_programming;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/predict-the-winner
5+
* Difficulty: Medium
6+
* Time complexity: O(n^2)
7+
* Space complexity: O(n^2)
8+
*/
9+
public class PredictWinner {
10+
11+
private static final int FIRST_PLAYER = 0;
12+
private static final int SECOND_PLAYER = 1;
13+
14+
public boolean predictTheWinner(int[] nums) {
15+
// we track FIRST_PLAYER's score as positive and SECOND_PLAYER's as negative
16+
// if total score is greater or equal than 0 -> FIRST_PLAYER wins
17+
return play(nums, 0, nums.length - 1, FIRST_PLAYER, new int[nums.length][nums.length]) >= 0;
18+
}
19+
20+
private int play(int[] nums, int left, int right, int player, int[][] memo) {
21+
if (left > right) return 0;
22+
if (memo[left][right] != 0) return memo[left][right];
23+
24+
if (player == FIRST_PLAYER) {
25+
int takeLeft = nums[left] + play(nums, left + 1, right, SECOND_PLAYER, memo);
26+
int takeRight = nums[right] + play(nums, left, right - 1, SECOND_PLAYER, memo);
27+
memo[left][right] = Math.max(takeLeft, takeRight);
28+
} else {
29+
int takeLeft = -nums[left] + play(nums, left + 1, right, FIRST_PLAYER, memo);
30+
int takeRight = -nums[right] + play(nums, left, right - 1, FIRST_PLAYER, memo);
31+
memo[left][right] = Math.min(takeLeft, takeRight);
32+
}
33+
34+
return memo[left][right];
35+
}
36+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package linked_list;
2+
3+
import java.util.Deque;
4+
import java.util.LinkedList;
5+
6+
/**
7+
* Description: https://leetcode.com/problems/plus-one-linked-list
8+
* Difficulty: Medium
9+
*/
10+
public class PlusOneLinkedList {
11+
12+
/**
13+
* Time complexity: O(n)
14+
* Space complexity: O(1)
15+
*/
16+
public ListNode plusOneOptimalApproach(ListNode head) {
17+
ListNode dummy = new ListNode(0, head);
18+
19+
ListNode nonNine = dummy;
20+
while (head != null) {
21+
if (head.val != 9) nonNine = head;
22+
head = head.next;
23+
}
24+
25+
nonNine.val += 1;
26+
27+
ListNode nine = nonNine.next;
28+
while (nine != null) {
29+
nine.val = 0;
30+
nine = nine.next;
31+
}
32+
33+
return dummy.val == 0 ? dummy.next : dummy;
34+
}
35+
36+
/**
37+
* Time complexity: O(n)
38+
* Space complexity: O(n)
39+
*/
40+
public ListNode plusOneViaStack(ListNode head) {
41+
ListNode dummy = new ListNode(0, head);
42+
43+
Deque<ListNode> stack = buildStack(dummy);
44+
45+
int carry = 0;
46+
while (!stack.isEmpty()) {
47+
ListNode current = stack.pop();
48+
int sum = current.val + carry;
49+
current.val = sum % 10;
50+
carry = sum / 10;
51+
}
52+
53+
return dummy.val == 0 ? dummy.next : dummy;
54+
}
55+
56+
private Deque<ListNode> buildStack(ListNode dummy) {
57+
Deque<ListNode> stack = new LinkedList<>();
58+
59+
ListNode current = dummy;
60+
while (current != null) {
61+
stack.push(current);
62+
if (current.next == null) current.val += 1;
63+
current = current.next;
64+
}
65+
66+
return stack;
67+
}
68+
69+
private static class ListNode {
70+
int val;
71+
ListNode next;
72+
73+
public ListNode(int val, ListNode next) {
74+
this.val = val;
75+
this.next = next;
76+
}
77+
}
78+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package math;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/count-odd-numbers-in-an-interval-range
5+
* Difficulty: Easy
6+
* Time complexity: O(1)
7+
* Space complexity: O(1)
8+
*/
9+
public class CountOddNumbersInIntervalRange {
10+
11+
public int countOdds(int low, int high) {
12+
int count = (high - low) / 2;
13+
if (low % 2 != 0 || high % 2 != 0) {
14+
count++;
15+
}
16+
17+
return count;
18+
}
19+
}

0 commit comments

Comments
 (0)