Samsung R&D Bangalore | OA | Combination Lock Problem
Anonymous User
249

Given a random configuration of a combination lock with N circular dials (3 <= N<=20) and the actual SET lock code, print the minimal number of moves required to transform the random starting input to the actual set code with the below constraints:

  1. A move is defined as the scrolling/switching up/down to change a dial number -> Only 3 numbers up/down from the current one can be made in a single move. E.g. valid moves: 3 -> 0, 3 -> 5, 3 -> 2.
  2. Upto 3 adjacent dial numbers can be selected to move up/down together in one move.

Examples are as follows:


1>
INPUT: 117
SET CODE: 777

ANSWER: 2
1st move (117->227->337->447)
2nd move (447 -> 557 -> 667 -> 777)
SOLUTION: 117 -> 447 -> 777

2>
INPUT: 555
SET CODE: 464

ANSWER: 2

SOLUTION:
 
1 possbility is 555 -> 455 -> 465 -> 464 (3 moves)
Expected possibility is 555 -> 444 -> 464 (2 moves)

3>
INPUT: 118
SET CODE: 888

ANSWER: 1 move as dials are circular (118 -> 008 -> 998 -> 888 Total 3 switches)
118 -> 888

4>
INPUT: 06012004
SET CODE: 06012006

ANSWER: 1 move (2 switches)
06012004 -> 06012005 -> 06012006

5>
INPUT: 123456789
SET CODE: 567412490

ANSWER: 5
(123456789 -> 567456789 -> 567423489->56741289->56741290)

6>
INPUT: 2388754767623454321
SET CODE: 1432754767762549321

ANSWER: 10

I came up with a greedy approach which proved insufficient. Can see the applicability of a DP solution but couldn't formalise it in the given time constraint. Looking forward to the approach if anyone can solve it.

Comments (1)