|
| 1 | +#752. Open the Lock |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Array, Hash Table, String, Breadth-First Search. |
| 5 | +- Similar Questions: Reachable Nodes With Restrictions. |
| 6 | + |
| 7 | +##Problem |
| 8 | + |
| 9 | +You have a lock in front of you with 4 circular wheels. Each wheel has 10 slots:`'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'`. The wheels can rotate freely and wrap around: for example we can turn`'9'` to be`'0'`, or`'0'` to be`'9'`. Each move consists of turning one wheel one slot. |
| 10 | + |
| 11 | +The lock initially starts at`'0000'`, a string representing the state of the 4 wheels. |
| 12 | + |
| 13 | +You are given a list of`deadends` dead ends, meaning if the lock displays any of these codes, the wheels of the lock will stop turning and you will be unable to open it. |
| 14 | + |
| 15 | +Given a`target` representing the value of the wheels that will unlock the lock, return the minimum total number of turns required to open the lock, or -1 if it is impossible. |
| 16 | + |
| 17 | + |
| 18 | +Example 1: |
| 19 | + |
| 20 | +``` |
| 21 | +Input: deadends = ["0201","0101","0102","1212","2002"], target = "0202" |
| 22 | +Output: 6 |
| 23 | +Explanation: |
| 24 | +A sequence of valid moves would be "0000" -> "1000" -> "1100" -> "1200" -> "1201" -> "1202" -> "0202". |
| 25 | +Note that a sequence like "0000" -> "0001" -> "0002" -> "0102" -> "0202" would be invalid, |
| 26 | +because the wheels of the lock become stuck after the display becomes the dead end "0102". |
| 27 | +``` |
| 28 | + |
| 29 | +Example 2: |
| 30 | + |
| 31 | +``` |
| 32 | +Input: deadends = ["8888"], target = "0009" |
| 33 | +Output: 1 |
| 34 | +Explanation: We can turn the last wheel in reverse to move from "0000" -> "0009". |
| 35 | +``` |
| 36 | + |
| 37 | +Example 3: |
| 38 | + |
| 39 | +``` |
| 40 | +Input: deadends = ["8887","8889","8878","8898","8788","8988","7888","9888"], target = "8888" |
| 41 | +Output: -1 |
| 42 | +Explanation: We cannot reach the target without getting stuck. |
| 43 | +``` |
| 44 | + |
| 45 | + |
| 46 | +**Constraints:** |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | +-`1 <= deadends.length <= 500` |
| 51 | + |
| 52 | +-`deadends[i].length == 4` |
| 53 | + |
| 54 | +-`target.length == 4` |
| 55 | + |
| 56 | +- target**will not be** in the list`deadends`. |
| 57 | + |
| 58 | +-`target` and`deadends[i]` consist of digits only. |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +##Solution |
| 63 | + |
| 64 | +```javascript |
| 65 | +/** |
| 66 | + *@param{string[]}deadends |
| 67 | + *@param{string}target |
| 68 | + *@return{number} |
| 69 | +*/ |
| 70 | +varopenLock=function(deadends,target) { |
| 71 | +if (target==='0000')return0; |
| 72 | +var arr= ['0000']; |
| 73 | +var visited= {'0000':true }; |
| 74 | +var deadendsMap= {}; |
| 75 | +for (var i=0; i<deadends.length; i++) { |
| 76 | +if (deadends[i]==='0000')return-1; |
| 77 | + deadendsMap[deadends[i]]=true; |
| 78 | + } |
| 79 | +var res=0; |
| 80 | +while (arr.length) { |
| 81 | +var newArr= []; |
| 82 | + res+=1; |
| 83 | +for (var i=0; i<arr.length; i++) { |
| 84 | +var nexts=getNexts(arr[i]); |
| 85 | +for (var j=0; j<nexts.length; j++) { |
| 86 | +if (nexts[j]=== target)return res; |
| 87 | +if (!visited[nexts[j]]&&!deadendsMap[nexts[j]]) { |
| 88 | +newArr.push(nexts[j]); |
| 89 | + visited[nexts[j]]=true; |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | + arr= newArr; |
| 94 | + } |
| 95 | +return-1; |
| 96 | +}; |
| 97 | + |
| 98 | +vargetNexts=function(str) { |
| 99 | +var res= []; |
| 100 | +for (var i=0; i<str.length; i++) { |
| 101 | +var num=Number(str[i]); |
| 102 | +res.push(str.slice(0, i)+ (num===9?0: num+1)+str.slice(i+1)); |
| 103 | +res.push(str.slice(0, i)+ (num===0?9: num-1)+str.slice(i+1)); |
| 104 | + } |
| 105 | +return res; |
| 106 | +}; |
| 107 | +``` |
| 108 | + |
| 109 | +**Explain:** |
| 110 | + |
| 111 | +nope. |
| 112 | + |
| 113 | +**Complexity:** |
| 114 | + |
| 115 | +* Time complexity : O(n). |
| 116 | +* Space complexity : O(n). |