Problem Description: Enter a high-precision positive integer n with the keyboard, and the remaining digits after the s digits are removed to form a new positive integer in the left and right order.
Programming given N and S, looking for a scheme to make the remaining numbers oh the smallest number of new numbers.
Thinking analysis: Using the greedy method to approximate the target to delete one of the S characters, the total number of each step is to choose a number of the remaining number of the smallest character delete. Such a greedy choice because removing the S-character's straight solution contains the deletion of a
The optimal solution of the sub-problem of a digital character. Find the descending interval from left to right, and delete the first number. If no descending interval is found, the number at the end is deleted.
1 voidFind_min_integer (vector<int> &nums,intS)2 {3vector<int>result;4 5 if(s>nums.size ())6 {7 nums.clear ();8 return;9 }Ten //finding recursive sequences from left to right One A while(s>0) - { - inti; the for(i=0;(i<nums.size ()) && (nums[i]<=nums[i+1]); i++) - ; -Nums.erase (i);//Delete the first character of the interval, including the end when the descending interval is not found -s--; + } - + while(Nums.size () >1&&nums[0]==0)//Delete Invalid number of head 0 ANums.erase (0); at}
Greedy Choice Property: The Global optimal solution can be achieved by local optimal selection.
The problem of deletion (the classical problem of greedy law)