Skip to content

Commit 1a0b43d

Browse files
committed
sliding window medium done
1 parent d9b4234 commit 1a0b43d

File tree

1 file changed

+287
-16
lines changed

1 file changed

+287
-16
lines changed

12. Sliding Window/2. LeetCode Medium.ipynb

+287-16
Original file line numberDiff line numberDiff line change
@@ -290,64 +290,335 @@
290290
"cell_type": "markdown",
291291
"metadata": {},
292292
"source": [
293-
"#### 6. "
293+
"#### 6. [1343]. Number of Sub-arrays of Size K and Average Greater than or Equal to Threshold"
294294
]
295295
},
296296
{
297297
"cell_type": "code",
298-
"execution_count": null,
298+
"execution_count": 7,
299299
"metadata": {},
300300
"outputs": [],
301-
"source": []
301+
"source": [
302+
"class Solution:\n",
303+
" def numOfSubarrays(self, arr: List[int], k: int, threshold: int) -> int:\n",
304+
" res = sum_ = 0\n",
305+
" for j in range(len(arr)):\n",
306+
" if j<k:\n",
307+
" sum_+=arr[j]\n",
308+
" if j==k-1 and sum_/k >= threshold: res+=1\n",
309+
" else:\n",
310+
" sum_ = sum_ - arr[j-k] + arr[j]\n",
311+
" if sum_/k >= threshold: res+=1\n",
312+
" return res"
313+
]
302314
},
303315
{
304316
"cell_type": "code",
305-
"execution_count": null,
317+
"execution_count": 10,
318+
"metadata": {},
319+
"outputs": [
320+
{
321+
"data": {
322+
"text/plain": [
323+
"6"
324+
]
325+
},
326+
"execution_count": 10,
327+
"metadata": {},
328+
"output_type": "execute_result"
329+
}
330+
],
331+
"source": [
332+
"arr = [2,2,2,2,5,5,5,8]; k = 3; threshold = 4\n",
333+
"arr = [11,13,17,23,29,31,7,5,2,3]; k = 3; threshold = 5\n",
334+
"Solution().numOfSubarrays(arr, k, threshold)"
335+
]
336+
},
337+
{
338+
"cell_type": "markdown",
339+
"metadata": {},
340+
"source": [
341+
"#### 7. [1004]. Max Consecutive Ones III"
342+
]
343+
},
344+
{
345+
"cell_type": "code",
346+
"execution_count": 20,
306347
"metadata": {},
307348
"outputs": [],
308-
"source": []
349+
"source": [
350+
"class Solution:\n",
351+
" def longestOnes(self, nums: List[int], k: int) -> int:\n",
352+
" l = max_len = 0\n",
353+
" for r, val in enumerate(nums):\n",
354+
" k -= 1 - val\n",
355+
" if k<0:\n",
356+
" k+=1 - nums[l]\n",
357+
" l+=1\n",
358+
" else:\n",
359+
" max_len = max(max_len, r-l+1)\n",
360+
" return max_len"
361+
]
362+
},
363+
{
364+
"cell_type": "code",
365+
"execution_count": 21,
366+
"metadata": {},
367+
"outputs": [
368+
{
369+
"data": {
370+
"text/plain": [
371+
"10"
372+
]
373+
},
374+
"execution_count": 21,
375+
"metadata": {},
376+
"output_type": "execute_result"
377+
}
378+
],
379+
"source": [
380+
"nums = [1,1,1,0,0,0,1,1,1,1,0]; k = 2\n",
381+
"nums = [0,0,1,1,0,0,1,1,1,0,1,1,0,0,0,1,1,1,1]; k = 3\n",
382+
"Solution().longestOnes(nums, k)"
383+
]
309384
},
310385
{
311386
"cell_type": "markdown",
312387
"metadata": {},
313388
"source": [
314-
"#### 7. "
389+
"#### 8. [1493]. Longest Subarray of 1's After Deleting One Element"
315390
]
316391
},
317392
{
318393
"cell_type": "code",
319-
"execution_count": null,
394+
"execution_count": 22,
320395
"metadata": {},
321396
"outputs": [],
322-
"source": []
397+
"source": [
398+
"class Solution:\n",
399+
" def longestSubarray(self, nums: List[int]) -> int:\n",
400+
" l=max_len=0\n",
401+
" k = 1\n",
402+
" for r, val in enumerate(nums):\n",
403+
" k -= 1 - val\n",
404+
" if k<0:\n",
405+
" k+=1-nums[l]\n",
406+
" l+=1\n",
407+
" else:\n",
408+
" max_len = max(max_len, r-l+1)\n",
409+
" return max_len-1"
410+
]
323411
},
324412
{
325413
"cell_type": "code",
326-
"execution_count": null,
414+
"execution_count": 23,
415+
"metadata": {},
416+
"outputs": [
417+
{
418+
"data": {
419+
"text/plain": [
420+
"2"
421+
]
422+
},
423+
"execution_count": 23,
424+
"metadata": {},
425+
"output_type": "execute_result"
426+
}
427+
],
428+
"source": [
429+
"nums = [1,1,0,1]\n",
430+
"nums = [0,1,1,1,0,1,1,0,1]\n",
431+
"nums = [1,1,1]\n",
432+
"Solution().longestSubarray(nums)"
433+
]
434+
},
435+
{
436+
"cell_type": "markdown",
437+
"metadata": {},
438+
"source": [
439+
"#### 9. [1358]. Number of Substrings Containing All Three Characters"
440+
]
441+
},
442+
{
443+
"cell_type": "code",
444+
"execution_count": 44,
327445
"metadata": {},
328446
"outputs": [],
329-
"source": []
447+
"source": [
448+
"class Solution:\n",
449+
" def numberOfSubstrings(self, s):\n",
450+
" res = i = 0\n",
451+
" count = {c: 0 for c in 'abc'}\n",
452+
" for j in range(len(s)):\n",
453+
" count[s[j]] += 1\n",
454+
" while all(count.values()):\n",
455+
" count[s[i]] -= 1\n",
456+
" i += 1\n",
457+
" res += i\n",
458+
" return res"
459+
]
330460
},
331461
{
332462
"cell_type": "code",
333-
"execution_count": null,
463+
"execution_count": 45,
334464
"metadata": {},
335465
"outputs": [],
336-
"source": []
466+
"source": [
467+
"class Solution:\n",
468+
" def numberOfSubstrings(self, s: str) -> int:\n",
469+
" count = 0\n",
470+
" ind_a,ind_b,ind_c = -1,-1,-1\n",
471+
" \n",
472+
" for i,x in enumerate(s):\n",
473+
" if x == 'a':\n",
474+
" ind_a = i\n",
475+
" elif x == 'b':\n",
476+
" ind_b = i\n",
477+
" else:\n",
478+
" ind_c = i\n",
479+
" \n",
480+
" if i > 1:\n",
481+
" count += min(ind_a,ind_b,ind_c)+1\n",
482+
" return count"
483+
]
337484
},
338485
{
339486
"cell_type": "code",
340-
"execution_count": null,
487+
"execution_count": 46,
488+
"metadata": {},
489+
"outputs": [
490+
{
491+
"data": {
492+
"text/plain": [
493+
"3"
494+
]
495+
},
496+
"execution_count": 46,
497+
"metadata": {},
498+
"output_type": "execute_result"
499+
}
500+
],
501+
"source": [
502+
"s = \"abcabc\"\n",
503+
"s = \"aaacb\"\n",
504+
"Solution().numberOfSubstrings(s)"
505+
]
506+
},
507+
{
508+
"cell_type": "markdown",
509+
"metadata": {},
510+
"source": [
511+
"#### 10. [1248]. Count Number of Nice Subarrays"
512+
]
513+
},
514+
{
515+
"cell_type": "code",
516+
"execution_count": 98,
341517
"metadata": {},
342518
"outputs": [],
343-
"source": []
519+
"source": [
520+
"class Solution:\n",
521+
" def numberOfSubarrays(self, nums: List[int], k: int) -> int:\n",
522+
" i = count = nice_count = odd_count = 0\n",
523+
" for j in range(len(nums)):\n",
524+
" if nums[j] % 2:\n",
525+
" odd_count += 1\n",
526+
" if odd_count == k:\n",
527+
" count = 0\n",
528+
" while odd_count == k:\n",
529+
" if nums[i] % 2:\n",
530+
" odd_count -= 1\n",
531+
" i += 1\n",
532+
" count += 1\n",
533+
" nice_count += count\n",
534+
"\n",
535+
" return nice_count"
536+
]
344537
},
345538
{
346539
"cell_type": "code",
347-
"execution_count": null,
540+
"execution_count": 99,
541+
"metadata": {},
542+
"outputs": [
543+
{
544+
"data": {
545+
"text/plain": [
546+
"16"
547+
]
548+
},
549+
"execution_count": 99,
550+
"metadata": {},
551+
"output_type": "execute_result"
552+
}
553+
],
554+
"source": [
555+
"#nums = [1,1,2,1,1]; k = 3\n",
556+
"#nums = [2,4,6]; k = 1\n",
557+
"nums = [2,2,2,1,2,2,1,2,2,2]; k = 2\n",
558+
"Solution().numberOfSubarrays(nums,k)"
559+
]
560+
},
561+
{
562+
"cell_type": "markdown",
563+
"metadata": {},
564+
"source": [
565+
"#### 11. [1456]. Maximum Number of Vowels in a Substring of Given Length"
566+
]
567+
},
568+
{
569+
"cell_type": "code",
570+
"execution_count": 104,
348571
"metadata": {},
349572
"outputs": [],
350-
"source": []
573+
"source": [
574+
"class Solution:\n",
575+
" def maxVowels(self, s: str, k: int) -> int:\n",
576+
" i=res=0\n",
577+
" vowels='aeiou'\n",
578+
" vowel_count = 0\n",
579+
" for j, val in enumerate(s):\n",
580+
" if j<k:\n",
581+
" if val in vowels: \n",
582+
" vowel_count+=1\n",
583+
" else:\n",
584+
" if s[i] in vowels:\n",
585+
" vowel_count-=1\n",
586+
" if val in vowels:\n",
587+
" vowel_count+=1\n",
588+
" i+=1\n",
589+
" res = max(res, vowel_count)\n",
590+
" return res\n",
591+
" "
592+
]
593+
},
594+
{
595+
"cell_type": "code",
596+
"execution_count": 106,
597+
"metadata": {},
598+
"outputs": [
599+
{
600+
"data": {
601+
"text/plain": [
602+
"2"
603+
]
604+
},
605+
"execution_count": 106,
606+
"metadata": {},
607+
"output_type": "execute_result"
608+
}
609+
],
610+
"source": [
611+
"s = \"abciiidef\"; k = 3\n",
612+
"s = \"leetcode\"; k = 3\n",
613+
"Solution().maxVowels(s,k)"
614+
]
615+
},
616+
{
617+
"cell_type": "markdown",
618+
"metadata": {},
619+
"source": [
620+
"#### 12."
621+
]
351622
},
352623
{
353624
"cell_type": "code",

0 commit comments

Comments
 (0)