Skip to content

Commit 173fcfd

Browse files
committed
Code is Added By anjha
1 parent 91d6f69 commit 173fcfd

File tree

24 files changed

+319
-0
lines changed

24 files changed

+319
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [1822. Sign of the Product of an Array](https://leetcode.com/problems/sign-of-the-product-of-an-array/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n)$. $n$ is the length of `nums`.
7+
- Space Complexity: $O(1)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class SignOfTheProductOfAnArray {
2+
public int arraySign(int[] nums) {
3+
int sign = 1;
4+
for (int num: nums) {
5+
if (num == 0) {
6+
return 0;
7+
}
8+
if (num < 0) {
9+
sign *= -1;
10+
}
11+
}
12+
return sign;
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class CheckIfTheSentenceIsPangram {
2+
public boolean checkIfPangram(String sentence) {
3+
boolean[] found = new boolean[26];
4+
for (char c : sentence.toCharArray())
5+
found[c - 'a'] = true;
6+
for (boolean f : found)
7+
if (!f) return false;
8+
9+
return true;
10+
}
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1832. Check if the Sentence Is Pangram](https://leetcode.com/problems/check-if-the-sentence-is-pangram/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import java.util.Arrays;
2+
3+
class MaximumIceCreamBars {
4+
public int maxIceCream(int[] costs, int coins) {
5+
Arrays.sort(costs);
6+
int n = costs.length;
7+
for (int i = 0; i < n; i++) {
8+
if (costs[i] > coins)
9+
return i;
10+
coins -= costs[i];
11+
}
12+
return n;
13+
}
14+
}

1833.maximum-ice-cream-bars/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1833. Maximum Ice Cream Bars](https://leetcode.com/problems/maximum-ice-cream-bars/)
2+

1834.single-threaded-cpu/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [1834. Single-Threaded CPU](https://leetcode.com/problems/single-threaded-cpu/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n\log n)$. Array Sort costs $O(n\log n)$, add priority queue costs $O(\log n)$ and we running $n$ times which is the length of tasks. The total time complexity is $O(2n\log n)=O(n\log n)$.
7+
- Space Complexity: $O(n)$. newTasks costs $O(n)$ and priority queue costs $O(n)$ as well. The total space complexity is $O(2n)=O(n)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import java.util.*;
2+
3+
class SingleThreadedCpu {
4+
public int[] getOrder(int[][] tasks) {
5+
int[][] newTasks = new int[tasks.length][3];
6+
for (int i = 0; i < tasks.length; i++) {
7+
newTasks[i][0] = tasks[i][0];
8+
newTasks[i][1] = tasks[i][1];
9+
newTasks[i][2] = i;
10+
}
11+
12+
Arrays.sort(newTasks, Comparator.comparingInt(a -> a[0]));
13+
// pq data: new int[]{processingTime, index}
14+
PriorityQueue<int[]> pq = new PriorityQueue<>((a, b) -> (a[0] != b[0] ? (a[0] - b[0]) : (a[1] - b[1])));
15+
int[] ans = new int[tasks.length];
16+
int ansIndex = 0, currTime = 0;
17+
for (int i = 0; i < newTasks.length; i++) {
18+
while (currTime < newTasks[i][0] && !pq.isEmpty()) {
19+
int[] topTask = pq.remove();
20+
ans[ansIndex++] = topTask[1];
21+
currTime += topTask[0];
22+
}
23+
currTime = Math.max(currTime, newTasks[i][0]);
24+
pq.add(new int[]{newTasks[i][1], newTasks[i][2]});
25+
}
26+
while (!pq.isEmpty()) {
27+
int[] topTask = pq.remove();
28+
ans[ansIndex++] = topTask[1];
29+
}
30+
31+
return ans;
32+
}
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
class NearestExitFromEntranceInMaze {
5+
public int nearestExit(char[][] maze, int[] entrance) {
6+
int rows = maze.length, cols = maze[0].length;
7+
int[][] dirs = new int[][]{{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
8+
9+
int startRow = entrance[0], startCol = entrance[1];
10+
maze[startRow][startCol] = '+';
11+
12+
Queue<int[]> queue = new LinkedList<>();
13+
queue.offer(new int[]{startRow, startCol, 0});
14+
15+
while (!queue.isEmpty()) {
16+
int[] currentState = queue.poll();
17+
int currRow = currentState[0], currCol = currentState[1], currDistance = currentState[2];
18+
19+
for (int[] dir : dirs) {
20+
int nextRow = currRow + dir[0], nextCol = currCol + dir[1];
21+
22+
if (0 <= nextRow && nextRow < rows && 0 <= nextCol && nextCol < cols && maze[nextRow][nextCol] == '.') {
23+
if (nextRow == 0 || nextRow == rows - 1 || nextCol == 0 || nextCol == cols - 1)
24+
return currDistance + 1;
25+
26+
maze[nextRow][nextCol] = '+';
27+
queue.offer(new int[]{nextRow, nextCol, currDistance + 1});
28+
}
29+
}
30+
}
31+
32+
return -1;
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1926. Nearest Exit from Entrance in Maze](https://leetcode.com/problems/nearest-exit-from-entrance-in-maze/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [1962. Remove Stones to Minimize the Total](https://leetcode.com/problems/remove-stones-to-minimize-the-total/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n + k\log n)$. $n$ is the length of piles. Create priority queue and put piles to it cost $O(n)$. Moreover, add value and remove value cost $O(\log n)$. In the final step, calculate the result costs $O(n)$. The total time complexity is $O(2n+k\log n)=O(n + k\log n)$.
7+
- Space Complexity: $O(n)$. The space complexity of priority queue is $O(n)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.PriorityQueue;
2+
3+
class RemoveStonesToMinimizeTheTotal {
4+
public int minStoneSum(int[] piles, int k) {
5+
PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> b - a);
6+
for (int i = 0; i < piles.length; i++)
7+
pq.add(piles[i]);
8+
9+
for (int i = 0; i < k; i++) {
10+
int stone = pq.remove();
11+
pq.add(stone - (stone / 2));
12+
}
13+
14+
int stones = 0;
15+
for (int stone : pq)
16+
stones += stone;
17+
18+
return stones;
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.ArrayList;
2+
import java.util.LinkedList;
3+
import java.util.List;
4+
import java.util.Queue;
5+
6+
class FindIfPathExistsInGraph {
7+
public boolean validPath(int n, int[][] edges, int source, int destination) {
8+
List<Integer>[] adj = new List[n];
9+
for (int i = 0; i < n; i++)
10+
adj[i] = new ArrayList<>();
11+
for (int[] edge : edges) {
12+
int x = edge[0], y = edge[1];
13+
adj[x].add(y);
14+
adj[y].add(x);
15+
}
16+
boolean[] visited = new boolean[n];
17+
Queue<Integer> queue = new LinkedList<>();
18+
queue.offer(source);
19+
visited[source] = true;
20+
while (!queue.isEmpty()) {
21+
int vertex = queue.poll();
22+
if (vertex == destination) break;
23+
for (int next : adj[vertex]) {
24+
if (!visited[next]) {
25+
queue.offer(next);
26+
visited[next] = true;
27+
}
28+
}
29+
}
30+
return visited[destination];
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1971. Find if Path Exists in Graph](https://leetcode.com/problems/find-if-path-exists-in-graph/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1996. The Number of Weak Characters in the Game](https://leetcode.com/problems/the-number-of-weak-characters-in-the-game/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.Arrays;
2+
3+
class TheNumberOfWeakCharactersInTheGame {
4+
public int numberOfWeakCharacters(int[][] properties) {
5+
Arrays.sort(properties, (a, b) -> (a[0] == b[0]) ? Integer.compare(b[1], a[1]) : Integer.compare(a[0], b[0]));
6+
7+
int noOfWeakCharacters = 0;
8+
int len = properties.length;
9+
int max = properties[len-1][1];
10+
11+
for (int i = len - 2; i >= 0; i--) {
12+
if (properties[i][1] < max) noOfWeakCharacters++;
13+
else max = properties[i][1];
14+
}
15+
return noOfWeakCharacters;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import java.util.Arrays;
2+
import java.util.HashMap;
3+
4+
class FindOriginalArrayFromDoubledArray {
5+
public int[] findOriginalArray(int[] changed) {
6+
int[] emptyArr = new int[0];
7+
int l = changed.length;
8+
if (l % 2 != 0) return emptyArr;
9+
HashMap<Integer, Integer> map = new HashMap<>();
10+
int[] res = new int[l / 2];
11+
for (int j : changed) {
12+
map.put(j, map.getOrDefault(j, 0) + 1);
13+
}
14+
int temp = 0;
15+
Arrays.sort(changed);
16+
for (int num : changed) {
17+
if (map.get(num) <= 0) continue;
18+
if (map.getOrDefault(2 * num, 0) <= 0) return emptyArr;
19+
res[temp++] = num;
20+
map.put(num, map.get(num) - 1);
21+
map.put(2 * num, map.get(2 * num) - 1);
22+
}
23+
24+
return res;
25+
}
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [2007. Find Original Array From Doubled Array](https://leetcode.com/problems/find-original-array-from-doubled-array/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/**
2+
* Definition for singly-linked list.
3+
* public class ListNode {
4+
* int val;
5+
* ListNode next;
6+
* ListNode() {}
7+
* ListNode(int val) { this.val = val; }
8+
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
9+
* }
10+
*/
11+
class DeleteTheMiddleNodeOfALinkedList {
12+
public ListNode deleteMiddle(ListNode head) {
13+
if (head.next == null) return null;
14+
int count = 0;
15+
ListNode countList = head;
16+
while (countList != null) {
17+
count++;
18+
countList = countList.next;
19+
}
20+
countList = head;
21+
for (int i = 0; i < count / 2 - 1; i++) {
22+
countList = countList.next;
23+
}
24+
countList.next = countList.next.next;
25+
return head;
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [2095. Delete the Middle Node of a Linked List](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list/)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import java.util.HashMap;
2+
import java.util.Map;
3+
4+
class LongestPalindromeByConcatenatingTwoLetterWords {
5+
public int longestPalindrome(String[] words) {
6+
Map<String, Integer> map = new HashMap<>();
7+
int unpaired = 0, res = 0;
8+
for (String word : words) {
9+
if (!map.containsKey(word)) map.put(word, 0);
10+
if (word.charAt(0) == word.charAt(1)) {
11+
if (map.get(word) > 0) {
12+
unpaired--;
13+
map.put(word, map.get(word) - 1);
14+
res += 4;
15+
} else {
16+
map.put(word, map.get(word) + 1);
17+
unpaired++;
18+
}
19+
} else {
20+
String rev = Character.toString(word.charAt(1)) + Character.toString(word.charAt(0));
21+
if (map.containsKey(rev) && map.get(rev) > 0) {
22+
res += 4;
23+
map.put(rev, map.get(rev) - 1);
24+
} else {
25+
map.put(word, map.get(word) + 1);
26+
}
27+
}
28+
}
29+
if (unpaired > 0) res += 2;
30+
return res;
31+
}
32+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [2131. Longest Palindrome by Concatenating Two Letter Words](https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.ArrayList;
2+
import java.util.Comparator;
3+
import java.util.List;
4+
5+
class EarliestPossibleDayOfFullBloom {
6+
public int earliestFullBloom(int[] plantTime, int[] growTime) {
7+
int n = growTime.length;
8+
List<Integer> indices = new ArrayList<>();
9+
for (int i = 0; i < growTime.length; i++) {
10+
indices.add(i);
11+
}
12+
indices.sort(Comparator.comparingInt(i -> -growTime[i]));
13+
int result = 0;
14+
for (int i = 0, currPlantTime = 0; i < n; i++) {
15+
int index = indices.get(i);
16+
int time = currPlantTime + plantTime[index] + growTime[index];
17+
result = Math.max(result, time);
18+
currPlantTime += plantTime[index];
19+
}
20+
return result;
21+
}
22+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [2136. Earliest Possible Day of Full Bloom](https://leetcode.com/problems/earliest-possible-day-of-full-bloom/)
2+

0 commit comments

Comments
 (0)