java.lang.Object
g2301_2400.s2338_count_the_number_of_ideal_arrays.Solution

public class Solution extends Object
2338 - Count the Number of Ideal Arrays.<p>Hard</p> <p>You are given two integers <code>n</code> and <code>maxValue</code>, which are used to describe an <strong>ideal</strong> array.</p> <p>A <strong>0-indexed</strong> integer array <code>arr</code> of length <code>n</code> is considered <strong>ideal</strong> if the following conditions hold:</p> <ul> <li>Every <code>arr[i]</code> is a value from <code>1</code> to <code>maxValue</code>, for <code>0 <= i < n</code>.</li> <li>Every <code>arr[i]</code> is divisible by <code>arr[i - 1]</code>, for <code>0 < i < n</code>.</li> </ul> <p>Return <em>the number of <strong>distinct</strong> ideal arrays of length</em> <code>n</code>. Since the answer may be very large, return it modulo <code>10<sup>9</sup> + 7</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> n = 2, maxValue = 5</p> <p><strong>Output:</strong> 10</p> <p><strong>Explanation:</strong> The following are the possible ideal arrays:</p> <ul> <li> <p>Arrays starting with the value 1 (5 arrays): [1,1], [1,2], [1,3], [1,4], [1,5]</p> </li> <li> <p>Arrays starting with the value 2 (2 arrays): [2,2], [2,4]</p> </li> <li> <p>Arrays starting with the value 3 (1 array): [3,3]</p> </li> <li> <p>Arrays starting with the value 4 (1 array): [4,4]</p> </li> <li> <p>Arrays starting with the value 5 (1 array): [5,5]</p> </li> </ul> <p>There are a total of 5 + 2 + 1 + 1 + 1 = 10 distinct ideal arrays.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> n = 5, maxValue = 3</p> <p><strong>Output:</strong> 11</p> <p><strong>Explanation:</strong> The following are the possible ideal arrays:</p> <ul> <li> <p>Arrays starting with the value 1 (9 arrays):</p> <ul> <li> <p>With no other distinct values (1 array): [1,1,1,1,1]</p> </li> <li> <p>With 2<sup>nd</sup> distinct value 2 (4 arrays): [1,1,1,1,2], [1,1,1,2,2], [1,1,2,2,2], [1,2,2,2,2]</p> </li> <li> <p>With 2<sup>nd</sup> distinct value 3 (4 arrays): [1,1,1,1,3], [1,1,1,3,3], [1,1,3,3,3], [1,3,3,3,3]</p> </li> </ul> </li> <li> <p>Arrays starting with the value 2 (1 array): [2,2,2,2,2]</p> </li> <li> <p>Arrays starting with the value 3 (1 array): [3,3,3,3,3]</p> </li> </ul> <p>There are a total of 9 + 1 + 1 = 11 distinct ideal arrays.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= n <= 10<sup>4</sup></code></li> <li><code>1 <= maxValue <= 10<sup>4</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • idealArrays

      public int idealArrays(int n, int maxValue)