Skip to content

Commit 6421625

Browse files
committed
first contest in c++
1 parent e764654 commit 6421625

File tree

3 files changed

+94
-0
lines changed

3 files changed

+94
-0
lines changed

3467. Transform Array by Parity.cpp

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#include <bits/stdc++.h>
2+
3+
using namespace std;
4+
5+
class Solution {
6+
public:
7+
vector<int> transformArray(vector<int> &nums) {
8+
for (int i = 0; i < nums.size(); i++) {
9+
nums[i] &= 1;
10+
}
11+
sort(nums.begin(), nums.end());
12+
return nums;
13+
}
14+
};
15+
16+
int main() {
17+
ios_base::sync_with_stdio(false);
18+
cin.tie(nullptr);
19+
20+
Solution sol;
21+
vector<int> input = {1, 2, 3, 4};
22+
vector<int> ans = sol.transformArray(input);
23+
24+
for (int i: ans) {
25+
cout << i << "\n";
26+
}
27+
28+
return 0;
29+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public:
3+
int countArrays(vector<int> &original, vector <vector<int>> &bounds) {
4+
int n = original.size();
5+
for (int i = 0; i < n; i++) {
6+
bounds[i][0] -= original[i];
7+
bounds[i][1] -= original[i];
8+
}
9+
10+
int low = -(1 << 30);
11+
int high = 1 << 30;
12+
for (int i = 0; i < n; i++) {
13+
low = max(low, bounds[i][0]);
14+
high = min(high, bounds[i][1]);
15+
}
16+
return max(0, high - low + 1);
17+
}
18+
};
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// https://leetcode.com/problems/find-minimum-cost-to-remove-array-elements/
2+
// standard array DP
3+
// - for each index, we can take one of three elements
4+
// - the one not taken will be split off from the array, there will be at most
5+
// one element that is split off since we take 2 of three each time
6+
7+
#include <bits/stdc++.h>
8+
9+
using namespace std;
10+
11+
class Solution {
12+
public:
13+
vector<unordered_map<int, int>> memo;
14+
15+
int solve(vector<int> &arr, int idx, int extra) {
16+
if (idx == arr.size() - 1)
17+
return max(arr[idx], extra);
18+
if (idx == arr.size())
19+
return extra;
20+
if (memo[idx].count(extra))
21+
return memo[idx][extra];
22+
23+
int best = 1 << 30;
24+
best = min(best, solve(arr, idx + 2, arr[idx + 1]) + max(extra, arr[idx]));
25+
best = min(best, solve(arr, idx + 2, extra) + max(arr[idx], arr[idx + 1]));
26+
best = min(best, solve(arr, idx + 2, arr[idx]) + max(arr[idx + 1], extra));
27+
28+
return memo[idx][extra] = best;
29+
}
30+
31+
int minCost(vector<int> &nums) {
32+
memo.resize(nums.size() + 2);
33+
if (nums.size() < 3)
34+
return *max_element(nums.begin(), nums.end());
35+
36+
return solve(nums, 1, nums[0]);
37+
}
38+
};
39+
40+
41+
int main() {
42+
ios_base::sync_with_stdio(false);
43+
cin.tie(nullptr);
44+
45+
46+
return 0;
47+
}

0 commit comments

Comments
 (0)