Skip to content

Commit 338313d

Browse files
committed
solution for matrix problem
1 parent 1a5d132 commit 338313d

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

leetcode_kuro_RnD.ipynb

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8968,12 +8968,34 @@
89688968
},
89698969
{
89708970
"cell_type": "code",
8971-
"execution_count": null,
8971+
"execution_count": 9,
89728972
"metadata": {},
8973-
"outputs": [],
8973+
"outputs": [
8974+
{
8975+
"name": "stdout",
8976+
"output_type": "stream",
8977+
"text": [
8978+
"2\n"
8979+
]
8980+
}
8981+
],
89748982
"source": [
89758983
"# https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k\n",
8976-
"\n"
8984+
"# solution : https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k/discuss/2678950/Python-DP-solution\n",
8985+
"import collections\n",
8986+
"from typing import Counter\n",
8987+
"\n",
8988+
"A = [[5,2,4],[3,0,5],[0,7,2]]\n",
8989+
"k = 3\n",
8990+
"\n",
8991+
"n, m, mod = len(A), len(A[0]), 10**9 + 7\n",
8992+
"dp = [Counter() for j in range(m + 1)]\n",
8993+
"dp[0][0] = 1\n",
8994+
"for i, r in enumerate(A):\n",
8995+
" for j, a in enumerate(r):\n",
8996+
" dp[j] = Counter({(a + b) % k: c % mod for b, c in (dp[j] + dp[j-1]).items()})\n",
8997+
"print(dp[m - 1][0])\n",
8998+
"#return dp[m - 1][0]"
89778999
]
89789000
}
89799001
],

0 commit comments

Comments
 (0)