diff --git a/Easy/Find Minimum Log Transportation Cost.java b/Easy/Find Minimum Log Transportation Cost.java new file mode 100644 index 00000000..3820474e --- /dev/null +++ b/Easy/Find Minimum Log Transportation Cost.java @@ -0,0 +1,12 @@ +class Solution { + public long minCuttingCost(int n, int m, int k) { + long cost = 0; + if (n > k) { + cost += ((long) (n - k)) * (k); + } + if (m > k) { + cost += ((long) (m - k)) * (k); + } + return cost; + } +} diff --git a/Easy/Generate Tag for Video Caption.java b/Easy/Generate Tag for Video Caption.java new file mode 100644 index 00000000..420bce85 --- /dev/null +++ b/Easy/Generate Tag for Video Caption.java @@ -0,0 +1,26 @@ +class Solution { + public String generateTag(String caption) { + StringBuilder sb = new StringBuilder(); + sb.append('#'); + int idx = 0; + int n = caption.length(); + while (idx < n && caption.charAt(idx) == ' ') { + idx++; + } + boolean upperCase = false; + while (idx < n) { + if (Character.isLetter(caption.charAt(idx))) { + if (upperCase) { + sb.append(Character.toUpperCase(caption.charAt(idx))); + upperCase = false; + } else { + sb.append(Character.toLowerCase(caption.charAt(idx))); + } + } else if (caption.charAt(idx) == ' ') { + upperCase = true; + } + idx++; + } + return sb.toString().substring(0, Math.min(sb.length(), 100)); + } +} diff --git a/Easy/Maximum Product of Two Digits.java b/Easy/Maximum Product of Two Digits.java new file mode 100644 index 00000000..c61f3c3a --- /dev/null +++ b/Easy/Maximum Product of Two Digits.java @@ -0,0 +1,17 @@ +class Solution { + public int maxProduct(int n) { + int maximum = Integer.MIN_VALUE; + int secondMaximum = Integer.MIN_VALUE; + while (n > 0) { + int num = n % 10; + n /= 10; + if (num > maximum) { + secondMaximum = maximum; + maximum = num; + } else if (num > secondMaximum) { + secondMaximum = num; + } + } + return maximum * secondMaximum; + } +} diff --git a/Medium/Calculate Score After Performing Instructions.java b/Medium/Calculate Score After Performing Instructions.java new file mode 100644 index 00000000..789e5669 --- /dev/null +++ b/Medium/Calculate Score After Performing Instructions.java @@ -0,0 +1,19 @@ +class Solution { + public long calculateScore(String[] instructions, int[] values) { + long result = 0; + int n = values.length; + int idx = 0; + Set executed = new HashSet<>(); + while (idx < n && idx >= 0) { + if (!executed.add(idx)) { + break; + } + if (instructions[idx].equals("add")) { + result += values[idx++]; + } else { + idx += values[idx]; + } + } + return result; + } +} diff --git a/Medium/Distribute Candies Among Children II.java b/Medium/Distribute Candies Among Children II.java new file mode 100644 index 00000000..aa9648f2 --- /dev/null +++ b/Medium/Distribute Candies Among Children II.java @@ -0,0 +1,14 @@ +class Solution { + public long distributeCandies(int n, int limit) { + long result = 0; + for (int i = 0; i <= Math.min(limit, n); i++) { + // distribution not possible as we will have to allocate at least one child with + // more than limit candies + if (n - i > 2 * limit) { + continue; + } + result += Math.min(n - i, limit) - Math.max(0, n - i - limit) + 1; + } + return result; + } +} diff --git a/Medium/Find Minimum Time to Reach Last Room I.java b/Medium/Find Minimum Time to Reach Last Room I.java new file mode 100644 index 00000000..0247baa4 --- /dev/null +++ b/Medium/Find Minimum Time to Reach Last Room I.java @@ -0,0 +1,36 @@ +class Solution { + + private static final int[][] DIRS = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}}; + + public int minTimeToReach(int[][] moveTime) { + int n = moveTime.length; + int m = moveTime[0].length; + boolean[][] visited = new boolean[n][m]; + Integer[][] minTime = new Integer[n][m]; + minTime[0][0] = 0; + PriorityQueue pq = new PriorityQueue<>((a, b) -> a[2] - b[2]); + pq.add(new int[]{0, 0, 0}); + while (!pq.isEmpty()) { + int[] removed = pq.remove(); + int x = removed[0]; + int y = removed[1]; + if (visited[x][y]) { + continue; + } + visited[x][y] = true; + for (int[] dir : DIRS) { + int newX = x + dir[0]; + int newY = y + dir[1]; + if (newX >= 0 && newY >= 0 && newX < n && newY < m) { + int distance = Math.max(minTime[x][y], moveTime[newX][newY]) + 1; + int currMinTime = minTime[newX][newY] == null ? Integer.MAX_VALUE : minTime[newX][newY]; + if (currMinTime > distance) { + minTime[newX][newY] = distance; + pq.add(new int[]{newX, newY, distance}); + } + } + } + } + return minTime[n - 1][m - 1]; + } +} diff --git a/Medium/Find the Lexicographically Largest String From the Box I.java b/Medium/Find the Lexicographically Largest String From the Box I.java new file mode 100644 index 00000000..1133cb93 --- /dev/null +++ b/Medium/Find the Lexicographically Largest String From the Box I.java @@ -0,0 +1,16 @@ +class Solution { + public String answerString(String word, int numFriends) { + if (numFriends == 1) { + return word; + } + int n = word.length(); + String result = ""; + for (int i = 0; i < n; i++) { + String curr = word.substring(i, Math.min(i + n - numFriends + 1, n)); + if (result.compareTo(curr) <= 0) { + result = curr; + } + } + return result; + } +} diff --git a/Medium/Lexicographically Minimum String After Removing Stars.java b/Medium/Lexicographically Minimum String After Removing Stars.java new file mode 100644 index 00000000..794ee712 --- /dev/null +++ b/Medium/Lexicographically Minimum String After Removing Stars.java @@ -0,0 +1,28 @@ +class Solution { + public String clearStars(String s) { + Deque[] count = new Deque[26]; + for (int i = 0; i < 26; i++) { + count[i] = new ArrayDeque<>(); + } + char[] letters = s.toCharArray(); + for (int i = 0; i < letters.length; i++) { + if (letters[i] != '*') { + count[letters[i] - 'a'].push(i); + } else { + for (int j = 0; j < 26; j++) { + if (!count[j].isEmpty()) { + letters[count[j].pop()] = '*'; + break; + } + } + } + } + StringBuilder sb = new StringBuilder(); + for (char c : letters) { + if (c != '*') { + sb.append(c); + } + } + return sb.toString(); + } +} diff --git a/Medium/Lexicographically Smallest Equivalent String.java b/Medium/Lexicographically Smallest Equivalent String.java index c5856615..f4d01c21 100644 --- a/Medium/Lexicographically Smallest Equivalent String.java +++ b/Medium/Lexicographically Smallest Equivalent String.java @@ -1,31 +1,46 @@ class Solution { - public String smallestEquivalentString(String s1, String s2, String baseStr) { - int[] graph = new int[26]; - for (int i = 0; i < 26; i++) { - graph[i] = i; + public String smallestEquivalentString(String s1, String s2, String baseStr) { + UnionFind unionFind = new UnionFind(26); + for (int i = 0; i < s1.length(); i++) { + char c1 = s1.charAt(i); + char c2 = s2.charAt(i); + unionFind.union(c1 - 'a', c2 - 'a'); + } + StringBuilder result = new StringBuilder(); + for (char c : baseStr.toCharArray()) { + result.append((char) (unionFind.find(c - 'a') + 'a')); + } + return result.toString(); } - for (int i = 0; i < s1.length(); i++) { - int idxOne = s1.charAt(i) - 'a'; - int idxTwo = s2.charAt(i) - 'a'; - int parentOne = find(graph, idxOne); - int parentTwo = find(graph, idxTwo); - if(parentOne < parentTwo) { - graph[parentTwo] = parentOne; - } else { - graph[parentOne] = parentTwo; - } + + class UnionFind { + private int[] parent; + + public UnionFind(int n) { + parent = new int[n]; + for (int i = 0; i < n; i++) { + parent[i] = i; + } + } + + public int find(int node) { + if (parent[node] == node) { + return node; + } + parent[node] = find(parent[node]); + return parent[node]; + } + + public void union(int nodeOne, int nodeTwo) { + int parentOne = find(nodeOne); + int parentTwo = find(nodeTwo); + if (parentOne != parentTwo) { + if (parentOne < parentTwo) { + parent[parentTwo] = parentOne; + } else { + parent[parentOne] = parentTwo; + } + } + } } - StringBuilder sb = new StringBuilder(); - for (char c : baseStr.toCharArray()) { - sb.append((char) ('a' + find(graph, c - 'a'))); - } - return sb.toString(); - } - - private int find(int[] graph, int idx) { - while(graph[idx] != idx) { - idx = graph[idx]; - } - return idx; - } } diff --git a/Medium/Resulting String After Adjacent Removals.java b/Medium/Resulting String After Adjacent Removals.java new file mode 100644 index 00000000..efa43bf6 --- /dev/null +++ b/Medium/Resulting String After Adjacent Removals.java @@ -0,0 +1,21 @@ +class Solution { + public String resultingString(String s) { + Stack stack = new Stack<>(); + for (char c : s.toCharArray()) { + if (!stack.isEmpty() && isAdjacent(stack.peek(), c)) { + stack.pop(); + } else { + stack.push(c); + } + } + StringBuilder result = new StringBuilder(); + for (char c : stack) { + result.append(c); + } + return result.toString(); + } + + private static boolean isAdjacent(char first, char second) { + return Math.abs(first - second) == 1 || (first == 'a' && second == 'z') || (first == 'z' && second == 'a'); + } +} diff --git a/Medium/Total Characters in String After Transformations I.java b/Medium/Total Characters in String After Transformations I.java new file mode 100644 index 00000000..3751ab7e --- /dev/null +++ b/Medium/Total Characters in String After Transformations I.java @@ -0,0 +1,25 @@ +class Solution { + + private static final int MOD = 1000_0000_07; + + public int lengthAfterTransformations(String s, int t) { + int[] frequency = new int[26]; + for (char c : s.toCharArray()) { + frequency[c - 'a']++; + } + for (int round = 0; round < t; round++) { + int[] next = new int[26]; + next[0] = frequency[25]; // Transformation from 'z' + next[1] = (frequency[25] + frequency[0]) % MOD; // Transformation from either 'z' or 'a' + for (int i = 2; i < 26; i++) { + next[i] = frequency[i - 1]; // Transformation for next character + } + frequency = next; + } + int result = 0; + for (int i = 0; i < 26; i++) { + result = (result + frequency[i]) % MOD; + } + return result; + } +} diff --git a/Medium/Using a Robot to Print the Lexicographically Smallest String.java b/Medium/Using a Robot to Print the Lexicographically Smallest String.java new file mode 100644 index 00000000..8d9c2064 --- /dev/null +++ b/Medium/Using a Robot to Print the Lexicographically Smallest String.java @@ -0,0 +1,31 @@ +class Solution { + public String robotWithString(String s) { + int[] counter = new int[26]; + for (char c : s.toCharArray()) { + counter[c - 'a']++; + } + Stack stack = new Stack<>(); + StringBuilder sb = new StringBuilder(); + for (char c : s.toCharArray()) { + stack.push(c); + counter[c - 'a']--; + while (!stack.isEmpty() && isSmallest(stack.peek(), counter)) { + sb.append(stack.pop()); + } + } + while (!stack.isEmpty()) { + sb.append(stack.pop()); + } + return sb.toString(); + } + + private boolean isSmallest(char c, int[] counter) { + int limit = c - 'a'; + for (int i = 0; i < limit; i++) { + if (counter[i] > 0) { + return false; + } + } + return true; + } +} diff --git a/Medium/Zero Array Transformation I.java b/Medium/Zero Array Transformation I.java new file mode 100644 index 00000000..2b9ab602 --- /dev/null +++ b/Medium/Zero Array Transformation I.java @@ -0,0 +1,24 @@ +class Solution { + public boolean isZeroArray(int[] nums, int[][] queries) { + int n = nums.length; + int[] diff = new int[n + 1]; + for (int[] query : queries) { + int left = query[0]; + int right = query[1]; + diff[left] += 1; + diff[right + 1] -= 1; + } + int[] operation = new int[n + 1]; + int currOperation = 0; + for (int i = 0; i < n + 1; i++) { + currOperation += diff[i]; + operation[i] = currOperation; + } + for (int i = 0; i < n; i++) { + if (operation[i] < nums[i]) { + return false; + } + } + return true; + } +}