Skip to content

Commit 576a6a8

Browse files
committed
Code is Added
1 parent bfc720d commit 576a6a8

File tree

20 files changed

+316
-0
lines changed

20 files changed

+316
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Arrays;
2+
3+
class MaximumProfitInJobScheduling {
4+
private int binarySearch(int[][] jobs, int right, int target) {
5+
int left = 0;
6+
while (left < right) {
7+
int mid = left + (right - left) / 2;
8+
if (jobs[mid][1] > target) {
9+
right = mid;
10+
} else {
11+
left = mid + 1;
12+
}
13+
}
14+
return left;
15+
}
16+
17+
public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
18+
int n = startTime.length;
19+
int[][] jobs = new int[n][];
20+
for (int i = 0; i < n; i++)
21+
jobs[i] = new int[]{startTime[i], endTime[i], profit[i]};
22+
Arrays.sort(jobs, (a, b) -> a[1] - b[1]);
23+
int[] dp = new int[n + 1];
24+
for (int i = 1; i <= n; i++) {
25+
int k = binarySearch(jobs, i - 1, jobs[i - 1][0]);
26+
dp[i] = Math.max(dp[i - 1], dp[k] + jobs[i - 1][2]);
27+
}
28+
return dp[n];
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [1235. Maximum Profit in Job Scheduling](https://leetcode.com/problems/maximum-profit-in-job-scheduling/)
2+
3+
4+
5+
Complexity Analysis:
6+
7+
- Time Complexity: $O(n\log^n)$. Sort is $O(n\log^n)$ and iteration + binary search is also $O(n\log^n)$. Thus, the total time complexity is $O(n\log^n)$.
8+
- Space Complexity: $O(n)$. We use $O(n)$ space to save dp.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.List;
2+
3+
class MaximumLengthOfAConcatenatedStringWithUniqueCharacters {
4+
private int max = 0;
5+
6+
private boolean isValid(String currentStr, String newStr) {
7+
int[] array = new int[26];
8+
for (int i = 0; i < newStr.length(); i++) {
9+
if (++array[newStr.charAt(i) - 'a'] == 2) return false;
10+
if (currentStr.contains(newStr.charAt(i) + ""))
11+
return false;
12+
}
13+
return true;
14+
}
15+
16+
private void backtrack(List<String> arr, String current, int start) {
17+
if (max < current.length())
18+
max = current.length();
19+
for (int i = start; i < arr.size(); i++) {
20+
if (!isValid(current, arr.get(i))) continue;
21+
backtrack(arr, current + arr.get(i), i + 1);
22+
}
23+
}
24+
25+
public int maxLength(List<String> arr) {
26+
backtrack(arr, "", 0);
27+
return max;
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1239. Maximum Length of a Concatenated String with Unique Characters](https://leetcode.com/problems/maximum-length-of-a-concatenated-string-with-unique-characters/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class NumberOfClosedIslands {
2+
private int m, n;
3+
private int res = 0;
4+
private final int[] dx = {-1, 0, 1, 0};
5+
private final int[] dy = {0, 1, 0, -1};
6+
7+
public int closedIsland(int[][] grid) {
8+
m = grid.length;
9+
n = grid[0].length;
10+
if (m <= 2 || n <= 2) return 0;
11+
for (int i = 1; i < m - 1; i++) {
12+
for (int j = 1; j < n - 1; j++) {
13+
if (grid[i][j] == 0)
14+
if (dfs(grid, i, j)) res++;
15+
}
16+
}
17+
18+
return res;
19+
}
20+
21+
private boolean dfs(int[][] grid, int x, int y) {
22+
if ((x == 0 || x == m - 1 || y == 0 || y == n - 1) && grid[x][y] == 0) {
23+
return false;
24+
}
25+
boolean f = true;
26+
grid[x][y] = 1;
27+
for (int i = 0; i < 4; i++) {
28+
int x1 = x + dx[i], y1 = y + dy[i];
29+
if (x1 < 0 || x1 >= m || y1 < 0 || y1 >= n || grid[x1][y1] == 1) continue;
30+
f = f & dfs(grid, x1, y1);
31+
}
32+
return f;
33+
}
34+
}
+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [1254. Number of Closed Islands](https://leetcode.com/problems/number-of-closed-islands/)
2+
3+
4+
5+
Complexity Analysis:
6+
7+
- Time Complexity: $O(m \times n)$.
8+
- Space Complexity: $O(m \times n)$.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1293. Shortest Path in a Grid with Obstacles Elimination](https://leetcode.com/problems/shortest-path-in-a-grid-with-obstacles-elimination/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import java.util.LinkedList;
2+
import java.util.Queue;
3+
4+
class ShortestPathInAGridWithObstaclesElimination {
5+
private static final int[][] directions = {{1, 0}, {-1, 0}, {0, -1}, {0, 1}};
6+
7+
public int shortestPath(int[][] grid, int k) {
8+
int m = grid.length, n = grid[0].length;
9+
boolean[][][] visited = new boolean[m][n][k + 1];
10+
11+
Queue<int[]> queue = new LinkedList<>();
12+
queue.add(new int[]{0, 0, k});
13+
14+
int dist = 0;
15+
16+
while (!queue.isEmpty()) {
17+
int size = queue.size();
18+
19+
while (size-- > 0) {
20+
int[] curr = queue.remove();
21+
int x = curr[0];
22+
int y = curr[1];
23+
int obs = curr[2];
24+
if (x == m - 1 && y == n - 1 && obs >= 0) return dist;
25+
26+
if (obs < 0 || visited[x][y][obs]) continue;
27+
visited[x][y][obs] = true;
28+
if (x - 1 >= 0) queue.add(new int[]{x - 1, y, obs - grid[x - 1][y]});
29+
if (x + 1 < m) queue.add(new int[]{x + 1, y, obs - grid[x + 1][y]});
30+
if (y - 1 >= 0) queue.add(new int[]{x, y - 1, obs - grid[x][y - 1]});
31+
if (y + 1 < n) queue.add(new int[]{x, y + 1, obs - grid[x][y + 1]});
32+
}
33+
dist++;
34+
}
35+
return -1;
36+
}
37+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import java.util.ArrayList;
2+
import java.util.List;
3+
4+
class NumberOfOperationsToMakeNetworkConnected {
5+
List<Integer>[] edges;
6+
boolean[] visited;
7+
8+
public int makeConnected(int n, int[][] connections) {
9+
if (connections.length < n - 1) {
10+
return -1;
11+
}
12+
13+
edges = new List[n];
14+
for (int i = 0; i < n; i++) {
15+
edges[i] = new ArrayList<>();
16+
}
17+
for (int[] connection : connections) {
18+
edges[connection[0]].add(connection[1]);
19+
edges[connection[1]].add(connection[0]);
20+
}
21+
22+
visited = new boolean[n];
23+
int res = 0;
24+
for (int i = 0; i < n; i++) {
25+
if (!visited[i]) {
26+
dfs(i);
27+
res++;
28+
}
29+
}
30+
31+
return res - 1;
32+
}
33+
34+
private void dfs(int n) {
35+
visited[n] = true;
36+
for (int v : edges[n]) {
37+
if (!visited[v]) {
38+
dfs(v);
39+
}
40+
}
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# [1319. Number of Operations to Make Network Connected](https://leetcode.com/problems/number-of-operations-to-make-network-connected/)
2+
3+
4+
5+
Complexity Analysis:
6+
7+
- Time Complexity: $O(n+m)$. $m$ is the length of `connections`.
8+
- Space Complexity: $O(n+m)$. It costs $O(m)$ to save all edges and costs $O(n)$ for DFS stack.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Maximum69Number {
2+
public int maximum69Number (int num) {
3+
int numCopy = num;
4+
int indexSix = -1;
5+
int currDigit = 0;
6+
7+
while (numCopy > 0) {
8+
if (numCopy % 10 == 6)
9+
indexSix = currDigit;
10+
numCopy /= 10;
11+
currDigit++;
12+
}
13+
14+
return indexSix == -1 ? num : num + 3 * (int) Math.pow(10, indexSix);
15+
}
16+
}

1323.maximum-69-number/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1323. Maximum 69 Number](https://leetcode.com/problems/maximum-69-number/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class BreakAPalindrome {
2+
public String breakPalindrome(String palindrome) {
3+
if (palindrome.length() <= 1) return "";
4+
char[] chars = palindrome.toCharArray();
5+
int len = palindrome.length();
6+
for (int i = 0; i < len; i++) {
7+
if (i == len - 1) {
8+
chars[i] = 'b';
9+
}
10+
if (palindrome.charAt(i) != 'a' && i != (len / 2)) {
11+
chars[i] = 'a';
12+
break;
13+
}
14+
}
15+
return String.valueOf(chars);
16+
}
17+
}

1328.break-a-palindrome/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1328. Break a Palindrome](https://leetcode.com/problems/break-a-palindrome/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1329. Sort the Matrix Diagonally](https://leetcode.com/problems/sort-the-matrix-diagonally/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
class SortTheMatrixDiagonally {
2+
private void pass(int[][] a, int row, int col, int n, int m) {
3+
int[] arr = new int[101];
4+
int i = row, j = col;
5+
while (row < n && col < m) {
6+
arr[a[row++][col++]]++;
7+
}
8+
for (int k = 0; k < 101; k++) {
9+
if (arr[k] > 0) {
10+
while (arr[k] != 0) {
11+
a[i++][j++] = k;
12+
arr[k]--;
13+
}
14+
}
15+
}
16+
}
17+
18+
public int[][] diagonalSort(int[][] mat) {
19+
int row = mat.length;
20+
int col = mat[0].length;
21+
int count = 0, i = 0, k = 0;
22+
while (count < row + col) {
23+
if (i == row - 1 && k < col) k++;
24+
if (i != row - 1) i++;
25+
pass(mat, row - 1 - i, k, row, col);
26+
count++;
27+
}
28+
return mat;
29+
}
30+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class MinimumDifficultyOfAJobSchedule {
2+
public int minDifficulty(int[] jobDifficulty, int d) {
3+
int n = jobDifficulty.length;
4+
if (d > n) return -1;
5+
int[][] dp = new int[d + 1][n + 1];
6+
for (int i = 1; i <= n; i++)
7+
dp[1][i] = Math.max(dp[1][i - 1], jobDifficulty[i - 1]);
8+
for (int i = 2; i <= d; i++) {
9+
for (int j = i; j <= n; j++) {
10+
dp[i][j] = Integer.MAX_VALUE;
11+
int currMax = 0;
12+
for (int k = j; k >= i; k--) {
13+
currMax = Math.max(currMax, jobDifficulty[k - 1]);
14+
dp[i][j] = Math.min(dp[i][j], dp[i - 1][k - 1] + currMax);
15+
}
16+
}
17+
}
18+
return dp[d][n];
19+
}
20+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1335. Minimum Difficulty of a Job Schedule](https://leetcode.com/problems/minimum-difficulty-of-a-job-schedule/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# [1338. Reduce Array Size to The Half](https://leetcode.com/problems/reduce-array-size-to-the-half/)
2+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import java.util.ArrayList;
2+
import java.util.Collections;
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
6+
class ReduceArraySizeToTheHalf {
7+
public int minSetSize(int[] arr) {
8+
Map<Integer, Integer> map = new HashMap<>();
9+
for (int i : arr) {
10+
map.put(i, map.getOrDefault(i, 0) + 1);
11+
}
12+
ArrayList<Integer> arrayList = new ArrayList<>(map.values());
13+
arrayList.sort(Collections.reverseOrder());
14+
15+
int currentSize = 0, minimumSet = 0;
16+
while (currentSize < arr.length / 2) {
17+
currentSize += arrayList.remove(0);
18+
minimumSet++;
19+
}
20+
21+
return minimumSet;
22+
}
23+
}

0 commit comments

Comments
 (0)