Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commite433db1

Browse files
committed
Add solution #2435
1 parentd80c0b0 commite433db1

File tree

2 files changed

+47
-1
lines changed

2 files changed

+47
-1
lines changed

‎README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#1,945 LeetCode solutions in JavaScript
1+
#1,946 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1837,6 +1837,7 @@
18371837
2432|[The Employee That Worked on the Longest Task](./solutions/2432-the-employee-that-worked-on-the-longest-task.js)|Easy|
18381838
2433|[Find The Original Array of Prefix Xor](./solutions/2433-find-the-original-array-of-prefix-xor.js)|Medium|
18391839
2434|[Using a Robot to Print the Lexicographically Smallest String](./solutions/2434-using-a-robot-to-print-the-lexicographically-smallest-string.js)|Medium|
1840+
2435|[Paths in Matrix Whose Sum Is Divisible by K](./solutions/2435-paths-in-matrix-whose-sum-is-divisible-by-k.js)|Hard|
18401841
2444|[Count Subarrays With Fixed Bounds](./solutions/2444-count-subarrays-with-fixed-bounds.js)|Hard|
18411842
2460|[Apply Operations to an Array](./solutions/2460-apply-operations-to-an-array.js)|Easy|
18421843
2462|[Total Cost to Hire K Workers](./solutions/2462-total-cost-to-hire-k-workers.js)|Medium|
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* 2435. Paths in Matrix Whose Sum Is Divisible by K
3+
* https://leetcode.com/problems/paths-in-matrix-whose-sum-is-divisible-by-k/
4+
* Difficulty: Hard
5+
*
6+
* You are given a 0-indexed m x n integer matrix grid and an integer k. You are currently at
7+
* position (0, 0) and you want to reach position (m - 1, n - 1) moving only down or right.
8+
*
9+
* Return the number of paths where the sum of the elements on the path is divisible by k.
10+
* Since the answer may be very large, return it modulo 109 + 7.
11+
*/
12+
13+
/**
14+
*@param {number[][]} grid
15+
*@param {number} k
16+
*@return {number}
17+
*/
18+
varnumberOfPaths=function(grid,k){
19+
constrows=grid.length;
20+
constcols=grid[0].length;
21+
constmodulo=1e9+7;
22+
constdp=Array.from({length:rows},()=>{
23+
returnArray.from({length:cols},()=>Array(k).fill(0));
24+
});
25+
26+
dp[0][0][grid[0][0]%k]=1;
27+
28+
for(letrow=0;row<rows;row++){
29+
for(letcol=0;col<cols;col++){
30+
constcurrentValue=grid[row][col]%k;
31+
for(letsum=0;sum<k;sum++){
32+
if(row>0){
33+
constprevSum=(sum-currentValue+k)%k;
34+
dp[row][col][sum]=(dp[row][col][sum]+dp[row-1][col][prevSum])%modulo;
35+
}
36+
if(col>0){
37+
constprevSum=(sum-currentValue+k)%k;
38+
dp[row][col][sum]=(dp[row][col][sum]+dp[row][col-1][prevSum])%modulo;
39+
}
40+
}
41+
}
42+
}
43+
44+
returndp[rows-1][cols-1][0];
45+
};

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp