Skip to content

Commit a5c343c

Browse files
authored
935. Knight Dialer (#169)
1 parent 56d7577 commit a5c343c

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@
291291
| 920 | Number of Music Playlists | [Ruby](./algorithms/ruby/0920-number-of-music-playlists.rb) | Hard |
292292
| 933 | Number of Recent Calls | [Ruby](./algorithms/ruby/0933-number-of-recent-calls.rb) | Easy |
293293
| 934 | Shortest Bridge | [Ruby](./algorithms/ruby/0934-shortest-bridge.rb) | Medium |
294+
| 935 | Knight Dialer | [Ruby](./algorithms/ruby/0935-knight-dialer.rb) | Medium |
294295
| 946 | Validate Stack Sequences | [Ruby](./algorithms/ruby/0946-validate-stack-sequences.rb) | Medium |
295296
| 947 | Most Stones Removed with Same Row or Column | [Ruby](./algorithms/ruby/0947-most-stones-removed-with-same-row-or-column.rb) | Medium |
296297
| 953 | Verifying an Alien Dictionary | [Ruby](./algorithms/ruby/0953-verifying-an-alien-dictionary.rb) | Easy |

algorithms/ruby/0935-knight-dialer.rb

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# frozen_string_literal: true
2+
3+
# 935. Knight Dialer
4+
# Medium
5+
# leetcode.com/problems/knight-dialer
6+
7+
=begin
8+
The chess knight has a unique movement, it may move two squares vertically and one square horizontally, or two squares horizontally and one square vertically (with both forming the shape of an L). The possible movements of chess knight are shown in this diagaram:
9+
A chess knight can move as indicated in the chess diagram below:
10+
We have a chess knight and a phone pad as shown below, the knight can only stand on a numeric cell (i.e. blue cell).
11+
Given an integer n, return how many distinct phone numbers of length n we can dial.
12+
You are allowed to place the knight on any numeric cell initially and then you should perform n - 1 jumps to dial a number of length n. All jumps should be valid knight jumps.
13+
As the answer may be very large, return the answer modulo 109 + 7.
14+
15+
Example 1:
16+
Input: n = 1
17+
Output: 10
18+
Explanation: We need to dial a number of length 1, so placing the knight over any numeric cell of the 10 cells is sufficient.
19+
20+
Example 2:
21+
Input: n = 2
22+
Output: 20
23+
Explanation: All the valid number we can dial are [04, 06, 16, 18, 27, 29, 34, 38, 40, 43, 49, 60, 61, 67, 72, 76, 81, 83, 92, 94]
24+
25+
Example 3:
26+
Input: n = 3131
27+
Output: 136006598
28+
Explanation: Please take care of the mod.
29+
30+
Constraints:
31+
1 <= n <= 5000
32+
=end
33+
34+
JUMP_TO = [[4, 6], [6, 8], [7, 9], [4, 8], [0, 3, 9], [], [0, 1, 7], [2, 6], [1, 3], [2, 4]]
35+
MOD = 10**9 + 7
36+
37+
def knight_dialer(n)
38+
(n - 1).times.inject(Array.new(10, 1)) { |cur, i|
39+
cur.zip(JUMP_TO).each_with_object(Array.new(10, 0)) { |(v, jt), res|
40+
jt.each { |jti| res[jti] = (res[jti] + v) % MOD }
41+
}
42+
}.inject(0) { |res, v| (res + v) % MOD }
43+
end
44+
45+
# **************** #
46+
# TEST #
47+
# **************** #
48+
49+
require "test/unit"
50+
class Test_knight_dialer < Test::Unit::TestCase
51+
def test_
52+
assert_equal 10, knight_dialer(1)
53+
assert_equal 20, knight_dialer(2)
54+
assert_equal 136006598, knight_dialer(3131)
55+
end
56+
end

0 commit comments

Comments
 (0)