当前位置:网站首页>CSDN(成长一夏竞赛)- 最大数
CSDN(成长一夏竞赛)- 最大数
2022-08-02 13:13:00 【放羊的牧码】
题目大意
给定任意一个数字 m,然后给出数字 n,则需在 m 中去掉 n 位数,保持各位顺序不变的情况下,得到最大数。
输入描述
输入整数n,m (1<=n<=1e100,1<=m<=100)
输出描述
输出删除后的最大数。
示例
输入:1234 2
输出:34
解题思路
- 计算出结果坑位数(size = n.length - m)
- 每一个坑位数的最大值下标[0, size]、[0, size + 1]、[0, size + 2]……
- 最难理解在第二点,因为题目说保障前后顺序,所以比如 12345 2,那么,坑位数为 3,第一个位置的最大值在[0, 2],第二个[0, 3],第三个[0, 4],中途如果被用过的数字需要做下标记,后面的坑位不能再使用
相关企业
- CSDN
AC 代码
import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String str_0 = scan.nextLine();
String[] line_list_0 = str_0.trim().split(" ");
ArrayList<String> arr = new ArrayList<>();
for(int i = 0; i < line_list_0.length; i++){
arr.add(line_list_0[i]);
}
scan.close();
String result = solution(arr);
System.out.println(result);
}
public static String solution(ArrayList<String> arr){
String str = arr.get(0);
int cnt = Integer.valueOf(arr.get(1));
int len = str.length();
int diff = len - cnt;
char[] chars = new char[diff];
char[] pre = str.toCharArray();
for (int i = 0, from = 0; i < diff; i++) {
// j = from,优化,因为下一个坑位数不可能在 from 之前
for (int j = from; j <= cnt + i; j++) {
if (chars[i] < pre[j]) {
chars[i] = pre[j];
from = j + 1;
}
}
}
return String.valueOf(chars);
}
}边栏推荐
猜你喜欢

基于华为eNSP的企业网络规划

Introduction to Scala Basic Syntax (3) Various Operators in Scala

暑假集训-week2图论
![Embedded system driver primary [2] - based on character device driver _ basic framework](/img/71/4feb9e42fd2ff1bc4a2d49798657b2.png)
Embedded system driver primary [2] - based on character device driver _ basic framework

"Second Uncle" is popular, do you know the basic elements of "exploding" short videos from the media?

【622. 设计循环队列】

Ribbon负载均衡的深度分析和使用

Enterprise Network Planning Based on Huawei eNSP

A powerful js pop-up alert plugin

86.(cesium之家)cesium叠加面接收阴影效果(gltf模型)
随机推荐
[C language] Analysis of function recursion (3)
Automatically generate code generator recommendation-code-gen
Redis全部
3 ways for OpenFeign to set headers
qt 编译报错 No rule to make target
RESTful style (detailed introduction + case implementation)
Basic operations of openGauss database (super detailed)
SQL Server 2014 installation tutorial (nanny-level graphic tutorial)
ETL(二):表达式组件的使用
Redis all
[C language] Explicit array solution (1)
Summer training camp-week2 graph theory
Do you know Dijkstra of graph theory?
Win11怎么修改关机界面颜色?Win11修改关机界面颜色的方法
数值的整数次方
GCC版本升级到指定版本
WPF效果第一百九十三篇之登录实现
第48篇-timestamp2参数分析【2022-08-01】
【C语言】虐打循环练习题(2)
.Net 5.0 Quick Start Redis