This video could not play here.
It may be members-only, region-restricted, or unavailable in embedded players.
Sum of subarray minimums: the contribution technique
To find the sum of the minimums of all subarrays, you could find each subarray's minimum and add them up. Or you can go element by element: count how many subarrays each element is the minimum of, and add that up. Both give the same answer. This is the contribution technique.
Take 1, 3, 5, 2, 6, 4, 1. How many subarrays have 2 as the minimum? Going left, 5 and 3 keep 2 as the minimum, but 1 doesn't, so that's a bound. The right side has one too. You find both bounds with previous smaller and next smaller element, a standard stack application. That gives 3 places to start and 3 places to end, so 9 subarrays have 2 as their minimum. That is 2's contribution to the answer.
Full stream: https://youtu.be/e1MR7-WTA3s
#leetcode #dsa #codinginterview #stack #competitiveprogramming
Leave a like, comment, and subscribe for more.
Check out AlgoZenith: https://maang.in/
-----------------------------------------------
😃 ABOUT ME
I’m Vivek, an engineer, Ex-Googler, ICPC World Finalist turned educator, and founder of AlgoZenith. I’ve taught Data Structures, Algorithms, and Competitive Programming to over 3000+ students and worked with 10+ engineering colleges to bridge the gap between college learning and real-world tech careers. Through AlgoZenith, I help students crack top product-based companies and become industry-ready. On this channel, you’ll find videos on coding strategies, roadmaps, interview prep, problem-solving techniques, and everything you need to level up your software career. If that’s your thing, do subscribe and hit the bell icon so you don’t miss a thing 🙂
-----------------------------------------------
📲 SOCIALS
Instagram: https://www.instagram.com/vivekgupta.ai/ Twitter: https://x.com/vg19988 LinkedIn: https://www.linkedin.com/in/acraider/ Codechef (5 ⭐️): https://www.codechef.com/users/acraider Codeforces (Master, max. 2269): https://codeforces.com/profile/acraider
-----------------------------------------------
🏷️ TAGS
-----------------------------------------------
#️⃣ HASHTAGS