Skip to content

Commit 5de27c4

Browse files
committed
leetcode
1 parent 2af51a4 commit 5de27c4

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed
+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
3+
4+
-* 2090. K Radius SubArray Averages *-
5+
6+
7+
8+
You are given a 0-indexed array nums of n integers, and an integer k.
9+
10+
The k-radius average for a subarray of nums centered at some index i with the radius k is the average of all elements in nums between the indices i - k and i + k (inclusive). If there are less than k elements before or after the index i, then the k-radius average is -1.
11+
12+
Build and return an array avgs of length n where avgs[i] is the k-radius average for the subarray centered at index i.
13+
14+
The average of x elements is the sum of the x elements divided by x, using integer division. The integer division truncates toward zero, which means losing its fractional part.
15+
16+
For example, the average of four elements 2, 3, 1, and 5 is (2 + 3 + 1 + 5) / 4 = 11 / 4 = 2.75, which truncates to 2.
17+
18+
19+
Example 1:
20+
21+
22+
Input: nums = [7,4,3,9,1,8,5,2,6], k = 3
23+
Output: [-1,-1,-1,5,4,4,-1,-1,-1]
24+
Explanation:
25+
- avg[0], avg[1], and avg[2] are -1 because there are less than k elements before each index.
26+
- The sum of the subarray centered at index 3 with radius 3 is: 7 + 4 + 3 + 9 + 1 + 8 + 5 = 37.
27+
Using integer division, avg[3] = 37 / 7 = 5.
28+
- For the subarray centered at index 4, avg[4] = (4 + 3 + 9 + 1 + 8 + 5 + 2) / 7 = 4.
29+
- For the subarray centered at index 5, avg[5] = (3 + 9 + 1 + 8 + 5 + 2 + 6) / 7 = 4.
30+
- avg[6], avg[7], and avg[8] are -1 because there are less than k elements after each index.
31+
Example 2:
32+
33+
Input: nums = [100000], k = 0
34+
Output: [100000]
35+
Explanation:
36+
- The sum of the subarray centered at index 0 with radius 0 is: 100000.
37+
avg[0] = 100000 / 1 = 100000.
38+
Example 3:
39+
40+
Input: nums = [8], k = 100000
41+
Output: [-1]
42+
Explanation:
43+
- avg[0] is -1 because there are less than k elements before and after index 0.
44+
45+
46+
Constraints:
47+
48+
n == nums.length
49+
1 <= n <= 105
50+
0 <= nums[i], k <= 105
51+
52+
*/
53+
54+
class A {
55+
List<int> getAverages(List<int> nums, int k) {
56+
int n = nums.length;
57+
int windowSize = 2 * k + 1;
58+
List<int> ans = List.filled(n, -1);
59+
// Arrays.fill(ans,-1);
60+
61+
if (n < windowSize) {
62+
return ans;
63+
}
64+
65+
List<int> prefixSum = List.filled(n + 1, 0);
66+
for (int i = 0; i < n; ++i) {
67+
prefixSum[i + 1] = prefixSum[i] + nums[i];
68+
}
69+
70+
for (int i = k; i + k < n; ++i) {
71+
ans[i] = ((prefixSum[i + k + 1] - prefixSum[i - k]) ~/ windowSize);
72+
}
73+
74+
return ans;
75+
}
76+
}
77+
78+
class B {
79+
List<int> getAverages(List<int> nums, int k) {
80+
final int n = nums.length;
81+
final int windowSize = 2 * k + 1;
82+
83+
int windowSum = 0;
84+
final List<int> result = List.filled(n, -1);
85+
86+
if (n < windowSize) return result;
87+
88+
for (int i = 0; i < n; ++i) {
89+
windowSum += nums[i];
90+
91+
if (i - windowSize >= 0) windowSum -= nums[i - windowSize];
92+
93+
if (i >= windowSize - 1) result[i - k] = windowSum ~/ windowSize;
94+
}
95+
96+
return result;
97+
}
98+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package main
2+
3+
func getAverages(nums []int, k int) []int {
4+
n := len(nums)
5+
windowSize := 2*k + 1
6+
7+
var windowSum int64
8+
result := make([]int, n)
9+
for i := 0; i < n; i++ {
10+
result[i]=-1
11+
windowSum += int64(nums[i])
12+
13+
if i >= windowSize {
14+
windowSum -= int64(nums[i-windowSize])
15+
}
16+
17+
if i >= windowSize-1 {
18+
result[i-k] = int(windowSum / int64(windowSize))
19+
}
20+
}
21+
22+
return result
23+
}
+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# 🔥 2 Solutions 🔥 || Simple Fast and Easy || with Explanation 😈
2+
3+
## Intuition
4+
5+
The problem aims to calculate the averages of subArrays within a given array, where each subArray has a fixed radius of k (i.e., a subarray of length 2k + 1). The goal is to efficiently compute these averages for each subArray.
6+
7+
## Approach 1: Prefix Sum
8+
9+
First, it checks if the size of the input list nums is too small to form a valid subArray. If it is, the code returns a list of -1s as there are not enough numbers to calculate the averages.
10+
If the input list is large enough, the code proceeds to calculate the averages. It creates a new list called ans with the same size as nums and fills it with -1s. This list will store the calculated averages.
11+
The code then creates another list called prefixSum to help efficiently calculate the sum of subArrays. This list will store the cumulative sums of the numbers in nums.
12+
It goes through each number in nums and calculates the cumulative sum up to that point. It does this by adding the current number to the sum of all previous numbers. The cumulative sums are stored in the prefixSum list.
13+
Next, the code enters a loop that goes through the indices of nums, starting from k and ending at n - k - 1, where n is the size of nums. These indices represent the starting positions of the subarrays for which we want to calculate the averages.
14+
For each index i, the code calculates the sum of the subarray by subtracting the prefix sum at index i - k from the prefix sum at index i + k + 1. This gives us the sum of all the numbers within the subarray.
15+
After obtaining the sum of the subarray, the code divides it by the size of the subarray (2k + 1) to calculate the average. This average represents the average value of the numbers within the subarray.
16+
The code stores the calculated average in the ans list at the corresponding index i. This way, each element in the ans list represents the average value of the subarray starting at that index.
17+
Once the loop completes, the code has calculated the averages for all valid subarrays. It returns the ans list, which contains the computed averages.
18+
19+
```dart
20+
class Solution {
21+
List<int> getAverages(List<int> nums, int k) {
22+
final int n = nums.length;
23+
final int windowSize = 2 * k + 1;
24+
final List<int> ans = List.filled(n, -1);
25+
26+
if (n < windowSize) {
27+
return ans;
28+
}
29+
30+
List<int> prefixSum = List.filled(n + 1, 0);
31+
for (int i = 0; i < n; ++i) {
32+
prefixSum[i + 1] = prefixSum[i] + nums[i];
33+
}
34+
35+
for (int i = k; i + k < n; ++i) {
36+
ans[i] = ((prefixSum[i + k + 1] - prefixSum[i - k]) ~/ windowSize);
37+
}
38+
39+
return ans;
40+
}
41+
}
42+
```
43+
44+
## Approach 2: Sliding Window
45+
46+
It starts by initializing variables, such as the size of the input list nums (n), the window size (2k + 1), a variable windowSum to track the sum of the current window, and a list called result to store the calculated averages.
47+
It checks if the size of the input list nums is too small to form a valid subarray. If it is, the code returns the initialized result list filled with -1s.
48+
The code enters a loop that iterates over each index i in the list nums.
49+
Inside the loop, it updates the windowSum by adding the current number nums[i] to it. This step represents adding the new number to the current window.
50+
The code checks if the current index i minus the window size (i - windowSize) is greater than or equal to zero. If this condition is met, it means the current window has moved beyond the first window (starting at index 0). In this case, the code subtracts the number at nums[i - windowSize] from the windowSum to remove it from the window. This step represents removing the number that is no longer part of the window.
51+
The code checks if the current index i is greater than or equal to windowSize - 1. If this condition is met, it means the current window has reached the desired size. In this case, the code calculates the average of the window by dividing the windowSum by the window size (windowSum / windowSize). It then stores the calculated average in the result list at the corresponding index i - k, where k represents half the window size. This step represents storing the average value in the result list.
52+
Once the loop completes, the code has calculated the averages for all valid subarrays. It returns the result list, which contains the computed averages.
53+
54+
```dart
55+
class Solution {
56+
List<int> getAverages(List<int> nums, int k) {
57+
final int n = nums.length;
58+
final int windowSize = 2 * k + 1;
59+
60+
int windowSum = 0;
61+
final List<int> result = List.filled(n, -1);
62+
63+
if (n < windowSize) return result;
64+
65+
for (int i = 0; i < n; ++i) {
66+
windowSum += nums[i];
67+
68+
if (i - windowSize >= 0) windowSum -= nums[i - windowSize];
69+
70+
if (i >= windowSize - 1) result[i - k] = (windowSum ~/ windowSize);
71+
}
72+
73+
return result;
74+
}
75+
}
76+
```

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
234234
- [**1569.** Number of Ways to Reorder Array to Get Same BST](NumberOfWaysToReorderArrayToGetSameBst/number_of_ways_to_reorder_array_to_get_same_bst.dart)
235235
- [**1187.** Make Array Strictly Increasing](MakeArrayStrictlyIncreasing\make_array_strictly_increasing.dart)
236236
- [**1732.** Find the Highest Altitude](FindTheHighestAltitude/find_the_highest_altitude.dart)
237+
- [**2090.** K Radius SubArray Averages](KRadiusSubarrayAverages/k_radius_subarray_averages.dart)
237238

238239
## Reach me via
239240

0 commit comments

Comments
 (0)