<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>paths Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/paths/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/paths/</link>
	<description></description>
	<lastBuildDate>Tue, 11 Oct 2022 01:09:30 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>paths Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/paths/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 2435. Paths in Matrix Whose Sum Is Divisible by K</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2435-paths-in-matrix-whose-sum-is-divisible-by-k/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2435-paths-in-matrix-whose-sum-is-divisible-by-k/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Tue, 11 Oct 2022 01:04:27 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[grid]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[paths]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9854</guid>

					<description><![CDATA[<p>You are given a&#160;0-indexed&#160;m x n&#160;integer matrix&#160;grid&#160;and an integer&#160;k. You are currently at position&#160;(0, 0)&#160;and you want to reach position&#160;(m - 1, n - 1)&#160;moving&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2435-paths-in-matrix-whose-sum-is-divisible-by-k/">花花酱 LeetCode 2435. Paths in Matrix Whose Sum Is Divisible by K</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="花花酱 LeetCode 2435. Paths in Matrix Whose Sum Is Divisible by K - 刷题找工作 EP403" width="500" height="281" src="https://www.youtube.com/embed/Gj8mvPsRkBI?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div></figure>



<p>You are given a&nbsp;<strong>0-indexed</strong>&nbsp;<code>m x n</code>&nbsp;integer matrix&nbsp;<code>grid</code>&nbsp;and an integer&nbsp;<code>k</code>. You are currently at position&nbsp;<code>(0, 0)</code>&nbsp;and you want to reach position&nbsp;<code>(m - 1, n - 1)</code>&nbsp;moving only&nbsp;<strong>down</strong>&nbsp;or&nbsp;<strong>right</strong>.</p>



<p>Return<em>&nbsp;the number of paths where the sum of the elements on the path is divisible by&nbsp;</em><code>k</code>. Since the answer may be very large, return it&nbsp;<strong>modulo</strong>&nbsp;<code>10<sup>9</sup>&nbsp;+ 7</code>.</p>



<p><strong>Example 1:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/08/13/image-20220813183124-1.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[5,2,4],[3,0,5],[0,7,2]], k = 3
<strong>Output:</strong> 2
<strong>Explanation:</strong> There are two paths where the sum of the elements on the path is divisible by k.
The first path highlighted in red has a sum of 5 + 2 + 4 + 5 + 2 = 18 which is divisible by 3.
The second path highlighted in blue has a sum of 5 + 3 + 0 + 5 + 2 = 15 which is divisible by 3.
</pre>



<p><strong>Example 2:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/08/17/image-20220817112930-3.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[0,0]], k = 5
<strong>Output:</strong> 1
<strong>Explanation:</strong> The path highlighted in red has a sum of 0 + 0 = 0 which is divisible by 5.
</pre>



<p><strong>Example 3:</strong></p>



<figure class="wp-block-image"><img src="https://assets.leetcode.com/uploads/2022/08/12/image-20220812224605-3.png" alt=""/></figure>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> grid = [[7,3,4,9],[2,3,6,2],[2,3,7,0]], k = 1
<strong>Output:</strong> 10
<strong>Explanation:</strong> Every integer is divisible by 1 so the sum of the elements on every possible path is divisible by k.
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>m == grid.length</code></li><li><code>n == grid[i].length</code></li><li><code>1 &lt;= m, n &lt;= 5 * 10<sup>4</sup></code></li><li><code>1 &lt;= m * n &lt;= 5 * 10<sup>4</sup></code></li><li><code>0 &lt;= grid[i][j] &lt;= 100</code></li><li><code>1 &lt;= k &lt;= 50</code></li></ul>



<h2><strong>Solution: DP</strong></h2>



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-1.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-1.png" alt="" class="wp-image-9858" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-1.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-1-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-1-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-2.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-2.png" alt="" class="wp-image-9859" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-2.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-2-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-2-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<figure class="wp-block-image size-full"><a href="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-3.png"><img width="960" height="540" src="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-3.png" alt="" class="wp-image-9861" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-3.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-3-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2022/10/2435-ep403-3-768x432.png 768w" sizes="(max-width: 960px) 100vw, 960px" /></a></figure>



<p>Let dp[i][j][r] := # of paths from (0,0) to (i,j) with path sum % k == r.<br><br>init: dp[0][0][grid[0][0] % k] = 1</p>



<p>dp[i][j][(r + grid[i][j]) % k] = dp[i-1][j][r] + dp[i][j-1][r]</p>



<p>ans = dp[m-1][n-1][0]</p>



<p>Time complexity: O(m*n*k)<br>Space complexity: O(m*n*k) -&gt; O(n*k)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  int numberOfPaths(vector&lt;vector&lt;int&gt;&gt;&amp; grid, int k) {
    const int kMod = 1e9 + 7;
    const int m = grid.size();
    const int n = grid[0].size();
    vector&lt;vector&lt;vector&lt;int&gt;&gt;&gt; dp(m, vector&lt;vector&lt;int&gt;&gt;(n, vector&lt;int&gt;(k)));
    dp[0][0][grid[0][0] % k] = 1;
    for (int i = 0; i &lt; m; ++i)
      for (int j = 0; j &lt; n; ++j) {
        if (i == 0 &amp;&amp; j == 0) continue;
        for (int r = 0; r &lt; k; ++r)
          dp[i][j][(r + grid[i][j]) % k] = 
            ((j ? dp[i][j - 1][r] : 0) + (i ? dp[i - 1][j][r] : 0)) % kMod;          
      }
    return dp[m - 1][n - 1][0];
  }
};</pre>
</div></div>



<p>Related Problems:</p>



<ul><li><a href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-62-unique-paths/" data-type="post" data-id="187">花花酱 LeetCode 62. Unique Paths</a></li></ul>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2435-paths-in-matrix-whose-sum-is-divisible-by-k/">花花酱 LeetCode 2435. Paths in Matrix Whose Sum Is Divisible by K</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-2435-paths-in-matrix-whose-sum-is-divisible-by-k/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 62. Unique Paths</title>
		<link>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-62-unique-paths/</link>
					<comments>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-62-unique-paths/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Sun, 10 Sep 2017 00:25:07 +0000</pubDate>
				<category><![CDATA[Dynamic Programming]]></category>
		<category><![CDATA[dp]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[paths]]></category>
		<category><![CDATA[unique]]></category>
		<guid isPermaLink="false">http://zxi.mytechroad.com/blog/?p=187</guid>

					<description><![CDATA[<p>Problem: A robot is located at the top-left corner of a m x n grid (marked &#8216;Start&#8217; in the diagram below). The robot can only move either down or&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-62-unique-paths/">花花酱 LeetCode 62. Unique Paths</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[<p><iframe width="500" height="375" src="https://www.youtube.com/embed/fmpP5Ll0Azc?feature=oembed" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe></p>
<p><strong>Problem:</strong></p>
<p>A robot is located at the top-left corner of a <i>m</i> x <i>n</i> grid (marked &#8216;Start&#8217; in the diagram below).</p>
<p>The robot can only move either down or right at any point in time. The robot is trying to reach the bottom-right corner of the grid (marked &#8216;Finish&#8217; in the diagram below).</p>
<p>How many possible unique paths are there?</p>
<p><img src="https://leetcode.com/static/images/problemset/robot_maze.png" /></p>
<p>Above is a 3 x 7 grid. How many possible unique paths are there?</p>
<p><b>Note:</b> <i>m</i> and <i>n</i> will be at most 100.</p>
<p><strong>Idea:</strong></p>
<p>Dynamic Programming</p>
<p><a href="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/62-ep45.png"><img class="alignnone size-full wp-image-196" src="http://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/62-ep45.png" alt="" width="960" height="540" srcset="https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/62-ep45.png 960w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/62-ep45-300x169.png 300w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/62-ep45-768x432.png 768w, https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/62-ep45-624x351.png 624w" sizes="(max-width: 960px) 100vw, 960px" /></a></p>
<p><strong>Solution1:</strong></p>
<p>C++</p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {    
public:
    int uniquePaths(int m, int n) {
        if (m &lt; 0 || n &lt; 0) return 0;
        if (m == 1 &amp;&amp; n == 1) return 1;
        if (f_[m][n] &gt; 0) return f_[m][n];        
        int left_paths = uniquePaths(m - 1, n);
        int top_paths = uniquePaths(m, n - 1);
        f_[m][n] = left_paths + top_paths;
        return f_[m][n];
    }
private:
    unordered_map&lt;int, unordered_map&lt;int, int&gt;&gt; f_;
};</pre><p>Java</p><pre class="crayon-plain-tag">// Author: Huahua
// Running time: 0 ms
class Solution {
  private int[][] dp;
  private int m;
  private int n;
  
  public int uniquePaths(int m, int n) {
    this.dp = new int[m][n];    
    this.m = m;
    this.n = n;
    return dfs(0, 0);
  }
  
  private int dfs(int x, int y) {
    if (x &gt; m - 1 || y &gt; n - 1)
      return 0;
    if (x == m - 1 &amp;&amp; y == n - 1)
      return 1;
    if (dp[x][y] == 0)     
      dp[x][y] = dfs(x + 1, y) + dfs(x , y + 1);
    return dp[x][y];
  } 
}</pre><p>&nbsp;</p>
<p><strong>Solution2:</strong></p><pre class="crayon-plain-tag">// Author: Huahua
class Solution {    
public:
    int uniquePaths(int m, int n) {
        auto f = vector&lt;vector&lt;int&gt;&gt;(n + 1, vector&lt;int&gt;(m + 1, 0));
        f[1][1] = 1;
        
        for (int y = 1; y &lt;= n; ++y)
            for(int x = 1; x &lt;= m; ++x) {
                if (x == 1 &amp;&amp; y == 1) {
                    continue;
                } else {
                    f[y][x] = f[y-1][x] + f[y][x-1];
                }
            }
        
        return f[n][m];
    }
};</pre><p>&nbsp;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-62-unique-paths/">花花酱 LeetCode 62. Unique Paths</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/dynamic-programming/leetcode-62-unique-paths/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
