Skip to content

Commit 6525e26

Browse files
authored
Added tasks 2144, 2145, 2146, 2147, 2148.
1 parent b524ef7 commit 6525e26

File tree

15 files changed

+665
-0
lines changed

15 files changed

+665
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package g2101_2200.s2144_minimum_cost_of_buying_candies_with_discount;
2+
3+
// #Easy #Array #Sorting #Greedy #2022_06_07_Time_2_ms_(97.50%)_Space_41.4_MB_(99.04%)
4+
5+
import java.util.Arrays;
6+
7+
public class Solution {
8+
public int minimumCost(int[] cost) {
9+
Arrays.sort(cost);
10+
int size = 0;
11+
int sum = 0;
12+
for (int i = cost.length - 1; i >= 0; i--) {
13+
size++;
14+
if (size % 3 != 0) {
15+
sum += cost[i];
16+
}
17+
}
18+
19+
return sum;
20+
}
21+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
2144\. Minimum Cost of Buying Candies With Discount
2+
3+
Easy
4+
5+
A shop is selling candies at a discount. For **every two** candies sold, the shop gives a **third** candy for **free**.
6+
7+
The customer can choose **any** candy to take away for free as long as the cost of the chosen candy is less than or equal to the **minimum** cost of the two candies bought.
8+
9+
* For example, if there are `4` candies with costs `1`, `2`, `3`, and `4`, and the customer buys candies with costs `2` and `3`, they can take the candy with cost `1` for free, but not the candy with cost `4`.
10+
11+
Given a **0-indexed** integer array `cost`, where `cost[i]` denotes the cost of the <code>i<sup>th</sup></code> candy, return _the **minimum cost** of buying **all** the candies_.
12+
13+
**Example 1:**
14+
15+
**Input:** cost = [1,2,3]
16+
17+
**Output:** 5
18+
19+
**Explanation:** We buy the candies with costs 2 and 3, and take the candy with cost 1 for free.
20+
21+
The total cost of buying all candies is 2 + 3 = 5. This is the **only** way we can buy the candies.
22+
23+
Note that we cannot buy candies with costs 1 and 3, and then take the candy with cost 2 for free.
24+
25+
The cost of the free candy has to be less than or equal to the minimum cost of the purchased candies.
26+
27+
**Example 2:**
28+
29+
**Input:** cost = [6,5,7,9,2,2]
30+
31+
**Output:** 23
32+
33+
**Explanation:** The way in which we can get the minimum cost is described below:
34+
35+
- Buy candies with costs 9 and 7
36+
37+
- Take the candy with cost 6 for free
38+
39+
- We buy candies with costs 5 and 2
40+
41+
- Take the last remaining candy with cost 2 for free
42+
43+
Hence, the minimum cost to buy all candies is 9 + 7 + 5 + 2 = 23.
44+
45+
**Example 3:**
46+
47+
**Input:** cost = [5,5]
48+
49+
**Output:** 10
50+
51+
**Explanation:** Since there are only 2 candies, we buy both of them. There is not a third candy we can take for free.
52+
53+
Hence, the minimum cost to buy all candies is 5 + 5 = 10.
54+
55+
**Constraints:**
56+
57+
* `1 <= cost.length <= 100`
58+
* `1 <= cost[i] <= 100`
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package g2101_2200.s2145_count_the_hidden_sequences;
2+
3+
// #Medium #Array #Prefix_Sum #2022_06_07_Time_7_ms_(36.03%)_Space_111.3_MB_(13.23%)
4+
5+
public class Solution {
6+
public int numberOfArrays(int[] diff, int lower, int upper) {
7+
int n = diff.length;
8+
if (lower == upper) {
9+
for (int j : diff) {
10+
if (j != 0) {
11+
return 0;
12+
}
13+
}
14+
}
15+
int max = -(int) 1e9;
16+
int min = (int) 1e9;
17+
int[] hidden = new int[n + 1];
18+
hidden[0] = 0;
19+
for (int i = 1; i <= n; i++) {
20+
hidden[i] = hidden[i - 1] + diff[i - 1];
21+
}
22+
for (int i = 0; i <= n; i++) {
23+
if (hidden[i] > max) {
24+
max = hidden[i];
25+
}
26+
if (hidden[i] < min) {
27+
min = hidden[i];
28+
}
29+
}
30+
int low = lower - min;
31+
int high = upper - max;
32+
if (low > high) {
33+
return 0;
34+
}
35+
return (high - low) + 1;
36+
}
37+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
2145\. Count the Hidden Sequences
2+
3+
Medium
4+
5+
You are given a **0-indexed** array of `n` integers `differences`, which describes the **differences** between each pair of **consecutive** integers of a **hidden** sequence of length `(n + 1)`. More formally, call the hidden sequence `hidden`, then we have that `differences[i] = hidden[i + 1] - hidden[i]`.
6+
7+
You are further given two integers `lower` and `upper` that describe the **inclusive** range of values `[lower, upper]` that the hidden sequence can contain.
8+
9+
* For example, given `differences = [1, -3, 4]`, `lower = 1`, `upper = 6`, the hidden sequence is a sequence of length `4` whose elements are in between `1` and `6` (**inclusive**).
10+
* `[3, 4, 1, 5]` and `[4, 5, 2, 6]` are possible hidden sequences.
11+
* `[5, 6, 3, 7]` is not possible since it contains an element greater than `6`.
12+
* `[1, 2, 3, 4]` is not possible since the differences are not correct.
13+
14+
Return _the number of **possible** hidden sequences there are._ If there are no possible sequences, return `0`.
15+
16+
**Example 1:**
17+
18+
**Input:** differences = [1,-3,4], lower = 1, upper = 6
19+
20+
**Output:** 2
21+
22+
**Explanation:** The possible hidden sequences are:
23+
24+
- [3, 4, 1, 5]
25+
26+
- [4, 5, 2, 6]
27+
28+
Thus, we return 2.
29+
30+
**Example 2:**
31+
32+
**Input:** differences = [3,-4,5,1,-2], lower = -4, upper = 5
33+
34+
**Output:** 4
35+
36+
**Explanation:** The possible hidden sequences are:
37+
38+
- [-3, 0, -4, 1, 2, 0]
39+
40+
- [-2, 1, -3, 2, 3, 1]
41+
42+
- [-1, 2, -2, 3, 4, 2]
43+
44+
- [0, 3, -1, 4, 5, 3]
45+
46+
Thus, we return 4.
47+
48+
**Example 3:**
49+
50+
**Input:** differences = [4,-7,2], lower = 3, upper = 6
51+
52+
**Output:** 0
53+
54+
**Explanation:** There are no possible hidden sequences. Thus, we return 0.
55+
56+
**Constraints:**
57+
58+
* `n == differences.length`
59+
* <code>1 <= n <= 10<sup>5</sup></code>
60+
* <code>-10<sup>5</sup> <= differences[i] <= 10<sup>5</sup></code>
61+
* <code>-10<sup>5</sup> <= lower <= upper <= 10<sup>5</sup></code>
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
package g2101_2200.s2146_k_highest_ranked_items_within_a_price_range;
2+
3+
// #Medium #Array #Sorting #Breadth_First_Search #Matrix #Heap_Priority_Queue
4+
// #2022_06_07_Time_81_ms_(88.84%)_Space_65.7_MB_(99.30%)
5+
6+
import java.util.Arrays;
7+
import java.util.LinkedList;
8+
import java.util.List;
9+
import java.util.Queue;
10+
11+
public class Solution {
12+
static class Item {
13+
int row;
14+
int col;
15+
int dist;
16+
int price;
17+
18+
public Item(int row, int col, int dist, int price) {
19+
this.row = row;
20+
this.col = col;
21+
this.dist = dist;
22+
this.price = price;
23+
}
24+
}
25+
26+
public List<List<Integer>> highestRankedKItems(
27+
int[][] grid, int[] pricing, int[] start, int k) {
28+
int n = grid.length;
29+
int m = grid[0].length;
30+
Queue<int[]> bfs = new LinkedList<>();
31+
LinkedList<Item> items = new LinkedList<>();
32+
33+
bfs.add(start);
34+
if (grid[start[0]][start[1]] >= pricing[0] && grid[start[0]][start[1]] <= pricing[1]) {
35+
items.add(new Item(start[0], start[1], 0, grid[start[0]][start[1]]));
36+
}
37+
grid[start[0]][start[1]] = -1;
38+
39+
int distance = 0;
40+
while (!bfs.isEmpty()) {
41+
int size = bfs.size();
42+
distance++;
43+
while (size-- > 0) {
44+
int[] loc = bfs.poll();
45+
int[] dirX = {0, 1, -1, 0};
46+
int[] dirY = {-1, 0, 0, 1};
47+
for (int i = 0; i < 4; i++) {
48+
int newX = loc[0] + dirX[i];
49+
int newY = loc[1] + dirY[i];
50+
if (newX < 0
51+
|| newX >= n
52+
|| newY < 0
53+
|| newY >= m
54+
|| grid[newX][newY] == -1
55+
|| grid[newX][newY] == 0) {
56+
continue;
57+
}
58+
if (grid[newX][newY] >= pricing[0] && grid[newX][newY] <= pricing[1]) {
59+
items.add(new Item(newX, newY, distance, grid[newX][newY]));
60+
}
61+
grid[newX][newY] = -1;
62+
bfs.add(new int[] {newX, newY});
63+
}
64+
}
65+
}
66+
items.sort(
67+
(a, b) -> {
68+
int distDiff = a.dist - b.dist;
69+
if (distDiff == 0) {
70+
int priceDiff = a.price - b.price;
71+
if (priceDiff == 0) {
72+
int rowDiff = a.row - b.row;
73+
if (rowDiff == 0) {
74+
return a.col - b.col;
75+
}
76+
return rowDiff;
77+
}
78+
return priceDiff;
79+
}
80+
return distDiff;
81+
});
82+
List<List<Integer>> ans = new LinkedList<>();
83+
while (k-- > 0 && !items.isEmpty()) {
84+
Item item = items.poll();
85+
ans.add(Arrays.asList(item.row, item.col));
86+
}
87+
return ans;
88+
}
89+
}
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
2146\. K Highest Ranked Items Within a Price Range
2+
3+
Medium
4+
5+
You are given a **0-indexed** 2D integer array `grid` of size `m x n` that represents a map of the items in a shop. The integers in the grid represent the following:
6+
7+
* `0` represents a wall that you cannot pass through.
8+
* `1` represents an empty cell that you can freely move to and from.
9+
* All other positive integers represent the price of an item in that cell. You may also freely move to and from these item cells.
10+
11+
It takes `1` step to travel between adjacent grid cells.
12+
13+
You are also given integer arrays `pricing` and `start` where `pricing = [low, high]` and `start = [row, col]` indicates that you start at the position `(row, col)` and are interested only in items with a price in the range of `[low, high]` (**inclusive**). You are further given an integer `k`.
14+
15+
You are interested in the **positions** of the `k` **highest-ranked** items whose prices are **within** the given price range. The rank is determined by the **first** of these criteria that is different:
16+
17+
1. Distance, defined as the length of the shortest path from the `start` (**shorter** distance has a higher rank).
18+
2. Price (**lower** price has a higher rank, but it must be **in the price range**).
19+
3. The row number (**smaller** row number has a higher rank).
20+
4. The column number (**smaller** column number has a higher rank).
21+
22+
Return _the_ `k` _highest-ranked items within the price range **sorted** by their rank (highest to lowest)_. If there are fewer than `k` reachable items within the price range, return _**all** of them_.
23+
24+
**Example 1:**
25+
26+
![](https://assets.leetcode.com/uploads/2021/12/16/example1drawio.png)
27+
28+
**Input:** grid = [[1,2,0,1],[1,3,0,1],[0,2,5,1]], pricing = [2,5], start = [0,0], k = 3
29+
30+
**Output:** [[0,1],[1,1],[2,1]]
31+
32+
**Explanation:** You start at (0,0).
33+
34+
With a price range of [2,5], we can take items from (0,1), (1,1), (2,1) and (2,2).
35+
36+
The ranks of these items are:
37+
38+
- (0,1) with distance 1
39+
40+
- (1,1) with distance 2
41+
42+
- (2,1) with distance 3
43+
44+
- (2,2) with distance 4
45+
46+
Thus, the 3 highest ranked items in the price range are (0,1), (1,1), and (2,1).
47+
48+
**Example 2:**
49+
50+
![](https://assets.leetcode.com/uploads/2021/12/16/example2drawio1.png)
51+
52+
**Input:** grid = [[1,2,0,1],[1,3,3,1],[0,2,5,1]], pricing = [2,3], start = [2,3], k = 2
53+
54+
**Output:** [[2,1],[1,2]]
55+
56+
**Explanation:** You start at (2,3).
57+
58+
With a price range of [2,3], we can take items from (0,1), (1,1), (1,2) and (2,1).
59+
60+
The ranks of these items are:
61+
62+
- (2,1) with distance 2, price 2
63+
64+
- (1,2) with distance 2, price 3
65+
66+
- (1,1) with distance 3
67+
68+
- (0,1) with distance 4
69+
70+
Thus, the 2 highest ranked items in the price range are (2,1) and (1,2).
71+
72+
**Example 3:**
73+
74+
![](https://assets.leetcode.com/uploads/2021/12/30/example3.png)
75+
76+
**Input:** grid = [[1,1,1],[0,0,1],[2,3,4]], pricing = [2,3], start = [0,0], k = 3
77+
78+
**Output:** [[2,1],[2,0]]
79+
80+
**Explanation:** You start at (0,0).
81+
82+
With a price range of [2,3], we can take items from (2,0) and (2,1).
83+
84+
The ranks of these items are:
85+
86+
- (2,1) with distance 5
87+
88+
- (2,0) with distance 6
89+
90+
Thus, the 2 highest ranked items in the price range are (2,1) and (2,0).
91+
92+
Note that k = 3 but there are only 2 reachable items within the price range.
93+
94+
**Constraints:**
95+
96+
* `m == grid.length`
97+
* `n == grid[i].length`
98+
* <code>1 <= m, n <= 10<sup>5</sup></code>
99+
* <code>1 <= m * n <= 10<sup>5</sup></code>
100+
* <code>0 <= grid[i][j] <= 10<sup>5</sup></code>
101+
* `pricing.length == 2`
102+
* <code>2 <= low <= high <= 10<sup>5</sup></code>
103+
* `start.length == 2`
104+
* `0 <= row <= m - 1`
105+
* `0 <= col <= n - 1`
106+
* `grid[row][col] > 0`
107+
* `1 <= k <= m * n`

0 commit comments

Comments
 (0)