Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package g1201_1300.s1233_remove_sub_folders_from_the_filesystem;

// #Medium #Array #String #Trie #2022_03_12_Time_32_ms_(96.54%)_Space_51.8_MB_(87.12%)

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Solution {
public List<String> removeSubfolders(String[] folder) {
Set<String> paths = new HashSet<>();
Collections.addAll(paths, folder);

List<String> res = new ArrayList<>();
for (String f : folder) {
int lastSlash = f.lastIndexOf("/");
boolean isSub = false;
while (lastSlash > 0) {
String upperDir = f.substring(0, lastSlash);
if (paths.contains(upperDir)) {
isSub = true;
break;
}
lastSlash = upperDir.lastIndexOf("/");
}
if (!isSub) {
res.add(f);
}
}

return res;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
1233\. Remove Sub-Folders from the Filesystem

Medium

Given a list of folders `folder`, return _the folders after removing all **sub-folders** in those folders_. You may return the answer in **any order**.

If a `folder[i]` is located within another `folder[j]`, it is called a **sub-folder** of it.

The format of a path is one or more concatenated strings of the form: `'/'` followed by one or more lowercase English letters.

* For example, `"/leetcode"` and `"/leetcode/problems"` are valid paths while an empty string and `"/"` are not.

**Example 1:**

**Input:** folder = ["/a","/a/b","/c/d","/c/d/e","/c/f"]

**Output:** ["/a","/c/d","/c/f"]

**Explanation:** Folders "/a/b" is a subfolder of "/a" and "/c/d/e" is inside of folder "/c/d" in our filesystem.

**Example 2:**

**Input:** folder = ["/a","/a/b/c","/a/b/d"]

**Output:** ["/a"]

**Explanation:** Folders "/a/b/c" and "/a/b/d" will be removed because they are subfolders of "/a".

**Example 3:**

**Input:** folder = ["/a/b/c","/a/b/ca","/a/b/d"]

**Output:** ["/a/b/c","/a/b/ca","/a/b/d"]

**Constraints:**

* <code>1 <= folder.length <= 4 * 10<sup>4</sup></code>
* `2 <= folder[i].length <= 100`
* `folder[i]` contains only lowercase letters and `'/'`.
* `folder[i]` always starts with the character `'/'`.
* Each folder name is **unique**.
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package g1201_1300.s1234_replace_the_substring_for_balanced_string;

// #Medium #String #Sliding_Window #2022_03_12_Time_7_ms_(89.84%)_Space_44.4_MB_(33.77%)

public class Solution {
public int balancedString(String s) {
int n = s.length();
int ans = n;
int excess = 0;
int[] cnt = new int[128];
cnt['Q'] = cnt['W'] = cnt['E'] = cnt['R'] = -n / 4;
for (char ch : s.toCharArray()) {
if (++cnt[ch] == 1) {
excess++;
}
}
if (excess == 0) {
return 0;
}
for (int i = 0, j = 0; i < n; i++) {
if (--cnt[s.charAt(i)] == 0) {
excess--;
}
while (excess == 0) {
if (++cnt[s.charAt(j)] == 1) {
excess++;
}
ans = Math.min(i - j + 1, ans);
j++;
}
}

return ans;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
1234\. Replace the Substring for Balanced String

Medium

You are given a string s of length `n` containing only four kinds of characters: `'Q'`, `'W'`, `'E'`, and `'R'`.

A string is said to be **balanced** if each of its characters appears `n / 4` times where `n` is the length of the string.

Return _the minimum length of the substring that can be replaced with **any** other string of the same length to make_ `s` _**balanced**_. If s is already **balanced**, return `0`.

**Example 1:**

**Input:** s = "QWER"

**Output:** 0

**Explanation:** s is already balanced.

**Example 2:**

**Input:** s = "QQWE"

**Output:** 1

**Explanation:** We need to replace a 'Q' to 'R', so that "RQWE" (or "QRWE") is balanced.

**Example 3:**

**Input:** s = "QQQW"

**Output:** 2

**Explanation:** We can replace the first "QQ" to "ER".

**Constraints:**

* `n == s.length`
* <code>4 <= n <= 10<sup>5</sup></code>
* `n` is a multiple of `4`.
* `s` contains only `'Q'`, `'W'`, `'E'`, and `'R'`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package g1201_1300.s1235_maximum_profit_in_job_scheduling;

// #Hard #Array #Dynamic_Programming #Sorting #Binary_Search
// #2022_03_12_Time_27_ms_(89.19%)_Space_73.8_MB_(49.40%)

import java.util.Arrays;
import java.util.Comparator;

public class Solution {

public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
int n = startTime.length;
int[][] time = new int[n][3];

for (int i = 0; i < n; i++) {
time[i][0] = startTime[i];
time[i][1] = endTime[i];
time[i][2] = profit[i];
}
Arrays.sort(time, Comparator.comparingDouble(a -> a[1]));

int[][] maxP = new int[n][2];
int lastPos = -1;
int currProfit;
for (int i = 0; i < n; i++) {
currProfit = time[i][2];
for (int j = lastPos; j >= 0; j--) {
if (maxP[j][1] <= time[i][0]) {
currProfit += maxP[j][0];
break;
}
}
if (lastPos == -1 || currProfit > maxP[lastPos][0]) {
lastPos++;
maxP[lastPos][0] = currProfit;
maxP[lastPos][1] = time[i][1];
}
}

return maxP[lastPos][0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
1235\. Maximum Profit in Job Scheduling

Hard

We have `n` jobs, where every job is scheduled to be done from `startTime[i]` to `endTime[i]`, obtaining a profit of `profit[i]`.

You're given the `startTime`, `endTime` and `profit` arrays, return the maximum profit you can take such that there are no two jobs in the subset with overlapping time range.

If you choose a job that ends at time `X` you will be able to start another job that starts at time `X`.

**Example 1:**

**![](https://assets.leetcode.com/uploads/2019/10/10/sample1_1584.png)**

**Input:** startTime = [1,2,3,3], endTime = [3,4,5,6], profit = [50,10,40,70]

**Output:** 120

**Explanation:** The subset chosen is the first and fourth job. Time range [1-3]+[3-6] , we get profit of 120 = 50 + 70.

**Example 2:**

**![](https://assets.leetcode.com/uploads/2019/10/10/sample22_1584.png)**

**Input:** startTime = [1,2,3,4,6], endTime = [3,5,10,6,9], profit = [20,20,100,70,60]

**Output:** 150

**Explanation:** The subset chosen is the first, fourth and fifth job. Profit obtained 150 = 20 + 70 + 60.

**Example 3:**

**![](https://assets.leetcode.com/uploads/2019/10/10/sample3_1584.png)**

**Input:** startTime = [1,1,1], endTime = [2,3,4], profit = [5,6,4]

**Output:** 6

**Constraints:**

* <code>1 <= startTime.length == endTime.length == profit.length <= 5 * 10<sup>4</sup></code>
* <code>1 <= startTime[i] < endTime[i] <= 10<sup>9</sup></code>
* <code>1 <= profit[i] <= 10<sup>4</sup></code>
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package g1201_1300.s1237_find_positive_integer_solution_for_a_given_equation;

abstract class CustomFunction {
public abstract int f(int x, int y);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package g1201_1300.s1237_find_positive_integer_solution_for_a_given_equation;

// #Medium #Math #Binary_Search #Two_Pointers #Interactive
// #2022_03_12_Time_2_ms_(79.60%)_Space_41.7_MB_(45.96%)

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Solution {

public List<List<Integer>> findSolution(CustomFunction customfunction, int z) {
List<List<Integer>> result = new ArrayList<>();
int x = 1;
int y = 1000;
while (x < 1001 && y > 0) {
int functionResult = customfunction.f(x, y);
if (functionResult < z) {
x++;
} else if (functionResult > z) {
y--;
} else {
result.add(Arrays.asList(x++, y--));
}
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
1237\. Find Positive Integer Solution for a Given Equation

Medium

Given a callable function `f(x, y)` **with a hidden formula** and a value `z`, reverse engineer the formula and return _all positive integer pairs_ `x` _and_ `y` _where_ `f(x,y) == z`. You may return the pairs in any order.

While the exact formula is hidden, the function is monotonically increasing, i.e.:

* `f(x, y) < f(x + 1, y)`
* `f(x, y) < f(x, y + 1)`

The function interface is defined like this:

interface CustomFunction { public: // Returns some positive integer f(x, y) for two positive integers x and y based on a formula. int f(int x, int y); };

We will judge your solution as follows:

* The judge has a list of `9` hidden implementations of `CustomFunction`, along with a way to generate an **answer key** of all valid pairs for a specific `z`.
* The judge will receive two inputs: a `function_id` (to determine which implementation to test your code with), and the target `z`.
* The judge will call your `findSolution` and compare your results with the **answer key**.
* If your results match the **answer key**, your solution will be `
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package g1201_1300.s1238_circular_permutation_in_binary_representation;

// #Medium #Math #Bit_Manipulation #Backtracking
// #2022_03_12_Time_4_ms_(100.00%)_Space_49.9_MB_(90.59%)

import java.util.ArrayList;
import java.util.List;

public class Solution {
public List<Integer> circularPermutation(int n, int start) {

List<Integer> l1 = new ArrayList<>();
for (int i = 0; i < (1 << n); i++) {
l1.add(start ^ (i ^ (i >> 1)));
}
return l1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
1238\. Circular Permutation in Binary Representation

Medium

Given 2 integers `n` and `start`. Your task is return **any** permutation `p` of `(0,1,2.....,2^n -1)` such that :

* `p[0] = start`
* `p[i]` and `p[i+1]` differ by only one bit in their binary representation.
* `p[0]` and `p[2^n -1]` must also differ by only one bit in their binary representation.

**Example 1:**

**Input:** n = 2, start = 3

**Output:** [3,2,0,1]

**Explanation:** The binary representation of the permutation is (11,10,00,01). All the adjacent element differ by one bit. Another valid permutation is [3,1,0,2]

**Example 2:**

**Input:** n = 3, start = 2

**Output:** [2,6,7,5,4,0,1,3]

**Explanation:** The binary representation of the permutation is (010,110,111,101,100,000,001,011).

**Constraints:**

* `1 <= n <= 16`
* `0 <= start < 2 ^ n`
16 changes: 16 additions & 0 deletions src/test/java/com_github_leetcode/CommonUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,22 @@ public static boolean compareArray(int[] arr1, int[] arr2) {
return true;
}

public static boolean compareArray(List<Integer> arr1, List<Integer> arr2) {
for (int i : arr1) {
boolean include = false;
for (int j : arr2) {
if (i == j) {
include = true;
break;
}
}
if (!include) {
return false;
}
}
return true;
}

public static boolean compareMatrix(List<List<Integer>> mt1, List<List<Integer>> mt2) {
for (List<Integer> i : mt1) {
boolean include = false;
Expand Down
Loading