Skip to content

Commit 4e2dfac

Browse files
committed
添加 C++ 解决方案以计算字符串的最大得分
1 parent 7c8ae4a commit 4e2dfac

7 files changed

Lines changed: 179 additions & 0 deletions

File tree

  • leetcode/Study Plan
    • 1422. 分割字符串的最大得分
    • 3432. 统计元素和差值为偶数的分区方案
    • 3577. 统计计算机解锁顺序排列数
    • 3583. 统计特殊三元组
    • 867. 转置矩阵
  • niuke/daily_problem
    • 25_12-10大撒币
    • 25_12-11小红的矩阵
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
int maxScore(string s) {
4+
int right1 = ranges::count(s, '1');
5+
int left0 = 0;
6+
int maxsocre = 0;
7+
for (int i = 0; i < s.size() - 1; ++i) {
8+
if (s[i] == '0')
9+
left0++;
10+
else
11+
right1--;
12+
maxsocre = max(maxsocre, left0 + right1);
13+
}
14+
return maxsocre;
15+
}
16+
};

leetcode/Study Plan/3432. 统计元素和差值为偶数的分区方案/代码解释.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,4 @@ double sum = accumulate(nums.begin(), nums.end(), 0.0);
278278

279279

280280

281+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
class Solution {
2+
public:
3+
int countPermutations(vector<int>& complexity) {
4+
int n = complexity.size();
5+
const int mod = 1e9+7;
6+
long long ans = 1;
7+
for(int j = 1;j<n;++j)
8+
{
9+
if(complexity[j] < complexity[0])
10+
{
11+
return 0;
12+
}
13+
ans = (1LL*ans*j)%mod;
14+
}
15+
return ans;
16+
}
17+
};
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution {
2+
public:
3+
int specialTriplets(vector<int> &nums) {
4+
const int mod = 1e9+7;
5+
unordered_map<int,int>suf;
6+
for(int x:nums)
7+
{
8+
suf[x]++;
9+
}
10+
unordered_map<int,int>pre;
11+
long long ans = 0;
12+
for(int x:nums)
13+
{
14+
suf[x]--;
15+
ans+=1LL*pre[x*2]*suf[x*2];
16+
pre[x]++;
17+
}
18+
return ans%mod;
19+
}
20+
};
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public:
3+
vector<vector<int>> transpose(vector<vector<int>> &matrix) {
4+
int n = matrix.size();
5+
int m = matrix[0].size();
6+
vector<vector<int>> res(m,vector<int>(n,0));
7+
for(int i = 0;i<n;++i)
8+
{
9+
for(int j = 0;j<m;++j)
10+
{
11+
res[j][i] = matrix[i][j];
12+
}
13+
}
14+
return res;
15+
}
16+
};
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include<bits/stdc++.h>
2+
#define il inline
3+
using namespace std;
4+
5+
#define pb push_back
6+
#define fastio \
7+
ios::sync_with_stdio(false); \
8+
cin.tie(0);
9+
10+
typedef long long ll;
11+
typedef unsigned long long ull;
12+
13+
const ll N = 5e5+5, mod = 1e9+7, inf = 2e18;
14+
const double eps = 1e-9;
15+
const double PI = 3.1415926;
16+
17+
il void solve(){
18+
int a,b,r;
19+
cin >> a >> b >> r;
20+
if(min(a,b) >= 2*r)
21+
cout << "Alice\n";
22+
else
23+
cout << "Bob\n";
24+
25+
}
26+
27+
int main()
28+
{
29+
fastio
30+
31+
int t = 1;
32+
// cin >> t;
33+
while(t--)
34+
{
35+
solve();
36+
}
37+
38+
return 0;
39+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
#include <bits/stdc++.h>
2+
#define il inline
3+
using namespace std;
4+
5+
#define pb push_back
6+
#define fastio \
7+
ios::sync_with_stdio(false); \
8+
cin.tie(0);
9+
10+
typedef long long ll;
11+
typedef unsigned long long ull;
12+
13+
const ll N = 5e5 + 5, mod = 1e9 + 7, inf = 2e18;
14+
const double eps = 1e-9;
15+
const double PI = 3.1415926;
16+
17+
long long n, m, k;
18+
19+
/**
20+
* 计算在 n×m 矩阵中,有多少个元素的值 ≤ x
21+
* 矩阵第 i 行第 j 列的值为 i*j
22+
* @param x 阈值
23+
* @return 小于等于 x 的元素个数
24+
*/
25+
long long work(long long x) {
26+
long long res = 0;
27+
// 遍历每一行
28+
for (int i = 1; i <= n; ++i) {
29+
// 第 i 行中,满足 i*j <= x 的 j 有 x/i 个
30+
// 但 j 最大为 m,所以取 min(x/i, m)
31+
res += min(x / i, m);
32+
// 优化:如果 x/i == 0,后面的行更不可能有贡献
33+
if (x / i == 0)
34+
break;
35+
}
36+
return res;
37+
}
38+
39+
il void solve() {
40+
cin >> n >> m >> k;
41+
// 二分查找第 k 小的元素
42+
// 搜索范围:[1, n*m](矩阵中最小值为1,最大值为n*m)
43+
long long l = 1, r = n * m, ans = 0;
44+
while (l <= r) {
45+
// 防止溢出的中点计算方式
46+
long long mid = l + ((r - l) >> 1);
47+
// 如果小于等于 mid 的元素个数 >= k
48+
// 说明第 k 小的元素 <= mid,在左半部分继续搜索
49+
if (work(mid) >= k) {
50+
ans = mid; // 记录可能的答案
51+
r = mid - 1; // 尝试找更小的值
52+
} else {
53+
// 否则第 k 小的元素在右半部分
54+
l = mid + 1;
55+
}
56+
}
57+
cout << ans << '\n';
58+
}
59+
60+
int main() {
61+
fastio // 快速输入输出
62+
63+
int t = 1;
64+
// cin >> t; // 多组测试数据时取消注释
65+
while (t--) {
66+
solve();
67+
}
68+
69+
return 0;
70+
}

0 commit comments

Comments
 (0)