Merge branch 'master' of github.com:jinbudaily/leetcode-master

This commit is contained in:
jinbudaily
2023-07-21 15:24:49 +08:00
3 changed files with 38 additions and 12 deletions

View File

@ -88,7 +88,7 @@ public:
return false;
}
for (int i = 0; i < magazine.length(); i++) {
// 通过recode数据记录 magazine里各个字符出现次数
// 通过record数据记录 magazine里各个字符出现次数
record[magazine[i]-'a'] ++;
}
for (int j = 0; j < ransomNote.length(); j++) {
@ -219,7 +219,7 @@ class Solution:
```go
func canConstruct(ransomNote string, magazine string) bool {
record := make([]int, 26)
for _, v := range magazine { // 通过recode数据记录 magazine里各个字符出现次数
for _, v := range magazine { // 通过record数据记录 magazine里各个字符出现次数
record[v-'a']++
}
for _, v := range ransomNote { // 遍历ransomNote在record里对应的字符个数做--操作

View File

@ -270,18 +270,29 @@ impl Solution {
**动态规划**
```Rust
impl Solution {
fn max(a: i32, b: i32) -> i32 {
if a > b { a } else { b }
}
pub fn max_profit(prices: Vec<i32>, fee: i32) -> i32 {
let n = prices.len();
let mut dp = vec![vec![0; 2]; n];
dp[0][0] -= prices[0];
for i in 1..n {
dp[i][0] = Self::max(dp[i - 1][0], dp[i - 1][1] - prices[i]);
dp[i][1] = Self::max(dp[i - 1][1], dp[i - 1][0] + prices[i] - fee);
let mut dp = vec![vec![0; 2]; prices.len()];
dp[0][0] = -prices[0];
for (i, &p) in prices.iter().enumerate().skip(1) {
dp[i][0] = dp[i - 1][0].max(dp[i - 1][1] - p);
dp[i][1] = dp[i - 1][1].max(dp[i - 1][0] + p - fee);
}
Self::max(dp[n - 1][0], dp[n - 1][1])
dp[prices.len() - 1][1]
}
}
```
**动态规划空间优化**
```rust
impl Solution {
pub fn max_profit(prices: Vec<i32>, fee: i32) -> i32 {
let (mut low, mut res) = (-prices[0], 0);
for p in prices {
low = low.max(res - p);
res = res.max(p + low - fee);
}
res
}
}
```

View File

@ -156,6 +156,21 @@ var balancedStringSplit = function(s) {
};
```
### TypeScript
```ts
function balancedStringSplit(s: string): number {
let count: number = 0
let res: number = 0
for(let i of s){
if(i === 'R') count++
else count--
if(count === 0) res++
}
return res
};
```
<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">