threats[1]
与 threats[2]
有相同的分数,因此它们按升序排序。
threats[1]
与 threats[2]
有相同的分数,因此它们按 ID 升序排序。
排序顺序:[[101, 4, 1], [102, 1, 5], [103, 1, 5]]
Table: books
表:books
+-------------+---------+ @@ -28,11 +28,11 @@ tags: | genre | varchar | | pages | int | +-------------+---------+ -book_id is the unique ID for this table. -Each row contains information about a book including its genre and page count. +book_id 是这张表的唯一主键。 +每一行包含关于一本书的信息,包括其类型和页数。-
Table: reading_sessions
表:reading_sessions
+----------------+---------+ @@ -44,31 +44,32 @@ Each row contains information about a book including its genre and page count. | pages_read | int | | session_rating | int | +----------------+---------+ -session_id is the unique ID for this table. -Each row represents a reading session where someone read a portion of a book. session_rating is on a scale of 1-5. +session_id 是这张表的唯一主键。 +每一行代表一次阅读事件,有人阅读了书籍的一部分。session_rating 在 1-5 的范围内。-
Write a solution to find books that have polarized opinions - books that receive both very high ratings and very low ratings from different readers.
+编写一个解决方案来找到具有 两极分化观点 的书 - 同时获得不同读者极高和极低评分的书籍。
at least one rating ≥ 4
and at least one rating ≤ 2
5
reading sessionshighest_rating - lowest_rating
)ratings ≤ 2 or ≥ 4
) divided by total sessionspolarization score ≥ 0.6
(at least 60%
extreme ratings)4
的评分和至少一个小于等于 2
的评分则是有两极分化观点的书5
次阅读事件的书籍highest_rating - lowest_rating
计算评分差幅 rating spread2
或大于等于 4
)的数量除以总阅读事件计算 极化得分 polarization score0.6
的书(至少 60%
极端评分)Return the result table ordered by polarization score in descending order, then by title in descending order.
+返回结果表按极化得分 降序 排序,然后按标题 降序 排序。
-The result format is in the following example.
+返回格式如下所示。
-
Example:
+ +示例:
Input:
+输入:
-books table:
+books 表:
+---------+------------------------+---------------+----------+-------+ @@ -82,7 +83,7 @@ Each row represents a reading session where someone read a portion of a book. se +---------+------------------------+---------------+----------+-------+-
reading_sessions table:
+reading_sessions 表:
+------------+---------+-------------+------------+----------------+ @@ -111,7 +112,7 @@ Each row represents a reading session where someone read a portion of a book. se +------------+---------+-------------+------------+----------------+-
Output:
+输出:
+---------+------------------+---------------+-----------+-------+---------------+--------------------+ @@ -122,43 +123,43 @@ Each row represents a reading session where someone read a portion of a book. se +---------+------------------+---------------+-----------+-------+---------------+--------------------+-
Explanation:
+解释:
The result table is ordered by polarization score in descending order, then by book title in descending order.
+结果表按极化得分降序排序,然后按标题降序排序。
You are given an integer array nums
of length n
, where nums
is a permutation of the numbers in the range [0..n - 1]
.
You are given an integer array nums
of length n
, where nums
is a permutation of the numbers in the range [0..n - 1]
.
You may swap elements at indices i
and j
only if nums[i] AND nums[j] == k
, where AND
denotes the bitwise AND operation and k
is a non-negative integer.
Return the maximum value of k
such that the array can be sorted in non-decreasing order using any number of such swaps. If nums
is already sorted, return 0.
A permutation is a rearrangement of all the elements of an array.
-
Example 1:
@@ -81,25 +79,74 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3644.Ma #### Python3 ```python - +class Solution: + def sortPermutation(self, nums: List[int]) -> int: + ans = -1 + for i, x in enumerate(nums): + if i != x: + ans &= x + return max(ans, 0) ``` #### Java ```java - +class Solution { + public int sortPermutation(int[] nums) { + int ans = -1; + for (int i = 0; i < nums.length; ++i) { + if (i != nums[i]) { + ans &= nums[i]; + } + } + return Math.max(ans, 0); + } +} ``` #### C++ ```cpp - +class Solution { +public: + int sortPermutation(vectorYou are given two integer arrays value
and limit
, both of length n
.
Initially, all elements are inactive. You may activate them in any order.
@@ -223,25 +222,100 @@ edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3645.Ma #### Python3 ```python - +class Solution: + def maxTotal(self, value: List[int], limit: List[int]) -> int: + g = defaultdict(list) + for v, lim in zip(value, limit): + g[lim].append(v) + ans = 0 + for lim, vs in g.items(): + vs.sort() + ans += sum(vs[-lim:]) + return ans ``` #### Java ```java - +class Solution { + public long maxTotal(int[] value, int[] limit) { + MapYou are given an integer n
.
A number is called special if:
k
in the number appears exactly k
times.Return the smallest special number strictly greater than n
.
An integer is a palindrome if it reads the same forward and backward. For example, 121
is a palindrome, while 123
is not.
Example 1:
diff --git a/solution/3600-3699/3647.Maximum Weight in Two Bags/README.md b/solution/3600-3699/3647.Maximum Weight in Two Bags/README.md new file mode 100644 index 0000000000000..99eb184436356 --- /dev/null +++ b/solution/3600-3699/3647.Maximum Weight in Two Bags/README.md @@ -0,0 +1,249 @@ +--- +comments: true +difficulty: 中等 +edit_url: https://github.com/doocs/leetcode/edit/main/solution/3600-3699/3647.Maximum%20Weight%20in%20Two%20Bags/README.md +--- + + + +# [3647. 两个袋子中的最大重量 🔒](https://leetcode.cn/problems/maximum-weight-in-two-bags) + +[English Version](/solution/3600-3699/3647.Maximum%20Weight%20in%20Two%20Bags/README_EN.md) + +## 题目描述 + + + +给定一个整数数组 weights
和两个整数 w1
和 w2
表示两个袋子的 最大 容量。
每个物品 最多 可以放入一个袋子中,使得:
+ +w1
重量。w2
重量。返回两个袋子可以装入的 最大 总重量。
+ ++ +
示例 1:
+ +输入:weights = [1,4,3,2], w1 = 5, w2 = 4
+ +输出:9
+ +解释:
+ +weights[2] = 3
和 weights[3] = 2
满足 3 + 2 = 5 <= w1
weights[1] = 4
满足 4 <= w2
5 + 4 = 9
示例 2:
+ +输入:weights = [3,6,4,8], w1 = 9, w2 = 7
+ +输出:15
+ +解释:
+ +weights[3] = 8
满足 8 <= w1
weights[0] = 3
和 weights[2] = 4
满足 3 + 4 = 7 <= w2
8 + 7 = 15
示例 3:
+ +输入:weights = [5,7], w1 = 2, w2 = 3
+ +输出:0
+ +解释:
+ +没有可以放入两个袋子中的重量,所以答案为 0。
++ +
提示:
+ +1 <= weights.length <= 100
1 <= weights[i] <= 100
1 <= w1, w2 <= 300
You are given an integer array weights
and two integers w1
and w2
representing the maximum capacities of two bags.
Each item may be placed in at most one bag such that:
+ +w1
total weight.w2
total weight.Return the maximum total weight that can be packed into the two bags.
+ ++
Example 1:
+ +Input: weights = [1,4,3,2], w1 = 5, w2 = 4
+ +Output: 9
+ +Explanation:
+ +weights[2] = 3
and weights[3] = 2
as 3 + 2 = 5 <= w1
weights[1] = 4
as 4 <= w2
5 + 4 = 9
Example 2:
+ +Input: weights = [3,6,4,8], w1 = 9, w2 = 7
+ +Output: 15
+ +Explanation:
+ +weights[3] = 8
as 8 <= w1
weights[0] = 3
and weights[2] = 4
as 3 + 4 = 7 <= w2
8 + 7 = 15
Example 3:
+ +Input: weights = [5,7], w1 = 2, w2 = 3
+ +Output: 0
+ +Explanation:
+ +No weight fits in either bag, thus the answer is 0.
++
Constraints:
+ +1 <= weights.length <= 100
1 <= weights[i] <= 100
1 <= w1, w2 <= 300