Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Swift Warmup : Android Unlock Patterns

Johnlin
January 03, 2017

Swift Warmup : Android Unlock Patterns

Johnlin

January 03, 2017
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. 351. Android Unlock Patterns • https://leetcode.com/problems/android-unlock-patterns/ • http://www.cnblogs.com/grandyang/p/5541012.html • 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. • څ㟬Ұݸ3*3 త android ղ࠯ᙘ໘ɼ࠶څ㟬ၷݸᏐࣈm᪑n, 1 ≤ m ≤ n ≤ 9 ɻٻ࢖༻࠷গm ݸᴍɼ࠷ଟ n ݸᴍతղ࠯ํ ๏తᏐ໨ɻ
  2. ղ࠯ํ๏తنଇ • All the keys must be distinct.
 㑌Ұݸᴍ୞ೳ࢖༻Ұ࣍ •

    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.
 ೗Ռၷݸᴍதؒៃա㠥Ұݸᴍɼಹݸᴍඞਢ೭લቮៃ૸ աɼ೗Ռᔒ૸աత࿩बෆ߹๏ɻ • The order of keys used matters.
 ᴍతॱংੋॏཁత
  3. ൣྫ • 4 -> 1 -> 3 -> 6 •

    Ұ౸ࡾత࣌ީ௨աྃೋ
  4. ൣྫ • 4 -> 1 -> 9 -> 2 •

    Ұ౸۝త࣌ީ௨աྃޒ • ۝౸ೋੋ߹๏త
  5. ൣྫ • 2 -> 4 -> 1 -> 3 ->

    6 • Ұ౸ࡾత࣌ީ௨աྃೋɼ
 ෆա೭લቮៃ૸աɼ
 ॴҎ߹๏
  6. ൣྫ • 6 -> 5 -> 4 -> 1 ->

    9 -> 2 • Ұ౸۝త࣌ީ௨աྃޒɼ
 ෆա೭લቮៃ૸աɼ
 ॴҎ߹๏
  7. ԋࢉ๏ • Divide and Conquer (෼࣏๏) • ཁࢉ m ݸᴍ౸

    n ݸᴍ => ݸผࢉ׬೭ޙ࠶Ճىိ • Ұڞ༗۝ݸՄೳతىᴍ => ݸผࢉ׬࠶Ճىိ
  8. ฤᥒ 0 | 1 | 2 3 | 4 |

    5 6 | 7 | 8 let f: (Int) -> String = {($0*3...$0*3+2).map{String($0)}.joined(separator: " | ")} print((0...2).map{f($0)}.joined(separator : "\n"))
  9. class Solution { func numberOfPatterns(_ m: Int, _ n: Int)

    -> Int { setupPrerequests() var sum = 0 for k in m...n { for start in 0..<9 { sum += numPatterns(lastNum: start, currentDotNum: 1, targetDotNum: k) } } return sum } }
  10. class Solution { var prerequests:[[Int]] = Array(repeating: Array(repeating:-1, count: 9),

    count: 9) func setupPrerequests() { for row in 0...2 { prerequests[row*3][row*3+2] = row*3+1 prerequests[row*3+2][row*3] = row*3+1 } for col in 0...2 { prerequests[col][col+6] = col + 3 prerequests[col+6][col] = col + 3 } prerequests[0][8] = 4 prerequests[8][0] = 4 prerequests[2][6] = 4 prerequests[6][2] = 4 } } ਫฏ௓֨ ਨ௚௓֨ ሣ֯௓֨
  11. class Solution { var visited:[Bool] = Array(repeating: false, count: 9)

    func numPatterns(lastNum:Int, currentDotNum:Int, targetDotNum:Int) -> Int { if currentDotNum == targetDotNum { return 1 } var result = 0 visited[lastNum] = true for nextNum in 0..<9 { guard !visited[nextNum] else { continue } let prereq = prerequests[lastNum][nextNum] if prereq == -1 || visited[prereq] { result += numPatterns(lastNum: nextNum, currentDotNum: currentDotNum+1, targetDotNum: targetDotNum) } } visited[lastNum] = false return result } } ఍ୡ໨ඪݸᏐ ᒾҰ߹ෆ߹๏ ᒾҰ༗ᔒ༗૸ա ࣌ؒෳᯑ౓: O(n)
 ۭؒෳᯑ౓: O(1)
 Runtime: 1032 ms
  12. Ճ଎1 0 | 1 | 2 3 | 4 |

    5 6 | 7 | 8 • ֨ࢠੋሣ᜝తɼኺ0։࢝త૸๏Ꮠ࿨ኺ2,6,8ੋҰᒬత
 1࿨3,5,7໵ሣ᜝
  13. class Solution { func numberOfPatterns(_ m: Int, _ n: Int)

    -> Int { setupPrerequests() var sum = 0 for k in m...n { sum += 4 * numPatterns(lastNum: 0, currentDotNum: 1, targetDotNum: k) sum += 4 * numPatterns(lastNum: 1, currentDotNum: 1, targetDotNum: k) sum += numPatterns(lastNum: 4, currentDotNum: 1, targetDotNum: k) } return sum } } ࣌ؒෳᯑ౓: O(n)
 ۭؒෳᯑ౓: O(1)
 Runtime: 382 ms
  14. Ճ଎2 • ࡏ༻ X ݸᴍ࣌తղ࠯ํ๏Ꮠੋݻఆత [9, 56, 320, 1624, 7152,

    26016, 72912, 140704, 140704] • ब೺ՄೳੑՃىိब޷
  15. class Solution { func numberOfPatterns(_ m: Int, _ n: Int)

    -> Int { let patterns = [9, 56, 320, 1624, 7152, 26016, 72912, 140704, 140704] return patterns[m-1...n-1].reduce(0) {$0+$1} } } ࣌ؒෳᯑ౓: O(n)
 ۭؒෳᯑ౓: O(1)
 Runtime: 16 ms
  16. Q&A