SRM 437 TheSwap ( Permutation + Memory Search )

My idea:

 

    At first I solve this problem with the greedy approach (keeping the number maximal for every iteration) and all the test cases given is right . Later on I find some cases from the discussion that my code gives the wrong answer .

    For example : 35766 3

    the correct result is that 35766->75366->76356->76653

    but the greedy approach works like this : 35766->75366->76365->76635

    Here I will use the make permutation and memory search to solve this problem , and I have learn much about STL in C++ this time .

 

Problem Statement

    

There is nothing more beautiful than just an integer number.

You are given an integer n. Write down n in decimal notation with no leading zeroes, and let M be the number of written digits. Perform the following operation exactly k times:

  • Choose two different 1-based positions, i and j, such that 1 <= i < j <= M. Swap the digits at positions i and j. This swap must not cause the resulting number to have a leading zero, i.e., if the digit at position j is zero, then i must be strictly greater than 1.

 

Return the maximal possible number you can get at the end of this procedure. If it's not possible to perform k operations, return -1 instead.

 

Definition

    
Class:TheSwap
Method:findMax
Parameters:int, int
Returns:int
Method signature:int findMax(int n, int k)
(be sure your method is public)
    
 
 

Constraints

-n will be between 1 and 1,000,000, inclusive.
-

k will be between 1 and 10, inclusive.

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值