Skip to content

Commit 9b043ee

Browse files
committed
Add new solutions
1 parent af23d6c commit 9b043ee

12 files changed

+452
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.andrewbayd;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
7+
/*
8+
Given two strings s and p, return an array of all the start indices of p's anagrams in s. You may return the answer in any order.
9+
10+
https://leetcode.com/problems/find-all-anagrams-in-a-string/
11+
*/
12+
13+
public class FindAllAnagrams {
14+
public static List<Integer> findAllAnagrams(String original, String check) {
15+
List<Integer> res = new ArrayList<>();
16+
if (check.length() > original.length()) {
17+
return res;
18+
}
19+
HashMap<Character, Integer> checkMap = new HashMap<>();
20+
HashMap<Character, Integer> currentMap = new HashMap<>();
21+
int slow = 0;
22+
int fast = check.length() - 1;
23+
for (int i = 0; i < check.length(); i++) {
24+
checkMap.put(check.charAt(i), checkMap.getOrDefault(check.charAt(i), 0) + 1);
25+
currentMap.put(original.charAt(i), currentMap.getOrDefault(original.charAt(i), 0) + 1);
26+
}
27+
while (fast < original.length()) {
28+
if (currentMap.equals(checkMap)) res.add(slow);
29+
if (currentMap.get(original.charAt(slow)) == 1) {
30+
currentMap.remove(original.charAt(slow));
31+
} else {
32+
currentMap.put(original.charAt(slow), currentMap.get(original.charAt(slow)) - 1);
33+
}
34+
slow++;
35+
fast++;
36+
if (fast < original.length()) {
37+
currentMap.put(original.charAt(fast), currentMap.getOrDefault(original.charAt(fast), 0) + 1);
38+
}
39+
40+
}
41+
return res;
42+
}
43+
44+
public static void main(String[] args) {
45+
System.out.println(findAllAnagrams("cbaebabacd", "abc")); //-> [0, 6]
46+
}
47+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.andrewbayd;
2+
3+
import java.util.HashSet;
4+
5+
/*
6+
Given a string s, find the length of the longest substring without repeating characters.
7+
8+
https://leetcode.com/problems/longest-substring-without-repeating-characters/
9+
*/
10+
11+
public class LongestSubstringWithoutRepeatingCharacters {
12+
public static int lengthOfLongestSubstring(String s) {
13+
HashSet<Character> set = new HashSet<>();
14+
int res = 0;
15+
int slow = 0;
16+
int fast = 0;
17+
while (fast < s.length()) {
18+
if (!set.contains(s.charAt(fast))) {
19+
set.add(s.charAt(fast));
20+
fast++;
21+
} else {
22+
while (s.charAt(slow) != s.charAt(fast)) {
23+
set.remove(s.charAt(slow));
24+
slow++;
25+
}
26+
set.remove(s.charAt(slow));
27+
slow++;
28+
}
29+
res = Math.max(res, fast - slow);
30+
}
31+
return res;
32+
}
33+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.andrewbayd;
2+
3+
import com.andrewbayd.datastructures.ListNode;
4+
5+
import java.util.Comparator;
6+
import java.util.PriorityQueue;
7+
8+
/*
9+
You are given an array of k linked-lists lists, each linked-list is sorted in ascending order.
10+
Merge all the linked-lists into one sorted linked-list and return it.
11+
12+
https://leetcode.com/problems/merge-k-sorted-lists/
13+
*/
14+
public class MergeKSortedLists {
15+
public static ListNode mergeKLists(ListNode[] lists) {
16+
PriorityQueue<ListNode> pq = new PriorityQueue<>(Comparator.comparing(e -> e.val));
17+
for (ListNode list : lists) {
18+
if (list != null) {
19+
pq.add(list);
20+
}
21+
}
22+
ListNode res = new ListNode(0);
23+
ListNode curr = res;
24+
while (!pq.isEmpty()) {
25+
curr.next = pq.poll();
26+
curr = curr.next;
27+
if (curr.next != null) {
28+
pq.add(curr.next);
29+
}
30+
}
31+
return res.next;
32+
}
33+
34+
public static void main(String[] args) {
35+
ListNode l1n1 = new ListNode(1);
36+
ListNode l1n2 = new ListNode(4);
37+
ListNode l1n3 = new ListNode(5);
38+
l1n1.next = l1n2;
39+
l1n2.next = l1n3;
40+
41+
ListNode l2n1 = new ListNode(1);
42+
ListNode l2n2 = new ListNode(3);
43+
ListNode l2n3 = new ListNode(4);
44+
l2n1.next = l2n2;
45+
l2n2.next = l2n3;
46+
47+
ListNode l3n1 = new ListNode(2);
48+
ListNode l3n2 = new ListNode(6);
49+
l3n1.next = l3n2;
50+
51+
ListNode[] lists = {l1n1, l2n1, l3n1};
52+
53+
ListNode result = mergeKLists(lists);
54+
while (result != null) {
55+
System.out.print(result.val + "->");
56+
result = result.next;
57+
}
58+
}
59+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.andrewbayd;
2+
3+
import com.andrewbayd.datastructures.ListNode;
4+
5+
/*
6+
Given the head of a singly linked list, return the middle node of the linked list. If there are two middle nodes, return the second middle node.
7+
8+
https://leetcode.com/problems/middle-of-the-linked-list/
9+
*/
10+
public class MiddleOfLinkedList {
11+
public ListNode middleNode(ListNode head) {
12+
ListNode slow = head;
13+
ListNode fast = head;
14+
while (fast.next != null) {
15+
if (fast.next.next != null) {
16+
fast = fast.next.next;
17+
} else {
18+
fast = fast.next;
19+
}
20+
slow = slow.next;
21+
}
22+
return slow;
23+
}
24+
}

src/com/andrewbayd/MinStack.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.andrewbayd;
2+
3+
import java.util.ArrayDeque;
4+
5+
/*
6+
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.
7+
8+
https://leetcode.com/problems/min-stack/
9+
*/
10+
class MinStack {
11+
private final ArrayDeque<Integer> elements;
12+
private final ArrayDeque<Integer> mins;
13+
14+
public MinStack() {
15+
elements = new ArrayDeque<>();
16+
mins = new ArrayDeque<>();
17+
}
18+
19+
public void push(int val) {
20+
elements.add(val);
21+
if (mins.isEmpty() || val <= mins.peekLast()) {
22+
mins.add(val);
23+
}
24+
}
25+
26+
public void pop() {
27+
int popped = elements.pollLast();
28+
if (popped == mins.peekLast()) {
29+
mins.pollLast();
30+
}
31+
}
32+
33+
public int top() {
34+
return elements.peekLast();
35+
}
36+
37+
public int getMin() {
38+
return mins.peekLast();
39+
}
40+
}
41+
42+
/**
43+
* Your MinStack object will be instantiated and called as such:
44+
* MinStack obj = new MinStack();
45+
* obj.push(val);
46+
* obj.pop();
47+
* int param_3 = obj.top();
48+
* int param_4 = obj.getMin();
49+
*/
50+

src/com/andrewbayd/OpenTheLock.java

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.andrewbayd;
2+
3+
import java.util.*;
4+
import java.util.stream.Collectors;
5+
6+
/*
7+
https://leetcode.com/problems/open-the-lock/
8+
*/
9+
public class OpenTheLock {
10+
public static Map<Character, Character> nextDigit = Map.of(
11+
'0', '1',
12+
'1', '2',
13+
'2', '3',
14+
'3', '4',
15+
'4', '5',
16+
'5', '6',
17+
'6', '7',
18+
'7', '8',
19+
'8', '9',
20+
'9', '0'
21+
);
22+
23+
public static Map<Character, Character> prevDigit = nextDigit.entrySet().stream().collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey));
24+
25+
public static int openLock(String combo, List<String> trappedCombos) {
26+
if (combo.equals("0000")) {
27+
return 0;
28+
}
29+
HashSet<String> trappedComboSet = new HashSet<>(trappedCombos);
30+
HashMap<String, Integer> bfsMap = new HashMap<>();
31+
bfsMap.put("0000", 0);
32+
ArrayDeque<String> bfsQueue = new ArrayDeque<>();
33+
bfsQueue.offer("0000");
34+
while (!bfsQueue.isEmpty()) {
35+
String top = bfsQueue.poll();
36+
for (int i = 0; i < 4; i++) {
37+
String newCombo = top.substring(0, i).concat(String.valueOf(nextDigit.get(top.charAt(i)))).concat(top.substring(i + 1));
38+
if (!trappedComboSet.contains(newCombo) && !bfsMap.containsKey(newCombo)) {
39+
bfsQueue.offer(newCombo);
40+
bfsMap.put(newCombo, bfsMap.get(top) + 1);
41+
if (newCombo.equals(combo)) {
42+
return bfsMap.get(newCombo);
43+
}
44+
}
45+
newCombo = top.substring(0, i).concat(String.valueOf(prevDigit.get(top.charAt(i)))).concat(top.substring(i + 1));
46+
if (!trappedComboSet.contains(newCombo) && !bfsMap.containsKey(newCombo)) {
47+
bfsQueue.offer(newCombo);
48+
bfsMap.put(newCombo, bfsMap.get(top) + 1);
49+
if (newCombo.equals(combo)) {
50+
return bfsMap.get(newCombo);
51+
}
52+
}
53+
}
54+
}
55+
return -1;
56+
}
57+
58+
public static void main(String[] args) {
59+
openLock("0202", List.of("0201","0101","0102","1212","2002"));
60+
}
61+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.andrewbayd;
2+
3+
/*
4+
Given an integer array nums sorted in non-decreasing order, remove the duplicates in-place such that each unique element appears only once.
5+
The relative order of the elements should be kept the same.
6+
7+
https://leetcode.com/problems/remove-duplicates-from-sorted-array
8+
*/
9+
public class RemoveDuplicatesFromSortedArray {
10+
public int removeDuplicates(int[] nums) {
11+
int slow = 0;
12+
int fast = 1;
13+
while (fast < nums.length) {
14+
if (nums[fast] == nums[fast - 1]) {
15+
fast++;
16+
} else {
17+
slow++;
18+
nums[slow] = nums[fast];
19+
fast++;
20+
}
21+
}
22+
return slow + 1;
23+
}
24+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package com.andrewbayd;
2+
3+
/*
4+
https://leetcode.com/problems/robot-bounded-in-circle/
5+
*/
6+
public class RobotInACircle {
7+
public boolean isRobotBounded(String instructions) {
8+
int x = 0;
9+
int y = 0;
10+
char dir = 'N';
11+
for (char c : instructions.toCharArray()) {
12+
if (c == 'G') {
13+
if (dir == 'N') y += 1;
14+
else if (dir == 'S') y -= 1;
15+
else if (dir == 'E') x += 1;
16+
else x -= 1;
17+
}
18+
if (c == 'L') {
19+
if (dir == 'N') dir = 'W';
20+
else if (dir == 'W') dir = 'S';
21+
else if (dir == 'S') dir = 'E';
22+
else dir = 'N';
23+
}
24+
if (c == 'R') {
25+
if (dir == 'N') dir = 'E';
26+
else if (dir == 'E') dir = 'S';
27+
else if (dir == 'S') dir = 'W';
28+
else dir = 'N';
29+
}
30+
}
31+
32+
if (x == 0 && y == 0) return true;
33+
else return dir != 'N';
34+
}
35+
}

src/com/andrewbayd/SlowestKey.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.andrewbayd;
2+
3+
/*
4+
https://leetcode.com/problems/slowest-key/
5+
*/
6+
public class SlowestKey {
7+
public static char slowestKey(int[] releaseTimes, String keysPressed) {
8+
char slowestKey = keysPressed.charAt(0);
9+
int longestTime = releaseTimes[0];
10+
for (int i = 1; i < releaseTimes.length; i++) {
11+
int releaseTime = releaseTimes[i] - releaseTimes[i - 1];
12+
char currKey = keysPressed.charAt(i);
13+
if (releaseTime > longestTime) {
14+
longestTime = releaseTime;
15+
slowestKey = currKey;
16+
} else if (releaseTime == longestTime && currKey > slowestKey) {
17+
slowestKey = currKey;
18+
}
19+
}
20+
return slowestKey;
21+
}
22+
23+
public static void main(String[] args) {
24+
slowestKey(new int[]{9, 29, 49, 50}, "cbcd"); //-> c
25+
}
26+
}

src/com/andrewbayd/SubarraySum.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.andrewbayd;
2+
3+
/*
4+
Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray
5+
of which the sum is greater than or equal to target. If there is no such subarray, return 0 instead.
6+
7+
https://leetcode.com/problems/minimum-size-subarray-sum/
8+
*/
9+
public class SubarraySum {
10+
public int minSubArrayLen(int target, int[] nums) {
11+
int sum = 0;
12+
int res = Integer.MAX_VALUE;
13+
int slow = 0;
14+
int fast = 0;
15+
while (fast < nums.length) {
16+
sum += nums[fast];
17+
fast++;
18+
while (sum >= target) {
19+
res = Math.min(res, fast - slow);
20+
sum -= nums[slow];
21+
slow++;
22+
}
23+
}
24+
return res == Integer.MAX_VALUE ? 0 : res;
25+
}
26+
}

0 commit comments

Comments
 (0)