Skip to content

Commit 7f9e96f

Browse files
committed
Solution is Added
1 parent 8aeeae6 commit 7f9e96f

File tree

20 files changed

+211
-0
lines changed

20 files changed

+211
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.HashSet;
2+
import java.util.Set;
3+
4+
class MinimumDeletionsToMakeCharacterFrequenciesUnique {
5+
public int minDeletions(String s) {
6+
int[] freq = new int[26];
7+
for (char c : s.toCharArray()) {
8+
freq[c - 'a']++;
9+
}
10+
Set<Integer> set = new HashSet<>();
11+
int deletions = 0;
12+
for (int f : freq) {
13+
if (f == 0) {
14+
continue;
15+
}
16+
while (f > 0 && set.contains(f)) {
17+
f--;
18+
deletions++;
19+
}
20+
set.add(f);
21+
}
22+
23+
return deletions;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1647. Minimum Deletions to Make Character Frequencies Unique](https://leetcode.com/problems/minimum-deletions-to-make-character-frequencies-unique/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Arrays;
2+
3+
class DetermineIfTwoStringsAreClose {
4+
public boolean closeStrings(String word1, String word2) {
5+
if (word1.length() != word2.length())
6+
return false;
7+
8+
int[] arr1 = new int[26];
9+
int[] arr2 = new int[26];
10+
for (int i = 0; i < word1.length(); i++) {
11+
arr1[word1.charAt(i) - 'a']++;
12+
arr2[word2.charAt(i) - 'a']++;
13+
}
14+
15+
for (int i = 0; i < 26; i++) {
16+
if ((arr1[i] == 0 && arr2[i] != 0) || (arr1[i] != 0 && arr2[i] == 0))
17+
return false;
18+
}
19+
20+
Arrays.sort(arr1);
21+
Arrays.sort(arr2);
22+
return Arrays.equals(arr1, arr2);
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [1657. Determine if Two Strings Are Close](https://leetcode.com/problems/determine-if-two-strings-are-close/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n)$. $n$ is the length of input String.
7+
- Space Complexity: $O(1)$. We only use constant size of array. The input of sorting is constant as well. Thus, the total space complexity is $O(1)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class CheckIfTwoStringArraysAreEquivalent {
2+
public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
3+
return String.join("", word1).equals(String.join("", word2));
4+
}
5+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1662. Check If Two String Arrays are Equivalent](https://leetcode.com/problems/check-if-two-string-arrays-are-equivalent/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import java.util.TreeSet;
2+
3+
class MinimizeDeviationInArray {
4+
public int minimumDeviation(int[] nums) {
5+
TreeSet<Integer> set = new TreeSet<>();
6+
for (int num : nums) {
7+
set.add(num % 2 == 0 ? num : num * 2);
8+
}
9+
int res = set.last() - set.first();
10+
while (res > 0 && set.last() % 2 == 0) {
11+
int max = set.last();
12+
set.remove(max);
13+
set.add(max / 2);
14+
res = Math.min(res, set.last() - set.first());
15+
}
16+
return res;
17+
}
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [1675. Minimize Deviation in Array](https://leetcode.com/problems/minimize-deviation-in-array/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n)$. $n$ is the length of `nums`.
7+
- Space Complexity: $O(n)$. We maintain at most $n$ numbers in the TreeSet.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class ConcatenationOfConsecutiveBinaryNumbers {
2+
private final int modulo = (int) Math.pow(10, 9) + 7;
3+
4+
public int concatenatedBinary(int n) {
5+
long res = 0;
6+
for (int i = 0; i <= n; i++) {
7+
int temp = i;
8+
while (temp > 0) {
9+
temp /= 2;
10+
res *= 2;
11+
}
12+
res = (res + i) % modulo;
13+
}
14+
return (int) res;
15+
}
16+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1680. Concatenation of Consecutive Binary Numbers](https://leetcode.com/problems/concatenation-of-consecutive-binary-numbers/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class PartitioningIntoMinimumNumberOfDeciBinaryNumbers {
2+
public int minPartitions(String n) {
3+
int m = 0;
4+
char[] chars = n.toCharArray();
5+
for (char c : chars) {
6+
if (c - '0' > m) {
7+
m = c - '0';
8+
}
9+
}
10+
return m;
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1689. Partitioning Into Minimum Number Of Deci-Binary Numbers](https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/)
2+

1696.jump-game-vi/JumpGameVi.java

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import java.util.PriorityQueue;
2+
import java.util.Queue;
3+
4+
class JumpGameVi {
5+
public int maxResult(int[] nums, int k) {
6+
if (nums.length == 0) return 0;
7+
Queue<int[]> pq = new PriorityQueue<>((a, b) -> (b[0] - a[0]));
8+
pq.offer(new int[]{nums[0], 0});
9+
10+
int max = nums[0];
11+
12+
for (int i = 1; i < nums.length; i++) {
13+
while(pq.peek()[1] < i - k) {
14+
pq.poll();
15+
}
16+
int[] cur = pq.peek();
17+
max = cur[0] + nums[i];
18+
pq.offer(new int[]{max, i});
19+
}
20+
return max;
21+
}
22+
}

1696.jump-game-vi/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1696. Jump Game VI](https://leetcode.com/problems/jump-game-vi/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class DetermineIfStringHalvesAreAlike {
2+
public boolean halvesAreAlike(String s) {
3+
String left = s.substring(0, s.length() / 2);
4+
String right = s.substring(s.length() / 2);
5+
String v = "aeiouAEIOU";
6+
int leftSum = 0, rightSum = 0;
7+
for (int i = 0; i < left.length(); i++)
8+
if (v.indexOf(left.charAt(i)) >= 0)
9+
leftSum++;
10+
for (int i = 0; i < right.length(); i++)
11+
if (v.indexOf(right.charAt(i)) >= 0)
12+
rightSum++;
13+
return leftSum == rightSum;
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [1704. Determine if String Halves Are Alike](https://leetcode.com/problems/determine-if-string-halves-are-alike/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n)$. $n$ is the length of $s$.
7+
- Space Complexity: $O(n)$. The space complexity for left and right are $O(n)$.
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1706. Where Will the Ball Fall](https://leetcode.com/problems/where-will-the-ball-fall/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class WhereWillTheBallFall {
2+
public int[] findBall(int[][] grid) {
3+
int n = grid[0].length;
4+
int[] res = new int[n];
5+
for (int i = 0; i < n; i++) {
6+
int i1 = i, i2;
7+
for (int[] ints : grid) {
8+
i2 = i1 + ints[i1];
9+
if (i2 < 0 || i2 >= n || ints[i2] != ints[i1]) {
10+
i1 = -1;
11+
break;
12+
}
13+
i1 = i2;
14+
}
15+
res[i] = i1;
16+
}
17+
return res;
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import java.util.Arrays;
2+
3+
class MaximumUnitsOnATruck {
4+
public int maximumUnits(int[][] boxTypes, int truckSize) {
5+
Arrays.sort(boxTypes, (a, b) -> b[1] - a[1]);
6+
7+
int units = 0;
8+
for (int[] box : boxTypes) {
9+
if (box[0] >= truckSize) {
10+
units += truckSize * box[1];
11+
break;
12+
} else {
13+
units += box[0] * box[1];
14+
truckSize -= box[0];
15+
}
16+
}
17+
18+
return units;
19+
}
20+
}
+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1710. Maximum Units on a Truck](https://leetcode.com/problems/maximum-units-on-a-truck/)
2+

0 commit comments

Comments
 (0)