美团校招机试 - 小美的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;
}
相关推荐
std860214 小时前
Rust 与 Python – 这是未来的语言吗?
开发语言·python·rust
东东2334 小时前
前端开发中如何取消Promise操作
前端·javascript·promise
Json_4 小时前
学习springBoot框架-开发一个酒店管理系统,熟悉springboot框架语法~
java·spring boot·后端
Moniane4 小时前
FastGPT 与 MCP 协议概述
算法
掘金安东尼4 小时前
官方:什么是 Vite+?
前端·javascript·vue.js
相偎4 小时前
用观察者模式通知UI刷新数据
c++
kkkkk0211064 小时前
微服务学习笔记(黑马商城)
java·spring boot·spring·spring cloud·sentinel·mybatis·java-rabbitmq
鄃鳕4 小时前
Flask【python】
后端·python·flask
2503_930123934 小时前
Kubernetes (六)调度策略详解:从节点匹配到Pod调度全流程
java·开发语言
weixin_46684 小时前
Python编程之面向对象
开发语言·人工智能·python