Skip to content

Commit d5ed891

Browse files
authored
Added tasks 1331, 1332, 1333, 1334, 1335.
1 parent 34a6369 commit d5ed891

File tree

15 files changed

+568
-0
lines changed

15 files changed

+568
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package g1301_1400.s1331_rank_transform_of_an_array;
2+
3+
// #Easy #Array #Hash_Table #Sorting #2022_03_19_Time_22_ms_(98.50%)_Space_58.4_MB_(93.72%)
4+
5+
import java.util.Arrays;
6+
import java.util.HashMap;
7+
8+
@SuppressWarnings({})
9+
public class Solution {
10+
public int[] arrayRankTransform(int[] arr) {
11+
12+
int[] tmp = Arrays.copyOf(arr, arr.length);
13+
Arrays.sort(tmp);
14+
HashMap<Integer, Integer> mp = new HashMap<>();
15+
int i = 1;
16+
for (Integer x : tmp) {
17+
if (!mp.containsKey(x)) {
18+
mp.put(x, i++);
19+
}
20+
}
21+
22+
i = 0;
23+
for (Integer x : arr) {
24+
arr[i++] = mp.get(x);
25+
}
26+
return arr;
27+
}
28+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
1331\. Rank Transform of an Array
2+
3+
Easy
4+
5+
Given an array of integers `arr`, replace each element with its rank.
6+
7+
The rank represents how large the element is. The rank has the following rules:
8+
9+
* Rank is an integer starting from 1.
10+
* The larger the element, the larger the rank. If two elements are equal, their rank must be the same.
11+
* Rank should be as small as possible.
12+
13+
**Example 1:**
14+
15+
**Input:** arr = [40,10,20,30]
16+
17+
**Output:** [4,1,2,3]
18+
19+
**Explanation:**: 40 is the largest element. 10 is the smallest. 20 is the second smallest. 30 is the third smallest.
20+
21+
**Example 2:**
22+
23+
**Input:** arr = [100,100,100]
24+
25+
**Output:** [1,1,1]
26+
27+
**Explanation:**: Same elements share the same rank.
28+
29+
**Example 3:**
30+
31+
**Input:** arr = [37,12,28,9,100,56,80,5,12]
32+
33+
**Output:** [5,3,4,2,8,6,7,1,3]
34+
35+
**Constraints:**
36+
37+
* <code>0 <= arr.length <= 10<sup>5</sup></code>
38+
* <code>-10<sup>9</sup> <= arr[i] <= 10<sup>9</sup></code>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package g1301_1400.s1332_remove_palindromic_subsequences;
2+
3+
// #Easy #String #Two_Pointers #2022_03_19_Time_0_ms_(100.00%)_Space_39.9_MB_(78.40%)
4+
5+
public class Solution {
6+
public int removePalindromeSub(String s) {
7+
if (s.isEmpty()) {
8+
return 0;
9+
}
10+
if (s.equals((new StringBuilder(s)).reverse().toString())) {
11+
return 1;
12+
}
13+
return 2;
14+
}
15+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
1332\. Remove Palindromic Subsequences
2+
3+
Easy
4+
5+
You are given a string `s` consisting **only** of letters `'a'` and `'b'`. In a single step you can remove one **palindromic subsequence** from `s`.
6+
7+
Return _the **minimum** number of steps to make the given string empty_.
8+
9+
A string is a **subsequence** of a given string if it is generated by deleting some characters of a given string without changing its order. Note that a subsequence does **not** necessarily need to be contiguous.
10+
11+
A string is called **palindrome** if is one that reads the same backward as well as forward.
12+
13+
**Example 1:**
14+
15+
**Input:** s = "ababa"
16+
17+
**Output:** 1
18+
19+
**Explanation:** s is already a palindrome, so its entirety can be removed in a single step.
20+
21+
**Example 2:**
22+
23+
**Input:** s = "abb"
24+
25+
**Output:** 2
26+
27+
**Explanation:** "abb" -> "bb" -> "". Remove palindromic subsequence "a" then "bb".
28+
29+
**Example 3:**
30+
31+
**Input:** s = "baabb"
32+
33+
**Output:** 2
34+
35+
**Explanation:** "baabb" -> "b" -> "". Remove palindromic subsequence "baab" then "b".
36+
37+
**Constraints:**
38+
39+
* `1 <= s.length <= 1000`
40+
* `s[i]` is either `'a'` or `'b'`.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1301_1400.s1333_filter_restaurants_by_vegan_friendly_price_and_distance;
2+
3+
// #Medium #Array #Sorting #2022_03_19_Time_10_ms_(55.43%)_Space_58_MB_(52.00%)
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.stream.Collectors;
8+
9+
public class Solution {
10+
public List<Integer> filterRestaurants(
11+
int[][] restaurants, int veganFriendly, int maxPrice, int maxDistance) {
12+
List<int[]> list = new ArrayList<>();
13+
for (int[] restaurant : restaurants) {
14+
if (((veganFriendly == 1 && restaurant[2] == 1) || veganFriendly == 0)
15+
&& restaurant[3] <= maxPrice
16+
&& restaurant[4] <= maxDistance) {
17+
list.add(restaurant);
18+
}
19+
}
20+
list.sort((a, b) -> b[1] - a[1] == 0 ? b[0] - a[0] : b[1] - a[1]);
21+
return list.stream().map(restaurant -> restaurant[0]).collect(Collectors.toList());
22+
}
23+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
1333\. Filter Restaurants by Vegan-Friendly, Price and Distance
2+
3+
Medium
4+
5+
Given the array `restaurants` where <code>restaurants[i] = [id<sub>i</sub>, rating<sub>i</sub>, veganFriendly<sub>i</sub>, price<sub>i</sub>, distance<sub>i</sub>]</code>. You have to filter the restaurants using three filters.
6+
7+
The `veganFriendly` filter will be either _true_ (meaning you should only include restaurants with <code>veganFriendly<sub>i</sub></code> set to true) or _false_ (meaning you can include any restaurant). In addition, you have the filters `maxPrice` and `maxDistance` which are the maximum value for price and distance of restaurants you should consider respectively.
8+
9+
Return the array of restaurant _**IDs**_ after filtering, ordered by **rating** from highest to lowest. For restaurants with the same rating, order them by _**id**_ from highest to lowest. For simplicity <code>veganFriendly<sub>i</sub></code> and `veganFriendly` take value _1_ when it is _true_, and _0_ when it is _false_.
10+
11+
**Example 1:**
12+
13+
**Input:** restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 1, maxPrice = 50, maxDistance = 10
14+
15+
**Output:** [3,1,5]
16+
17+
**Explanation:** The restaurants are:
18+
19+
Restaurant 1 [id=1, rating=4, veganFriendly=1, price=40, distance=10]
20+
21+
Restaurant 2 [id=2, rating=8, veganFriendly=0, price=50, distance=5]
22+
23+
Restaurant 3 [id=3, rating=8, veganFriendly=1, price=30, distance=4]
24+
25+
Restaurant 4 [id=4, rating=10, veganFriendly=0, price=10, distance=3]
26+
27+
Restaurant 5 [id=5, rating=1, veganFriendly=1, price=15, distance=1]
28+
29+
After filter restaurants with veganFriendly = 1, maxPrice = 50 and maxDistance = 10 we have restaurant 3, restaurant 1 and restaurant 5 (ordered by rating from highest to lowest).
30+
31+
**Example 2:**
32+
33+
**Input:** restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 0, maxPrice = 50, maxDistance = 10
34+
35+
**Output:** [4,3,2,1,5]
36+
37+
**Explanation:** The restaurants are the same as in example 1, but in this case the filter veganFriendly = 0, therefore all restaurants are considered.
38+
39+
**Example 3:**
40+
41+
**Input:** restaurants = [[1,4,1,40,10],[2,8,0,50,5],[3,8,1,30,4],[4,10,0,10,3],[5,1,1,15,1]], veganFriendly = 0, maxPrice = 30, maxDistance = 3
42+
43+
**Output:** [4,5]
44+
45+
**Constraints:**
46+
47+
* `1 <= restaurants.length <= 10^4`
48+
* `restaurants[i].length == 5`
49+
* <code>1 <= id<sub>i</sub>, rating<sub>i</sub>, price<sub>i</sub>, distance<sub>i</sub> <= 10^5</code>
50+
* `1 <= maxPrice, maxDistance <= 10^5`
51+
* <code>veganFriendly<sub>i</sub></code> and `veganFriendly` are 0 or 1.
52+
* All <code>id<sub>i</sub></code> are distinct.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package g1301_1400.s1334_find_the_city_with_the_smallest_number_of_neighbors_at_a_threshold_distance;
2+
3+
// #Medium #Dynamic_Programming #Graph #Shortest_Path
4+
// #2022_03_19_Time_21_ms_(49.75%)_Space_47_MB_(36.59%)
5+
6+
import java.util.ArrayList;
7+
import java.util.HashMap;
8+
import java.util.List;
9+
10+
public class Solution {
11+
public int findTheCity(int n, int[][] edges, int maxDist) {
12+
int[][] graph = new int[n][n];
13+
for (int[] edge : edges) {
14+
graph[edge[0]][edge[1]] = edge[2];
15+
graph[edge[1]][edge[0]] = edge[2];
16+
}
17+
return fllowdWarshall(graph, n, maxDist);
18+
}
19+
20+
private int fllowdWarshall(int[][] graph, int n, int maxDist) {
21+
int inf = 10001;
22+
int[][] dist = new int[n][n];
23+
for (int i = 0; i < n; i++) {
24+
for (int j = 0; j < n; j++) {
25+
if (i != j && graph[i][j] == 0) {
26+
dist[i][j] = inf;
27+
} else {
28+
dist[i][j] = graph[i][j];
29+
}
30+
}
31+
}
32+
for (int k = 0; k < n; k++) {
33+
for (int i = 0; i < n; i++) {
34+
for (int j = 0; j < n; j++) {
35+
if (dist[i][k] + dist[k][j] < dist[i][j]) {
36+
dist[i][j] = dist[i][k] + dist[k][j];
37+
}
38+
}
39+
}
40+
}
41+
return getList(dist, n, maxDist);
42+
}
43+
44+
private int getList(int[][] dist, int n, int maxDist) {
45+
HashMap<Integer, List<Integer>> map = new HashMap<>();
46+
for (int i = 0; i < n; i++) {
47+
for (int j = 0; j < n; j++) {
48+
49+
if (!map.containsKey(i)) {
50+
map.put(i, new ArrayList<>());
51+
if (dist[i][j] <= maxDist && i != j) {
52+
map.get(i).add(j);
53+
}
54+
} else if (map.containsKey(i) && dist[i][j] <= maxDist && i != j) {
55+
56+
map.get(i).add(j);
57+
}
58+
}
59+
}
60+
int numOfEle = Integer.MAX_VALUE;
61+
int ans = 0;
62+
for (int i = 0; i < n; i++) {
63+
if (numOfEle >= map.get(i).size()) {
64+
numOfEle = Math.min(numOfEle, map.get(i).size());
65+
ans = i;
66+
}
67+
}
68+
return ans;
69+
}
70+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
1334\. Find the City With the Smallest Number of Neighbors at a Threshold Distance
2+
3+
Medium
4+
5+
There are `n` cities numbered from `0` to `n-1`. Given the array `edges` where <code>edges[i] = [from<sub>i</sub>, to<sub>i</sub>, weight<sub>i</sub>]</code> represents a bidirectional and weighted edge between cities <code>from<sub>i</sub></code> and <code>to<sub>i</sub></code>, and given the integer `distanceThreshold`.
6+
7+
Return the city with the smallest number of cities that are reachable through some path and whose distance is **at most** `distanceThreshold`, If there are multiple such cities, return the city with the greatest number.
8+
9+
Notice that the distance of a path connecting cities _**i**_ and _**j**_ is equal to the sum of the edges' weights along that path.
10+
11+
**Example 1:**
12+
13+
![](https://assets.leetcode.com/uploads/2020/01/16/find_the_city_01.png)
14+
15+
**Input:** n = 4, edges = [[0,1,3],[1,2,1],[1,3,4],[2,3,1]], distanceThreshold = 4
16+
17+
**Output:** 3
18+
19+
**Explanation:** The figure above describes the graph.
20+
21+
The neighboring cities at a distanceThreshold = 4 for each city are:
22+
23+
City 0 -> [City 1, City 2]
24+
25+
City 1 -> [City 0, City 2, City 3]
26+
27+
City 2 -> [City 0, City 1, City 3]
28+
29+
City 3 -> [City 1, City 2]
30+
31+
Cities 0 and 3 have 2 neighboring cities at a distanceThreshold = 4, but we have to return city 3 since it has the greatest number.
32+
33+
**Example 2:**
34+
35+
![](https://assets.leetcode.com/uploads/2020/01/16/find_the_city_02.png)
36+
37+
**Input:** n = 5, edges = [[0,1,2],[0,4,8],[1,2,3],[1,4,2],[2,3,1],[3,4,1]], distanceThreshold = 2
38+
39+
**Output:** 0
40+
41+
**Explanation:** The figure above describes the graph.
42+
43+
The neighboring cities at a distanceThreshold = 2 for each city are:
44+
45+
City 0 -> [City 1]
46+
47+
City 1 -> [City 0, City 4]
48+
49+
City 2 -> [City 3, City 4]
50+
51+
City 3 -> [City 2, City 4]
52+
53+
City 4 -> [City 1, City 2, City 3]
54+
55+
The city 0 has 1 neighboring city at a distanceThreshold = 2.
56+
57+
**Constraints:**
58+
59+
* `2 <= n <= 100`
60+
* `1 <= edges.length <= n * (n - 1) / 2`
61+
* `edges[i].length == 3`
62+
* <code>0 <= from<sub>i</sub> < to<sub>i</sub> < n</code>
63+
* <code>1 <= weight<sub>i</sub>, distanceThreshold <= 10^4</code>
64+
* All pairs <code>(from<sub>i</sub>, to<sub>i</sub>)</code> are distinct.
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package g1301_1400.s1335_minimum_difficulty_of_a_job_schedule;
2+
3+
// #Hard #Array #Dynamic_Programming #2022_03_19_Time_11_ms_(79.28%)_Space_41.6_MB_(59.99%)
4+
5+
public class Solution {
6+
public int minDifficulty(int[] jobDifficulty, int d) {
7+
int totalJobs = jobDifficulty.length;
8+
if (totalJobs < d) {
9+
return -1;
10+
}
11+
12+
int maxJobsOneDay = totalJobs - d + 1;
13+
int[] map = new int[totalJobs];
14+
15+
int maxDiff = Integer.MIN_VALUE;
16+
for (int k = totalJobs - 1; k > totalJobs - 1 - maxJobsOneDay; k--) {
17+
maxDiff = Math.max(maxDiff, jobDifficulty[k]);
18+
map[k] = maxDiff;
19+
}
20+
21+
for (int day = d - 1; day > 0; day--) {
22+
23+
int maxEndIndex = (totalJobs - 1) - (d - day);
24+
int maxStartIndex = maxEndIndex - maxJobsOneDay + 1;
25+
26+
for (int startIndex = maxStartIndex; startIndex <= maxEndIndex; startIndex++) {
27+
map[startIndex] = Integer.MAX_VALUE;
28+
int maxDiffOfTheDay = Integer.MIN_VALUE;
29+
for (int endIndex = startIndex; endIndex <= maxEndIndex; endIndex++) {
30+
maxDiffOfTheDay = Math.max(maxDiffOfTheDay, jobDifficulty[endIndex]);
31+
32+
int totalDiff = maxDiffOfTheDay + map[endIndex + 1];
33+
34+
map[startIndex] = Math.min(map[startIndex], totalDiff);
35+
}
36+
}
37+
}
38+
39+
return map[0];
40+
}
41+
}

0 commit comments

Comments
 (0)