lcp364

8048. 最大二进制奇数

方法一:贪心

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Solution {
public String maximumOddBinaryNumber(String s) {
int n = s.length(), cnt = 0;
for (int i = 0; i < n; ++i)
if (s.charAt(i) == '1')
++cnt;
if (cnt == 0)
return s;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cnt - 1; ++i)
sb.append('1');
for (int i = 0; i < n - cnt; ++i)
sb.append('0');
sb.append('1');
return sb.toString();
}
}

100049. 美丽塔 I

方法一:暴力\(O(n^2)\)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
class Solution {
public long maximumSumOfHeights(List<Integer> nums) {
int n = nums.size();
long res = 0;
for (int i = 0; i < n; ++i) {
int x = nums.get(i), pre = x;
long sum = x;
for (int j = i - 1; j >= 0; --j) {
int y = nums.get(j);
if (y > pre)
y = pre;
sum += y;
pre = y;
}
pre = x;
for (int j = i + 1; j < n; ++j) {
int y = nums.get(j);
if (y > pre)
y = pre;
sum += y;
pre = y;
}
res = Math.max(res, sum);
}
return res;
}
}

100048. 美丽塔 II


lcp364
https://leopol1d.github.io/2023/09/24/lcp364/
作者
Leopold
发布于
2023年9月24日
许可协议