Skip to content

Commit 977cccb

Browse files
author
Danieldu
committed
uodate code
1 parent 765d972 commit 977cccb

4 files changed

+135
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* @lc app=leetcode id=2206 lang=cpp
3+
*
4+
* [2206] Divide Array Into Equal Pairs
5+
*/
6+
7+
// @lc code=start
8+
#include "bits/stdc++.h"
9+
10+
class Solution {
11+
public:
12+
bool divideArray(std::vector<int>& nums) {
13+
// Fixed-size array to count occurrences (since x is in [1, 500])
14+
int count[501] = {0};
15+
16+
// Count each number's occurrence
17+
for (int num : nums) {
18+
count[num]++;
19+
}
20+
21+
for (int i = 1; i <= 500; ++i) {
22+
if (count[i] % 2 != 0) {
23+
return false;
24+
}
25+
}
26+
27+
return true;
28+
}
29+
};
30+
// @lc code=end
31+

Array/3467.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include "bits/stdc++.h"
2+
using namespace std;
3+
4+
class Solution {
5+
public:
6+
vector<int> transformArray(vector<int>& nums) {
7+
int n = nums.size();
8+
int odd =0;
9+
for(int i = 0; i < n; i++){
10+
if (nums[i]%2)
11+
odd++;
12+
}
13+
for(int i = n-1; i>=0; i--){
14+
if (odd>0){
15+
nums[i]=1;
16+
odd--;
17+
continue;
18+
}
19+
nums[i]=0;
20+
}
21+
22+
return nums;
23+
}
24+
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* @lc app=leetcode id=2594 lang=cpp
3+
*
4+
* [2594] Minimum Time to Repair Cars
5+
*/
6+
7+
// @lc code=start
8+
#include "bits/stdc++.h"
9+
using namespace std;
10+
11+
class Solution {
12+
public:
13+
bool canRepairInTime(const vector<int>& ranks, int cars, long long time) {
14+
long long total_cars = 0;
15+
for (const auto& rank : ranks) {
16+
total_cars += static_cast<long long>(std::sqrt(time / rank));
17+
if (total_cars >= cars) {
18+
return true;
19+
}
20+
}
21+
return total_cars >= cars;
22+
}
23+
24+
long long repairCars(std::vector<int>& ranks, int cars) {
25+
long long left = 0;
26+
long long min_rank = *std::min_element(ranks.begin(), ranks.end());
27+
long long right = static_cast<long long>(min_rank) * cars * cars;
28+
long long answer = right;
29+
30+
while (left <= right) {
31+
long long mid = left + (right - left) / 2;
32+
if (canRepairInTime(ranks, cars, mid)) {
33+
answer = mid;
34+
right = mid - 1;
35+
} else {
36+
left = mid + 1;
37+
}
38+
}
39+
40+
return answer;
41+
}
42+
};
43+
// @lc code=end
44+
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* @lc app=leetcode id=2379 lang=cpp
3+
*
4+
* [2379] Minimum Recolors to Get K Consecutive Black Blocks
5+
*/
6+
7+
// @lc code=start
8+
#include "bits/stdc++.h"
9+
class Solution {
10+
public:
11+
int minimumRecolors(std::string blocks, int k) {
12+
int current_white = 0;
13+
int min_white;
14+
15+
for (int i = 0; i < k; ++i) {
16+
if (blocks[i] == 'W') {
17+
++current_white;
18+
}
19+
}
20+
min_white = current_white;
21+
22+
for (int i = k; i < blocks.size(); ++i) {
23+
if (blocks[i - k] == 'W') {
24+
--current_white;
25+
}
26+
if (blocks[i] == 'W') {
27+
++current_white;
28+
}
29+
min_white = min(min_white, current_white);
30+
}
31+
return min_white;
32+
}
33+
};
34+
35+
// @lc code=end
36+

0 commit comments

Comments
 (0)