java.lang.Object
g2001_2100.s2090_k_radius_subarray_averages.Solution

public class Solution extends Object
2090 - K Radius Subarray Averages.<p>Medium</p> <p>You are given a <strong>0-indexed</strong> array <code>nums</code> of <code>n</code> integers, and an integer <code>k</code>.</p> <p>The <strong>k-radius average</strong> for a subarray of <code>nums</code> <strong>centered</strong> at some index <code>i</code> with the <strong>radius</strong> <code>k</code> is the average of <strong>all</strong> elements in <code>nums</code> between the indices <code>i - k</code> and <code>i + k</code> ( <strong>inclusive</strong> ). If there are less than <code>k</code> elements before <strong>or</strong> after the index <code>i</code>, then the <strong>k-radius average</strong> is <code>-1</code>.</p> <p>Build and return <em>an array</em> <code>avgs</code> <em>of length</em> <code>n</code> <em>where</em> <code>avgs[i]</code> <em>is the <strong>k-radius average</strong> for the subarray centered at index</em> <code>i</code>.</p> <p>The <strong>average</strong> of <code>x</code> elements is the sum of the <code>x</code> elements divided by <code>x</code>, using <strong>integer division</strong>. The integer division truncates toward zero, which means losing its fractional part.</p> <ul> <li>For example, the average of four elements <code>2</code>, <code>3</code>, <code>1</code>, and <code>5</code> is <code>(2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75</code>, which truncates to <code>2</code>.</li> </ul> <p><strong>Example 1:</strong></p> <p><img src="https://assets.leetcode.com/uploads/2021/11/07/eg1.png" alt="" /></p> <p><strong>Input:</strong> nums = [7,4,3,9,1,8,5,2,6], k = 3</p> <p><strong>Output:</strong> [-1,-1,-1,5,4,4,-1,-1,-1]</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>avg[0], avg[1], and avg[2] are -1 because there are less than k elements <strong>before</strong> each index.</p> </li> <li> <p>The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37.</p> <p>Using <strong>integer division</strong> , avg[3] = 37 / 7 = 5.</p> </li> <li> <p>For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4.</p> </li> <li> <p>For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4.</p> </li> <li> <p>avg[6], avg[7], and avg[8] are -1 because there are less than k elements <strong>after</strong> each index.</p> </li> </ul> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> nums = [100000], k = 0</p> <p><strong>Output:</strong> [100000]</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>The sum of the subarray centered at index 0 with radius 0 is: 100000.</p> <p>avg[0] = 100000 / 1 = 100000.</p> </li> </ul> <p><strong>Example 3:</strong></p> <p><strong>Input:</strong> nums = [8], k = 100000</p> <p><strong>Output:</strong> [-1]</p> <p><strong>Explanation:</strong></p> <ul> <li>avg[0] is -1 because there are less than k elements before and after index 0.</li> </ul> <p><strong>Constraints:</strong></p> <ul> <li><code>n == nums.length</code></li> <li><code>1 <= n <= 10<sup>5</sup></code></li> <li><code>0 <= nums[i], k <= 10<sup>5</sup></code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • getAverages

      public int[] getAverages(int[] nums, int k)