美团校招机试 - 小美的MT(20240309-T3)

题目来源

美团校招笔试真题_小美的MT

题目描述

MT 是美团的缩写,因此小美很喜欢这两个字母。

现在小美拿到了一个仅由大写字母组成字符串,她可以最多操作 k 次,每次可以修改任意一个字符。

小美想知道,操作结束后最多共有多少个 'M' 和 'T' 字符?

输入描述

第一行输入两个正整数 n,k,代表字符串长度和操作次数。

第二行输入一个长度为 n 的、仅由大写字母组成的字符串。

  • 1 ≤ k ≤ n ≤ 10^5

输出描述

输出操作结束后最多共有多少个 'M' 和 'T' 字符。

用例

|----|--------------------------------------------|
| 输入 | 5 2 MTUAN |
| 输出 | 4 |
| 说明 | 修改第三个和第五个字符,形成的字符串为 MTTAM,这样共有 4 个'M'和'T'。 |

题目解析

本题我们只需要统计出第二行输入的s串中:

M和T字符的数量:mt_count,以及非M、T字符的数量:not_mt_count。

由于我们最多可以操作k次,即最多将k个非M、T字符修改为M或T字符:

  • 若 not_mt_count >= k,则我们最多增加 k 个 M或T 字符
  • 若 not_mt_count < k, 则我们最多增加 not_mt_count 个 M或T 字符

即最终最多有 mt_count + min(k, not_mt_count) 个 M或T 字符。

JS算法源码

javascript 复制代码
const rl = require("readline").createInterface({ input: process.stdin });
var iter = rl[Symbol.asyncIterator]();
const readline = async () => (await iter.next()).value;

void (async function () {
  const [n, k] = (await readline()).split(" ").map(Number);
  const s = await readline();

  let mt_count = 0;
  let not_mt_count = 0;

  for (let c of s) {
    if (c == "M" || c == "T") {
      mt_count++;
    } else {
      not_mt_count++;
    }
  }

  const ans = Math.min(k, not_mt_count) + mt_count;

  console.log(ans);
})();

Java算法源码

java 复制代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int k = sc.nextInt();
        String s = sc.next();

        int mt_count = 0;
        int not_mt_count = 0;

        for (int i = 0; i < n; i++) {
            char c = s.charAt(i);

            if (c == 'M' || c == 'T') {
                mt_count++;
            } else {
                not_mt_count++;
            }
        }

        int ans = Math.min(k, not_mt_count) + mt_count;

        System.out.println(ans);
    }
}

Python算法源码

python 复制代码
if __name__ == "__main__":
    n, k = map(int, input().split())
    s = input()

    mt_count = 0
    not_mt_count = 0

    for c in s:
        if c == "M" or c == "T":
            mt_count += 1
        else:
            not_mt_count += 1

    ans = min(k, not_mt_count) + mt_count

    print(ans)

C算法源码

cpp 复制代码
#include <stdio.h>
#include <math.h>

#define MAX_LEN 100001

int main() {
    int n, k;
    scanf("%d %d", &n, &k);

    char s[MAX_LEN];
    scanf("%s", s);

    int mt_count = 0;
    int not_mt_count = 0;

    int i = 0;
    while (s[i] != '\0') {
        if (s[i] == 'M' || s[i] == 'T') {
            mt_count++;
        } else {
            not_mt_count++;
        }
        i++;
    }

    int ans = (int) fmin(k, not_mt_count) + mt_count;

    printf("%d\n", ans);

    return 0;
}

C++算法源码

cpp 复制代码
#include <bits/stdc++.h>

using namespace std;

int main() {
    int n, k;
    cin >> n >> k;

    string s;
    cin >> s;

    int mt_count = 0;
    int not_mt_count = 0;

    for (const auto &c: s) {
        if (c == 'M' || c == 'T') {
            mt_count++;
        } else {
            not_mt_count++;
        }
    }

    int ans = min(k, not_mt_count) + mt_count;

    cout << ans << endl;

    return 0;
}
相关推荐
MZ_ZXD0011 分钟前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
PP东4 分钟前
Flowable学习(二)——Flowable概念学习
java·后端·学习·flowable
ManThink Technology9 分钟前
如何使用EBHelper 简化EdgeBus的代码编写?
java·前端·网络
invicinble13 分钟前
springboot的核心实现机制原理
java·spring boot·后端
人道领域14 分钟前
AI抢人大战:谁在收割你的红包
大数据·人工智能·算法
人道领域21 分钟前
SSM框架从入门到入土(AOP面向切面编程)
java·开发语言
铅笔侠_小龙虾22 分钟前
Flutter 实战: 计算器
开发语言·javascript·flutter
TracyCoder12333 分钟前
LeetCode Hot100(34/100)——98. 验证二叉搜索树
算法·leetcode
A尘埃33 分钟前
电信运营商用户分群与精准运营(K-Means聚类)
算法·kmeans·聚类
全栈老石39 分钟前
Python 异步生存手册:给被 JS async/await 宠坏的全栈工程师
后端·python