Build Lowest Number by Removing n digits from a given number

Given a string ‘str’ of digits and an integer ‘n’, build the lowest possible number by removing ‘n’ digits from the string and not changing the order of input digits.

Examples:

Input: str = "4325043", n = 3 Output: "2043"

Input: str = "765028321", n = 5 Output: "0221"

Input: str = "121198", n = 2 Output: "1118"

解法一:目标是要得到用字符串表示的最小数字,所以要使高位的数字尽量小,可以使用贪心策略,从左往右扫描字符串中的数字,比较相邻两个数字,如果左边的数字比右边的大,则应该删除左边的数字,否则,两个指针同时右移一位,直到比较到最后两个数字或者已经删除的数字的个数等于n为止;如果扫描完一遍,已经删除的数字的个数为n‘,假设n‘小于n,则对新的数字重复上述操作,此时有一种特殊情况是,假设n‘为0,说明字符串中的数字已经是升序排列,这时,只需要删除字符串最后的n个数字即可。java的代码如下:

public class LowestNumber {
  public String lowestNumber(String str, int n){

    if(n <= 0){
      return str;
    }

    int len = str.length();
    if(n >= len){
      return "";
    }

    String rst = "";

    int l = 0;
    int r = 1;
    int d = 0;
    while((d < n)&&(r < len)){
      char cl = str.charAt(l);
      char cr = str.charAt(r);
      if(cl <= cr){
        rst += String.valueOf(cl);
      } else {
        d += 1;
      }
      l = r;
      r += 1;
    }

    if(d == 0){
      return str.substring(0, len-n);
    }
    rst += str.substring(l);

    return lowestNumber(rst, n-d);
  }
}

解法一的时间复杂度最差的情况下应该是O(nm),其中m表示输入字符串的长度。显然一位一位地删除太低效了,事实上,我们会注意到一个事实就是,假设输入字符串的长度为m,需要删除的字符的数目为n,则前n+1个字符中的最小值一定在结果字符串中,这一点可以用反正法来证明,假设前n+1个字符全不在结果字符串中,则输出字符串的长度小于m-n,所以不符合要求,而假设前n+1个字符串的非最小值在结果字符串中,则显然可以得到更小的结果字符串,所以基于这一点,我们可以一次找到前n+1个字符中的最小值,然后删除从第一位到最小值之前的所有数字,然后从最小值后的第一个字符开始,递归处理。这是解法二,可以参考:http://www.geeksforgeeks.org/build-lowest-number-by-removing-n-digits-from-a-given-number/ 中的code;

时间: 2024-11-08 22:49:26

Build Lowest Number by Removing n digits from a given number的相关文章

Leetcode-1085 sum of digits in the minimum number(最小元素各数位之和)

1 #define _for(i,a,b) for(int i = (a);i < b;i ++) 2 3 class Solution 4 { 5 public: 6 int sumOfDigits(vector<int>& A) 7 { 8 int mm = A[0]; 9 _for(i,0,A.size()) 10 { 11 if(A[i]<mm) 12 mm = A[i]; 13 } 14 int S = 0; 15 while(mm) 16 { 17 S += m

c# BlowFish 高速 对称加密

BlowFish 高速 对称加密 string key = "this is my key"; BlowFish algo = new BlowFish(key); string encryptedTxt = algo.Encrypt_CBC("this is my test string"); string decryptedTxt = algo.Decrypt_CBC(encryptedTxt); algo = new BlowFish(key); byte[]

[codeforces 509]C. Sums of Digits

试题描述 Vasya had a strictly increasing sequence of positive integers a1, ..., an. Vasya used it to build a new sequence b1, ..., bn, where bi is the sum of digits of ai's decimal representation. Then sequence ai got lost and all that remained is sequen

python3.4 build in functions from 官方文档 翻译中

2. Built-in Functions https://docs.python.org/3.4/library/functions.html?highlight=file The Python interpreter has a number of functions and types built into it that are always available. They are listed here in alphabetical order.     Built-in Funct

Sum of Digits is Prime

Sum of Digits is Prime Daoyi Peng August 19, 2014 For an integer $q\geqslant2$ let $s_q(n)$ denote the $q$-ary sum-of-digits function of a non-negative integer $n$, that is, if $n$ is given by its $q$-ary digits expansion $n=\sum\limits_{k=0}^{r} a_k

ZOJ - 3816 Generalized Palindromic Number

Description A number that will be the same when it is written forwards or backwards is known as a palindromic number. For example, 1234321 is a palindromic number. We call a number generalized palindromic number, if after merging all the consecutive

hdu 2665 Kth number(划分树)

Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4602 Accepted Submission(s): 1468 Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The first l

202. Happy Number【leetcode】java,hashSet,算法

202. Happy Number Write an algorithm to determine if a number is "happy". A happy number is a number defined by the following process: Starting with any positive integer, replace the number by the sum of the squares of its digits, and repeat the

poj 2720 Last Digits

Last Digits Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2233   Accepted: 474 Description Exponentiation of one integer by another often produces very large results. In this problem, we will compute a function based on repeated expone