LeetCode Android Unlock Patterns

解锁模式计数
本文针对LeetCode上的一道题目——Android解锁模式计数问题进行了详细解答。通过深度优先搜索(DFS)策略,实现了一个Java解决方案,该方案考虑了所有合法路径的可能性,并避免了无效跳转。同时分析了时间复杂度和空间复杂度。

原题链接在这里:https://leetcode.com/problems/android-unlock-patterns/description/

题目:

Given an Android 3x3 key lock screen and two integers m and n, where 1 ≤ m ≤ n ≤ 9, count the total number of unlock patterns of the Android lock screen, which consist of minimum of m keys and maximum n keys.

Rules for a valid pattern:

  1. Each pattern must connect at least m keys and at most n keys.
  2. All the keys must be distinct.
  3. If the line connecting two consecutive keys in the pattern passes through any other keys, the other keys must have previously selected in the pattern. No jumps through non selected key is allowed.
  4. The order of keys used matters.

 

Explanation:

| 1 | 2 | 3 |
| 4 | 5 | 6 |
| 7 | 8 | 9 |

Invalid move: 4 - 1 - 3 - 6 
Line 1 - 3 passes through key 2 which had not been selected in the pattern.

Invalid move: 4 - 1 - 9 - 2
Line 1 - 9 passes through key 5 which had not been selected in the pattern.

Valid move: 2 - 4 - 1 - 3 - 6
Line 1 - 3 is valid because it passes through key 2, which had been selected in the pattern

Valid move: 6 - 5 - 4 - 1 - 9 - 2
Line 1 - 9 is valid because it passes through key 5, which had been selected in the pattern.

Example:
Given m = 1, n = 1, return 9.

题解:

从当前点cur开始做dfs. 每次remain steps 减一. stop condition 是remain steps为0, 此时只有一种可能, return 1.

在做dfs时需要确保下个数要么相连要么隔着的数已经visit过.

Time Complexity: O(n!). n是maximum pattern length. 共有O(n!)种可能结果.

Space: O(n). stack space.

AC Java:

 1 class Solution {
 2     public int numberOfPatterns(int m, int n) {
 3         int [][] skip = new int[10][10];
 4         skip[1][3] = skip[3][1] = 2;
 5         skip[1][7] = skip[7][1] = 4;
 6         skip[3][9] = skip[9][3] = 6;
 7         skip[7][9] = skip[9][7] = 8;
 8         skip[1][9] = skip[9][1] = skip[3][7] = skip[7][3] = skip[2][8] = skip[8][2] = skip[4][6] = skip[6][4] = 5;
 9         
10         boolean [] visited = new boolean[10];
11         int res = 0;
12         for(int i = m; i<=n; i++){
13             // 1, 3, 7, 9是symmetric的
14             res += dfs(skip, visited, 1, i-1) * 4;
15             // 2, 4, 6, 8是symmetric的
16             res += dfs(skip, visited, 2, i-1) * 4;
17             res += dfs(skip, visited, 5, i-1);
18         }
19         return res;
20     }
21     
22     private int dfs(int [][] skip, boolean [] visited, int cur, int remain){
23         if(remain == 0){
24             return 1;
25         }
26         
27         int res = 0;
28         visited[cur] = true;
29         for(int i = 1; i<=9; i++){
30             //如果i没被visited过,并且要么cur和i连着 要么cur和i之间的skip number之前visit 过
31             if(!visited[i] && (skip[cur][i]==0 || visited[skip[cur][i]])){
32                 res += dfs(skip, visited, i, remain-1);
33             }
34         }
35         visited[cur] = false;
36         return res;
37     }
38 }

 

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/7597278.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值