Greatest English Letter in Upper and Lower Case
Given a string of English letters s
, return the greatest English letter which occurs as both a lowercase and uppercase letter in s
. The returned letter should be in uppercase. If no such letter exists, return an empty string.
An English letter b
is greater than another letter a
if b
appears after a
in the English alphabet.
Example 1:
Input: s = "lEeTcOdE"
Output: "E"
Explanation:
The letter 'E' is the only letter to appear in both lower and upper case.
找同一个字母大小写同时出现的,并且这个字母最靠后。
pii模拟
class Solution {
public:
string greatestLetter(string s) {
vector<pair<int, int>> m(26);
for (auto &ch: s) {
if (ch >= 'A' && ch <= 'Z') {
m[ch - 'A'].first = 1;
} else {
m[ch - 'a'].second = 1;
}
}
for (int i = 25; i >= 0; i--) {
if (m[i].first == 1 && m[i].second == 1) {
string t = "";
t += 'A' + i;
return t;
}
}
return "";
}
};
Sum of Numbers With Units Digit K
Given two integers num
and k
, consider a set of positive integers with the following properties:
- The units digit of each integer is
k
. - The sum of the integers is
num
.
Return the minimum possible size of such a set, or -1
if no such set exists.
Note:
- The set can contain multiple instances of the same integer, and the sum of an empty set is considered
0
. - The units digit of a number is the rightmost digit of the number.
Example 1:
Input: num = 58, k = 9
Output: 2
Explanation:
One valid set is [9,49], as the sum is 58 and each integer has a units digit of 9.
Another valid set is [19,39].
It can be shown that 2 is the minimum possible size of a valid set.
给num反复的减去一个尾数为k的数,最后把num减没,否则就是-1;
特判一下0
然后贪心一下
然后最大次数
class Solution {
public:
int minimumNumbers(int num, int k) {
int ans = 0, times = 0;
if (num == 0) { return 0; }
while (num != 0) {
if (num % 10 == k) {
return ans + 1;
} else {
num = num - k;
ans++;
}
if (++times > 10 || num < 0) { return -1; }
}
return -1;
}
};
Longest Binary Subsequence Less Than or Equal to K
You are given a binary string s
and a positive integer k
.
Return the length of the longest subsequence of s
that makes up a binary number less than or equal to k
.
Note:
- The subsequence can contain leading zeroes.
- The empty string is considered to be equal to
0
. - A subsequence is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.
Example 1:
Input: s = "1001010", k = 5
Output: 5
Explanation: The longest subsequence of s that makes up a binary number less than or equal to 5 is "00010", as this number is equal to 2 in decimal.
Note that "00100" and "00101" are also possible, which are equal to 4 and 5 in decimal, respectively.
The length of this subsequence is 5, so 5 is returned.
删除一些0 和1,然后让子串的十进制小于k
要保证出现更多的0和更多的1,也就是要看一下后面往前找的什么时候二进制大于k了,大于了就跳。否则就继续。
因为2^1000大于int128范围了,开个py吧
C++选手用py真的难受
class Solution:
def longestSubsequence(self, s: str, k: int) -> int:
pre=0;
for i in range(len(s)):
if(s[i]=='0'):
pre+=1
i = len(s)-1
c = 1
cnt=0
while(i>=0):
if(s[i]=='1'):
cnt=cnt+c
else:
pre-=1
if(cnt>k):
break
i-=1
c=c*2
return len(s)-i-1+pre
Selling Pieces of Wood
You are given two integers m
and n
that represent the height and width of a rectangular piece of wood. You are also given a 2D integer array prices
, where prices[i] = [hi, wi, pricei]
indicates you can sell a rectangular piece of wood of height hi
and width wi
for pricei
dollars.
To cut a piece of wood, you must make a vertical or horizontal cut across the entire height or width of the piece to split it into two smaller pieces. After cutting a piece of wood into some number of smaller pieces, you can sell pieces according to prices
. You may sell multiple pieces of the same shape, and you do not have to sell all the shapes. The grain of the wood makes a difference, so you cannot rotate a piece to swap its height and width.
Return the maximum money you can earn after cutting an m x n
piece of wood.
Note that you can cut the piece of wood as many times as you want.
Example 1:
Input: m = 3, n = 5, prices = [[1,4,2],[2,2,7],[2,1,3]]
Output: 19
Explanation: The diagram above shows a possible scenario. It consists of:
- 2 pieces of wood shaped 2 x 2, selling for a price of 2 * 7 = 14.
- 1 piece of wood shaped 2 x 1, selling for a price of 1 * 3 = 3.
- 1 piece of wood shaped 1 x 4, selling for a price of 1 * 2 = 2.
This obtains a total of 14 + 3 + 2 = 19 money earned.
It can be shown that 19 is the maximum amount of money that can be earned.
裸题啊,dp枚举每个分割点就出了
老年人自信了,不应该太自信的去记录的。。。
class Solution {
public:
long long dp[201][201];
long long pre[201][201];
long long sellingWood(int m, int n, vector<vector<int>> &prices) {
for (auto &i: prices) {
pre[i[0]][i[1]] = i[2];
}
for (int i = 1; i <= m; i++) {
for (int j = 1; j <= n; j++) {
dp[i][j] = pre[i][j];
for (int k = 1; k < i; k++) {
dp[i][j] = max(dp[i][j], dp[k][j] + dp[i - k][j]);
}
for (int k = 1; k < j; k++) {
dp[i][j] = max(dp[i][j], dp[i][j - k] + dp[i][k]);
}
}
}
return dp[m][n];
}
};
Comments | NOTHING