Skip to content

Commit 8aeeae6

Browse files
committed
Code Is Added
1 parent e954535 commit 8aeeae6

File tree

20 files changed

+229
-0
lines changed

20 files changed

+229
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class AverageSalaryExcludingTheMinimumAndMaximumSalary {
2+
public double average(int[] salary) {
3+
double max = Integer.MIN_VALUE;
4+
double min = Integer.MAX_VALUE;
5+
double sum = 0, count = salary.length - 2;
6+
for (int s: salary) {
7+
if (s > max) {
8+
max = s;
9+
}
10+
if (s < min) {
11+
min = s;
12+
}
13+
sum += s;
14+
}
15+
return (sum - min - max) / count;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [1491. Average Salary Excluding the Minimum and Maximum Salary](https://leetcode.com/problems/average-salary-excluding-the-minimum-and-maximum-salary/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n)$. $n$ is the length of `salary`.
7+
- Space Complexity: $O(1)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import java.util.Arrays;
2+
3+
class NumberOfSubsequencesThatSatisfyTheGivenSumCondition {
4+
static final int modulo = (int) Math.pow(10, 9) + 7;
5+
6+
public int numSubseq(int[] nums, int target) {
7+
Arrays.sort(nums);
8+
int[] tmp = new int[nums.length];
9+
tmp[0] = 1;
10+
for (int i = 1; i < nums.length; i++) {
11+
tmp[i] = (tmp[i - 1] * 2) % modulo;
12+
}
13+
int res = 0;
14+
int l = 0, r = nums.length - 1;
15+
while (l <= r) {
16+
if (nums[l] + nums[r] > target) {
17+
r--;
18+
} else {
19+
res = (res + tmp[r - l]) % modulo;
20+
l++;
21+
}
22+
}
23+
return res;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1498. Number of Subsequences That Satisfy the Given Sum Condition](https://leetcode.com/problems/number-of-subsequences-that-satisfy-the-given-sum-condition/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
class CountOddNumbersInAnIntervalRange {
2+
public int countOdds(int low, int high) {
3+
if ((low & 1) == 0)
4+
low++;
5+
return low > high ? 0 : (high - low) / 2 + 1;
6+
}
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [1523. Count Odd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/)
2+
3+
4+
5+
Complexity Analysis:
6+
7+
- Time Complexity: $O(1)$.
8+
- Space Complexity: $O(1)$.

1531.string-compression-ii/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1531. String Compression II](https://leetcode.com/problems/string-compression-ii/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import java.util.Arrays;
2+
3+
class StringCompressionIi {
4+
public int getLengthOfOptimalCompression(String s, int k) {
5+
int n = s.length();
6+
int[][] dp = new int[110][110];
7+
for (int[] d : dp)
8+
Arrays.fill(d, 9999);
9+
10+
dp[0][0] = 0;
11+
for (int i = 1; i <= n; i++) {
12+
for (int j = 0; j <= k; j++) {
13+
int cnt = 0, del = 0;
14+
for (int l = i; l >= 1; l--) {
15+
if (s.charAt(l - 1) == s.charAt(i - 1)) cnt++;
16+
else del++;
17+
if (j - del >= 0) {
18+
dp[i][j] = Math.min(dp[i][j], dp[l - 1][j - del] + 1 + (cnt >= 100 ? 3 : cnt >= 10 ? 2 : cnt >= 2 ? 1 : 0));
19+
}
20+
}
21+
if (j > 0) {
22+
dp[i][j] = Math.min(dp[i][j], dp[i - 1][j - 1]);
23+
}
24+
}
25+
}
26+
return dp[n][k];
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
class KthMissingPositiveNumber {
2+
public int findKthPositive(int[] arr, int k) {
3+
for (int i = 0; i < arr.length; i++) {
4+
if (arr[i] <= k)
5+
k++;
6+
}
7+
return k;
8+
}
9+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [1539. Kth Missing Positive Number](https://leetcode.com/problems/kth-missing-positive-number/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n)$. $n$ is the length of `arr`.
7+
- Space Complexity: $O(1)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.Stack;
2+
3+
class MakeTheStringGreat {
4+
public String makeGood(String s) {
5+
Stack<Character> stack = new Stack<>();
6+
for (char c : s.toCharArray()) {
7+
if (!stack.isEmpty() && Math.abs(stack.peek() - c) == 32) {
8+
stack.pop();
9+
} else {
10+
stack.push(c);
11+
}
12+
}
13+
14+
StringBuilder ans = new StringBuilder();
15+
for (char c : stack)
16+
ans.append(c);
17+
return ans.toString();
18+
}
19+
}

1544.make-the-string-great/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1544. Make The String Great](https://leetcode.com/problems/make-the-string-great/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class MatrixDiagonalSum {
2+
public int diagonalSum(int[][] mat) {
3+
int sum = 0;
4+
int n = mat.length;
5+
for (int i = 0; i < n; i++) {
6+
sum += mat[i][i];
7+
}
8+
for (int i = n - 1; i >= 0; i--) {
9+
sum += mat[i][n - 1 - i];
10+
}
11+
if (n % 2 == 0) {
12+
return sum;
13+
} else {
14+
int index = n / 2;
15+
sum -= mat[index][index];
16+
}
17+
return sum;
18+
}
19+
}

1572.matrix-diagonal-sum/README.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [1572. Matrix Diagonal Sum](https://leetcode.com/problems/matrix-diagonal-sum/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n)$.
7+
- Space Complexity: $O(1)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class MinimumTimeToMakeRopeColorful {
2+
public int minCost(String colors, int[] neededTime) {
3+
int totalTime = 0;
4+
int l = 0, r = 0;
5+
while (l < neededTime.length) {
6+
int currTotal = 0, currMax = 0;
7+
while (r < neededTime.length && colors.charAt(l) == colors.charAt(r)) {
8+
currTotal += neededTime[r];
9+
currMax = Math.max(currMax, neededTime[r]);
10+
r++;
11+
}
12+
totalTime += currTotal - currMax;
13+
l = r;
14+
}
15+
return totalTime;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1578. Minimum Time to Make Rope Colorful](https://leetcode.com/problems/minimum-time-to-make-rope-colorful/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import java.util.Arrays;
2+
3+
class BestTeamWithNoConflicts {
4+
public int bestTeamScore(int[] scores, int[] ages) {
5+
int n = scores.length;
6+
Integer[] order = new Integer[n];
7+
for (int i = 0; i < n; i++)
8+
order[i] = i;
9+
Arrays.sort(order, (a, b) -> ages[a] == ages[b] ? scores[a] - scores[b] : ages[a] - ages[b]);
10+
int[] dp = new int[n];
11+
int res = 0;
12+
for (int i = 0; i < dp.length; i++) {
13+
int currI = order[i];
14+
dp[i] = scores[currI];
15+
for (int j = 0; j < i; j++) {
16+
int currJ = order[j];
17+
if (scores[currI] >= scores[currJ])
18+
dp[i] = Math.max(dp[i], dp[j] + scores[currI]);
19+
}
20+
res = Math.max(res, dp[i]);
21+
}
22+
return res;
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# [1626. Best Team With No Conflicts](https://leetcode.com/problems/best-team-with-no-conflicts/)
2+
3+
4+
Complexity Analysis:
5+
6+
- Time Complexity: $O(n^2)$. $n$ is the length of `scores`.
7+
- Space Complexity: $O(n)$. We use an extra array order, so the space complexity is $O(n)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.util.PriorityQueue;
2+
3+
class FurthestBuildingYouCanReach {
4+
public int furthestBuilding(int[] heights, int bricks, int ladders) {
5+
PriorityQueue<Integer> minHeap = new PriorityQueue<>();
6+
for (int i = 0; i < heights.length - 1; i++) {
7+
int diff = heights[i + 1] - heights[i];
8+
if (diff <= 0) continue;
9+
minHeap.offer(diff);
10+
if (minHeap.size() > ladders && minHeap.peek() != null) {
11+
bricks -= minHeap.poll();
12+
}
13+
if (bricks < 0) return i;
14+
}
15+
return heights.length - 1;
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# [1642. Furthest Building You Can Reach](https://leetcode.com/problems/furthest-building-you-can-reach/)
2+
3+
# (https://assets.leetcode.com/uploads/2020/10/27/q4.gif)

0 commit comments

Comments
 (0)