Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ce75fe7

Browse files
authored
Added tasks 1471, 1472, 1473.
1 parent cbc059f commit ce75fe7

File tree

9 files changed

+370
-0
lines changed

9 files changed

+370
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package g1401_1500.s1471_the_k_strongest_values_in_an_array;
2+
3+
// #Medium #Array #Sorting #Two_Pointers #2022_03_29_Time_37_ms_(88.20%)_Space_81.7_MB_(70.81%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int[] getStrongest(int[] arr, int k) {
9+
Arrays.sort(arr);
10+
int[] array = new int[k];
11+
int median = arr[(arr.length - 1) / 2];
12+
int start = 0;
13+
int end = arr.length - 1;
14+
for (int i = 0; i < k; i++) {
15+
if (Math.abs(arr[end] - median) >= Math.abs(arr[start] - median)) {
16+
array[i] = arr[end];
17+
end -= 1;
18+
} else {
19+
array[i] = arr[start];
20+
start += 1;
21+
}
22+
}
23+
return array;
24+
}
25+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
1471\. The k Strongest Values in an Array
2+
3+
Medium
4+
5+
Given an array of integers `arr` and an integer `k`.
6+
7+
A value `arr[i]` is said to be stronger than a value `arr[j]` if `|arr[i] - m| > |arr[j] - m|` where `m` is the **median** of the array.
8+
If `|arr[i] - m| == |arr[j] - m|`, then `arr[i]` is said to be stronger than `arr[j]` if `arr[i] > arr[j]`.
9+
10+
Return _a list of the strongest `k`_ values in the array. return the answer **in any arbitrary order**.
11+
12+
**Median** is the middle value in an ordered integer list. More formally, if the length of the list is n, the median is the element in position `((n - 1) / 2)` in the sorted list **(0-indexed)**.
13+
14+
* For `arr = [6, -3, 7, 2, 11]`, `n = 5` and the median is obtained by sorting the array `arr = [-3, 2, 6, 7, 11]` and the median is `arr[m]` where `m = ((5 - 1) / 2) = 2`. The median is `6`.
15+
* For `arr = [-7, 22, 17, 3]`, `n = 4` and the median is obtained by sorting the array `arr = [-7, 3, 17, 22]` and the median is `arr[m]` where `m = ((4 - 1) / 2) = 1`. The median is `3`.
16+
17+
**Example 1:**
18+
19+
**Input:** arr = [1,2,3,4,5], k = 2
20+
21+
**Output:** [5,1]
22+
23+
**Explanation:** Median is 3, the elements of the array sorted by the strongest are [5,1,4,2,3]. The strongest 2 elements are [5, 1]. [1, 5] is also **accepted** answer. Please note that although |5 - 3| == |1 - 3| but 5 is stronger than 1 because 5 > 1.
24+
25+
**Example 2:**
26+
27+
**Input:** arr = [1,1,3,5,5], k = 2
28+
29+
**Output:** [5,5]
30+
31+
**Explanation:** Median is 3, the elements of the array sorted by the strongest are [5,5,1,1,3]. The strongest 2 elements are [5, 5].
32+
33+
**Example 3:**
34+
35+
**Input:** arr = [6,7,11,7,6,8], k = 5
36+
37+
**Output:** [11,8,6,6,7]
38+
39+
**Explanation:** Median is 7, the elements of the array sorted by the strongest are [11,8,6,6,7,7]. Any permutation of [11,8,6,6,7] is **accepted**.
40+
41+
**Constraints:**
42+
43+
* <code>1 <= arr.length <= 10<sup>5</sup></code>
44+
* <code>-10<sup>5</sup> <= arr[i] <= 10<sup>5</sup></code>
45+
* `1 <= k <= arr.length`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package g1401_1500.s1472_design_browser_history;
2+
3+
// #Medium #Array #Stack #Design #Linked_List #Data_Stream #Doubly_Linked_List
4+
// #2022_03_29_Time_76_ms_(82.33%)_Space_86.4_MB_(14.42%)
5+
6+
public class BrowserHistory {
7+
8+
static class Node {
9+
Node prev;
10+
Node next;
11+
String url;
12+
13+
Node(String url) {
14+
this.url = url;
15+
this.prev = null;
16+
this.next = null;
17+
}
18+
}
19+
20+
private Node curr;
21+
22+
public BrowserHistory(String homepage) {
23+
curr = new Node(homepage);
24+
}
25+
26+
public void visit(String url) {
27+
Node newNode = new Node(url);
28+
curr.next = newNode;
29+
newNode.prev = curr;
30+
curr = curr.next;
31+
}
32+
33+
public String back(int steps) {
34+
while (curr.prev != null && steps-- > 0) {
35+
curr = curr.prev;
36+
}
37+
return curr.url;
38+
}
39+
40+
public String forward(int steps) {
41+
while (curr.next != null && steps-- > 0) {
42+
curr = curr.next;
43+
}
44+
return curr.url;
45+
}
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
1472\. Design Browser History
2+
3+
Medium
4+
5+
You have a **browser** of one tab where you start on the `homepage` and you can visit another `url`, get back in the history number of `steps` or move forward in the history number of `steps`.
6+
7+
Implement the `BrowserHistory` class:
8+
9+
* `BrowserHistory(string homepage)` Initializes the object with the `homepage` of the browser.
10+
* `void visit(string url)` Visits `url` from the current page. It clears up all the forward history.
11+
* `string back(int steps)` Move `steps` back in history. If you can only return `x` steps in the history and `steps > x`, you will return only `x` steps. Return the current `url` after moving back in history **at most** `steps`.
12+
* `string forward(int steps)` Move `steps` forward in history. If you can only forward `x` steps in the history and `steps > x`, you will forward only `x` steps. Return the current `url` after forwarding in history **at most** `steps`.
13+
14+
**Example:**
15+
16+
**Input:** ["BrowserHistory","visit","visit","visit","back","back","forward","visit","forward","back","back"] [["leetcode.com"],["google.com"],["facebook.com"],["youtube.com"],[1],[1],[1],["linkedin.com"],[2],[2],[7]]
17+
18+
**Output:** [null,null,null,null,"facebook.com","google.com","facebook.com",null,"linkedin.com","google.com","leetcode.com"]
19+
20+
**Explanation:**
21+
22+
BrowserHistory browserHistory = new BrowserHistory("leetcode.com");
23+
24+
browserHistory.visit("google.com"); // You are in "leetcode.com". Visit "google.com"
25+
26+
browserHistory.visit("facebook.com"); // You are in "google.com". Visit "facebook.com"
27+
28+
browserHistory.visit("youtube.com"); // You are in "facebook.com". Visit "youtube.com"
29+
30+
browserHistory.back(1); // You are in "youtube.com", move back to "facebook.com" return "facebook.com"
31+
32+
browserHistory.back(1); // You are in "facebook.com", move back to "google.com" return "google.com"
33+
34+
browserHistory.forward(1); // You are in "google.com", move forward to "facebook.com" return "facebook.com"
35+
36+
browserHistory.visit("linkedin.com"); // You are in "facebook.com". Visit "linkedin.com"
37+
38+
browserHistory.forward(2); // You are in "linkedin.com", you cannot move forward any steps.
39+
40+
browserHistory.back(2); // You are in "linkedin.com", move back two steps to "facebook.com" then to "google.com". return "google.com"
41+
42+
browserHistory.back(7); // You are in "google.com", you can move back only one step to "leetcode.com". return "leetcode.com"
43+
44+
**Constraints:**
45+
46+
* `1 <= homepage.length <= 20`
47+
* `1 <= url.length <= 20`
48+
* `1 <= steps <= 100`
49+
* `homepage` and `url` consist of '.' or lower case English letters.
50+
* At most `5000` calls will be made to `visit`, `back`, and `forward`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package g1401_1500.s1473_paint_house_iii;
2+
3+
// #Hard #Array #Dynamic_Programming #2022_03_29_Time_26_ms_(89.13%)_Space_42.9_MB_(91.75%)
4+
5+
public class Solution {
6+
private int[][][] memo;
7+
private int[] houses;
8+
private int nColors;
9+
private int[][] cost;
10+
11+
public int minCost(int[] houses, int[][] cost, int nColors, int tGroups) {
12+
this.cost = cost;
13+
this.houses = houses;
14+
this.memo = new int[houses.length][nColors + 1][tGroups + 1];
15+
this.nColors = nColors;
16+
17+
int dp = dp(0, 0, tGroups);
18+
return dp == Integer.MAX_VALUE ? -1 : dp;
19+
}
20+
21+
private int dp(int ithEl, int prevClr, int tGroups) {
22+
if (ithEl == houses.length) {
23+
return tGroups == 0 ? 0 : Integer.MAX_VALUE;
24+
}
25+
if (ithEl < houses.length && tGroups < 0) {
26+
return Integer.MAX_VALUE;
27+
}
28+
if (memo[ithEl][prevClr][tGroups] == 0) {
29+
int currC = houses[ithEl];
30+
int res = Integer.MAX_VALUE;
31+
if (currC != 0) {
32+
int grpLeft = currC == prevClr ? tGroups : tGroups - 1;
33+
res = dp(ithEl + 1, currC, grpLeft);
34+
} else {
35+
for (int clr = 1; clr <= nColors; clr++) {
36+
int grpLeft = clr == prevClr ? tGroups : tGroups - 1;
37+
int dp = dp(ithEl + 1, clr, grpLeft);
38+
res =
39+
Math.min(
40+
res,
41+
dp != Integer.MAX_VALUE
42+
? cost[ithEl][clr - 1] + dp
43+
: Integer.MAX_VALUE);
44+
}
45+
}
46+
memo[ithEl][prevClr][tGroups] = res;
47+
}
48+
return memo[ithEl][prevClr][tGroups];
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
1473\. Paint House III
2+
3+
Hard
4+
5+
There is a row of `m` houses in a small city, each house must be painted with one of the `n` colors (labeled from `1` to `n`), some houses that have been painted last summer should not be painted again.
6+
7+
A neighborhood is a maximal group of continuous houses that are painted with the same color.
8+
9+
* For example: `houses = [1,2,2,3,3,2,1,1]` contains `5` neighborhoods `[{1}, {2,2}, {3,3}, {2}, {1,1}]`.
10+
11+
Given an array `houses`, an `m x n` matrix `cost` and an integer `target` where:
12+
13+
* `houses[i]`: is the color of the house `i`, and `0` if the house is not painted yet.
14+
* `cost[i][j]`: is the cost of paint the house `i` with the color `j + 1`.
15+
16+
Return _the minimum cost of painting all the remaining houses in such a way that there are exactly_ `target` _neighborhoods_. If it is not possible, return `-1`.
17+
18+
**Example 1:**
19+
20+
**Input:** houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3
21+
22+
**Output:** 9
23+
24+
**Explanation:** Paint houses of this way [1,2,2,1,1]
25+
26+
This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}].
27+
28+
Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9.
29+
30+
**Example 2:**
31+
32+
**Input:** houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3
33+
34+
**Output:** 11
35+
36+
**Explanation:** Some houses are already painted, Paint the houses of this way [2,2,1,2,2]
37+
38+
This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}].
39+
40+
Cost of paint the first and last house (10 + 1) = 11.
41+
42+
**Example 3:**
43+
44+
**Input:** houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3
45+
46+
**Output:** -1
47+
48+
**Explanation:** Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3.
49+
50+
**Constraints:**
51+
52+
* `m == houses.length == cost.length`
53+
* `n == cost[i].length`
54+
* `1 <= m <= 100`
55+
* `1 <= n <= 20`
56+
* `1 <= target <= m`
57+
* `0 <= houses[i] <= n`
58+
* <code>1 <= cost[i][j] <= 10<sup>4</sup></code>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package g1401_1500.s1471_the_k_strongest_values_in_an_array;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void getStrongest() {
11+
assertThat(
12+
new Solution().getStrongest(new int[] {1, 2, 3, 4, 5}, 2),
13+
equalTo(new int[] {5, 1}));
14+
}
15+
16+
@Test
17+
void getStrongest2() {
18+
assertThat(
19+
new Solution().getStrongest(new int[] {1, 1, 3, 5, 5}, 2),
20+
equalTo(new int[] {5, 5}));
21+
}
22+
23+
@Test
24+
void getStrongest3() {
25+
assertThat(
26+
new Solution().getStrongest(new int[] {6, 7, 11, 7, 6, 8}, 5),
27+
equalTo(new int[] {11, 8, 6, 6, 7}));
28+
}
29+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package g1401_1500.s1472_design_browser_history;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class BrowserHistoryTest {
9+
@Test
10+
void browserHistoryTest() {
11+
BrowserHistory browserHistory = new BrowserHistory("leetcode.com");
12+
browserHistory.visit("google.com");
13+
browserHistory.visit("facebook.com");
14+
browserHistory.visit("youtube.com");
15+
assertThat(browserHistory.back(1), equalTo("facebook.com"));
16+
assertThat(browserHistory.back(1), equalTo("google.com"));
17+
assertThat(browserHistory.forward(1), equalTo("facebook.com"));
18+
browserHistory.visit("linkedin.com");
19+
assertThat(browserHistory.forward(2), equalTo("linkedin.com"));
20+
assertThat(browserHistory.back(2), equalTo("google.com"));
21+
assertThat(browserHistory.back(7), equalTo("leetcode.com"));
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package g1401_1500.s1473_paint_house_iii;
2+
3+
import static org.hamcrest.CoreMatchers.equalTo;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
6+
import org.junit.jupiter.api.Test;
7+
8+
class SolutionTest {
9+
@Test
10+
void minCost() {
11+
assertThat(
12+
new Solution()
13+
.minCost(
14+
new int[] {0, 0, 0, 0, 0},
15+
new int[][] {{1, 10}, {10, 1}, {10, 1}, {1, 10}, {5, 1}},
16+
2,
17+
3),
18+
equalTo(9));
19+
}
20+
21+
@Test
22+
void minCost2() {
23+
assertThat(
24+
new Solution()
25+
.minCost(
26+
new int[] {0, 2, 1, 2, 0},
27+
new int[][] {{1, 10}, {10, 1}, {10, 1}, {1, 10}, {5, 1}},
28+
2,
29+
3),
30+
equalTo(11));
31+
}
32+
33+
@Test
34+
void minCost3() {
35+
assertThat(
36+
new Solution()
37+
.minCost(
38+
new int[] {3, 1, 2, 3},
39+
new int[][] {{1, 1, 1}, {1, 1, 1}, {1, 1, 1}, {1, 1, 1}},
40+
3,
41+
3),
42+
equalTo(-1));
43+
}
44+
}

0 commit comments

Comments
 (0)