Upgrade to Pro — share decks privately, control downloads, hide ads and more …

swift warmup Maximum Average Subarray

Johnlin
September 05, 2017

swift warmup Maximum Average Subarray

Johnlin

September 05, 2017
Tweet

More Decks by Johnlin

Other Decks in Programming

Transcript

  1. Maximum Average Subarray I • 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. • ༗Ұݸ༗ n ݸ੔Ꮠత arrayɻፙग़Ұݸ௕౓ k త࿈᠃ subarrayɼଞ༗࠷େతฏۉᆴɼવޙ༌ग़࠷େతฏۉ ᆴɻ
  2. ൣྫ • ༌ೖ: [ 1 , 12 , -5 ,

    -6 , 50 , 3 ], k = 4 • [1 , 12 , -5 , -6] తฏۉੋ 0.5 
 [12 , -5 , -6 , 50] తฏۉੋ 12.75 
 [-5 , -6 , 50 , 3] తฏۉੋ 10.5 • ༌ग़: 12.75
  3. ԋࢉ๏ • ሣ [1,2,3,4,5], k=3 ိ㘸ɼ༗Լ໘Ṝࠣ፤๏ • ։࢝తҐஔ 0, ݁ଋతҐஔ

    0+2=2ɼಘ౸ [1,2,3]
 ฏۉᆴ = (1 + 2 + 3) / 3
 ։࢝తҐஔ 1, ݁ଋతҐஔ 1+2=3ɼಘ౸ [2,3,4]
 ฏۉᆴ = (2 + 3 + 4) / 3
 ։࢝తҐஔ 2, ݁ଋతҐஔ 2+2=4ɼಘ౸ [3,4,5]
 ฏۉᆴ = (3 + 4 + 5) / 3 • ࠷େฏۉᆴ = 4
  4. class Solution0 { func findMaxAverage(_ nums: [Int], _ k: Int)

    -> Double { var maxSum = Int.min for start in 0...nums.count-k { //ፙग़ॴ༗։࢝Ґஔ var sum = 0 for idx in start..<start+k { //ࢉग़ṜҰ૊త័ ࿨ sum += nums[idx] } maxSum = max(sum, maxSum) //ߋ৽࠷େత័࿨ } return Double(exactly: maxSum)! / Double(exactly: k)! //ࢉग़ฏۉᆴ } } ࣌ؒෳᯑ౓: O(NK)
 ۭؒෳᯑ౓: O(1)
  5. class Solution0 { func findMaxAverage(_ nums: [Int], _ k: Int)

    -> Double { var maxSum = Int.min for start in 0...nums.count-k { //ፙग़ॴ༗։࢝Ґஔ var sum = 0 for idx in start..<start+k { //ࢉग़ṜҰ૊త័ ࿨ sum += nums[idx] } maxSum = max(sum, maxSum) //ߋ৽࠷େత័࿨ } return Double(exactly: maxSum)! / Double(exactly: k)! //ࢉग़ฏۉᆴ } }
  6. [3811,1895,455,4223,3295,9189,4374,1588,2171,8722,6640,2389,8745,9344,7520,4817,1405,26,3092,6368,9365,5779,2469,7811,974,2462,2384,2259,6582,6485,9137,393,8381,9592,969,8028,5134,1695,9616,7305,417,2608,9694,5514,1953,3566,6684,9710,3592,9776,6078,9309,55 55,8548,7120,2881,1010,5856,1492,3945,2341,6982,690,7074,2926,8011,5102,8060,9706,1070,1717,6475,3679,1411,1990,1984,1329,8674,1694,4921,4802,4125,4230,6709,2673,7702,5942,35,3558,3787,332,2252,769,1023,5678,3695,9034,781,8108,5093,1851,9825,1568,1882,7589 ,9910,3866,8918,4936,1913,192,6090,6038,774,2799,8711,8477,5094,5098,8387,8881,1783,6991,6002,9158,2670,6049,4544,9803,4157,9637,1654,335,7558,9889,4276,3820,3755,3194,5109,5668,9738,1199,1706,513,351,6769,5342,1797,8220,3729,678,6355,721,3032,5513,9743,90 81,57,9546,9591,6047,7552,6278,9957,7441,554,3777,7549,3748,8886,9569,9839,6438,7628,352,3141,749,5694,4938,8969,5775,1968,1676,2848,5000,7189,2591,433,3599,8489,6376,9646,2394,2654,9603,9835,3208,9732,3736,3309,4971,3306,3148,7761,934,9852,902,1683,1898,5 840,7005,7673,4160,8681,6874,5512,2223,9465,5945,5822,4307,8674,1820,6701,1328,7775,2888,4537,7507,6625,4198,8830,9931,3698,6591,865,3550,7493,8900,5448,9685,5905,9473,197,4587,6347,5709,3162,2165,8007,8984,6472,6681,804,9525,4361,4931,2413,8898,2438,9038, 9448,7621,8969,3146,4212,6186,6696,8058,5087,8496,4095,7344,7970,4293,8283,4317,6354,1445,6482,4361,429,9306,7394,7585,8831,1756,2516,1245,7006,1307,283,6455,5280,5605,5953,9492,8143,2650,3902,3230,1146,4350,6927,5468,8643,5210,9786,1349,3008,2620,2063,343 7,1927,9457,7375,758,7565,6243,2003,4572,3902,8639,7379,9182,596,3332,5027,8739,2334,5281,8322,3481,9631,1601,5301,4626,6811,5087,5976,9819,7708,8039,9609,5987,3848,3336,6745,1414,5931,5101,5986,9834,92,3365,5368,7040,3049,6747,5779,5384,2029,453,5217,1660 ,2054,518,6287,5218,1958,8615,1389,9666,6654,998,2005,502,686,5102,1916,6618,203,4254,2804,6647,3971,8172,3687,7021,1272,5819,2405,3301,6272,7622,1313,4679,4492,7600,9897,6450,6215,1286,2468,9221,8637,4473,9724,5675,9576,7992,8645,6131,2247,1449,2779,2570, 5974,2818,9591,7246,8637,8348,547,1262,2322,1860,2293,6815,5813,2190,3265,8380,9828,5734,7602,4817,6559,3678,6845,6135,8022,5490,2267,6621,6940,1398,9192,2914,4216,5135,6512,2854,3484,7059,468,5806,5271,2761,8973,1084,1303,2239,5817,7483,4325,9771,2301,884 ,3449,9146,7020,7823,4636,5639,4445,7928,7037,9989,7194,1253,5124,3706,459,4960,765,7279,7119,2389,40,6092,9825,7695,4683,5642,5179,5360,5413,7480,6245,5214,2978,9617,9390,3966,1608,3835,1895,8645,176,9089,6250,1652,2796,3062,6613,9913,341,3732,2302,6734,6

    176,8480,4429,7212,4122,9608,2572,5888,3440,8817,7454,2770,4786,6844,6737,6394,7031,4984,1391,7207,425,3994,8860,9573,7056,1825,9487,3749,5557,8141,6835,8085,6621,7617,5297,7096,7225,7870,9336,7018,3039,6790,9788,4178,3635,2877,572,666,4213,8316,4226,4639, 2310,9438,4212,5718,1263,51,5819,6820,8193,2655,1257,1166,272,6555,4614,3849,4425,3950,7219,3816,7093,3360,7994,728,2589,4919,7746,6803,3235,8324,1442,1897,7762,5654,7615,9025,5706,3434,2197,251,2441,3455,7769,9065,10,8736,9267,787,9038,2838,955,6131,6198, 8950,3211,8788,3869,7310,5591,3456,5634,3385,5353,9749,9039,9320,8774,1097,9106,972,7700,1548,779,1822,6965,7141,558,2584,7928,5948,5423,5235,2080,7973,4185,1643,6761,4406,8953,8704,4214,940,2089,9567,689,7481,8887,5815,8578,7994,6787,2631,5894,3918,4453,9 211,1059,1363,1796,5339,7311,7219,575,5743,1544,1112,7387,8306,1871,2692,7010,6085,3632,5452,5653,4321,2933,892,137,7863,5238,3276,6846,1132,3547,7651,6696,4606,9014,8492,6298,2678,2063,3225,4773,3607,4337,8512,8265,6208,1205,1628,2294,1189,7080,4299,5511, 6365,5191,2000,580,6782,1628,7427,4266,1527,5078,962,6134,445,5806,8784,9475,7869,2009,4248,7829,6346,9113,2446,8907,318,4074,7553,1507,7506,1852,3370,3871,3395,1722,804,6529,9703,8231,796,1230,3309,1758,3716,106,7565,2500,9581,1786,4509,182,5967,7208,9295 ,8414,6115,9613,8840,20,7472,6347,8224,7195,6570,7971,8917,7374,4501,8620,1957,5297,6203,5267,7055,9919,5373,972,2420,1307,9111,3281,1489,5078,489,784,3492,2956,6749,8685,2976,4221,5032,1200,1416,7954,9172,6686,1681,3673,1658,3638,5322,7861,5257,2377,7781, 6983,9702,6553,8290,8813,9834,9779,3891,324,6915,3736,3280,3664,8773,6257,4237,3805,3809,2006,8111,2981,8692,9792,3006,350,9783,8328,4564,5040,7058,2345,2023,3112,5250,313,1925,5084,6444,2168,5408,3359,2256,5041,3375,1029,7650,7613,1186,1459,5971,5650,793, 4663,5442,3799,1365,5225,8480,2281,6618,1890,978,8641,5002,6228,5307,3279,1313,8103,1799,6721,7815,4056,8114,1190,1437,5764,5155,8976,7224,1126,4626,8017,2141,68,8168,9859,1646,3000,2140,8264,4890,9471,3257,6244,5699,4916,9523,7012,3020,1323,86,835,1731,82 00,2025,9520,3965,3533,8496,7541,1011,3122,1910,3153,9543,6430,9364,7541,9431,7856,2157,673,7327,5414,3270,3027,6683,2793,6391,9703,468,6477,538,8551,1030,8915,8072,4995,2448,6568,8888,3460,6043,798,2965,5586,7228,8681,9479,3011,6537,1636,37,217,3402,3307, 3244,85,6100,9635,6140,2921,2465,6678,1472,3495,1946,5896,4842,746,8817,3730,4206,4860,880,3523,6798,4460,2204,6277,7472,5094,4265,7509,5311,7667,816,4907,4105,3268,894,245,2541,3359,3276,366,3206,5222,6262,8048,5968,5079,8130,6527,6291,9010,6402,3089,3471 ,4959,5718,7295,53,9983,4804,5364,7651,1972,6623,8108,1592,7517,8353,486,877,1629,852,4083,3203,3466,8484,9172,8546,2966,2051,1189,1977,8453,4279,1800,3412,9997,9095,3465,6333,251,5181,3984,8575,1804,8444,6519,9322,6797,7005,6551,4779,7857,6986,7982,7676,1 822,7154,6222,4789,5557,3763,3118,4011,8042,4918,7423,4392,365,7241,725,616,8774,1061,5543,579,9505,2062,6253,2654,9068,9156,7433,3277,6142,1768,953,7965,5274,3527,9106,832,7291,2224,4843,1685,7142,8618,2429,3859,5859,9506,827,4634,6919,6370,1565,2776,8432 ,7818,5431,3852,6974,9216,3482,9468,984,4435,3785,6259,4315,2891,3443,7958,5115,8286,9643,8609,6904,8425,2468,2764,7931,3295,3750,1203,6017,5315,3979,802,9485,5762,4654,6459,4979,8136,2279,2315,8924,6065,4926,9591,8956,8369,7549,4072,3007,3544,2681,9912,19 69,1502,9028,6253,1149,9130,3808,7167,4445,7787,7969,3930,3550,2623,6741,4881,7112,9020,3548,2388,5085,4827,8331,4042,9548,5880,4466,2556,5776,3499,8820,7746,1353,4200,351,2503,3330,4159,9670,4127,8298,7639,4409,8200,6614,1150,3081,78,6522,2982,2466,7960,7 809,797,8354,7357,3029,2820,6265,8806,2671,5085,2904,4025,9285,9607,6528,8967,118,6198,3094,8416,189,7503,6617,6803,5005,6050,6882,1528,9032,5700,5840,3193,2850,4194,551,2231,3366,6816,1037,6037,8254,293,6414,7539,9900,2942,6507,18,5492,9601,8435,2033,3457 ,1404,8837,8462,7454,2071,6342,6487,7771,2182,6032,6973,6376,6583,9205,9742,9752,6594,2132,8006,6888,8546,1897,6788,7841,8404,6807,9685,4358,1594,1719,7815,2998,6908,2629,6804,8979,8972,9643,3102,1154,5676,6428,3883,8611,5633,3625,8363,8579,2109,2721,5467, 656,4619,2256,4849,9375,5415,886,85,3361,2605,4252,2711,5865,6882,9515,1196,5854,9159,4299,3360,4835,727,7243,9798,2712,7221,8162,1291,9330,7235,3111,6338,1854,5367,7539,7582,7134,8426,7667,6847,7383,1920,9558,3249,5154,5425,4445,7360,4584,5096,720,5771,21 75,4316,1922,4887,1537,84,2531,7219,7319,5642,3558,5526,7361,1097,9460,847,9523,7127,7694,3259,5399,3604,6508,553,9029,7305,7913,9966,2402,8634,2089,4577,9302,4011,5817,839,447,4700,8058,4119,342,7968,5997,4055,9066,5457,4902,4941,2584,8948,8200,4336,8904, 1060,4889,7933,8366,2803,4251,7120,7789,2693,8049,7091,6704,218,7930,3504,4918,2340,3975,1612,309,9972,5667,5727,5429,6921,668,4365,2221,5221,8701,1125,2633,3591,5411,7351,2746,9662,4471,535,8707,8873,7626,5412,9091,1908,5268,362,600,9243,1974,909,9215,399 4,2988,996,7267,9,1713,9489,5230,415,6966,7863,358,2377,1567,9456,8392,6038,9991,7099,1263,3969,8863,6707,2229,4131,7069,2829,9726,9043,91,8941,9389,9431,6289,6657,9440,8003,2498,1022,4770,9464,5238,1480,8194,6805,936,6586,9195,7279,3685,459,1248,8901,7166 ,9829,3032,4235,9010,9111,9630,5453,4404,5372,4885,694,2029,677,5049,879,1700,9819,6695,6938,1299,4889,3743,8587,1475,2938,5866,1513,9749,3466,414,6915,3295,9798,7502,8657,8909,3485,463,3314,8857,5348,360,7238,6025,5409,8117,4077,5228,1164,1015,2879,6054,4 758,1466,3881,4049,3684,1746,150,7150,2160,7066,6797,8311,920,5454,3572,757,5917,3238,9614,7617,3598,3204,9995,5359,1321,4072,6939,8838,1440,9818,4892,2550,7636,8773,6599,1320,520,6750,8470,9032,168,1619,3695,7440,7074,3620,8198,9343,6858,4164,3313,6809,73 69,3308,2168,5042,3732,9108,3880,5172,5278,8772,7723,9267,3898,674,587,770,3776,5410,6154,296,7029,6202,7737,455,9822,2287,6151,3032,6451,9464,9841,172,2772,8362,5215,6504,7470,9095,8029,9100,4220,5752,8367,4470,2778,5307,5240,6555,717,7746,3203,4098,3948, 7292,906,122,9579,7057,3155,2383,6521,2996,2555,9293,1358,4122,2149,5180,3218,178,633,3790,2282,9000,8260,5061,4307,9852,7968,1376,7598,1171,1827,7899,4816,2733,4373,4395,9790,7528,3130,6311,6877,5686,1956,4587,9808,457,9768,9378,6988,401,3168,9270,5753,77 80,683,6413,7632,5003,7789,1583,2527,9616,5834,7343,8701,207,8090,8491,4088,1221,1154,965,3259,9462,5552,9419,9920,1672,5150,6908,8425,4670,2530,4179,2451,3214,592,6435,4569,4733,8018,7096,702,204,4439,9403,6764,8882,4247,852,103,5401,1817,9714,1216,3721,9 133,1136,1746,4283,4396,171,8954,6926,702,7757,6492,7646,4192,7414,8732,8563,4510,9434,8767,5302,8837,1883,4184,3084,2735,639,4838,904,353,6054,4626,5838,3542,6372,122,4290,2895,5428,7568,3598,3185,413,7596,3729,7827,6328,2292,2337,5762,7412,3991,952,9295, 4527,388,2031,5166,5226,2935,5519,7632,3913,1358,7526,6637,7832,1816,9533,9612,9385,9483,9149,9798,3431,2878,3977,9760,5171,2666,1874,2583,6658,9178,1878,1185,9567,261,6352,1145,9549,8223,8778,3462,5933,6304,100,117,4473,5985,9729,210,5468,8878,6360,5251,1 757,337,5011,3280,3003,3238,5863,9661,2416,4093,7199,1983,4355,9903,3129,256,8126,8259,3718,412,915,170,529,5388,2507,259,1950,4327,5489,8310,9579,7246,8647,942,6878,8003,4180,2741,7664,6597,3187,1215,4932,7542,1118,8061,4150,5597,2672,4220,6009,3588,4391, 6538,5328,3250,3149,7279,7578,8639,5589,3509,2237,589,4451,9116,8592,8632,8209,2608,1581,1396,3824,6513,5290,1294,927,9440,6891,3599,3661,2900,3539,4404,5791,8868,4006,5292,2499,1584,283,4440,5093,8873,5029,5897,4341,9973,4529,2550,8934,6110,299,9110,2623, 1941,404,9902,1382,7296,3502,1395,6548,7041,2151,8691,2261,6157,3984,4760,4094,4267,5553,5539,3140,582,1436,3833,6908,5965,6384,5842,8427,3035,4952,7403,4976,1708,7305,2710,5356,7159,4105,1905,4201,2608,6948,2814,8766,932,7575,2860,1552,9480,8399,1044,6414 ,9836,4878,3322,2153,7614,5516,6933,649,6820,688,1977,8529,7993,4688,3885,5153,5145,2142,5706,7754,9091,8520,2872,23,2447,5732,7927,1927,483,8972,8342,6671,202,8016,8825,7816,3533,2110,4817,6705,2798,6794,5234,7143,1482,5472,8648,2980,7614,4354,734,6705,92 27,9958,3081,8026,5690,7360,6306,2525,6332,1000,9197,6534,5368,4374,702,8901,6484,5519,1959,5634,2314,7193,2777,148,9017,1426,3128,6632,2132,214,9689,1359,172,9122,5738,2214,6483,8396,4740,9167,9396,3937,2054,4764,4663,2756,18,1147,4628,1977,3133,6942,5522 ,5910,3442,4540,7336,2923,7524,9469,9489,7213,7180,6014,2688,9270,8228,9171,7666,9320,4690,7062,9609,3096,8179,4272,2205,8197,1771,6833,6526,4904,127,2048,7167,3569,2940,855,2844,464,324,2334,7678,3857,4700,6718,3127,9280,2241,7146,8601,6931,560,4562,6380, 8739,5187,8585,3288,3310,5418,9814,4567,1897,8215,1734,1818,7507,2589,4663,7972,9266,3349,2002,9475,8049,8720,8954,7329,961,6100,2282,4244,6661,3197,624,1752,8384,9209,5041,1694,979,1207,6261,2876,5774,4347,4695,3282,6937,5710,1254,6203,5411,9608,5678,3460 ,8328,984,7141,5641,7085,5776,9885,3746,8973,6862,5498,3709,6071,6891,1755,3403,8099,4369,2631,225,8716,3678,3507,5653,9388,1113,8208,4799,721,238,4611,5401,1223,8105,1042,4660,3881,7280,4758,9206,4142,256,9267,6565,3500,1022,9968,7951,5391,8952,8176,4108, 2630,1684,6113,2019,2797,674,3170,3519,7264,7782,8920,4839,5887,6315,9499,6120,3595,4257,1678,7737,866,7297,654,4366,8319,6975,2317,63,5927,6845,523,8557,8529,2988,576,7679,14,3747,7550,7279,7881,2822,2118,120,9137,7970,2592,9084,2227,622,6821,9445,7919,38 28,3811,2590,803,2480,2653,6730,9326,9528,5287,4207,2517,2216,1886,2531,2315,9436,9810,196,2259,8281,6668,7748,6251,9260,6833,4830,9882,6,4276,7801,3834,4439,391,4637,3272,9397,1367,8950,8925,3007,3157,7794,5223,1396,326,7538,7184,6488,4086,5795,1121,754,3 544,3724,14,377,8555,6248,383,9183,4049,570,9974,792,5207,3246,6541,2927,8548,5467,5934,8058,3261,7509,9454,9939,1399,6638,2780,5485,2434,3901,2591,2330,3978,2605,2707,2533,5205,9442,8068,5606,12,8042,6398,1572,7641,2940,4499,6189,4759,6785,599,4372,4294,5 3,664,5693,3044,3444,7530,1830,3697,121,4160,7675,9078,3219,6560,4283,2661,4628,9889,9026,9023,2639,598,3016,1931,1449,5557,3042,8234,6157,7415,8880,6210,4431,925,5606,7875,8455,7436,7924,4928,7948,5600,4006,1167,2160,4641,181,3141,882,9207,8516,3521,6157, 1532,1805,7606,7089,4847,5840,3246,2262,4720,5809,6693,5645,1415,920,452,5204,8845,1732,3152,4445,2090,672,2957,6731,853,2450,3965,6412,966,7486,2569,2498,9291,175,5940,4139,2367,5538,2753,3439,1347,5799,5436,9115,6719,2240,671,1916,3972,3823,2713,6062,847 ,5671,9145,1700,4473,3110,4464,5440,596,3385,4290,6240,3560,230,6731,2279,5769,9484,5718,7116,1635,1154,2583,4707,3394,3254,6623,3718,3430,9337,9780,4277,1360,8925,2330,5833,8387,6794,7625,5336,180,1916,1576,92,2146,8307,8724,7915,4143,794,1384,2131,8301,3 19,6838,8047,3574,3461,1766,7004,9150,1546,7633,6862,6824,9963,9048,5211,3110,6673,547,9642,4941,8475,6086,7088,3134,4810,1355,3630,5605,9091,5761,3906,9411,2599,1953,9337,2412,71,6341,7915,7970,3974,1129,4794,290,177,6357,3400,3203,3257,3042,8144,1732,548 0,1584,1219,291,2940,4849,2248,2031,610,2506,7794,9561,811,7131,8325,883,9824,6240,8853,3799,7370,9999,441,3899,2708,3841,7102,5965,3235,5247,4050,8715,3183,1621,5358,6123,6470,7606,4507,3432,6464,8653,9345,7276,5785,7670,4511,5609,3911,3364,5760,7633,9715 ,6201,1532,8775,6394,8635,4741,9629,234,5143,8345,3417,3116,55,5893,9586,7662,400,9370,4126,9053,8715,7754,1190,6385,2265,3152,6648,1981,8912,4281,1696,1466,5814,472,7860,801,1565,3842,1035,3060,8539,804,6176,8594,6697,2114,6256,3449,7836,6735,8855,2903,44 89,45,9288,6755,3197,2289,8736,8462,6570,6785,9928,2384,3609,4140,3185,1526,7982,572,4586,6521,7729,7114,5116,778,5580,1372,4228,9768,4459,9435,2671,8949,9480,1959,2056,9030,600,7144,7492,7171,281,3772,5907,3890,7912,5445,5416,5895,2369,6354,2416,98,9820,7 532,877,5400,5257,1457,5168,6068,892,4191,5017,372,6151,3425,5754,6751,570,9598,274,851,3370,6182,1094,7635,1627,6510,9882,3996,9217,2298,447,9037,6183,1324,790,7792,2781,2310,3860,25,6502,5230,6749,9005,5007,2504,2108,1929,8454,2383,2781,1825,4917,227,581 2,6544,3089,5694,6892,2306,4344,3691,1344,6879,5015,8486,4671,4148,796,4884,4173,7298,6466,923,6303,7825,9779,8412,9755,8233,7147,8888,6410,2064,9115,8574,4960,2204,4268,8204,4511,8613,1896,2207,5492,3263,7045,6516,3764,7841,7752,4289,1492,570,1564,7795,83 95,1343,2559,8150,5929,6058,7038,2339,8122,2505,914,9434,4710,1534,7639,5573,6499,9535,4132,8344,9150,1177,1212,2914,5370,8964,7204,6862,5886,8768,1010,4281,6464,9921,8784,2393,5980,2174,1084,4102,4680,8350,9889,5742,6237,7528,7667,2736,3415,1799,1080,2565 ,9328,2292,1832,4698,7608,9036,7913,9846,4156,8923,480,6972,8844,9264,9365,4824,7790,6802,5279,2470,1504,1520,4564,7741,9048,2231,478,8815,382,7910,1380,6062,6555,9564,7113,515,4952,5026,362,9109,301,842,6081,9145,6458,1799,322,4248,4953,1953,3071,6457,347 3,7635,4199,8873,6219,1029,7688,6601,5291,5420,9016,1846,4985,6129,2362,9937,1155,9076,5398,1456,6270,1480,6953,2728,9631,3627,3328,4584,5580,6399,7393,5405,387,1592,4278,6606,8973,8318,9559,4265,3739,8575,2463,8724,1056,1177,8661,8563,253,4060,6371,2875,1 892,3325,5603,7875,3304,5284,8811,5237,8035,6204,642,8422,4149,1273,1380,3122,9591,940,7387,3330,5867,6203,8406,3276,7380,7068,1839,7634,7480,8211,509,5724,7888,2465,3599,1192,7749,2410,6429,5784,4966,3424,559,9115,4697,1939,2238,640,9231,5977,323,1451,218 0,8729,4727,9561,5797,6566,3547,9629,1129,408,5353,9017,9225,5304,210,3326,4066,2991,9111,9033,6415,6022,8148,7464,4313,6738,8105,3545,9068,8428,1348,1248,3509,6075,7161,9307,8993,7060,5288,123,7469,6994,9140,6694,2298,5702,21,2717,8694,5484,1750,1461,1506 ,6250,8926,5819,2989,3383,5716,2057,8163,7064,9657,1672,9491,6819,7331,8485,3879,2620,4960,7700,9614,452,747,8264,2507,768,981,7553,2604,9083,9014,462,5334,4292,2633,8323,7675,8350,6732,5838,1766,6389,3863,1258,9560,1194,6095,9792,166,7407,3844,6132,7859,4 591,4397,6718,1711,1730,4271,4315,814,3286,1129,6148,7578,3763,823,5254,8465,3907,7444,231,6648,7659,7841,2561,5206,288,2353,5372,4047,2549,1505,1907,7141,2254,8625,5204,336,2897,9520,1150,2535,7001,7298,113,764,4473,1719,9229,8380,5516,5813,5029,9527,6,75 90,4733,6647,6295,106,694,5196,7963,8953,2337,6569,7579,3894,6905,6828,3414,8056,5715,6767,1706,2180,7532,2532,3900,3113,7264,5768,8926,8645,5295,5285,6235,29,1932,8882,6487,2626,4079,4450,1580,2768,7371,5511,6662,4276,8691,6428,8684,4406,3196,6743,6586,70 80,5627,6838,193,2891,8958,5472,1537,4254,757,4124,635,2689,3007,7122,1667,3438,7924,9599,2558,5295,5110,9221,5923,153,2001,960,911,5197,4055,3850,8629,9682,688,8823,2573,5999,4295,4110,253,5052,4587,7240,4093,7594,4362,5760,7384,8638,1712,9942,285,6822,55 15,6208,3328,3869,3520,4239,5418,7575,8089,4048,3609,5130,2871,6183,1129,7166,6645,7734,8570,1232,4974,2663,5178,5688,4775,8914,4326,6487,5209,4611,9662,7076,7171,9342,945,7044,3581,6364,971,8023,412,4581,3153,3283,7116,634,6801,113,4720,5371,7698,9694,438 6,9228,1734,5513,8143,6060,8353,3352,7023,4367,428,546,3709,1374,7590,7290,4090,8562,1665,4502,9495,1170,4137,2963,1804,938,3076,6524,2661,774,2570,3399,3,4304,8912,4498,6716,3617,7850,91,7984,4630,638,1693,2356,4580,5336,6446,9494,7001,7300,8989,4524,1437 ,1952,6328,8727,5029,9205,7740,2155,8127,1139,2158,8784,6404,6656,1852,21,858,1944,4358,1841,2582,2403,549,7162,4091,6996,6657,7445,648,1998,1969,2086,3951,4649,7165,5332,206,4906,3839,8334,6045,2350,3470,8801,5358,5322,5175,6217,7266,9533,4410,6200,8288,4 959,3363,2380,8307,6372,9825,8956,4722,1794,7394,8673,2795,4559,357,3002,5817,549,1336,8215,2899,4806,7016,8257,128,2191,826,3747,8076,5236,9947,6365,6548,9662,8745,4855,6034,4922,3811,757,3068,7557,5782,5863,8469,2492,8865,4286,3041,201,2501,5940,1359,951 8,549,7840,8061,1376,1587,6138,2964,1534,2503,9512,1197,7600,720,3583,2522,883,692,5590,8441,6475,7805,6910,5319,6671,7548,8360,3224,50,652,4584,5920,1201,8776,3981,8929,363,6471,8246,8249,5326,7758,5798,2926,8478,9382,1800,9362,6426,7390,4155,2901,5196,74 17,4572,8219,4965,9284,1443,1367,9936,6027,7287,7490,1155,7621,2771,1518,444,1017,9768,5771,5128,1918,8697,9958,1300,498,5672,7727,4240,9827,6980,5788,7244,7905,4007,8562,7189,1803,9929,3478,7830,3569,7320,8986,7542,91,6856,7986,7461,2976,3757,2589,4895,88 07,2547,2547,5657,8220,6626,9897,8047,9959,2038,1644,7864,2397,206,1405,4200,6487,4883,8383,56,2203,3721,3950,8647,577,1937,6108,9906,2046,8697,4801,7205,7596,3700,2862,5816,327,9112,216,6638,1150,1860,4502,3547,8418,2259,7748,1257,7143,6131,7666,5698,6204 ,1616,4345,6781,9905,6805,6687,1952,1854,7840,9157,9451,7893,8372,5267,4572,7484,5483,1210,4986,3695,2064,8533,8465,4323,2633,9723,1466,5116,7389,3517,1320,5357,4214,4454,5263,1020,7493,7215,2874,1686,2724,2325,9579,1096,3945,4151,4932,5780,1713,9918,5828, 3777,4804,4293,8100,7437,368,5919,8906,4109,5788,226,9467,2,1032,4730,7374,8526,8297,249,212,7373,8926,6143,8470,9223,294,9754,5004,2007,9673,832,2136,4477,1477,236,8266,1846,2507,7172,5955,8295,3751,1774,8298,4783,6504,5672,9661,4801,2273,6225,2175,7552,2 368,6997,6775,9014,6751,8131,1021,2776,5315,3157,3605,6793,9746,8224,4991,8605,1748,946,6901,5499,2721,1551,6635,5577,3575,2648,379,2201,8874,8906,9753,1242,2255,2880,257,5358,7364,7630,8135,2679,7140,8092,9472,3238,6316,4463,1843,8065,1762,5096,9916,835,2 999,2903,6412,6575,1904,3143,5128,778,8401,4881,8372,656,4113,4981,6015,1477,8964,502,4157,2456,4946,9981,5694,1263,797,3889,5680,2559,8986,1948,9746,1985,1204,2510,4912,3108,5654,40,3886,407,1273,8610,1064,5387,9944,3431,3216,8908,3933,3725,1364,8879,3707 ,3410,6494,4504,7299,8526,3415,6285,475,3161,4623,1679,5671,9535,1139,7677,9576,5025,8085,849,9987,5501,2588,9931,8932,5805,5191,9217,9530,6555,4448,9589,9965,943,445,7265,9469,212,9902,6296,3373,4525,4327,5397,4061,5466,9426,3637,6843,7511,838,6831,9364,3 427,3114,8296,9232,8306,3865,5114,1213,8314,1056,7531,5609,1501,4796,5078,1714,1050,7727,1439,5576,8406,3188,5989,3873,2615,9626,716,6478,6816,3899,5843,243,3366,491,5827,8024,4357,7294,9237,2671,8350,6768,8280,6203,7916,9710,4269,8967,3789,2061,895,2196,1 601,6884,6069,4216,2862,3137,7047,9678,3389,9242,6274,3107,9733,2101,1131,442,5747,368,9465,449,3489,4097,3005,1405,160,7274,372,3949,5687,7619,2497,7289,4503,4918,7857,7365,4408,4904,3396,7797,4146,9670,7256,232,8123,8387,674,3871,8755,6492,4320,2244,589, 7325,2,749,952,6726,1051,2991,4346,3548,280,8849,8467,4490,2567,2875,5746,2315,7024,9893,1985,4280,6477,108,2667,3503,331,7774,9995,1004,6371,585,8329,6373,7686,5633,9451,8737,8625,3797,8638,5257,8999,3457,6099,1566,6332,1846,3881,9708,8091,2218,340,4568,8 678,9359,8071,9010,3485,8067,14,6208,5004,4695,2581,9042,6681,8385,7780,5306,2182,6418,6915,1181,9875,3015,9099,2559,1213,9332,8619,9304,1550,5311,3872,229,4670,8295,5591,8155,6362,5605,4364,7718,6652,3297,6761,3333,1682,893,4991,217,3663,1907,1398,9890,12 74,6850,8801,2487,6182,7420,1791,7733,2731,2015,7962,3753,310,9905,8260,3025,5510,8976,743,2162,8626,3856,1848,308,1101,6839,6877,4764,5098,4628,4654,2724,1478,9807,5211,7660,7227,3354,5393,6310,1721,9707,6415,2032,9612,4676,5057,1474,3652,2152,9989,2278,2 361,1837,8939,3462,5028,5816,8227,127,6796,9233,2851,8274,9041,4415,5935,2620,7769,7680,5283,5843,3740,1698,7875,9704,6374,9284,7531,6379,1436,7520,8657,149,5709,3948,3612,737,6117,8191,7216,2913,7424,68,7540,2817,4483,3475,1790,8604,7507,7073,4447,7599,87 71,8674,7304,5146,7958,4835,7877,5747,8707,2886,5896,768,6835,5860,1505,9304,4051,8722,2217,7828,5142,9757,6997,5977,9584,8787,4581,3444,5860,5381,1043,4632,407,8347,6130,8366,9534,4007,465,4593,6893,6361,5361,80,8574,6867,9384,2625,1941,1602,6805,3435,771 1,3803,9412,3648,2590,345,7092,4803,5726,8135,9435,6134,2835,1917,852,8721,2276,1317,3315,9169,4030,8676,5602,2604,1895,4986,1582,188,2940,8387,3623,7004,8542,3035,652,7485,3381,7744,2288,5459,2231,8075,7945,5066,9992,8797,140,8620,6466,3455,4141,497,8483, 9743,9453,379,4730,1035,567,4022,5775,4191,1026,4317,3578,1678,8154,6959,9422,442,8771,8006,4869,6716,3072,1213,5514,3212,9833,1980,3019,3975,8829,1503,3718,8283,8234,4800,5670,8801,8823,7797,9344,9849,2115,2923,1528,6621,6234,7302,7064,5005,5308,1933,1722 ௕౓ 10000
  7. class Solution { func findMaxAverage(_ nums: [Int], _ k: Int)

    -> Double { var sum = 0 for i in 0..<k { // ࢉલ k ݸᆴ֥⊺ބ sum += nums[i] } var maxSum = sum for i in 0..<nums.count-k { sum -= nums[i] //ݮᎃඌ೹ sum += nums[i+k] //Ճ্։಄ maxSum = max(maxSum, sum) } return Double(exactly: maxSum)! / Double(exactly: k)! } } ࣌ؒෳᯑ౓: O(N)
 ۭؒෳᯑ౓: O(1)
  8. Q&A