Minimum value after deleting K numbers (a greedy algorithm)

Gives a new Integer integer, k is removed from the integer numbers, the remaining required digital form as small as possible. How should select the numbers are removed?
Wherein a length greater than or equal to the integer k, an integer given size may exceed the long range digital type.
 
Ideas:
All original integer numbers are compared from left to right, if you find a single digit number greater than its right side, then after deleting the numbers, is bound to make the value of the digit decreases. This seeking local optimal solution, end up thinking globally optimal solution, called " greedy algorithm " .
If you want to delete k numbers, then the numbers will go through as the outer loop to k as the outer loop, combined with the use of the stack. The traversal numbers one by one onto the stack, the stack encounter came in numbers less than the number of the stack, the stack of digital pop, and then traverse the numbers stack, otherwise it will direct digital push. When the numbers stack up to the k, all the rest of the stack can be digital.
 
Example: "541270936", 3 numbers removed.
  1. 5 stack, stack = [5]
  2. Drawing 4, 4 is determined <5,5 the stack, the stack 4. stack = [4]. Once the stack, num = 1
  3. A stack, is determined 1 <4,4 the stack, a stack. stack = [1]. Once the stack, num = 2
  4. Stack 2, is determined 2> stack 1 directly. stack = [1,2]
  5. Drawing 7, 7 judges> 2,7 stack directly. stack = [1,2,7]
  6. 0 stack, it is judged 0 <7,7 Stack, 0 stack. stack = [1,2,0]. Once the stack, num = 3. At this time, the stack 3 times, in addition to the equivalent of 3 digits. fulfil requirements. Then the rest of the characters directly push on it.
  7. All remaining characters stack. stack = [1,2,0,9,3,6]

Code:

. 1  Package blogSrc;
 2  
. 3  Import com.sun.deploy.util.StringUtils;
 . 4  
. 5  Import java.util.Arrays;
 . 6  Import ; the java.util.Stack
 . 7  
. 8  / ** 
. 9  Min K * delete digits after
 10  *
 11  * gives an integer, k is removed from the integer numbers, the remaining required digital form new integers as small as possible. How should select the numbers are removed?
12  * wherein a length greater than or equal to the integer k, an integer given size may exceed the long range digital type.
13  *
 14  * ideas: the original integer from left to right all the numbers compare, if you find a single digit number greater than its right side, then after deleting the numbers, is bound to make the value of the digit decreases.
15  * If you want to delete k numbers, then the numbers will go through as the outer loop to k as the outer loop, combined with the use of the stack. The traversal numbers stack one by one, otherwise it will direct digital push.
16 * Drawing encountered incoming digital number is less than the stack, the stack numbers of the stack, and then the digital traversal stack, when the stack reaches the k numbers,
 17  * All the remaining figures the stack can be.
18 is   * / 
. 19  public  class GetMinAfterRemoveKDigits {
 20 is  
21 is      public  static  void main (String [] args) {
 22 is          String originStr = "541 270 936" ;
 23 is          String = newStr new new . GetMinAfterRemoveKDigits () getMinAfterRemoveKDigits (originStr,. 3 );
 24          the System.out. the println ( "oldString IS:" + originStr);
 25          System.out.println ( "NewString IS:" + newStr);
 26 is      }
 27  
28     public String getMinAfterRemoveKDigits(String originStr, int k){
29         char[] originArr = originStr.toCharArray();
30         Stack<Character> stack = new Stack<>();
31         stack.push(originArr[0]);
32         int num = 0;
33         for (int i=0; i<originArr.length; i++){
34             if (num == k){
35                 stack.push(originArr[i]);
36             }else {
37                 if (originArr[i] <= stack.peek()) {
38                     stack.pop();
39                     stack.push(originArr[i]);
40                     num++;
41                 }else {
42                     stack.push(originArr[i]);
43                 }
44             }
45 
46         }
47 
48         StringBuffer result = new StringBuffer();
49         for (int i=0; i<stack.size(); i++){
50             result.append(stack.get(i));
51         }
52         return result.toString();
53     }
54 }

result:

OldString is: 541270936
NewString is: 1270936

 

 

Guess you like

Origin www.cnblogs.com/zldmy/p/11517369.html