<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by madness on Medium]]></title>
        <description><![CDATA[Stories by madness on Medium]]></description>
        <link>https://medium.com/@shops.flud?source=rss-d855a3acc178------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*nmvQcIcu-Vc6m-do</url>
            <title>Stories by madness on Medium</title>
            <link>https://medium.com/@shops.flud?source=rss-d855a3acc178------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 14:13:05 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shops.flud/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[I Solved LeetCode (But My Brain Still Hurts) ]]></title>
            <link>https://medium.com/@shops.flud/i-solved-leetcode-but-my-brain-still-hurts-1d6eb9ef252f?source=rss-d855a3acc178------2</link>
            <guid isPermaLink="false">https://medium.com/p/1d6eb9ef252f</guid>
            <category><![CDATA[software-development]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[algorithms]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[web-development]]></category>
            <dc:creator><![CDATA[madness]]></dc:creator>
            <pubDate>Sat, 27 Apr 2024 11:18:30 GMT</pubDate>
            <atom:updated>2024-04-27T11:18:30.748Z</atom:updated>
            <content:encoded><![CDATA[<p>Hey there, my dear fellow problem solvers! <br>Have you ever gotten stuck on a LeetCode task that seemed impossible at first and even a second and third time? 🫣 Don’t worry, we’ve all been there</p><p>Today, I want to share my journey with a challenge called “<a href="https://leetcode.com/problems/minimum-cost-to-make-all-characters-equal/">Minimum Cost to Make All Characters Equal.</a>” We’ll crack this code together, and I’m here to walk you through it. Enjoy!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*OKCekynLDBuc0knl5wy5YA.jpeg" /></figure><p><strong>The rules of the game: </strong>You are given a string <strong>s</strong> of length <strong>n</strong> on which you can apply two types of operations: Choose an index <strong>i</strong> and invert all characters from index <strong>0</strong> to index<strong> i</strong> (both inclusive), with a cost of <strong>i + 1</strong> Choose an index <strong>i</strong> and invert all characters from index <strong>i</strong> to index <strong>n — 1</strong> (both inclusive), with a cost of <strong>n — i</strong> Return the minimum cost to make all characters of the string equal. Invert a character means if its value is ‘0’ it becomes ‘1’ and vice-versa.</p><p>In other words, in one action we can reverse some substring from the beginning, or from the very end.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*dY2AyJG-wzTWkbBQnyrW-Q.png" /></figure><p>See you on the other side<br> <br> <strong>Let’s start the game!</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*UC7MtD4m39kpIDyXuw5ZGQ.jpeg" /></figure><ol><li>Let’s imagine our string as a bunch of tiles. For example, we have a string: “010101”</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*MPUwWseff3Ob0SIne6TlnQ.png" /></figure><p>We aim to make all tiles “000000” or “111111” by flipping them</p><p><strong>1. </strong>Let’s Test Our Flipping Muscles (Tile #1):</p><p>First tile — oh no, it’s different from its right neighbor! (first tile = “0”, the second = “1”). We gotta flip something. Flipping from the beginning costs 1 brain-burning flip (phew!). But flipping just from the second tile to the end would cost 5 flips (yikes!). Easy choice — let’s flip just the first tile and keep our brain muscles happy. Our “flip counter” goes up to 1!</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*PxU9WPWQO_3bTLFB7xk3fw.png" /></figure><p><strong>2 . Round Two: Tile Tango </strong>(Tile #2):</p><p>Okay, comrade coder, let’s move onto tile number two. Same story — it’s different from its right neighbor!(second tile = “1”, the third = “0”)Again, we gotta decide: flip everything from the beginning (which is 2 flips now, since we already flipped the first tile), or just flip from the third tile onwards? We decide to flip the left part = 2 flips<br>Our “flip counter” goes up to 1 (from the first tile) + 2 (for the second tile) = 3</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*DP6ZuXQO6x8SiD_pBDqdAA.png" /></figure><p>3. The Great Flip-Off (Tile #3):<br>Looks like it’s having a disagreement with its right neighbor again. In this case, the code doesn’t actually care which side we flip. It just picks one (doesn’t matter which) to keep things moving. Our “flip counter” goes up to 3 (from the previous value) + 3(the current flip cost) = 6</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*W8J9FttP_6_d9jTi_stykw.png" /></figure><p>4. The Flip Flop (Tile #4):</p><p>Guess what? It’s causing trouble again, we need to make flips. Flipping everything from the beginning costs more than flipping just from the end. So, we cleverly choose the easier flip (flip from the end). Our counter = 6(the previous counter value) + 2(current cost) = 8</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0mkwG-F1CYtKgbwwGXNHsA.png" /></figure><p>5. The Last Fight (Tile #5)<br>Since we’re already flipping something (gotta make those tiles uniform!), we add the previous total flips (which was 8 from the last tile) to the cost of flipping this tile (which is just 1 flip). So, our final “flip counter” goes up to 9</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u-YkSJ9FkJ28paaKWHLwNQ.png" /></figure><h3>The result is 9!</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*w7ysspbcgIXfyEDXLf8r3Q.png" /></figure><p>You can view the code below</p><pre>const minimumCost = (s: string): number =&gt; {<br>    let cost = 0;<br><br>    for (let i = 0; i &lt; s.length; i++) {<br>        if (s[i] !== s[i - 1]) {<br>            cost += Math.min(i, s.length - i)<br>        }<br>    }<br><br>    return cost<br>};</pre><blockquote>Time Complexity: 0(n)</blockquote><blockquote>Space Complexity: 0(1)</blockquote><p><strong>Whew! Made it to the end?</strong> <strong>Congratulations!</strong> You’ve just completed the LeetCode Brain Power Gameshow! If your brain doesn’t feel like it just ran a marathon, you clearly weren’t paying attention 🫵</p><p>Now go forth, dear code struggler!</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=1d6eb9ef252f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>