Algorithm---n digits remove K-bit and find the decimal number

Source: Internet
Author: User

Remove K-bit to find minimum number

An n-bit number, minus the K-bit, how to make the Left (n-k) digits in the original sequence of the number of the smallest

For example

8314925 minus 4 numbers, leaving 125 the smallest, note that there is a pre-order requirement, if not the order of course is 123.

Solution Solutions

The greedy algorithm is guaranteed to have an optimal solution at each location visited.

Idea One

Analysis : To find a total of n bits, the smallest number of M-bits. Then the number of the M-bit, the highest bit should be in the highest position of the original number to find, or not as the first bit, such as (get 3 digits of the smallest, if the hundred in the 25 to find, will not be the Hundred number):

The same 10-digit number can only be found in the number of points in the middle of the current search, the whole process is shown as follows:

Attention

In the interval there are several minimum values, take the maximum distance, to ensure that the next number of large enough to find space.

Reference Code

#include <iostream>#include<cstdlib>#include<cassert>using namespacestd;int*Q;intFindminindex (intArr[],intBegintEnd//[]{    if(Beg >end)return-1; intMINV =Arr[beg]; intMinindex =Beg;  for(inti = Beg +1; I <= end; ++i) {if(Arr[i] <MINV) {MINV=Arr[i]; Minindex=i; }    }    returnMinindex;}intGetremain (intArr[],intSizeintk) {Assert (Size> k && k >=0); intRev =0, Revindex =-1;  for(inti = size-k; i < size; ++i) {Revindex= Findminindex (arr, Revindex +1, i); Rev= Rev *Ten+Arr[revindex]; }    returnRev;}intMain () {intArr[] = {3,1,6,4,8,5,7}; size_t size=sizeof(arr)/sizeof(int); intRemainnum;  for(intK = size-1; K >0; --k) {intRemainnum = Getremain (arr, size, size-k); cout<<"When k ="<< k <<", the remaining value is:"<< Remainnum <<Endl; }}

Results

Analysis

Time complexity O (KN)

Two ideas

Analysis: From the go after, each visit one, compare the number in front of the bit, if bigger than that bit, decisive kill, for example:

The same goes backwards, knowing that the number of kills is K or access to the last, the whole process diagram as. Of course, the traversal to the end has not killed the K elements, indicating that the rest is already ascending, so that in the number left before the n-k, integrated into an integer is the minimum value.

Reference Code

#include <iostream>#include<cassert>using namespacestd;intGetremain (int*arr,intSizeintk) {Assert (Size> K && k >0); intTMP = size-K; intCur =0, pre; intRev =0;  while(k! =0&& Cur <size) {Pre= cur-1;  while(Pre >=0)        {            if(Arr[pre] >=Arr[cur]) {                 for(inti = Pre; i < size; ++i) arr[i]= arr[i+1]; --cur; --K; --size; }            --Pre; }        ++cur; }     for(inti =0; I < TMP; ++i) {rev= Rev *Ten+Arr[i]; }    returnRev;}intMain () {intArr[] = {3,1,6,4,8,5,7}; size_t size=sizeof(arr)/sizeof(int); intRemainnum; Remainnum= Getremain (arr, size,3); cout<<"When k ="<<3<<", the remaining value is:"<< Remainnum <<Endl;}

Analysis

Time complexity O (KN)

Algorithm---n digits remove K-bit and find the decimal number

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.