import java.io.* ;
// Problem Description:
// Get the least number after deleting k digits of a number
// Supposing the number is represented as a string
class DecreasingResult {
public int firstDecreasing;
public int nextStart;
}
class Ideone
{
// Solution 1: O(n*k)
public static String getLeastNumberDeletingDigits_1
( String number,
int k
) { while ( k > 0 && leastNumber.length ( ) > 0 ) {
int firstDecreasingDigit = getFirstDecreasing( leastNumber) ;
if ( firstDecreasingDigit >= 0 ) {
leastNumber = removeDigit( leastNumber, firstDecreasingDigit) ;
}
else {
leastNumber = removeDigit( leastNumber, leastNumber.length ( ) - 1 ) ;
}
-- k;
}
return leastNumber;
}
private static int getFirstDecreasing
( String number
) { for ( int i = 0 ; i < number.length ( ) - 1 ; ++ i) {
int curDigit = number.charAt ( i) - '0' ;
int nextDigit = number.charAt ( i + 1 ) - '0' ;
if ( curDigit > nextDigit) {
return i;
}
}
return - 1 ;
}
private static String removeDigit
( String number,
int digitIndex
) { if ( digitIndex > 0 ) {
result = number.substring ( 0 , digitIndex) ;
}
if ( digitIndex < number.length ( ) - 1 ) {
result += number.substring ( digitIndex + 1 ) ;
}
return result;
}
// Solution 2: O(n)
public static String getLeastNumberDeletingDigits_2
( String number,
int k
) { int start = 0 ;
while ( k > 0 && leastNumber.length ( ) > 0 ) {
DecreasingResult result = getNextDecreasing( leastNumber, start) ;
if ( result.firstDecreasing >= 0 ) {
leastNumber = removeDigit( leastNumber, result.firstDecreasing ) ;
}
else {
leastNumber = removeDigit( leastNumber, leastNumber.length ( ) - 1 ) ;
}
start = result.nextStart ;
-- k;
}
return leastNumber;
}
private static DecreasingResult getNextDecreasing
( String number,
int start
) { int firstDecreasing = - 1 ;
int nextStart;
for ( int i = start; i < number.length ( ) - 1 ; ++ i) {
int curDigit = number.charAt ( i) - '0' ;
int nextDigit = number.charAt ( i + 1 ) - '0' ;
if ( curDigit > nextDigit) {
firstDecreasing = i;
break ;
}
}
if ( firstDecreasing == 0 ) {
nextStart = 0 ;
}
else if ( firstDecreasing > 0 ) {
nextStart = firstDecreasing - 1 ;
}
else {
nextStart = number.length ( ) ;
}
DecreasingResult result = new DecreasingResult( ) ;
result.firstDecreasing = firstDecreasing;
result.nextStart = nextStart;
return result;
}
//////////////////////////////////////////////////////////////
// Test Code Begins:
//////////////////////////////////////////////////////////////
private static void testSolution1
( String number,
int k,
String expected
) { String result
= getLeastNumberDeletingDigits_1
( number, k
) ; if ( result.equals ( expected) ) {
System .
out .
println ( "Solution 1 passed, with number " + number
+ ", k: " + k
) ; }
else {
System .
out .
println ( "Solution 1 FAILED, with number " + number
+ ", k: " + k
) ; }
}
private static void testSolution2
( String number,
int k,
String expected
) { String result
= getLeastNumberDeletingDigits_2
( number, k
) ; if ( result.equals ( expected) ) {
System .
out .
println ( "Solution 2 passed, with number " + number
+ ", k: " + k
) ; }
else {
System .
out .
println ( "Solution 2 FAILED, with number " + number
+ ", k: " + k
) ; }
}
private static void test
( String number,
int k,
String expected
) { testSolution1( number, k, expected) ;
testSolution2( number, k, expected) ;
}
private static void test1( ) {
int k = 5 ;
test( number, k, expected) ;
}
private static void test2( ) {
int k = 3 ;
test( number, k, expected) ;
}
private static void test3( ) {
int k = 3 ;
test( number, k, expected) ;
}
private static void test4( ) {
int k = 3 ;
test( number, k, expected) ;
}
private static void test5( ) {
int k = 5 ;
test( number, k, expected) ;
}
private static void test6( ) {
int k = 6 ;
test( number, k, expected) ;
}
private static void test7( ) {
int k = 0 ;
test( number, k, expected) ;
}
private static void test8( ) {
int k = 3 ;
test( number, k, expected) ;
}
public static void main
( String [ ] args
) { test1( ) ;
test2( ) ;
test3( ) ;
test4( ) ;
test5( ) ;
test6( ) ;
test7( ) ;
test8( ) ;
}
}
aW1wb3J0IGphdmEuaW8uKjsKCi8vIFByb2JsZW0gRGVzY3JpcHRpb246IAovLyBHZXQgdGhlIGxlYXN0IG51bWJlciBhZnRlciBkZWxldGluZyBrIGRpZ2l0cyBvZiBhIG51bWJlcgovLyBTdXBwb3NpbmcgdGhlIG51bWJlciBpcyByZXByZXNlbnRlZCBhcyBh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jdGVkKTsKCX0KCQkKCXByaXZhdGUgc3RhdGljIHZvaWQgdGVzdDYoKSB7CgkJU3RyaW5nIG51bWJlciA9ICIxMjM0NSI7CgkJaW50IGsgPSA2OwoJCVN0cmluZyBleHBlY3RlZCA9ICIiOwoJCQoJCXRlc3QobnVtYmVyLCBrLCBleHBlY3RlZCk7Cgl9CgkJCglwcml2YXRlIHN0YXRpYyB2b2lkIHRlc3Q3KCkgewoJCVN0cmluZyBudW1iZXIgPSAiMTIzNDUiOwoJCWludCBrID0gMDsKCQlTdHJpbmcgZXhwZWN0ZWQgPSAiMTIzNDUiOwoJCQoJCXRlc3QobnVtYmVyLCBrLCBleHBlY3RlZCk7Cgl9CgoJcHJpdmF0ZSBzdGF0aWMgdm9pZCB0ZXN0OCgpIHsKCQlTdHJpbmcgbnVtYmVyID0gIjI0NjM1IjsKCQlpbnQgayA9IDM7CgkJU3RyaW5nIGV4cGVjdGVkID0gIjIzIjsKCQkKCQl0ZXN0KG51bWJlciwgaywgZXhwZWN0ZWQpOwoJfQoKCXB1YmxpYyBzdGF0aWMgdm9pZCBtYWluIChTdHJpbmdbXSBhcmdzKSB7CgkJdGVzdDEoKTsKCQl0ZXN0MigpOwoJCXRlc3QzKCk7CgkJdGVzdDQoKTsKCQl0ZXN0NSgpOwoJCXRlc3Q2KCk7CgkJdGVzdDcoKTsKCQl0ZXN0OCgpOwoJfQp9
stdout
Solution 1 passed, with number 13243221, k: 5
Solution 2 passed, with number 13243221, k: 5
Solution 1 passed, with number 1234567, k: 3
Solution 2 passed, with number 1234567, k: 3
Solution 1 passed, with number 7654321, k: 3
Solution 2 passed, with number 7654321, k: 3
Solution 1 passed, with number 66666666, k: 3
Solution 2 passed, with number 66666666, k: 3
Solution 1 passed, with number 12345, k: 5
Solution 2 passed, with number 12345, k: 5
Solution 1 passed, with number 12345, k: 6
Solution 2 passed, with number 12345, k: 6
Solution 1 passed, with number 12345, k: 0
Solution 2 passed, with number 12345, k: 0
Solution 1 passed, with number 24635, k: 3
Solution 2 passed, with number 24635, k: 3