Skip to content

Commit 70af54f

Browse files
authored
Improved task 1911.
1 parent 51f8920 commit 70af54f

File tree

2 files changed

+21
-37
lines changed

2 files changed

+21
-37
lines changed

src/main/java/g1901_2000/s1911_maximum_alternating_subsequence_sum/Solution.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package g1901_2000.s1911_maximum_alternating_subsequence_sum;
22

3-
// #Medium #Array #Dynamic_Programming #2022_05_14_Time_13_ms_(49.60%)_Space_104.2_MB_(47.17%)
3+
// #Medium #Array #Dynamic_Programming #2022_05_14_Time_12_ms_(51.75%)_Space_97.4_MB_(59.03%)
44

55
public class Solution {
66
public long maxAlternatingSum(int[] nums) {
@@ -11,7 +11,6 @@ public long maxAlternatingSum(int[] nums) {
1111
even = Math.max(even, Math.max(odd + nums[i], nums[i]));
1212
odd = Math.max(odd, Math.max(even - nums[i], 0));
1313
}
14-
1514
return Math.max(even, odd);
1615
}
1716
}
Lines changed: 20 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,55 +1,40 @@
1-
1912\. Design Movie Rental System
1+
1911\. Maximum Alternating Subsequence Sum
22

3-
Hard
3+
Medium
44

5-
You have a movie renting company consisting of `n` shops. You want to implement a renting system that supports searching for, booking, and returning movies. The system should also support generating a report of the currently rented movies.
5+
The **alternating sum** of a **0-indexed** array is defined as the **sum** of the elements at **even** indices **minus** the **sum** of the elements at **odd** indices.
66

7-
Each movie is given as a 2D integer array `entries` where <code>entries[i] = [shop<sub>i</sub>, movie<sub>i</sub>, price<sub>i</sub>]</code> indicates that there is a copy of movie <code>movie<sub>i</sub></code> at shop <code>shop<sub>i</sub></code> with a rental price of <code>price<sub>i</sub></code>. Each shop carries **at most one** copy of a movie <code>movie<sub>i</sub></code>.
7+
* For example, the alternating sum of `[4,2,5,3]` is `(4 + 5) - (2 + 3) = 4`.
88

9-
The system should support the following functions:
9+
Given an array `nums`, return _the **maximum alternating sum** of any subsequence of_ `nums` _(after **reindexing** the elements of the subsequence)_.
1010

11-
* **Search**: Finds the **cheapest 5 shops** that have an **unrented copy** of a given movie. The shops should be sorted by **price** in ascending order, and in case of a tie, the one with the **smaller** <code>shop<sub>i</sub></code> should appear first. If there are less than 5 matching shops, then all of them should be returned. If no shop has an unrented copy, then an empty list should be returned.
12-
* **Rent**: Rents an **unrented copy** of a given movie from a given shop.
13-
* **Drop**: Drops off a **previously rented copy** of a given movie at a given shop.
14-
* **Report**: Returns the **cheapest 5 rented movies** (possibly of the same movie ID) as a 2D list `res` where <code>res[j] = [shop<sub>j</sub>, movie<sub>j</sub>]</code> describes that the <code>j<sup>th</sup></code> cheapest rented movie <code>movie<sub>j</sub></code> was rented from the shop <code>shop<sub>j</sub></code>. The movies in `res` should be sorted by **price** in ascending order, and in case of a tie, the one with the **smaller** <code>shop<sub>j</sub></code> should appear first, and if there is still tie, the one with the **smaller** <code>movie<sub>j</sub></code> should appear first. If there are fewer than 5 rented movies, then all of them should be returned. If no movies are currently being rented, then an empty list should be returned.
15-
16-
Implement the `MovieRentingSystem` class:
17-
18-
* `MovieRentingSystem(int n, int[][] entries)` Initializes the `MovieRentingSystem` object with `n` shops and the movies in `entries`.
19-
* `List<Integer> search(int movie)` Returns a list of shops that have an **unrented copy** of the given `movie` as described above.
20-
* `void rent(int shop, int movie)` Rents the given `movie` from the given `shop`.
21-
* `void drop(int shop, int movie)` Drops off a previously rented `movie` at the given `shop`.
22-
* `List<List<Integer>> report()` Returns a list of cheapest **rented** movies as described above.
23-
24-
**Note:** The test cases will be generated such that `rent` will only be called if the shop has an **unrented** copy of the movie, and `drop` will only be called if the shop had **previously rented** out the movie.
11+
A **subsequence** of an array is a new array generated from the original array by deleting some elements (possibly none) without changing the remaining elements' relative order. For example, `[2,7,4]` is a subsequence of `[4,2,3,7,2,1,4]` (the underlined elements), while `[2,4,2]` is not.
2512

2613
**Example 1:**
2714

28-
**Input** ["MovieRentingSystem", "search", "rent", "rent", "report", "drop", "search"] [[3, [[0, 1, 5], [0, 2, 6], [0, 3, 7], [1, 1, 4], [1, 2, 7], [2, 1, 5]]], [1], [0, 1], [1, 2], [], [1, 2], [2]]
15+
**Input:** nums = [4,2,5,3]
16+
17+
**Output:** 7
2918

30-
**Output:** [null, [1, 0, 2], null, null, [[0, 1], [1, 2]], null, [0, 1]]
19+
**Explanation:** It is optimal to choose the subsequence [4,2,5] with alternating sum (4 + 5) - 2 = 7.
3120

32-
**Explanation:**
21+
**Example 2:**
3322

34-
MovieRentingSystem movieRentingSystem = new MovieRentingSystem(3, [[0, 1, 5], [0, 2, 6], [0, 3, 7], [1, 1, 4], [1, 2, 7], [2, 1, 5]]);
23+
**Input:** nums = [5,6,7,8]
3524

36-
movieRentingSystem.search(1); // return [1, 0, 2], Movies of ID 1 are unrented at shops 1, 0, and 2. Shop 1 is cheapest; shop 0 and 2 are the same price, so order by shop number.
25+
**Output:** 8
3726

38-
movieRentingSystem.rent(0, 1); // Rent movie 1 from shop 0. Unrented movies at shop 0 are now [2,3].
27+
**Explanation:** It is optimal to choose the subsequence [8] with alternating sum 8.
3928

40-
movieRentingSystem.rent(1, 2); // Rent movie 2 from shop 1. Unrented movies at shop 1 are now [1].
29+
**Example 3:**
4130

42-
movieRentingSystem.report(); // return [[0, 1], [1, 2]]. Movie 1 from shop 0 is cheapest, followed by movie 2 from shop 1.
31+
**Input:** nums = [6,2,1,2,4,5]
4332

44-
movieRentingSystem.drop(1, 2); // Drop off movie 2 at shop 1. Unrented movies at shop 1 are now [1,2].
33+
**Output:** 10
4534

46-
movieRentingSystem.search(2); // return [0, 1]. Movies of ID 2 are unrented at shops 0 and 1. Shop 0 is cheapest, followed by shop 1.
35+
**Explanation:** It is optimal to choose the subsequence [6,1,5] with alternating sum (6 + 5) - 1 = 10.
4736

4837
**Constraints:**
4938

50-
* <code>1 <= n <= 3 * 10<sup>5</sup></code>
51-
* <code>1 <= entries.length <= 10<sup>5</sup></code>
52-
* <code>0 <= shop<sub>i</sub> < n</code>
53-
* <code>1 <= movie<sub>i</sub>, price<sub>i</sub> <= 10<sup>4</sup></code>
54-
* Each shop carries **at most one** copy of a movie <code>movie<sub>i</sub></code>.
55-
* At most <code>10<sup>5</sup></code> calls **in total** will be made to `search`, `rent`, `drop` and `report`.
39+
* <code>1 <= nums.length <= 10<sup>5</sup></code>
40+
* <code>1 <= nums[i] <= 10<sup>5</sup></code>

0 commit comments

Comments
 (0)