Skip to content

Commit 8d2a341

Browse files
committed
Add solution 0643
1 parent 1030741 commit 8d2a341

File tree

11 files changed

+228
-40
lines changed

11 files changed

+228
-40
lines changed

README.md

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,16 @@
125125

126126
| | Easy | Medium | Hard | Total |
127127
|:--------:|:--------:|:--------:|:--------:|:--------:|
128-
|Optimizing|39|49|18|106|
128+
|Optimizing|38|49|18|105|
129129
|Accepted|**251**|**326**|**99**|**676**|
130130
|Total|461|921|373|1755|
131-
|Perfection Rate|84.5%|85.0%|81.8%|84.3%|
131+
|Perfection Rate|84.9%|85.0%|81.8%|84.5%|
132132
|Completion Rate|54.4%|35.4%|26.5%|38.5%|
133133
|------------|----------------------------|----------------------------|----------------------------|----------------------------|
134134

135135
## 二. 目录
136136

137-
以下已经收录了 570 道题的题解,还有 10 道题在尝试优化到 beats 100%
137+
以下已经收录了 571 道题的题解,还有 10 道题在尝试优化到 beats 100%
138138

139139
| No. | Title | Solution | Acceptance | Difficulty | Frequency |
140140
|:--------:|:--------------------------------------------------------------|:--------:|:--------:|:--------:|:--------:|
@@ -532,7 +532,7 @@
532532
|0392|Is Subsequence|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0392.Is-Subsequence)|49.5%|Easy||
533533
|0393|UTF-8 Validation|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0393.UTF-8-Validation)|38.0%|Medium||
534534
|0394|Decode String|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0394.Decode-String)|52.5%|Medium||
535-
|0395|Longest Substring with At Least K Repeating Characters||43.4%|Medium||
535+
|0395|Longest Substring with At Least K Repeating Characters||43.5%|Medium||
536536
|0396|Rotate Function||36.7%|Medium||
537537
|0397|Integer Replacement|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0397.Integer-Replacement)|33.4%|Medium||
538538
|0398|Random Pick Index||57.8%|Medium||
@@ -566,15 +566,15 @@
566566
|0426|Convert Binary Search Tree to Sorted Doubly Linked List||61.0%|Medium||
567567
|0427|Construct Quad Tree||62.5%|Medium||
568568
|0428|Serialize and Deserialize N-ary Tree||61.3%|Hard||
569-
|0429|N-ary Tree Level Order Traversal||66.6%|Medium||
569+
|0429|N-ary Tree Level Order Traversal||66.5%|Medium||
570570
|0430|Flatten a Multilevel Doubly Linked List||56.7%|Medium||
571571
|0431|Encode N-ary Tree to Binary Tree||74.5%|Hard||
572572
|0432|All O`one Data Structure||33.1%|Hard||
573573
|0433|Minimum Genetic Mutation|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0433.Minimum-Genetic-Mutation)|43.2%|Medium||
574574
|0434|Number of Segments in a String||37.8%|Easy||
575575
|0435|Non-overlapping Intervals|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0435.Non-overlapping-Intervals)|43.9%|Medium||
576576
|0436|Find Right Interval|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0436.Find-Right-Interval)|48.5%|Medium||
577-
|0437|Path Sum III|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0437.Path-Sum-III)|48.0%|Medium||
577+
|0437|Path Sum III|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0437.Path-Sum-III)|48.1%|Medium||
578578
|0438|Find All Anagrams in a String|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0438.Find-All-Anagrams-in-a-String)|44.8%|Medium||
579579
|0439|Ternary Expression Parser||56.7%|Medium||
580580
|0440|K-th Smallest in Lexicographical Order||29.8%|Hard||
@@ -647,7 +647,7 @@
647647
|0507|Perfect Number|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0507.Perfect-Number)|36.1%|Easy||
648648
|0508|Most Frequent Subtree Sum|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0508.Most-Frequent-Subtree-Sum)|59.0%|Medium||
649649
|0509|Fibonacci Number|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0509.Fibonacci-Number)|67.4%|Easy||
650-
|0510|Inorder Successor in BST II||59.9%|Medium||
650+
|0510|Inorder Successor in BST II||60.0%|Medium||
651651
|0511|Game Play Analysis I||81.2%|Easy||
652652
|0512|Game Play Analysis II||55.9%|Easy||
653653
|0513|Find Bottom Left Tree Value|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0513.Find-Bottom-Left-Tree-Value)|62.4%|Medium||
@@ -780,7 +780,7 @@
780780
|0640|Solve the Equation||42.7%|Medium||
781781
|0641|Design Circular Deque||54.6%|Medium||
782782
|0642|Design Search Autocomplete System||46.1%|Hard||
783-
|0643|Maximum Average Subarray I||42.0%|Easy||
783+
|0643|Maximum Average Subarray I|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0643.Maximum-Average-Subarray-I)|42.0%|Easy||
784784
|0644|Maximum Average Subarray II||34.0%|Hard||
785785
|0645|Set Mismatch|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0645.Set-Mismatch)|42.5%|Easy||
786786
|0646|Maximum Length of Pair Chain||52.8%|Medium||
@@ -907,7 +907,7 @@
907907
|0767|Reorganize String|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0767.Reorganize-String)|50.0%|Medium||
908908
|0768|Max Chunks To Make Sorted II||49.7%|Hard||
909909
|0769|Max Chunks To Make Sorted||55.5%|Medium||
910-
|0770|Basic Calculator IV||54.1%|Hard||
910+
|0770|Basic Calculator IV||54.0%|Hard||
911911
|0771|Jewels and Stones|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0771.Jewels-and-Stones)|86.9%|Easy||
912912
|0772|Basic Calculator III||43.4%|Hard||
913913
|0773|Sliding Puzzle||60.7%|Hard||
@@ -958,7 +958,7 @@
958958
|0818|Race Car||39.8%|Hard||
959959
|0819|Most Common Word|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0819.Most-Common-Word)|45.5%|Easy||
960960
|0820|Short Encoding of Words||51.5%|Medium||
961-
|0821|Shortest Distance to a Character||68.0%|Easy||
961+
|0821|Shortest Distance to a Character||68.2%|Easy||
962962
|0822|Card Flipping Game||43.6%|Medium||
963963
|0823|Binary Trees With Factors||36.5%|Medium||
964964
|0824|Goat Latin||66.4%|Easy||
@@ -1101,7 +1101,7 @@
11011101
|0961|N-Repeated Element in Size 2N Array|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/0961.N-Repeated-Element-in-Size-2N-Array)|74.4%|Easy||
11021102
|0962|Maximum Width Ramp||46.5%|Medium||
11031103
|0963|Minimum Area Rectangle II||51.8%|Medium||
1104-
|0964|Least Operators to Express Number||44.9%|Hard||
1104+
|0964|Least Operators to Express Number||45.0%|Hard||
11051105
|0965|Univalued Binary Tree||67.8%|Easy||
11061106
|0966|Vowel Spellchecker||47.9%|Medium||
11071107
|0967|Numbers With Same Consecutive Differences||44.7%|Medium||
@@ -1257,7 +1257,7 @@
12571257
|1117|Building H2O||53.1%|Medium||
12581258
|1118|Number of Days in a Month||57.6%|Easy||
12591259
|1119|Remove Vowels from a String||90.5%|Easy||
1260-
|1120|Maximum Average Subtree||63.6%|Medium||
1260+
|1120|Maximum Average Subtree||63.5%|Medium||
12611261
|1121|Divide Array Into Increasing Sequences||58.0%|Hard||
12621262
|1122|Relative Sort Array|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1122.Relative-Sort-Array)|68.1%|Easy||
12631263
|1123|Lowest Common Ancestor of Deepest Leaves|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1123.Lowest-Common-Ancestor-of-Deepest-Leaves)|67.9%|Medium||
@@ -1271,7 +1271,7 @@
12711271
|1131|Maximum of Absolute Value Expression||52.1%|Medium||
12721272
|1132|Reported Posts II||34.6%|Medium||
12731273
|1133|Largest Unique Number||67.2%|Easy||
1274-
|1134|Armstrong Number||78.3%|Easy||
1274+
|1134|Armstrong Number||78.2%|Easy||
12751275
|1135|Connecting Cities With Minimum Cost||59.3%|Medium||
12761276
|1136|Parallel Courses||61.2%|Hard||
12771277
|1137|N-th Tribonacci Number|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1137.N-th-Tribonacci-Number)|56.0%|Easy||
@@ -1449,7 +1449,7 @@
14491449
|1309|Decrypt String from Alphabet to Integer Mapping||77.3%|Easy||
14501450
|1310|XOR Queries of a Subarray||69.2%|Medium||
14511451
|1311|Get Watched Videos by Your Friends||44.2%|Medium||
1452-
|1312|Minimum Insertion Steps to Make a String Palindrome||59.5%|Hard||
1452+
|1312|Minimum Insertion Steps to Make a String Palindrome||59.4%|Hard||
14531453
|1313|Decompress Run-Length Encoded List|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1313.Decompress-Run-Length-Encoded-List)|85.4%|Easy||
14541454
|1314|Matrix Block Sum||73.6%|Medium||
14551455
|1315|Sum of Nodes with Even-Valued Grandparent||84.2%|Medium||
@@ -1469,9 +1469,9 @@
14691469
|1329|Sort the Matrix Diagonally|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1329.Sort-the-Matrix-Diagonally)|81.8%|Medium||
14701470
|1330|Reverse Subarray To Maximize Array Value||36.5%|Hard||
14711471
|1331|Rank Transform of an Array||57.6%|Easy||
1472-
|1332|Remove Palindromic Subsequences||62.7%|Easy||
1472+
|1332|Remove Palindromic Subsequences||62.8%|Easy||
14731473
|1333|Filter Restaurants by Vegan-Friendly, Price and Distance||57.1%|Medium||
1474-
|1334|Find the City With the Smallest Number of Neighbors at a Threshold Distance||46.7%|Medium||
1474+
|1334|Find the City With the Smallest Number of Neighbors at a Threshold Distance||46.8%|Medium||
14751475
|1335|Minimum Difficulty of a Job Schedule||57.0%|Hard||
14761476
|1336|Number of Transactions per Visit||47.8%|Hard||
14771477
|1337|The K Weakest Rows in a Matrix||70.4%|Easy||
@@ -1490,7 +1490,7 @@
14901490
|1350|Students With Invalid Departments||90.4%|Easy||
14911491
|1351|Count Negative Numbers in a Sorted Matrix||76.0%|Easy||
14921492
|1352|Product of the Last K Numbers||44.2%|Medium||
1493-
|1353|Maximum Number of Events That Can Be Attended||30.4%|Medium||
1493+
|1353|Maximum Number of Events That Can Be Attended||30.5%|Medium||
14941494
|1354|Construct Target Array With Multiple Sums||31.3%|Hard||
14951495
|1355|Activity Participants||74.4%|Medium||
14961496
|1356|Sort Integers by The Number of 1 Bits||69.7%|Easy||
@@ -1549,7 +1549,7 @@
15491549
|1409|Queries on a Permutation With Key||81.5%|Medium||
15501550
|1410|HTML Entity Parser||54.4%|Medium||
15511551
|1411|Number of Ways to Paint N × 3 Grid||60.4%|Hard||
1552-
|1412|Find the Quiet Students in All Exams||65.3%|Hard||
1552+
|1412|Find the Quiet Students in All Exams||65.2%|Hard||
15531553
|1413|Minimum Value to Get Positive Step by Step Sum||65.5%|Easy||
15541554
|1414|Find the Minimum Number of Fibonacci Numbers Whose Sum Is K||63.1%|Medium||
15551555
|1415|The k-th Lexicographical String of All Happy Strings of Length n||69.9%|Medium||
@@ -1599,7 +1599,7 @@
15991599
|1459|Rectangles Area||64.6%|Medium||
16001600
|1460|Make Two Arrays Equal by Reversing Sub-arrays||72.2%|Easy||
16011601
|1461|Check If a String Contains All Binary Codes of Size K||47.2%|Medium||
1602-
|1462|Course Schedule IV||44.6%|Medium||
1602+
|1462|Course Schedule IV||44.5%|Medium||
16031603
|1463|Cherry Pickup II|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1463.Cherry-Pickup-II)|69.4%|Hard||
16041604
|1464|Maximum Product of Two Elements in an Array|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1464.Maximum-Product-of-Two-Elements-in-an-Array)|77.2%|Easy||
16051605
|1465|Maximum Area of a Piece of Cake After Horizontal and Vertical Cuts||32.5%|Medium||
@@ -1636,7 +1636,7 @@
16361636
|1496|Path Crossing||55.3%|Easy||
16371637
|1497|Check If Array Pairs Are Divisible by k||40.5%|Medium||
16381638
|1498|Number of Subsequences That Satisfy the Given Sum Condition||38.3%|Medium||
1639-
|1499|Max Value of Equation||45.3%|Hard||
1639+
|1499|Max Value of Equation||45.2%|Hard||
16401640
|1500|Design a File Sharing System||46.6%|Medium||
16411641
|1501|Countries You Can Safely Invest In||60.3%|Medium||
16421642
|1502|Can Make Arithmetic Progression From Sequence||71.2%|Easy||
@@ -1678,7 +1678,7 @@
16781678
|1538|Guess the Majority in a Hidden Array||61.6%|Medium||
16791679
|1539|Kth Missing Positive Number|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1539.Kth-Missing-Positive-Number)|55.1%|Easy||
16801680
|1540|Can Convert String in K Moves||30.7%|Medium||
1681-
|1541|Minimum Insertions to Balance a Parentheses String||42.7%|Medium||
1681+
|1541|Minimum Insertions to Balance a Parentheses String||42.6%|Medium||
16821682
|1542|Find Longest Awesome Substring||36.6%|Hard||
16831683
|1543|Fix Product Name Format||67.5%|Easy||
16841684
|1544|Make The String Great||55.3%|Easy||
@@ -1740,7 +1740,7 @@
17401740
|1600|Throne Inheritance||60.1%|Medium||
17411741
|1601|Maximum Number of Achievable Transfer Requests||47.8%|Hard||
17421742
|1602|Find Nearest Right Node in Binary Tree||73.9%|Medium||
1743-
|1603|Design Parking System||86.5%|Easy||
1743+
|1603|Design Parking System||86.6%|Easy||
17441744
|1604|Alert Using Same Key-Card Three or More Times in a One Hour Period||42.3%|Medium||
17451745
|1605|Find Valid Matrix Given Row and Column Sums||77.2%|Medium||
17461746
|1606|Find Servers That Handled Most Number of Requests||36.9%|Hard||
@@ -1781,7 +1781,7 @@
17811781
|1641|Count Sorted Vowel Strings|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1641.Count-Sorted-Vowel-Strings)|77.0%|Medium||
17821782
|1642|Furthest Building You Can Reach||50.6%|Medium||
17831783
|1643|Kth Smallest Instructions||43.3%|Hard||
1784-
|1644|Lowest Common Ancestor of a Binary Tree II||57.3%|Medium||
1784+
|1644|Lowest Common Ancestor of a Binary Tree II||57.2%|Medium||
17851785
|1645|Hopper Company Queries II||40.0%|Hard||
17861786
|1646|Get Maximum in Generated Array|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1646.Get-Maximum-in-Generated-Array)|53.5%|Easy||
17871787
|1647|Minimum Deletions to Make Character Frequencies Unique|[Go](https://github.com/halfrost/LeetCode-Go/tree/master/leetcode/1647.Minimum-Deletions-to-Make-Character-Frequencies-Unique)|54.3%|Medium||
@@ -1852,27 +1852,27 @@
18521852
|1712|Ways to Split Array Into Three Subarrays||29.8%|Medium||
18531853
|1713|Minimum Operations to Make a Subsequence||45.6%|Hard||
18541854
|1714|Sum Of Special Evenly-Spaced Elements In Array||48.2%|Hard||
1855-
|1715|Count Apples and Oranges||79.5%|Medium||
1855+
|1715|Count Apples and Oranges||79.4%|Medium||
18561856
|1716|Calculate Money in Leetcode Bank||67.6%|Easy||
18571857
|1717|Maximum Score From Removing Substrings||39.6%|Medium||
18581858
|1718|Construct the Lexicographically Largest Valid Sequence||44.3%|Medium||
18591859
|1719|Number Of Ways To Reconstruct A Tree||39.4%|Hard||
18601860
|1720|Decode XORed Array||85.9%|Easy||
18611861
|1721|Swapping Nodes in a Linked List||65.0%|Medium||
18621862
|1722|Minimize Hamming Distance After Swap Operations||55.4%|Medium||
1863-
|1723|Find Minimum Time to Finish All Jobs||44.4%|Hard||
1863+
|1723|Find Minimum Time to Finish All Jobs||44.3%|Hard||
18641864
|1724|Checking Existence of Edge Length Limited Paths II||58.5%|Hard||
18651865
|1725|Number Of Rectangles That Can Form The Largest Square||77.7%|Easy||
18661866
|1726|Tuple with Same Product||55.1%|Medium||
1867-
|1727|Largest Submatrix With Rearrangements||58.2%|Medium||
1867+
|1727|Largest Submatrix With Rearrangements||58.3%|Medium||
18681868
|1728|Cat and Mouse II||40.9%|Hard||
18691869
|1729|Find Followers Count||72.2%|Easy||
18701870
|1730|Shortest Path to Get Food||59.4%|Medium||
18711871
|1731|The Number of Employees Which Report to Each Employee||50.2%|Easy||
18721872
|1732|Find the Highest Altitude||82.0%|Easy||
18731873
|1733|Minimum Number of People to Teach||36.4%|Medium||
18741874
|1734|Decode XORed Permutation||50.7%|Medium||
1875-
|1735|Count Ways to Make Array With Product||49.4%|Hard||
1875+
|1735|Count Ways to Make Array With Product||49.3%|Hard||
18761876
|1736|Latest Time by Replacing Hidden Digits||41.3%|Easy||
18771877
|1737|Change Minimum Characters to Satisfy One of Three Conditions||28.9%|Medium||
18781878
|1738|Find Kth Largest XOR Coordinate Value||62.1%|Medium||
@@ -1882,17 +1882,17 @@
18821882
|1742|Maximum Number of Balls in a Box||74.9%|Easy||
18831883
|1743|Restore the Array From Adjacent Pairs||61.9%|Medium||
18841884
|1744|Can You Eat Your Favorite Candy on Your Favorite Day?||30.3%|Medium||
1885-
|1745|Palindrome Partitioning IV||48.7%|Hard||
1886-
|1746|Maximum Subarray Sum After One Operation||64.0%|Medium||
1885+
|1745|Palindrome Partitioning IV||48.8%|Hard||
1886+
|1746|Maximum Subarray Sum After One Operation||64.1%|Medium||
18871887
|1747|Leetflex Banned Accounts||69.5%|Medium||
1888-
|1748|Sum of Unique Elements||77.9%|Easy||
1889-
|1749|Maximum Absolute Sum of Any Subarray||45.3%|Medium||
1888+
|1748|Sum of Unique Elements||78.0%|Easy||
1889+
|1749|Maximum Absolute Sum of Any Subarray||45.5%|Medium||
18901890
|1750|Minimum Length of String After Deleting Similar Ends||39.3%|Medium||
1891-
|1751|Maximum Number of Events That Can Be Attended II||42.0%|Hard||
1891+
|1751|Maximum Number of Events That Can Be Attended II||42.1%|Hard||
18921892
|1752|Check if Array Is Sorted and Rotated||48.6%|Easy||
1893-
|1753|Maximum Score From Removing Stones||57.3%|Medium||
1894-
|1754|Largest Merge Of Two Strings||32.2%|Medium||
1895-
|1755|Closest Subsequence Sum||23.1%|Hard||
1893+
|1753|Maximum Score From Removing Stones||57.5%|Medium||
1894+
|1754|Largest Merge Of Two Strings||32.4%|Medium||
1895+
|1755|Closest Subsequence Sum||23.9%|Hard||
18961896
|------------|-------------------------------------------------------|-------| ----------------| ---------------|-------------|
18971897

18981898
------------------------------------------------------------------
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package leetcode
2+
3+
func findMaxAverage(nums []int, k int) float64 {
4+
sum := 0
5+
for _, v := range nums[:k] {
6+
sum += v
7+
}
8+
maxSum := sum
9+
for i := k; i < len(nums); i++ {
10+
sum = sum - nums[i-k] + nums[i]
11+
maxSum = max(maxSum, sum)
12+
}
13+
return float64(maxSum) / float64(k)
14+
}
15+
16+
func max(a, b int) int {
17+
if a > b {
18+
return a
19+
}
20+
return b
21+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package leetcode
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
)
7+
8+
type question643 struct {
9+
para643
10+
ans643
11+
}
12+
13+
// para 是参数
14+
// one 代表第一个参数
15+
type para643 struct {
16+
nums []int
17+
k int
18+
}
19+
20+
// ans 是答案
21+
// one 代表第一个答案
22+
type ans643 struct {
23+
one float64
24+
}
25+
26+
func Test_Problem643(t *testing.T) {
27+
28+
qs := []question643{
29+
30+
{
31+
para643{[]int{1, 12, -5, -6, 50, 3}, 4},
32+
ans643{12.75},
33+
},
34+
}
35+
36+
fmt.Printf("------------------------Leetcode Problem 643------------------------\n")
37+
38+
for _, q := range qs {
39+
_, p := q.ans643, q.para643
40+
fmt.Printf("【input】:%v 【output】:%v\n", p, findMaxAverage(p.nums, p.k))
41+
}
42+
fmt.Printf("\n\n\n")
43+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# 643. Maximum Average Subarray I
2+
3+
Added By: halfrost
4+
Created Time: Feb 7, 2021 5:07 PM
5+
Difficulty: Easy
6+
Link: https://leetcode.com/problems/maximum-average-subarray-i/
7+
Tags: Array
8+
9+
## 题目
10+
11+
Given an array consisting of `n` integers, find the contiguous subarray of given length `k` that has the maximum average value. And you need to output the maximum average value.
12+
13+
**Example 1:**
14+
15+
```
16+
Input: [1,12,-5,-6,50,3], k = 4
17+
Output: 12.75
18+
Explanation: Maximum average is (12-5-6+50)/4 = 51/4 = 12.75
19+
```
20+
21+
**Note:**
22+
23+
1. 1 <= `k` <= `n` <= 30,000.
24+
2. Elements of the given array will be in the range [-10,000, 10,000].
25+
26+
## 题目大意
27+
28+
给定 n 个整数,找出平均数最大且长度为 k 的连续子数组,并输出该最大平均数。
29+
30+
## 解题思路
31+
32+
- 简单题。循环一次,扫描数组过程中累加窗口大小为 k 的元素值。不断更新这个最大值。循环结束求出平均值即可。
33+
34+
## 代码
35+
36+
```go
37+
package leetcode
38+
39+
func findMaxAverage(nums []int, k int) float64 {
40+
sum := 0
41+
for _, v := range nums[:k] {
42+
sum += v
43+
}
44+
maxSum := sum
45+
for i := k; i < len(nums); i++ {
46+
sum = sum - nums[i-k] + nums[i]
47+
maxSum = max(maxSum, sum)
48+
}
49+
return float64(maxSum) / float64(k)
50+
}
51+
52+
func max(a, b int) int {
53+
if a > b {
54+
return a
55+
}
56+
return b
57+
}
58+
```

website/content/ChapterFour/0600~0699/0638.Shopping-Offers.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,5 @@ func dfsShoppingOffers(price []int, special [][]int, needs []int, pay int, res *
132132
----------------------------------------------
133133
<div style="display: flex;justify-content: space-between;align-items: center;">
134134
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0637.Average-of-Levels-in-Binary-Tree/">⬅️上一页</a></p>
135-
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0645.Set-Mismatch/">下一页➡️</a></p>
135+
<p><a href="https://books.halfrost.com/leetcode/ChapterFour/0600~0699/0643.Maximum-Average-Subarray-I/">下一页➡️</a></p>
136136
</div>

0 commit comments

Comments
 (0)