You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: solution/2100-2199/2163.Minimum Difference in Sums After Removal of Elements/README_EN.md
+9-1
Original file line number
Diff line number
Diff line change
@@ -78,7 +78,15 @@ It can be shown that it is not possible to obtain a difference smaller than 1.
78
78
79
79
<!-- solution:start -->
80
80
81
-
### Solution 1
81
+
### Solution 1: Priority Queue (Max and Min Heap) + Prefix and Suffix Sum + Enumeration of Split Points
82
+
83
+
The problem is essentially equivalent to finding a split point in $nums$, dividing the array into two parts. In the first part, select the smallest $n$ elements, and in the second part, select the largest $n$ elements, so that the difference between the sums of the two parts is minimized.
84
+
85
+
We can use a max heap to maintain the smallest $n$ elements in the prefix, and a min heap to maintain the largest $n$ elements in the suffix. We define $pre[i]$ as the sum of the smallest $n$ elements among the first $i$ elements of the array $nums$, and $suf[i]$ as the sum of the largest $n$ elements from the $i$-th element to the last element of the array. During the process of maintaining the max and min heaps, update the values of $pre[i]$ and $suf[i]$.
86
+
87
+
Finally, we enumerate the split points in the range of $i \in [n, 2n]$, calculate the value of $pre[i] - suf[i + 1]$, and take the minimum value.
88
+
89
+
The time complexity is $O(n \times \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $nums$.
Copy file name to clipboardexpand all lines: solution/2100-2199/2164.Sort Even and Odd Indices Independently/README_EN.md
+42-6
Original file line number
Diff line number
Diff line change
@@ -76,7 +76,11 @@ The resultant array formed is [2,1], which is the same as the initial array.
76
76
77
77
<!-- solution:start -->
78
78
79
-
### Solution 1
79
+
### Solution 1: Sorting
80
+
81
+
We can extract the elements at odd and even indices separately, then sort the array of odd indices in non-increasing order and the array of even indices in non-decreasing order. Finally, merge the two arrays back together.
82
+
83
+
The time complexity is $O(n \log n)$, and the space complexity is $O(n)$. Here, $n$ is the length of the array $\text{nums}$.
80
84
81
85
<!-- tabs:start -->
82
86
@@ -131,16 +135,21 @@ public:
131
135
vector<int> a;
132
136
vector<int> b;
133
137
for (int i = 0; i < n; ++i) {
134
-
if (i % 2 == 0)
138
+
if (i % 2 == 0) {
135
139
a.push_back(nums[i]);
136
-
else
140
+
} else {
137
141
b.push_back(nums[i]);
142
+
}
138
143
}
139
144
sort(a.begin(), a.end());
140
-
sort(b.begin(), b.end(), greater<int>());
145
+
sort(b.rbegin(), b.rend());
141
146
vector<int> ans(n);
142
-
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) ans[i] = a[j];
143
-
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) ans[i] = b[j];
147
+
for (int i = 0, j = 0; j < a.size(); i += 2, ++j) {
148
+
ans[i] = a[j];
149
+
}
150
+
for (int i = 1, j = 0; j < b.size(); i += 2, ++j) {
Copy file name to clipboardexpand all lines: solution/2700-2799/2732.Find a Good Subset of the Matrix/README_EN.md
+13-1
Original file line number
Diff line number
Diff line change
@@ -82,7 +82,19 @@ The length of the chosen subset is 1.
82
82
83
83
<!-- solution:start -->
84
84
85
-
### Solution 1
85
+
### Solution 1: Case Analysis
86
+
87
+
We can consider the number of rows $k$ chosen for the answer from smallest to largest.
88
+
89
+
- If $k = 1$, the maximum sum of each column is $0$. Therefore, there must be a row where all elements are $0$, otherwise, the condition cannot be met.
90
+
- If $k = 2$, the maximum sum of each column is $1$. There must exist two rows, and the bitwise OR result of these two rows' elements is $0$, otherwise, the condition cannot be met.
91
+
- If $k = 3$, the maximum sum of each column is also $1$. If the condition for $k = 2$ is not met, then the condition for $k = 3$ will definitely not be met either. Therefore, we do not need to consider any case where $k > 2$ and $k$ is odd.
92
+
- If $k = 4$, the maximum sum of each column is $2$. This situation definitely occurs when the condition for $k = 2$ is not met, meaning that for any two selected rows, there exists at least one column with a sum of $2$. When choosing any 2 rows out of 4, there are a total of $C_4^2 = 6$ ways to choose, so there are at least $6$ columns with a sum of $2$. Since the number of columns $n \le 5$, there must be at least one column with a sum greater than $2$, so the condition for $k = 4$ is also not met.
93
+
- For $k > 4$ and $k$ being even, we can draw the same conclusion, that $k$ definitely does not meet the condition.
94
+
95
+
In summary, we only need to consider the cases of $k = 1$ and $k = 2$. That is, to check whether there is a row entirely composed of $0$s, or whether there exist two rows whose bitwise OR result is $0$.
96
+
97
+
The time complexity is $O(m \times n + 4^n)$, and the space complexity is $O(2^n)$. Here, $m$ and $n$ are the number of rows and columns of the matrix, respectively.
0 commit comments