Skip to content

Commit f027242

Browse files
committed
Update
1 parent 5d1cfe5 commit f027242

File tree

5 files changed

+117
-4
lines changed

5 files changed

+117
-4
lines changed

README.md

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

88
Problems are separated into 19 topics:
@@ -20,13 +20,13 @@ Problems are separated into 19 topics:
2020
| 9 | Greedy Algorithms | [greedy](src/greedy) | 21 |
2121
| 10 | Heap | [heap](src/heap) | 19 |
2222
| 11 | Interval | [interval](src/interval) | 11 |
23-
| 12 | Linked List | [linked_list](src/linked_list) | 31 |
24-
| 13 | Math | [math](src/math) | 19 |
23+
| 12 | Linked List | [linked_list](src/linked_list) | 32 |
24+
| 13 | Math | [math](src/math) | 20 |
2525
| 14 | Matrix | [matrix](src/matrix) | 18 |
2626
| 15 | Quad Tree | [quad_tree](src/quad_tree) | 1 |
2727
| 16 | Recursion | [recursion](src/recursion) | 24 |
2828
| 17 | Stack | [stack](src/stack) | 25 |
29-
| 18 | String | [string](src/string) | 54 |
29+
| 18 | String | [string](src/string) | 56 |
3030
| 19 | Trie | [trie](src/trie) | 4 |
3131

3232
## Good places to start your own LeetCode journey
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://leetcode.com/problems/delete-n-nodes-after-m-nodes-of-a-linked-list
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class DeleteNNodesAfterMNodesOfLinkedList {
10+
11+
public ListNode deleteNodes(ListNode head, int m, int n) {
12+
ListNode current = head;
13+
ListNode prev = head;
14+
15+
int toKeep = m;
16+
int toSkip = n;
17+
while (current != null) {
18+
if (toKeep > 0) {
19+
prev = current;
20+
current = current.next;
21+
toKeep--;
22+
} else if (toSkip > 0) {
23+
prev.next = null;
24+
current = current.next;
25+
toSkip--;
26+
} else {
27+
prev.next = current;
28+
toKeep = m;
29+
toSkip = n;
30+
}
31+
}
32+
33+
return head;
34+
}
35+
36+
private static class ListNode {
37+
private ListNode next;
38+
}
39+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package math;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/missing-number-in-arithmetic-progression
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class MissingNumberInArithmeticProgression {
10+
11+
public int missingNumber(int[] arr) {
12+
int expectedSum = (int) ((arr[0] + arr[arr.length - 1]) / 2.0 * (arr.length + 1));
13+
int actualSum = 0;
14+
for (int num : arr) {
15+
actualSum += num;
16+
}
17+
18+
return expectedSum - actualSum;
19+
}
20+
}

src/string/PalindromePermutation.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package string;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/palindrome-permutation
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class PalindromePermutation {
10+
11+
public boolean canPermutePalindrome(String s) {
12+
int[] freqMap = new int[26];
13+
for (char c : s.toCharArray()) {
14+
freqMap[c - 'a']++;
15+
}
16+
17+
boolean isOddFound = false;
18+
for (int freq : freqMap) {
19+
if (freq % 2 != 0) {
20+
if (isOddFound) return false;
21+
isOddFound = true;
22+
}
23+
}
24+
25+
return true;
26+
}
27+
}

src/string/SingleRowKeyboard.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package string;
2+
3+
/**
4+
* Description: https://leetcode.com/problems/single-row-keyboard
5+
* Difficulty: Easy
6+
* Time complexity: O(n)
7+
* Space complexity: O(1)
8+
*/
9+
public class SingleRowKeyboard {
10+
11+
public int calculateTime(String keyboard, String word) {
12+
int[] keyboardMap = new int[26];
13+
for (int i = 0; i < keyboard.length(); i++) {
14+
keyboardMap[keyboard.charAt(i) - 'a'] = i;
15+
}
16+
17+
int prev = 0;
18+
int time = 0;
19+
for (int i = 0; i < word.length(); i++) {
20+
int current = keyboardMap[word.charAt(i) - 'a'];
21+
time += Math.abs(current - prev);
22+
prev = current;
23+
}
24+
25+
return time;
26+
}
27+
}

0 commit comments

Comments
 (0)