{"id":39,"date":"2018-11-08T21:37:50","date_gmt":"2018-11-08T21:37:50","guid":{"rendered":"https:\/\/andybase.com\/?p=39"},"modified":"2021-01-12T16:44:50","modified_gmt":"2021-01-12T21:44:50","slug":"935-knight-dialer","status":"publish","type":"post","link":"https:\/\/andybase.com\/?p=39","title":{"rendered":"935 Knight Dialer"},"content":{"rendered":"\n<p>link <a href=\"https:\/\/leetcode.com\/problems\/knight-dialer\/\">Knight Dialer<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Thought<\/h2>\n\n\n\n<p>Since it ask how many distinct numbers could be generated, DP jumps to my mind immediately. Firstly, the relation is, if knight stop at number 6, it has to come from 1, 7, or 0. So I keep a DP array, DP[j][i] stands for how many unique numbers it could generate if knight jumps <strong>j<\/strong> steps and stops at <strong>i<\/strong> position. Then, I find actually DP[j+i] only depends on DP[j] level, so I could optimize 2D DP array into 1D array.<\/p>\n\n\n\n<p>One tircky part to notice: DP[1][5] is 1, other than that, DP[j][5] is 0 iif j &gt; 1.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Code<\/h2>\n\n\n\n<pre class=\"wp-block-syntaxhighlighter-code brush: python; notranslate\">class Solution:\n    def knightDialer(self, N):\n        \"\"\"\n        :type N: int\n        :rtype: int\n        \"\"\"\n        MOD = 10**9 + 7\n        DP = [1] * 10 #init state\n        for hops in range(N-1):\n            Q = [0] * 10\n            Q[0] = (DP[4] + DP[6]) % MOD\n            Q[1] = (DP[8] + DP[6]) % MOD\n            Q[2] = (DP[7] + DP[9]) % MOD\n            Q[3] = (DP[4] + DP[8]) % MOD\n            Q[4] = (DP[3] + DP[0] +DP[9]) % MOD\n            Q[6] = (DP[1] +DP[7] + DP[0]) % MOD\n            Q[7] = (DP[2] + DP[6]) % MOD\n            Q[8] = (DP[1] + DP[3]) % MOD\n            Q[9] = (DP[4] + DP[2]) % MOD\n            DP = Q\n        return sum(DP) % MOD<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Runtime<\/h2>\n\n\n\n<p>Time: O(n) Space: O(n)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>link Knight Dialer Thought Since it ask how many distinct numbers could be generated, DP jumps to my mind immediately. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"enabled":false},"version":2}},"categories":[2],"tags":[4,3],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-leetcode","tag-contest","tag-leetcode"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/paWPni-D","_links":{"self":[{"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=39"}],"version-history":[{"count":4,"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions"}],"predecessor-version":[{"id":416,"href":"https:\/\/andybase.com\/index.php?rest_route=\/wp\/v2\/posts\/39\/revisions\/416"}],"wp:attachment":[{"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andybase.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}