diff --git a/solution/2900-2999/2959.Number of Possible Sets of Closing Branches/README.md b/solution/2900-2999/2959.Number of Possible Sets of Closing Branches/README.md index cfda725e63bd7..2c94f18124a6a 100644 --- a/solution/2900-2999/2959.Number of Possible Sets of Closing Branches/README.md +++ b/solution/2900-2999/2959.Number of Possible Sets of Closing Branches/README.md @@ -123,7 +123,7 @@ class Solution: for mask in range(1 << n): g = [[inf] * n for _ in range(n)] for u, v, w in roads: - if mask >> u & 1 and mask > v & 1: + if mask >> u & 1 and mask >> v & 1: g[u][v] = min(g[u][v], w) g[v][u] = min(g[v][u], w) for k in range(n): diff --git a/solution/2900-2999/2959.Number of Possible Sets of Closing Branches/README_EN.md b/solution/2900-2999/2959.Number of Possible Sets of Closing Branches/README_EN.md index 25f003b42b0a1..45f6717a7d288 100644 --- a/solution/2900-2999/2959.Number of Possible Sets of Closing Branches/README_EN.md +++ b/solution/2900-2999/2959.Number of Possible Sets of Closing Branches/README_EN.md @@ -117,7 +117,7 @@ class Solution: for mask in range(1 << n): g = [[inf] * n for _ in range(n)] for u, v, w in roads: - if mask >> u & 1 and mask > v & 1: + if mask >> u & 1 and mask >> v & 1: g[u][v] = min(g[u][v], w) g[v][u] = min(g[v][u], w) for k in range(n): diff --git a/solution/2900-2999/2959.Number of Possible Sets of Closing Branches/Solution.py b/solution/2900-2999/2959.Number of Possible Sets of Closing Branches/Solution.py index 3ab1fdac62623..80bb9851a5dd5 100644 --- a/solution/2900-2999/2959.Number of Possible Sets of Closing Branches/Solution.py +++ b/solution/2900-2999/2959.Number of Possible Sets of Closing Branches/Solution.py @@ -4,7 +4,7 @@ def numberOfSets(self, n: int, maxDistance: int, roads: List[List[int]]) -> int: for mask in range(1 << n): g = [[inf] * n for _ in range(n)] for u, v, w in roads: - if mask >> u & 1 and mask > v & 1: + if mask >> u & 1 and mask >> v & 1: g[u][v] = min(g[u][v], w) g[v][u] = min(g[v][u], w) for k in range(n): diff --git a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README.md b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README.md index 7ef28fbbb5218..d4b85d7db924c 100644 --- a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README.md +++ b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README.md @@ -133,15 +133,15 @@ class Solution: g[v].append((u, w)) dist = [inf] * n dist[0] = 0 - q = [(0, 0)] - while q: - du, u = heappop(q) + pq = [(0, 0)] + while pq: + du, u = heappop(pq) if du > dist[u]: continue for v, w in g[u]: if dist[v] > dist[u] + w and dist[u] + w < disappear[v]: dist[v] = dist[u] + w - heappush(q, (dist[v], v)) + heappush(pq, (dist[v], v)) return [a if a < b else -1 for a, b in zip(dist, disappear)] ``` diff --git a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README_EN.md b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README_EN.md index 08d820e5ab273..5d150f2fbba32 100644 --- a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README_EN.md +++ b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/README_EN.md @@ -131,15 +131,15 @@ class Solution: g[v].append((u, w)) dist = [inf] * n dist[0] = 0 - q = [(0, 0)] - while q: - du, u = heappop(q) + pq = [(0, 0)] + while pq: + du, u = heappop(pq) if du > dist[u]: continue for v, w in g[u]: if dist[v] > dist[u] + w and dist[u] + w < disappear[v]: dist[v] = dist[u] + w - heappush(q, (dist[v], v)) + heappush(pq, (dist[v], v)) return [a if a < b else -1 for a, b in zip(dist, disappear)] ``` diff --git a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.py b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.py index 8695ae255103f..621600ee6e4c8 100644 --- a/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.py +++ b/solution/3100-3199/3112.Minimum Time to Visit Disappearing Nodes/Solution.py @@ -8,13 +8,13 @@ def minimumTime( g[v].append((u, w)) dist = [inf] * n dist[0] = 0 - q = [(0, 0)] - while q: - du, u = heappop(q) + pq = [(0, 0)] + while pq: + du, u = heappop(pq) if du > dist[u]: continue for v, w in g[u]: if dist[v] > dist[u] + w and dist[u] + w < disappear[v]: dist[v] = dist[u] + w - heappush(q, (dist[v], v)) + heappush(pq, (dist[v], v)) return [a if a < b else -1 for a, b in zip(dist, disappear)] diff --git a/solution/3200-3299/3220.Odd and Even Transactions/README.md b/solution/3200-3299/3220.Odd and Even Transactions/README.md new file mode 100644 index 0000000000000..365fdba217daa --- /dev/null +++ b/solution/3200-3299/3220.Odd and Even Transactions/README.md @@ -0,0 +1,154 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md +--- + + + +# [3220. Odd and Even Transactions](https://leetcode.cn/problems/odd-and-even-transactions) + +[English Version](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md) + +## 题目描述 + + + +

Table: transactions

+ +
++------------------+------+
+| Column Name      | Type | 
++------------------+------+
+| transaction_id   | int  |
+| amount           | int  |
+| transaction_date | date |
++------------------+------+
+The transactions_id column uniquely identifies each row in this table.
+Each row of this table contains the transaction id, amount and transaction date.
+
+ +

Write a solution to find the sum of amounts for odd and even transactions for each day. If there are no odd or even transactions for a specific date, display as 0.

+ +

Return the result table ordered by transaction_date in ascending order.

+ +

The result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

transactions table:

+ +
++----------------+--------+------------------+
+| transaction_id | amount | transaction_date |
++----------------+--------+------------------+
+| 1              | 150    | 2024-07-01       |
+| 2              | 200    | 2024-07-01       |
+| 3              | 75     | 2024-07-01       |
+| 4              | 300    | 2024-07-02       |
+| 5              | 50     | 2024-07-02       |
+| 6              | 120    | 2024-07-03       |
++----------------+--------+------------------+
+  
+ +

Output:

+ +
++------------------+---------+----------+
+| transaction_date | odd_sum | even_sum |
++------------------+---------+----------+
+| 2024-07-01       | 75      | 350      |
+| 2024-07-02       | 0       | 350      |
+| 2024-07-03       | 0       | 120      |
++------------------+---------+----------+
+  
+ +

Explanation:

+ + + +

Note: The output table is ordered by transaction_date in ascending order.

+
+ + + +## 解法 + + + +### 方法一:分组求和 + +我们可以将数据按照 `transaction_date` 进行分组,然后分别计算奇数和偶数的交易金额之和。最后按照 `transaction_date` 升序排序。 + + + +#### MySQL + +```sql +# Write your MySQL query statement below +SELECT + transaction_date, + SUM(IF(amount % 2 = 1, amount, 0)) AS odd_sum, + SUM(IF(amount % 2 = 0, amount, 0)) AS even_sum +FROM transactions +GROUP BY 1 +ORDER BY 1; +``` + +#### Pandas + +```python +import pandas as pd + + +def sum_daily_odd_even(transactions: pd.DataFrame) -> pd.DataFrame: + transactions["odd_sum"] = transactions["amount"].where( + transactions["amount"] % 2 == 1, 0 + ) + transactions["even_sum"] = transactions["amount"].where( + transactions["amount"] % 2 == 0, 0 + ) + + result = ( + transactions.groupby("transaction_date") + .agg(odd_sum=("odd_sum", "sum"), even_sum=("even_sum", "sum")) + .reset_index() + ) + + result = result.sort_values("transaction_date") + + return result +``` + + + + + + diff --git a/solution/3200-3299/3220.Odd and Even Transactions/README_EN.md b/solution/3200-3299/3220.Odd and Even Transactions/README_EN.md new file mode 100644 index 0000000000000..fc3bea24ca75c --- /dev/null +++ b/solution/3200-3299/3220.Odd and Even Transactions/README_EN.md @@ -0,0 +1,154 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md +--- + + + +# [3220. Odd and Even Transactions](https://leetcode.com/problems/odd-and-even-transactions) + +[中文文档](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md) + +## Description + + + +

Table: transactions

+ +
++------------------+------+
+| Column Name      | Type | 
++------------------+------+
+| transaction_id   | int  |
+| amount           | int  |
+| transaction_date | date |
++------------------+------+
+The transactions_id column uniquely identifies each row in this table.
+Each row of this table contains the transaction id, amount and transaction date.
+
+ +

Write a solution to find the sum of amounts for odd and even transactions for each day. If there are no odd or even transactions for a specific date, display as 0.

+ +

Return the result table ordered by transaction_date in ascending order.

+ +

The result format is in the following example.

+ +

 

+

Example:

+ +
+

Input:

+ +

transactions table:

+ +
++----------------+--------+------------------+
+| transaction_id | amount | transaction_date |
++----------------+--------+------------------+
+| 1              | 150    | 2024-07-01       |
+| 2              | 200    | 2024-07-01       |
+| 3              | 75     | 2024-07-01       |
+| 4              | 300    | 2024-07-02       |
+| 5              | 50     | 2024-07-02       |
+| 6              | 120    | 2024-07-03       |
++----------------+--------+------------------+
+  
+ +

Output:

+ +
++------------------+---------+----------+
+| transaction_date | odd_sum | even_sum |
++------------------+---------+----------+
+| 2024-07-01       | 75      | 350      |
+| 2024-07-02       | 0       | 350      |
+| 2024-07-03       | 0       | 120      |
++------------------+---------+----------+
+  
+ +

Explanation:

+ + + +

Note: The output table is ordered by transaction_date in ascending order.

+
+ + + +## Solutions + + + +### Solution 1: Grouping and Summing + +We can group the data by `transaction_date`, and then calculate the sum of transaction amounts for odd and even dates separately. Finally, sort by `transaction_date` in ascending order. + + + +#### MySQL + +```sql +# Write your MySQL query statement below +SELECT + transaction_date, + SUM(IF(amount % 2 = 1, amount, 0)) AS odd_sum, + SUM(IF(amount % 2 = 0, amount, 0)) AS even_sum +FROM transactions +GROUP BY 1 +ORDER BY 1; +``` + +#### Pandas + +```python +import pandas as pd + + +def sum_daily_odd_even(transactions: pd.DataFrame) -> pd.DataFrame: + transactions["odd_sum"] = transactions["amount"].where( + transactions["amount"] % 2 == 1, 0 + ) + transactions["even_sum"] = transactions["amount"].where( + transactions["amount"] % 2 == 0, 0 + ) + + result = ( + transactions.groupby("transaction_date") + .agg(odd_sum=("odd_sum", "sum"), even_sum=("even_sum", "sum")) + .reset_index() + ) + + result = result.sort_values("transaction_date") + + return result +``` + + + + + + diff --git a/solution/3200-3299/3220.Odd and Even Transactions/Solution.py b/solution/3200-3299/3220.Odd and Even Transactions/Solution.py new file mode 100644 index 0000000000000..1b9e097b06ce2 --- /dev/null +++ b/solution/3200-3299/3220.Odd and Even Transactions/Solution.py @@ -0,0 +1,20 @@ +import pandas as pd + + +def sum_daily_odd_even(transactions: pd.DataFrame) -> pd.DataFrame: + transactions["odd_sum"] = transactions["amount"].where( + transactions["amount"] % 2 == 1, 0 + ) + transactions["even_sum"] = transactions["amount"].where( + transactions["amount"] % 2 == 0, 0 + ) + + result = ( + transactions.groupby("transaction_date") + .agg(odd_sum=("odd_sum", "sum"), even_sum=("even_sum", "sum")) + .reset_index() + ) + + result = result.sort_values("transaction_date") + + return result diff --git a/solution/3200-3299/3220.Odd and Even Transactions/Solution.sql b/solution/3200-3299/3220.Odd and Even Transactions/Solution.sql new file mode 100644 index 0000000000000..9d454f9b3c341 --- /dev/null +++ b/solution/3200-3299/3220.Odd and Even Transactions/Solution.sql @@ -0,0 +1,8 @@ +# Write your MySQL query statement below +SELECT + transaction_date, + SUM(IF(amount % 2 = 1, amount, 0)) AS odd_sum, + SUM(IF(amount % 2 = 0, amount, 0)) AS even_sum +FROM transactions +GROUP BY 1 +ORDER BY 1; diff --git a/solution/3200-3299/3221.Maximum Array Hopping Score II/README.md b/solution/3200-3299/3221.Maximum Array Hopping Score II/README.md new file mode 100644 index 0000000000000..65f98df2f0f66 --- /dev/null +++ b/solution/3200-3299/3221.Maximum Array Hopping Score II/README.md @@ -0,0 +1,189 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3221.Maximum%20Array%20Hopping%20Score%20II/README.md +--- + + + +# [3221. Maximum Array Hopping Score II 🔒](https://leetcode.cn/problems/maximum-array-hopping-score-ii) + +[English Version](/solution/3200-3299/3221.Maximum%20Array%20Hopping%20Score%20II/README_EN.md) + +## 题目描述 + + + +

Given an array nums, you have to get the maximum score starting from index 0 and hopping until you reach the last element of the array.

+ +

In each hop, you can jump from index i to an index j > i, and you get a score of (j - i) * nums[j].

+ +

Return the maximum score you can get.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,5,8]

+ +

Output: 16

+ +

Explanation:

+ +

There are two possible ways to reach the last element:

+ + +
+ +

Example 2:

+ +
+

Input: nums = [4,5,2,8,9,1,3]

+ +

Output: 42

+ +

Explanation:

+ +

We can do the hopping 0 -> 4 -> 6 with a score of (4 - 0) * 9 + (6 - 4) * 3 = 42.

+
+ +

 

+

Constraints:

+ + + + + +## 解法 + + + +### 方法一:单调栈 + +我们观察发现,对于当前位置 $i$,我们应该跳到下一个值最大的位置 $j$,这样才能获得最大的分数。 + +因此,我们遍历数组 $\text{nums}$,维护一个从栈底到栈顶单调递减的栈 $\text{stk}$。对于当前遍历到的位置 $i$,如果栈顶元素对应的值小于等于 $\text{nums}[i]$,我们就不断地弹出栈顶元素,直到栈为空或者栈顶元素对应的值大于 $\text{nums}[i]$,然后将 $i$ 入栈。 + +然后,我们初始化答案 $\text{ans}$ 和当前位置 $i = 0$,遍历栈中的元素,每次取出栈顶元素 $j$,更新答案 $\text{ans} += \text{nums}[j] \times (j - i)$,然后更新 $i = j$。 + +最后返回答案 $\text{ans}$。 + +时间复杂度 $O(n)$,空间复杂度 $O(n)$。其中 $n$ 是数组的长度。 + + + +#### Python3 + +```python +class Solution: + def maxScore(self, nums: List[int]) -> int: + stk = [] + for i, x in enumerate(nums): + while stk and nums[stk[-1]] <= x: + stk.pop() + stk.append(i) + ans = i = 0 + for j in stk: + ans += nums[j] * (j - i) + i = j + return ans +``` + +#### Java + +```java +class Solution { + public long maxScore(int[] nums) { + Deque stk = new ArrayDeque<>(); + for (int i = 0; i < nums.length; ++i) { + while (!stk.isEmpty() && nums[stk.peek()] <= nums[i]) { + stk.pop(); + } + stk.push(i); + } + long ans = 0, i = 0; + while (!stk.isEmpty()) { + int j = stk.pollLast(); + ans += (j - i) * nums[j]; + i = j; + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + long long maxScore(vector& nums) { + vector stk; + for (int i = 0; i < nums.size(); ++i) { + while (stk.size() && nums[stk.back()] <= nums[i]) { + stk.pop_back(); + } + stk.push_back(i); + } + long long ans = 0, i = 0; + for (int j : stk) { + ans += (j - i) * nums[j]; + i = j; + } + return ans; + } +}; +``` + +#### Go + +```go +func maxScore(nums []int) (ans int64) { + stk := []int{} + for i, x := range nums { + for len(stk) > 0 && nums[stk[len(stk)-1]] <= x { + stk = stk[:len(stk)-1] + } + stk = append(stk, i) + } + i := 0 + for _, j := range stk { + ans += int64((j - i) * nums[j]) + i = j + } + return +} +``` + +#### TypeScript + +```ts +function maxScore(nums: number[]): number { + const stk: number[] = []; + for (let i = 0; i < nums.length; ++i) { + while (stk.length && nums[stk.at(-1)!] <= nums[i]) { + stk.pop(); + } + stk.push(i); + } + let ans = 0; + let i = 0; + for (const j of stk) { + ans += (j - i) * nums[j]; + i = j; + } + return ans; +} +``` + + + + + + diff --git a/solution/3200-3299/3221.Maximum Array Hopping Score II/README_EN.md b/solution/3200-3299/3221.Maximum Array Hopping Score II/README_EN.md new file mode 100644 index 0000000000000..d1b3ab984a648 --- /dev/null +++ b/solution/3200-3299/3221.Maximum Array Hopping Score II/README_EN.md @@ -0,0 +1,185 @@ +--- +comments: true +difficulty: Medium +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3200-3299/3221.Maximum%20Array%20Hopping%20Score%20II/README_EN.md +--- + + + +# [3221. Maximum Array Hopping Score II 🔒](https://leetcode.com/problems/maximum-array-hopping-score-ii) + +[中文文档](/solution/3200-3299/3221.Maximum%20Array%20Hopping%20Score%20II/README.md) + +## Description + + + +

Given an array nums, you have to get the maximum score starting from index 0 and hopping until you reach the last element of the array.

+ +

In each hop, you can jump from index i to an index j > i, and you get a score of (j - i) * nums[j].

+ +

Return the maximum score you can get.

+ +

 

+

Example 1:

+ +
+

Input: nums = [1,5,8]

+ +

Output: 16

+ +

Explanation:

+ +

There are two possible ways to reach the last element:

+ +
    +
  • 0 -> 1 -> 2 with a score of (1 - 0) * 5 + (2 - 1) * 8 = 13.
  • +
  • 0 -> 2 with a score of (2 - 0) * 8 = 16.
  • +
+
+ +

Example 2:

+ +
+

Input: nums = [4,5,2,8,9,1,3]

+ +

Output: 42

+ +

Explanation:

+ +

We can do the hopping 0 -> 4 -> 6 with a score of (4 - 0) * 9 + (6 - 4) * 3 = 42.

+
+ +

 

+

Constraints:

+ +
    +
  • 2 <= nums.length <= 105
  • +
  • 1 <= nums[i] <= 105
  • +
+ + + +## Solutions + + + +### Solution 1: Monotonic Stack + +We observe that for the current position $i$, we should jump to the next position $j$ with the maximum value to obtain the maximum score. + +Therefore, we traverse the array $\text{nums}$, maintaining a stack $\text{stk}$ that is monotonically decreasing from the bottom to the top of the stack. For the current position $i$ being traversed, if the value corresponding to the top element of the stack is less than or equal to $\text{nums}[i]$, we continuously pop the top element of the stack until the stack is empty or the value corresponding to the top element of the stack is greater than $\text{nums}[i]$, and then push $i$ into the stack. + +Next, we initialize the answer $\text{ans}$ and the current position $i = 0$, traverse the elements in the stack, each time taking out the top element $j$, updating the answer $\text{ans} += \text{nums}[j] \times (j - i)$, and then updating $i = j$. + + + +#### Python3 + +```python +class Solution: + def maxScore(self, nums: List[int]) -> int: + stk = [] + for i, x in enumerate(nums): + while stk and nums[stk[-1]] <= x: + stk.pop() + stk.append(i) + ans = i = 0 + for j in stk: + ans += nums[j] * (j - i) + i = j + return ans +``` + +#### Java + +```java +class Solution { + public long maxScore(int[] nums) { + Deque stk = new ArrayDeque<>(); + for (int i = 0; i < nums.length; ++i) { + while (!stk.isEmpty() && nums[stk.peek()] <= nums[i]) { + stk.pop(); + } + stk.push(i); + } + long ans = 0, i = 0; + while (!stk.isEmpty()) { + int j = stk.pollLast(); + ans += (j - i) * nums[j]; + i = j; + } + return ans; + } +} +``` + +#### C++ + +```cpp +class Solution { +public: + long long maxScore(vector& nums) { + vector stk; + for (int i = 0; i < nums.size(); ++i) { + while (stk.size() && nums[stk.back()] <= nums[i]) { + stk.pop_back(); + } + stk.push_back(i); + } + long long ans = 0, i = 0; + for (int j : stk) { + ans += (j - i) * nums[j]; + i = j; + } + return ans; + } +}; +``` + +#### Go + +```go +func maxScore(nums []int) (ans int64) { + stk := []int{} + for i, x := range nums { + for len(stk) > 0 && nums[stk[len(stk)-1]] <= x { + stk = stk[:len(stk)-1] + } + stk = append(stk, i) + } + i := 0 + for _, j := range stk { + ans += int64((j - i) * nums[j]) + i = j + } + return +} +``` + +#### TypeScript + +```ts +function maxScore(nums: number[]): number { + const stk: number[] = []; + for (let i = 0; i < nums.length; ++i) { + while (stk.length && nums[stk.at(-1)!] <= nums[i]) { + stk.pop(); + } + stk.push(i); + } + let ans = 0; + let i = 0; + for (const j of stk) { + ans += (j - i) * nums[j]; + i = j; + } + return ans; +} +``` + + + + + + diff --git a/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.cpp b/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.cpp new file mode 100644 index 0000000000000..7365af25c1e7f --- /dev/null +++ b/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.cpp @@ -0,0 +1,18 @@ +class Solution { +public: + long long maxScore(vector& nums) { + vector stk; + for (int i = 0; i < nums.size(); ++i) { + while (stk.size() && nums[stk.back()] <= nums[i]) { + stk.pop_back(); + } + stk.push_back(i); + } + long long ans = 0, i = 0; + for (int j : stk) { + ans += (j - i) * nums[j]; + i = j; + } + return ans; + } +}; \ No newline at end of file diff --git a/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.go b/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.go new file mode 100644 index 0000000000000..34ecd330347d6 --- /dev/null +++ b/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.go @@ -0,0 +1,15 @@ +func maxScore(nums []int) (ans int64) { + stk := []int{} + for i, x := range nums { + for len(stk) > 0 && nums[stk[len(stk)-1]] <= x { + stk = stk[:len(stk)-1] + } + stk = append(stk, i) + } + i := 0 + for _, j := range stk { + ans += int64((j - i) * nums[j]) + i = j + } + return +} \ No newline at end of file diff --git a/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.java b/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.java new file mode 100644 index 0000000000000..a862b3028d4fa --- /dev/null +++ b/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.java @@ -0,0 +1,18 @@ +class Solution { + public long maxScore(int[] nums) { + Deque stk = new ArrayDeque<>(); + for (int i = 0; i < nums.length; ++i) { + while (!stk.isEmpty() && nums[stk.peek()] <= nums[i]) { + stk.pop(); + } + stk.push(i); + } + long ans = 0, i = 0; + while (!stk.isEmpty()) { + int j = stk.pollLast(); + ans += (j - i) * nums[j]; + i = j; + } + return ans; + } +} \ No newline at end of file diff --git a/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.py b/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.py new file mode 100644 index 0000000000000..02a773ddd21f7 --- /dev/null +++ b/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.py @@ -0,0 +1,12 @@ +class Solution: + def maxScore(self, nums: List[int]) -> int: + stk = [] + for i, x in enumerate(nums): + while stk and nums[stk[-1]] <= x: + stk.pop() + stk.append(i) + ans = i = 0 + for j in stk: + ans += nums[j] * (j - i) + i = j + return ans diff --git a/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.ts b/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.ts new file mode 100644 index 0000000000000..a5411d1b49c0a --- /dev/null +++ b/solution/3200-3299/3221.Maximum Array Hopping Score II/Solution.ts @@ -0,0 +1,16 @@ +function maxScore(nums: number[]): number { + const stk: number[] = []; + for (let i = 0; i < nums.length; ++i) { + while (stk.length && nums[stk.at(-1)!] <= nums[i]) { + stk.pop(); + } + stk.push(i); + } + let ans = 0; + let i = 0; + for (const j of stk) { + ans += (j - i) * nums[j]; + i = j; + } + return ans; +} diff --git a/solution/DATABASE_README.md b/solution/DATABASE_README.md index 9552987e6c05e..5e07c7b269103 100644 --- a/solution/DATABASE_README.md +++ b/solution/DATABASE_README.md @@ -287,6 +287,7 @@ | 3198 | [查找每个州的城市](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README.md) | `数据库` | 简单 | 🔒 | | 3204 | [按位用户权限分析](/solution/3200-3299/3204.Bitwise%20User%20Permissions%20Analysis/README.md) | `数据库` | 中等 | 🔒 | | 3214 | [同比增长率](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README.md) | `数据库` | 困难 | 🔒 | +| 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md) | | 中等 | | ## 版权 diff --git a/solution/DATABASE_README_EN.md b/solution/DATABASE_README_EN.md index 18d7f63a90c8e..498c7ac873005 100644 --- a/solution/DATABASE_README_EN.md +++ b/solution/DATABASE_README_EN.md @@ -285,6 +285,7 @@ Press Control + F(or Command + F on | 3198 | [Find Cities in Each State](/solution/3100-3199/3198.Find%20Cities%20in%20Each%20State/README_EN.md) | `Database` | Easy | 🔒 | | 3204 | [Bitwise User Permissions Analysis](/solution/3200-3299/3204.Bitwise%20User%20Permissions%20Analysis/README_EN.md) | `Database` | Medium | 🔒 | | 3214 | [Year on Year Growth Rate](/solution/3200-3299/3214.Year%20on%20Year%20Growth%20Rate/README_EN.md) | `Database` | Hard | 🔒 | +| 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md) | | Medium | | ## Copyright diff --git a/solution/README.md b/solution/README.md index 0a4c9bc30b211..973b4707f8e17 100644 --- a/solution/README.md +++ b/solution/README.md @@ -3230,6 +3230,8 @@ | 3217 | [从链表中移除在数组中存在的节点](/solution/3200-3299/3217.Delete%20Nodes%20From%20Linked%20List%20Present%20in%20Array/README.md) | | 中等 | 第 406 场周赛 | | 3218 | [切蛋糕的最小总开销 I](/solution/3200-3299/3218.Minimum%20Cost%20for%20Cutting%20Cake%20I/README.md) | | 中等 | 第 406 场周赛 | | 3219 | [切蛋糕的最小总开销 II](/solution/3200-3299/3219.Minimum%20Cost%20for%20Cutting%20Cake%20II/README.md) | | 困难 | 第 406 场周赛 | +| 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README.md) | | 中等 | | +| 3221 | [Maximum Array Hopping Score II](/solution/3200-3299/3221.Maximum%20Array%20Hopping%20Score%20II/README.md) | | 中等 | 🔒 | ## 版权 diff --git a/solution/README_EN.md b/solution/README_EN.md index 1e25425af4baa..79c33c379eb5a 100644 --- a/solution/README_EN.md +++ b/solution/README_EN.md @@ -3228,6 +3228,8 @@ Press Control + F(or Command + F on | 3217 | [Delete Nodes From Linked List Present in Array](/solution/3200-3299/3217.Delete%20Nodes%20From%20Linked%20List%20Present%20in%20Array/README_EN.md) | | Medium | Weekly Contest 406 | | 3218 | [Minimum Cost for Cutting Cake I](/solution/3200-3299/3218.Minimum%20Cost%20for%20Cutting%20Cake%20I/README_EN.md) | | Medium | Weekly Contest 406 | | 3219 | [Minimum Cost for Cutting Cake II](/solution/3200-3299/3219.Minimum%20Cost%20for%20Cutting%20Cake%20II/README_EN.md) | | Hard | Weekly Contest 406 | +| 3220 | [Odd and Even Transactions](/solution/3200-3299/3220.Odd%20and%20Even%20Transactions/README_EN.md) | | Medium | | +| 3221 | [Maximum Array Hopping Score II](/solution/3200-3299/3221.Maximum%20Array%20Hopping%20Score%20II/README_EN.md) | | Medium | 🔒 | ## Copyright diff --git a/solution/main.py b/solution/main.py index 366e4aa9decf8..e6b60b0a9aa9f 100644 --- a/solution/main.py +++ b/solution/main.py @@ -453,7 +453,7 @@ def run(): ) or spider.get_question_detail_en(slug, retry=8) if not detail: continue - time.sleep(0.3) + time.sleep(1) question_details[slug] = Spider.format_question_detail( detail, str(qid).zfill(4) )