Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using LL = long long;
class Solution {
public:
long long maxPower(vector<int>& stations, int r, int k)
{
LL left = 0, right = LLONG_MAX;
while (left < right)
{
LL mid = right-(right-left)/2;
if (isOK(stations, r, k, mid))
left = mid;
else
right = mid-1;
}
return left;
}

bool isOK(vector<int>stations, int r, LL k, LL m)
{
int n = stations.size();
LL sum = 0;
for (int i=0; i<=min(n-1, r-1); i++)
sum += stations[i];

for (int i=0; i<n; i++)
{
if (i+r < n)
sum += stations[i+r];

if (i-r-1 >= 0)
sum -= stations[i-r-1];

if (sum >= m) continue;

LL diff = m - sum;
if (diff > k)
return false;
stations[min(n-1, i+r)] += diff;
sum = m;
k -= diff;
}
return true;
}
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### 2528.Maximize-the-Minimum-Powered-City

我们假设The minimum power of a city是m,那么意味着每个城市都可以得到至少m的供应。我们就顺着走一遍每个城市,看它是否已经实现了周围[i-r,i+r]范围内的滑窗和大于等于m。如果不够m,那么我们必然会贪心地增加最优端的那个城市的供给(因为它能覆盖更多未来的城市)补齐至m。一路走下去,如果总共需要补建的电力站小于等于k,那么就说明m是可以实现的,于是可以尝试猜测更大的m。如果需要补建的电力站大于k,那么就说明m不可行,就尝试猜测更小的m。
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Solution {
public:
int xorBeauty(vector<int>& nums)
{
int sum = 0;
for (int x: nums)
sum ^= x;
return sum;

}
};
2 changes: 2 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@
[2439.Minimize-Maximum-of-Array](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/2439.Minimize-Maximum-of-Array) (H-)
[2517.Maximum-Tastiness-of-Candy-Basket](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/2517.Maximum-Tastiness-of-Candy-Basket) (M+)
[2513.Minimize-the-Maximum-of-Two-Arrays](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/2513.Minimize-the-Maximum-of-Two-Arrays) (H)
[2528.Maximize-the-Minimum-Powered-City](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/2528.Maximize-the-Minimum-Powered-City) (H-)
* ``Find K-th Element``
[215.Kth-Largest-Element-in-an-Array](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/215.Kth-Largest-Element-in-an-Array) (M)
[287.Find-the-Duplicate-Number](https://github.com/wisdompeak/LeetCode/tree/master/Binary_Search/287.Find-the-Duplicate-Number) (H-)
Expand Down Expand Up @@ -859,6 +860,7 @@
[1734.Decode-XORed-Permutation](https://github.com/wisdompeak/LeetCode/tree/master/Bit_Manipulation/1734.Decode-XORed-Permutation) (M+)
[1738.Find-Kth-Largest-XOR-Coordinate-Value](https://github.com/wisdompeak/LeetCode/tree/master/Bit_Manipulation/1738.Find-Kth-Largest-XOR-Coordinate-Value) (M+)
[1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND](https://github.com/wisdompeak/LeetCode/tree/master/Bit_Manipulation/1835.Find-XOR-Sum-of-All-Pairs-Bitwise-AND) (M)
[2527.Find-Xor-Beauty-of-Array](https://github.com/wisdompeak/LeetCode/tree/master/Bit_Manipulation/2527.Find-Xor-Beauty-of-Array) (H)
* ``Bit Mask``
[320.Generalized-Abbreviation](https://github.com/wisdompeak/LeetCode/tree/master/String/320.Generalized-Abbreviation) (M)
[1239.Maximum-Length-of-a-Concatenated-String-with-Unique-Characters](https://github.com/wisdompeak/LeetCode/tree/master/Bit_Manipulation/1239.Maximum-Length-of-a-Concatenated-String-with-Unique-Characters) (M+)
Expand Down