Skip to content

Commit 3fda620

Browse files
committed
Maximum average subarray I.
1 parent acff683 commit 3fda620

File tree

4 files changed

+51
-0
lines changed

4 files changed

+51
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/**
2+
You are given an integer array nums consisting of n elements, and an integer k.
3+
4+
Find a contiguous subarray whose length is equal to k that has the maximum average value and return this value. Any answer with a calculation error less than 10-5 will be accepted.
5+
6+
 
7+
8+
Example 1:
9+
Input: nums = [1,12,-5,-6,50,3], k = 4
10+
Output: 12.75000
11+
Explanation: Maximum average is (12 - 5 - 6 + 50) / 4 = 51 / 4 = 12.75
12+
13+
Example 2:
14+
Input: nums = [5], k = 1
15+
Output: 5.00000
16+
 
17+
18+
Constraints:
19+
- n == nums.length
20+
- 1 <= k <= n <= 105
21+
- -104 <= nums[i] <= 104
22+
*/
23+
class Solution {
24+
func findMaxAverage(_ nums: [Int], _ k: Int) -> Double {
25+
if nums.count == 1 && k == 1 { return Double(nums[0]) }
26+
var max = Int.min
27+
for i in 0...(nums.count - k) {
28+
let sum = nums[i..<(i + k)].reduce(0, +)
29+
if sum > max {
30+
max = sum
31+
}
32+
}
33+
return (Double(max) * 100000 / Double(k)).rounded() / Double(100000)
34+
}
35+
}
36+
37+
let s = Solution()
38+
let r = s.findMaxAverage([8860,-853,6534,4477,-4589,8646,-6155,-5577,-1656,-5779,-2619,-8604,-1358,-8009,4983,7063,3104,-1560,4080,2763,5616,-2375,2848,1394,-7173,-5225,-8244,-809,8025,-4072,-4391,-9579,1407,6700,2421,-6685,5481,-1732,-8892,-6645,3077,3287,-4149,8701,-4393,-9070,-1777,2237,-3253,-506,-4931,-7366,-8132,5406,-6300,-275,-1908,67,3569,1433,-7262,-437,8303,4498,-379,3054,-6285,4203,6908,4433,3077,2288,9733,-8067,3007,9725,9669,1362,-2561,-4225,5442,-9006,-429,160,-9234,-4444,3586,-5711,-9506,-79,-4418,-4348,-5891], 93)
39+
print(r)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
2+
<playground version='5.0' target-platform='ios' buildActiveScheme='true' importAppTypes='true'>
3+
<timeline fileName='timeline.xctimeline'/>
4+
</playground>

Easy/643.Maximum Average Subarray I.playground/playground.xcworkspace/contents.xcworkspacedata

+7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
113. [Minimum Index Sum of Two Lists](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/599.Minimum%20Index%20Sum%20of%20Two%20Lists.playground/Contents.swift)
118118
114. [Can Place Flowers](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/605.Can%20Place%20Flowers.playground/Contents.swift)
119119
115. [Maximum Product of Three Numbers](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/628.Maximum%20Product%20of%20Three%20Numbers.playground/Contents.swift)
120+
116. [Maximum Average Subarray I](https://github.com/recherst/leetcode-algtorithm/blob/main/Easy/643.Maximum%20Average%20Subarray%20I.playground/Contents.swift)
120121

121122
#### Medium
122123

0 commit comments

Comments
 (0)