<?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 Miwesh Shehan K. on Medium]]></title>
        <description><![CDATA[Stories by Miwesh Shehan K. on Medium]]></description>
        <link>https://medium.com/@supremestrawhat?source=rss-498b7a28772b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*Tonm2AWPrtMxJjjsnXGshw.png</url>
            <title>Stories by Miwesh Shehan K. on Medium</title>
            <link>https://medium.com/@supremestrawhat?source=rss-498b7a28772b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 23:52:57 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@supremestrawhat/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[Escaping a prison of my own making]]></title>
            <link>https://medium.com/@supremestrawhat/escaping-a-prison-of-my-own-making-8bde83cf5c25?source=rss-498b7a28772b------2</link>
            <guid isPermaLink="false">https://medium.com/p/8bde83cf5c25</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[self-improvement]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Miwesh Shehan K.]]></dc:creator>
            <pubDate>Wed, 18 Mar 2026 11:37:30 GMT</pubDate>
            <atom:updated>2026-03-18T11:38:17.285Z</atom:updated>
            <content:encoded><![CDATA[<h4>I made a mistake</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*dQJXEVPEeVX_HWs0xRA3iw.png" /><figcaption>Painting in the background by Adolf Mosengel, Public domain, via Wikimedia Commons</figcaption></figure><p>A little background info for those who are not following: I am a computer science major who solves leetcode questions daily and primarily writes about the challenges I faced or things I learnt while attempting to solve the questions.</p><p>I haven’t been able to write anything the last few days since I was tied up by the mid terms.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/500/0*_ra1fvje2WapimM7.jpg" /></figure><p>There were two tests on two different days, one was on the 10th and the other was on 12th March. The first one was quite easy, easier than I had expected. I spent a lot of time going down rabbit holes learning a lot of interesting concepts for that one, maybe I’ll write some posts on the things I learnt from that time, maybe…</p><p>I didn’t study as much for the second one, but it turned out to be easier than the first one (<em>the first one was easy as well, so this was incredibly easy</em>).</p><p>The midterms broke my precious streak of leetcode + writing, and the void kept getting larger and larger as I procrastinated myself into a loop of thinking that I earned the rest after the gruesome study sessions I went through for the mid terms.</p><p>The worst part was the study sessions weren’t as hard as I thought at the moment. I studied more for the first one and then much less for the next one as I thought that I needed some rest after how much of effort was put to prep for the first test.</p><p>I think you see the pattern here. I took breaks under the impression that it would be healthy, a <em>reward</em> for working hard, but they quickly backfired as I found it difficult to break free from the rest, back into something productive.</p><p>So how did I break from this, did I become a better person by overcoming the struggle?</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*C7S-vDjf9eIvY4QcZW3UHg.png" /></figure><p>I caught a cold a few days ago and haven’t been able to do anything at all for the last two days. During that brief time I was thinking of how I broke my leetcode streak and haven’t done anything productive in a while. So I started fresh, with something new, I got into graphics programming and slowly started teaching myself drawing algorithms and opengl.</p><p>I would like to point out that graphics programming is not completely unrelated to what I do, I have a module called <em>graphics and visualization </em>as part of my coursework this semester. So it’s better for my grades if I get some additional knowledge on the subject, also I’m interested in the topic as it links into game development. It’s a non-zero-sum game.</p><p>Taking on this new task doesn’t mean that I would abandon leetcode, I will continue doing it, but will not be writing about it. I identified a long time ago that it takes less effort to solve a leetcode problem but much more to put the path to the solution into words. The writing part turned eventually turned into a bottleneck down the line. Therefore I will be switching the writing, from leetcode breakdowns to random stuff I learn in the field of computer science.</p><p>So follow if you are interested in that, I will be doing daily posts about interesting stuff that I learn.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=8bde83cf5c25" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[A small leap]]></title>
            <link>https://medium.com/@supremestrawhat/a-small-leap-d0d796529113?source=rss-498b7a28772b------2</link>
            <guid isPermaLink="false">https://medium.com/p/d0d796529113</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Miwesh Shehan K.]]></dc:creator>
            <pubDate>Sun, 08 Mar 2026 16:01:06 GMT</pubDate>
            <atom:updated>2026-03-08T16:01:06.290Z</atom:updated>
            <content:encoded><![CDATA[<h4>Solving minimum capacity box</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*S4OzBIHuce_3gRIjUGp4yg.png" /><figcaption>Picture on the background by J. M. W. Turner, Public domain, via Wikimedia Commons</figcaption></figure><p>I was near the end of the binary search section of <em>neetcode 150</em> when I realized that all the problems I solve don’t have high stakes as I know what data structure or algorithm I must use to solve it before I go into the problem (<em>maybe I should’ve done blind 75 instead</em>). Due to this reason I decided I am going to try to solve a question from the last <em>weekly leetcode contest</em>.</p><p>The problem I am going to tackle is <em>minimum capacity box (3861)</em>. It’s an easy problem but I think this is a good place to begin for now.</p><p><em>You are given an integer array </em><em>capacity, where </em><em>capacity[i] represents the capacity of the </em><em>ith box, and an integer </em><em>itemSize representing the size of an item.</em></p><p><em>The </em><em>ith box can store the item if </em><em>capacity[i] &gt;= itemSize.</em></p><p><em>Return an integer denoting the index of the box with the </em><strong><em>minimum</em></strong><em> capacity that can store the item. If multiple such boxes exist, return the </em><strong><em>smallest index</em></strong><em>.</em></p><p><em>If no box can store the item, return -1.</em></p><p><strong><em>Example 1:</em></strong></p><pre>Input: capacity = [1,5,3,7], itemSize = 3<br>Output: 2<br>Explanation:<br>The box at index 2 has a capacity of 3, which is the minimum capacity that can store the item. Thus, the answer is 2.</pre><p>I must do is look through the elements in the array given and find out which element has the minimum space that it could store the given item.</p><p>The first idea I got is to sort the array given, but then I would also have to track the position in some way which would be a hassle, so I decided that I would use a hashmap.</p><p>The key in the hashmap would be the value of the element and the value would be the index.</p><p>I could use a for loop to iterate through all the elements and if the current element is larger than the item size and the element is not in the hashmap, I take the minimum between the previous value and current element. Additionally putting it in the hashmap as well. Through this method I would be able to find the minimum capacity element.</p><p>Now to handle the edge case where the input array is empty, I would use a variable with a large number and after the loop if the value is the same then we can assume that there is no minimum capacity element.</p><p>Finally I’d return the element from the hashmap.</p><pre>class Solution:<br>    def minimumIndex(self, capacity: list[int], itemSize: int) -&gt; int:<br>        hashmap = {}<br>        <br>        val = 101   # largest capacity<br>        <br>        for i, num in enumerate(capacity):<br>            if num &gt;= itemSize and num not in hashmap:<br>                val = min(num, val)<br>                hashmap[num] = i<br>        <br>        if val == 101:<br>            return -1<br><br>        return hashmap[val]</pre><blockquote>Quick hint: I used 101 as the largest capacity as the constraints say that the capacity can only be <em>1 &lt;= capacity[i] &lt;= 100.</em></blockquote><p>Did you notice the problem with the solution I did here?</p><p>Yes!</p><p>I over-engineered a simple solution trying to push hashmaps into it. This solution doesn’t need a hashmap at all, I could just use a simple variable to store the minimum index, the fix is really simple as shown below.</p><pre>class Solution:<br>    def minimumIndex(self, capacity: list[int], itemSize: int) -&gt; int:        <br>        val = 101   # largest capacity<br>        ans = -1<br>        <br>        for i, num in enumerate(capacity):<br>            if num &gt;= itemSize and num &lt; val:<br>                val = num<br>                ans = i<br><br>        return ans</pre><p>Remember to always go for the most simple data structure or algorithm possible, the goal here is to solve the problem in the most efficient way possible. It’s not to showcase your intense knowledge on the material.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d0d796529113" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Perseverance]]></title>
            <link>https://medium.com/@supremestrawhat/perseverance-2234348ec70d?source=rss-498b7a28772b------2</link>
            <guid isPermaLink="false">https://medium.com/p/2234348ec70d</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Miwesh Shehan K.]]></dc:creator>
            <pubDate>Fri, 06 Mar 2026 18:31:03 GMT</pubDate>
            <atom:updated>2026-03-06T18:31:03.894Z</atom:updated>
            <content:encoded><![CDATA[<h4>Solving time based key-value store</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*i9ywrVcR-Vlnc0zBPQs5lg.png" /><figcaption>Photograph on the background by Adrian Pelletier (CC0)</figcaption></figure><p>One of the easiest way to become better at something would be to do it everyday. So here I am doing leetcode questions. I will be solving time based key-value store (981) now.</p><p><em>Design a time-based key-value data structure that can store multiple values for the same key at different time stamps and retrieve the key’s value at a certain timestamp.</em></p><p><em>Implement the </em><em>TimeMap class:</em></p><ul><li><em>TimeMap() Initializes the object of the data structure.</em></li><li><em>void set(String key, String value, int timestamp) Stores the key </em><em>key with the value </em><em>value at the given time </em><em>timestamp.</em></li><li><em>String get(String key, int timestamp) Returns a value such that </em><em>set was called previously, with </em><em>timestamp_prev &lt;= timestamp. If there are multiple such values, it returns the value associated with the largest </em><em>timestamp_prev. If there are no values, it returns </em><em>&quot;&quot;.</em></li></ul><p><strong><em>Example 1:</em></strong></p><pre>Input<br>[&quot;TimeMap&quot;, &quot;set&quot;, &quot;get&quot;, &quot;get&quot;, &quot;set&quot;, &quot;get&quot;, &quot;get&quot;]<br>[[], [&quot;foo&quot;, &quot;bar&quot;, 1], [&quot;foo&quot;, 1], [&quot;foo&quot;, 3], [&quot;foo&quot;, &quot;bar2&quot;, 4], [&quot;foo&quot;, 4], [&quot;foo&quot;, 5]]<br>Output<br>[null, null, &quot;bar&quot;, &quot;bar&quot;, null, &quot;bar2&quot;, &quot;bar2&quot;]</pre><p>I will be making a new data structure with a key and value, the value is not only one value, but a bunch of values. The key-value idea is makes this similar to a hashmap.</p><p>This is another very straight-forward problem, which is a rare sight in leetcode.</p><p>First, I have to implement a constructor, followed by two operations, get and set.</p><p>Looking at the set operation I can see that, it is composed of a key, value and a timestamp.</p><p>The get operation has the key and the timestamp.</p><p>I just realized after looking closely at the operations in the example, the timestamps are constantly increasing. (<em>It’s also mentioned in the constraints, however I did not see this at the time of solving this question</em>)</p><p>Therefore to implement the get operation I could simply use a hashmap with binary search. I could store the keys and values with a hashmap and then search for the particular element with the timestamp.</p><p>The code is as follows.</p><pre>class TimeMap:<br><br>    def __init__(self):<br>        self.hashmap = {}<br><br>    def set(self, key: str, value: str, timestamp: int) -&gt; None:<br>        if key not in self.hashmap:<br>            self.hashmap[key] = []<br>        self.hashmap[key].append([value, timestamp])<br><br>    def get(self, key: str, timestamp: int) -&gt; str:<br>        res = &quot;&quot;<br>        val = self.hashmap.get(key, [])<br><br>        left, right = 0, len(val) - 1<br>        <br>        while left &lt;= right:<br>            mid = (right + left) // 2<br>            if val[mid][1] &lt;= timestamp:<br>                res = val[mid][0]<br>                left = mid + 1<br>            else: <br>                right = mid - 1<br>        return res<br><br># Your TimeMap object will be instantiated and called as such:<br># obj = TimeMap()<br># obj.set(key,value,timestamp)<br># param_2 = obj.get(key,timestamp)</pre><p>Also I’ve thought of doing the <em>weekly leetcode contest</em> that’s coming up in a few days. I was scared of my ratings dropping if I entered a contest and failed to complete it, but just today I realized that the ratings don’t really matter at the moment, as I’m still learning, and this contest would really help me get accustomed to the environment of needing to solve a problem with limited time and no previous context. As long as I’m alive there are infinite chances, but that’s only if you’re willing to take the risk. It’s a leap of faith.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2234348ec70d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Great responsibility]]></title>
            <link>https://medium.com/@supremestrawhat/great-responsibility-acb63d10d8ea?source=rss-498b7a28772b------2</link>
            <guid isPermaLink="false">https://medium.com/p/acb63d10d8ea</guid>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Miwesh Shehan K.]]></dc:creator>
            <pubDate>Thu, 05 Mar 2026 18:33:24 GMT</pubDate>
            <atom:updated>2026-03-05T18:33:24.622Z</atom:updated>
            <content:encoded><![CDATA[<h4>Solving search in rotated sorted array</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*ZUGZnmuyG60zGiQPhU0rZQ.png" /></figure><p>I’m tired, I’ve been doing this for over forty days. Solving one problem daily, along with all the other work I must do in each day. I want to sleep, I want to rest. I sometimes wonder if I can let this one go, if I could have just this one day, to rest. But no, because one day was enough for Rome to fall, and the same applies to all the work I’ve put in so far.</p><p>I have definitely become better than I initially was, but maybe not as much as I was expecting. The growth is slow but with steady work I think I can surpass my expectations.</p><p>With that in mind I’ll start the problem, search in rotated sorted array (33).</p><p><em>There is an integer array </em><em>nums sorted in ascending order (with </em><strong><em>distinct</em></strong><em> values).</em></p><p><em>Prior to being passed to your function, </em><em>nums is </em><strong><em>possibly left rotated</em></strong><em> at an unknown index </em><em>k (</em><em>1 &lt;= k &lt; nums.length) such that the resulting array is </em><em>[nums[k], nums[k+1], ..., nums[n-1], nums[0], nums[1], ..., nums[k-1]] (</em><strong><em>0-indexed</em></strong><em>). For example, </em><em>[0,1,2,4,5,6,7] might be left rotated by </em><em>3 indices and become </em><em>[4,5,6,7,0,1,2].</em></p><p><em>Given the array </em><em>nums </em><strong><em>after</em></strong><em> the possible rotation and an integer </em><em>target, return the index of </em><em>target if it is in </em><em>nums, or </em><em>-1 if it is not in </em><em>nums.</em></p><p><em>You must write an algorithm with </em><em>O(log n) runtime complexity.</em></p><p><strong><em>Example 1:</em></strong></p><pre>Input: nums = [4,5,6,7,0,1,2], target = 0<br>Output: 4</pre><p>It’s similar to the question I did yesterday, find minimum In rotated sorted array (153). The rotation concept is the exact same, but what makes it different is that, there’s a target here instead of looking for the minimum. So how do I do that…</p><p>I’ll first think of an approach and then get into the implementation, only thinking about this through the lens of how to implement will shunt my creative veins, so I will be thinking of a way to get the output from the input, abstracting away the logic part for now.</p><p>I will have to look for the target now, the easiest way is to look at every single element and check for the target but that would be too slow, so we’ll try to think of another way.</p><p>Looking at the first example, we can see the rotation resulted in an array with two sorted portions, and the left portion will always be larger than the right portion.</p><p>We can use this information to our advantage, if the middle value is larger larger than the leftmost value in the array, we can deduce that we are in the left portion of the array.</p><p>So now, we are in the left sorted potion, we can search for the target, if the target is less than the middle value. Then we check if the target is less than the leftmost value and if yes, that would mean that the target is not in the left portion but in the right portion, Then we only search in the right portion.</p><p>If the target is in the right half, we do the same thing but for the right half.</p><p>Now for the code. We will be using binary search to search for the elements as it has a time complexity of O (log n).</p><pre>class Solution:<br>    def search(self, nums: List[int], target: int) -&gt; int:<br>        left = 0<br>        right = len(nums) - 1<br><br>        while left &lt;= right:<br>            mid = (right + left) // 2<br><br>            if target == nums[mid]:<br>                return mid<br>            <br>            if nums[left] &lt;= nums[mid]:<br>                if target &gt; nums[mid] or target &lt; nums[left]:<br>                    left = mid + 1<br>                else:<br>                    right = mid - 1<br>            else:<br>                if target &lt; nums[mid] or target &gt; nums[right]:<br>                    right = mid - 1<br>                else:<br>                    left = mid + 1<br>        <br>        return -1</pre><p>I cannot let it go, even if it seems to be the most difficult thing to do at first, I must persevere.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=acb63d10d8ea" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What goes around, comes around]]></title>
            <link>https://medium.com/@supremestrawhat/what-goes-around-comes-around-05f152c33901?source=rss-498b7a28772b------2</link>
            <guid isPermaLink="false">https://medium.com/p/05f152c33901</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Miwesh Shehan K.]]></dc:creator>
            <pubDate>Wed, 04 Mar 2026 16:53:59 GMT</pubDate>
            <atom:updated>2026-03-04T16:53:59.366Z</atom:updated>
            <content:encoded><![CDATA[<h4>Solving find minimum in rotated sorted array</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*VE6q9tp4GVIWLiezyboFTQ.png" /></figure><p>Some questions may seem difficult at first but they have solutions so simple that it wouldn’t even occur to you the first time you look at the problem.</p><p>I tried find minimum in rotated sorted array (153) today.</p><p><em>Suppose an array of length </em><em>n sorted in ascending order is </em><strong><em>rotated</em></strong><em> between </em><em>1 and </em><em>n times. For example, the array </em><em>nums = [0,1,2,4,5,6,7] might become:</em></p><ul><li><em>[4,5,6,7,0,1,2] if it was rotated </em><em>4 times.</em></li><li><em>[0,1,2,4,5,6,7] if it was rotated </em><em>7 times.</em></li></ul><p><em>Notice that </em><strong><em>rotating</em></strong><em> an array </em><em>[a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array </em><em>[a[n-1], a[0], a[1], a[2], ..., a[n-2]].</em></p><p><em>Given the sorted rotated array </em><em>nums of </em><strong><em>unique</em></strong><em> elements, return the minimum element of this array.</em></p><p><em>You must write an algorithm that runs in </em><em>O(log n) time.</em></p><p><strong><em>Example 1:</em></strong></p><pre>Input: nums = [3,4,5,1,2]<br>Output: 1<br>Explanation: The original array was [1,2,3,4,5] rotated 3 times.</pre><p>So the easiest way to do is to go through every single element and check for the minimum among them. That takes O(n) time and they expect us to get it in O(log n), so naturally you know that the solution will be using binary search, because that would be the only other search algorithm that can find an element in O(log n) time.</p><p>So how do we implement binary search here? So as usual you define the left and right pointer, then what?</p><p>I look at the pattern they gave and it shows that once the sorted array is rotated, the first element is usually larger than the last one, so we can use the two pointers we make for binary search in this as well.</p><p>So the final solution is as follows.</p><pre>class Solution:<br>    def findMin(self, nums: List[int]) -&gt; int:<br>        ans = nums[0]<br>        left = 0<br>        right = len(nums) - 1<br><br>        while left &lt;= right:<br>            if nums[left] &lt; nums[right]:<br>                ans = min(ans, nums[left])<br>                break<br><br>            mid = (right + left) // 2<br>            ans = min(ans, nums[mid])<br><br>            if nums[mid] &gt;= nums[left]:<br>                left = mid + 1<br>            else:<br>                right = mid - 1<br>        <br>        return ans</pre><p>Note that I did a check in the case that the array is not rotated at all.</p><p>Binary search by itself is extremely simple but the way these problems make us use binary search is truly mesmerizing to see.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=05f152c33901" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Banana Privilege]]></title>
            <link>https://medium.com/@supremestrawhat/banana-privilege-4fcc31c8d8ff?source=rss-498b7a28772b------2</link>
            <guid isPermaLink="false">https://medium.com/p/4fcc31c8d8ff</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <dc:creator><![CDATA[Miwesh Shehan K.]]></dc:creator>
            <pubDate>Tue, 03 Mar 2026 18:05:31 GMT</pubDate>
            <atom:updated>2026-03-03T18:05:31.417Z</atom:updated>
            <content:encoded><![CDATA[<h4>Solving Koko eating bananas</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*nfCOmi3g-BimjKqN9jbMbg.png" /></figure><p>A monkey is getting rejected from it’s society and has to look for comfort in an inanimate object while Koko is looking for the minimum amount of bananas she could eat to finish off all the piles she has in front of her, this my friend is how the world operates. Everyone has their own problems and no matter how big you may think your problem may be, in reality someone else could be having a bigger problem somewhere.</p><p>So as you may have already guessed the question today is Koko eating bananas (875).</p><p><em>Koko loves to eat bananas. There are </em><em>n piles of bananas, the </em><em>ith pile has </em><em>piles[i] bananas. The guards have gone and will come back in </em><em>h hours.</em></p><p><em>Koko can decide her bananas-per-hour eating speed of </em><em>k. Each hour, she chooses some pile of bananas and eats </em><em>k bananas from that pile. If the pile has less than </em><em>k bananas, she eats all of them instead and will not eat any more bananas during this hour.</em></p><p><em>Koko likes to eat slowly but still wants to finish eating all the bananas before the guards return.</em></p><p><em>Return the minimum integer </em><em>k such that she can eat all the bananas within </em><em>h hours.</em></p><p><strong><em>Example 1:</em></strong></p><pre>Input: piles = [3,6,7,11], h = 8<br>Output: 4</pre><p>So what we have to do, is find out the eating speed of Koko in which she can eat all of the piles in the time given.</p><p>Note that the number of piles will always be smaller than the total hours given, so that we won’t have a case where Koko will have remaining piles even after eating at the correct minimum speed.</p><p>The easiest way to solve this would be to see what’s the maximum size of the piles, in the first example the largest pile has a size of 11. So, we an deduce that if k is equal to 11 Koko can eat the bananas in less time than the time given, but we need to find out the minimum time taken, so then we can look for values smaller than 11, so we can start from 1 and all the way to 11, looking for the minimum time taken. That is the solution for this question. However it’s not very efficient at the moment and will be moving in linear time so to optimize this we can use binary search to find the size of k it takes to eat the bananas in minimum time.</p><p>The solution for this is as follows.</p><pre>class Solution:<br>    def minEatingSpeed(self, piles: List[int], h: int) -&gt; int:<br>        left = 1<br>        right = max(piles)<br><br>        res = right<br><br>        while left &lt;= right:<br>            mid = (right + left) // 2<br>            hours = 0<br>            for p in piles:<br>                hours += math.ceil(p / mid)<br><br>            if hours &lt;= h:<br>                res = min(right, mid) <br>                right = mid - 1<br>            else:<br>                left = mid + 1<br>        <br>        return res</pre><p>We start the left at one because Koko can’t eat zero bananas.</p><p>The for loop inside the while will calculate the totals hours taken to eat the bananas with the current choice of k (the k here is the mid).</p><p>The time complexity of the searching part of this solution is now more efficient as it’s logarithmic with the help of binary search.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4fcc31c8d8ff" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[In a hurry]]></title>
            <link>https://medium.com/@supremestrawhat/in-a-hurry-83a02332f978?source=rss-498b7a28772b------2</link>
            <guid isPermaLink="false">https://medium.com/p/83a02332f978</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Miwesh Shehan K.]]></dc:creator>
            <pubDate>Sun, 01 Mar 2026 18:27:09 GMT</pubDate>
            <atom:updated>2026-03-01T18:27:09.753Z</atom:updated>
            <content:encoded><![CDATA[<h4>Solving search a 2D matrix</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*3Z21Ycpsd7Pd74E3STjmyw.png" /></figure><p>I’m in a bit of a hurry today since, I’ve got limited time to finish up with the first sprint of a project in college. I didn’t think that I would be able to do it, but I somehow did so here I am, writing this quick post.</p><p>The question today was from the binary search section of <em>neetcode 150</em>, called search a 2D matrix (74).</p><p><em>You are given an </em><em>m x n integer matrix </em><em>matrix with the following two properties:</em></p><ul><li><em>Each row is sorted in non-decreasing order.</em></li><li><em>The first integer of each row is greater than the last integer of the previous row.</em></li></ul><p><em>Given an integer </em><em>target, return </em><em>true if </em><em>target is in </em><em>matrix or </em><em>false otherwise.</em></p><p><em>You must write a solution in </em><em>O(log(m * n)) time complexity.</em></p><p><strong><em>Example 1:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/322/0*FqwOrgj8rteCdGFd.jpg" /></figure><pre>Input: matrix = [[1,3,5,7],[10,11,16,20],[23,30,34,60]], target = 3<br>Output: true</pre><p>So, look at the matrix, do you see the answer. It’s looking right at you, as a matter of fact it’s there in the question as well. I’d like to point your attention towards the second property they have given. It says that the first number in each row is greater than the last number in the previous row, so by this rule we can see than the matrix can be simplified down to become an array, then solving this would become extremely easy, it would be just to implement binary search.</p><p>The difficult part of this question is find out this fact, once you’ve done that, you just have to implement the solution.</p><p>The solution is as follows.</p><pre>class Solution:<br>    def searchMatrix(self, matrix: List[List[int]], target: int) -&gt; bool:<br>        m, n = len(matrix), len(matrix[0])<br>        left, right = 0, m * n - 1<br><br>        while left &lt;= right:<br>            mid = (left + right) // 2<br>            row = mid // n<br>            col = mid % n<br>            val = matrix[row][col]<br><br>            if val == target:<br>                return True<br>            elif val &lt; target:<br>                left = mid + 1<br>            else:<br>                right = mid - 1<br><br>        return False</pre><p>We still have the while loop from before (binary search), the difference lies in how we calculate the left, right, mid and some new variables that help us effectively traverse through the matrix.</p><p>With the row and col variable we can find the mid point in the matrix.</p><p>And that’s it. I was in a massive hurry, but could still somehow come up with the solution and write this post before midnight, I can’t believe that I did that. The human mind can do anything as long as you have a deadline dangling in-front of it.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=83a02332f978" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[The oldest trick in the book]]></title>
            <link>https://medium.com/@supremestrawhat/the-oldest-trick-in-the-book-81ddd205a2da?source=rss-498b7a28772b------2</link>
            <guid isPermaLink="false">https://medium.com/p/81ddd205a2da</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Miwesh Shehan K.]]></dc:creator>
            <pubDate>Sat, 28 Feb 2026 17:35:02 GMT</pubDate>
            <atom:updated>2026-02-28T17:35:02.083Z</atom:updated>
            <content:encoded><![CDATA[<h4>Solving binary search</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*FtfE0mn_PT_3efZGB18gDg.png" /></figure><p>Binary search is one of the very first algorithms I learnt in a formal sense. I was introduced to it in David Malan’s CS50. In it he gracefully uses a phone book to explain how binary search works and the dramatic nature of the explanation sort of etched it into my mind.</p><p>Today I will solve the first binary search problem in <em>neetcode 150</em>, binary search (704).</p><p><em>Given an array of integers </em><em>nums which is sorted in ascending order, and an integer </em><em>target, write a function to search </em><em>target in </em><em>nums. If </em><em>target exists, then return its index. Otherwise, return </em><em>-1.</em></p><p><em>You must write an algorithm with </em><em>O(log n) runtime complexity.</em></p><p><strong><em>Example 1:</em></strong></p><pre>Input: nums = [-1,0,3,5,9,12], target = 9<br>Output: 4<br>Explanation: 9 exists in nums and its index is 4</pre><p>This question is as expected, an easy problem and solving it should not take any time at all.</p><p>I used two pointers, one in the far left and the other at the end of the array, we would also need to calculate the mid point of the array. We can easily do this since we have both the left and right places.</p><p>Then we move the left and right pointers to the mid according to where the target might be found. If the target is larger that the mid we move the left pointer, one position further from the midpoint. Additionally if the target is lower then we move the right pointer to one less than the mid point.</p><p>This is what David Malan explains with his phone book. He would first open to the middle of the book and check if the target is in the left half or the right half, he then rips off the half in which the target will not be there and tosses it away, saying that it is no longer part of the problem.</p><p>We have done the exact same here with for this problem.</p><p>The final solution is as follows.</p><pre>class Solution:<br>    def search(self, nums: List[int], target: int) -&gt; int:<br>        left = 0<br>        right = len(nums) - 1<br><br>        while left &lt;= right:<br>            mid = left + (right - left) // 2<br>            print(mid)<br>            if target &gt; nums[mid]:<br>                left = mid + 1<br>            elif target &lt; nums[mid]:<br>                right = mid - 1<br>            elif target == nums[mid]:<br>                return mid<br><br>        return -1</pre><p>The way someone teaches a concept can either make you instantly forget all about it or in a special case make you remember every single detail about it. It entirely depends on ones ability of teach.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=81ddd205a2da" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[How are you going to turn this, into the dragon warrior?]]></title>
            <link>https://medium.com/@supremestrawhat/how-are-you-going-to-turn-this-into-the-dragon-warrior-7ef4dde4ecb9?source=rss-498b7a28772b------2</link>
            <guid isPermaLink="false">https://medium.com/p/7ef4dde4ecb9</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Miwesh Shehan K.]]></dc:creator>
            <pubDate>Fri, 27 Feb 2026 17:32:29 GMT</pubDate>
            <atom:updated>2026-02-27T17:32:29.021Z</atom:updated>
            <content:encoded><![CDATA[<h4>Solving largest rectangle in histogram</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*7dBu5fh70QmIHCV0tLVoAw.png" /></figure><p>I don’t know.</p><p>I don’t know.</p><p>There are times when you come across questions that are so easy that you will go, “<em>This is so easy, I’m definitely the best guy out there</em>” and then you see that one question that humbles you for good. The kind of question that you might see in an interview before realizing that the interviewer doesn’t want to hire you at all.</p><p>The kind of question that requires you to have your third eye open to come up with a solution in one hour</p><p>Kindly note that I am talking to the beginners here, not the hardened veterans that may be able to solve this with both eyes shut<em> </em>(they would use their third eyes to solve it).</p><p>The question I got today is of this type, largest rectangle in histogram (84).</p><p><em>Given an array of integers </em><em>heights representing the histogram&#39;s bar height where the width of each bar is </em><em>1, return the area of the largest rectangle in the histogram.</em></p><p><strong><em>Example 1:</em></strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/522/0*xqAHiSEf4FQcyCYM.jpg" /></figure><pre>Input: heights = [2,1,5,6,2,3]<br>Output: 10<br>Explanation: The above is a histogram where width of each bar is 1.<br>The largest rectangle is shown in the red area, which has an area = 10 units.</pre><p>We will use a stack to solve this one, in fact this is the last stack question in <em>neetcode 150</em>.</p><p>The stack would store two elements, the index and the height value. If you have read my previous posts, you would have seen a similar approach and this kind of stack is called a monotonic stack.</p><p>First we iterate through the array and we assume that the bar starts at the current index.</p><p>We would pop the element from the stack if the top of the stack is larger than the current height, which means that the bar cannot extend to the current position, so we find the area of that bar, and then if its larger than the current maximum area then we put it there.</p><p>Then we push the current bar and continue doing what we did before until all the elements are in the correct order. In that case we iterate through the stack and see if its areas are larger than the current maximum we obtained.</p><p>This solution has a time complexity of O(n) as we go through each element once and space complexity of O(n) because it makes a new stack.</p><p>The final solution is as follows.</p><pre>class Solution:<br>    def largestRectangleArea(self, heights: List[int]) -&gt; int:<br>        maxArea = 0<br>        stack = []<br><br>        for i, h in enumerate(heights):<br>            start = i<br>            while stack and stack[-1][1] &gt; h:<br>                index, height = stack.pop()<br>                maxArea = max(maxArea, height * (i - index))<br>                start = index<br>            stack.append((start, h))<br><br>        for i, h in stack:<br>            maxArea = max(maxArea, h * (len(heights) - i))<br><br>        return maxArea</pre><p>The only way to get better is to keep going at it even if you suck hard at first.</p><p>Remember, there is <em>no</em> secret ingredient. It’s just you.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7ef4dde4ecb9" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Pick up a pencil]]></title>
            <link>https://medium.com/@supremestrawhat/pick-up-a-pencil-e586be4e33f0?source=rss-498b7a28772b------2</link>
            <guid isPermaLink="false">https://medium.com/p/e586be4e33f0</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[software-engineering]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Miwesh Shehan K.]]></dc:creator>
            <pubDate>Thu, 26 Feb 2026 17:18:27 GMT</pubDate>
            <atom:updated>2026-02-26T17:18:27.196Z</atom:updated>
            <content:encoded><![CDATA[<h4>Solving car fleet</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/800/1*q97PevvkdqkxzfEGLs8QEg.png" /></figure><p>Sometimes it becomes easier to solve a problem once you visualize it, so drawing it out on a piece of paper would actually increase the chance of your getting to the answer than just think about it in your head.</p><p>Car fleet (853) is such a question that requires a little bit of visualization to reach the solution.</p><p><em>There are </em><em>n cars at given miles away from the starting mile 0, traveling to reach the mile </em><em>target.</em></p><p><em>You are given two integer arrays </em><em>position and </em><em>speed, both of length </em><em>n, where </em><em>position[i] is the starting mile of the </em><em>ith car and </em><em>speed[i] is the speed of the </em><em>ith car in miles per hour.</em></p><p><em>A car cannot pass another car, but it can catch up and then travel next to it at the speed of the slower car.</em></p><p><em>A </em><strong><em>car fleet</em></strong><em> is a single car or a group of cars driving next to each other. The speed of the car fleet is the </em><strong><em>minimum</em></strong><em> speed of any car in the fleet.</em></p><p><em>If a car catches up to a car fleet at the mile </em><em>target, it will still be considered as part of the car fleet.</em></p><p><em>Return the number of car fleets that will arrive at the destination.</em></p><p><strong><em>Example 1:</em></strong></p><p><strong>Input:</strong> target = 12, position = [10,8,0,5,3], speed = [2,4,1,1,3]</p><p><strong>Output:</strong> 3</p><pre>Explanation:<br>The cars starting at 10 (speed 2) and 8 (speed 4) become a fleet, meeting each other at 12. The fleet forms at target.<br>The car starting at 0 (speed 1) does not catch up to any other car, so it is a fleet by itself.<br>The cars starting at 5 (speed 1) and 3 (speed 3) become a fleet, meeting each other at 6. The fleet moves at speed 1 until it reaches target.</pre><p>Let’s draw a graph to visualize this example.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xwrxkvLeTVCpDgRIschZkQ.jpeg" /></figure><p>So with this we can see that every time a fleet is formed we have an <em>intersection</em> between the lines. Therefore if we calculate the time taken to get to the target and if one element’s time is less than the other, we can say that those will then be traveling as a fleet.</p><p>We can code this up quickly like shown below.</p><pre>class Solution:<br>    def carFleet(self, target: int, position: List[int], speed: List[int]) -&gt; int:<br>        combined = [[p, s] for p, s in zip(position, speed)]<br>        <br>        stack = []<br>        for p, s in sorted(combined, reverse=True):<br>            stack.append((target - p) / s)<br>            if len(stack) &gt;= 2 and stack[-1] &lt;= stack[-2]:<br>                stack.pop()<br><br>        return len(stack)</pre><p>The weird part where I assign both elements of the two arrays is called <em>list comprehension</em>. It is an efficient way to create a new list with the existing lists.</p><p>A stack is used as a counter for the number of fleets.</p><p>The complexity of the question disappeared once we had a clear view of what was going on with a visual diagram, therefore keep in mind to always draw out or write down your thoughts so that you can bring out some order through the chaos.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e586be4e33f0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>