<?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[The Leetcode Grind - Medium]]></title>
        <description><![CDATA[Join me in my journey to practice Leetcode Problems - Medium]]></description>
        <link>https://medium.com/the-leetcode-grind?source=rss----1daa19c719ea---4</link>
        <image>
            <url>https://cdn-images-1.medium.com/proxy/1*TGH72Nnw24QL3iV9IOm4VA.png</url>
            <title>The Leetcode Grind - Medium</title>
            <link>https://medium.com/the-leetcode-grind?source=rss----1daa19c719ea---4</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 03:39:00 GMT</lastBuildDate>
        <atom:link href="https://medium.com/feed/the-leetcode-grind" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Leetcode #33: Climbing Stairs]]></title>
            <link>https://medium.com/the-leetcode-grind/leetcode-33-climbing-stairs-d8deaf98c70f?source=rss----1daa19c719ea---4</link>
            <guid isPermaLink="false">https://medium.com/p/d8deaf98c70f</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[python]]></category>
            <category><![CDATA[math]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Shruti Mandaokar]]></dc:creator>
            <pubDate>Tue, 02 Jun 2026 01:22:13 GMT</pubDate>
            <atom:updated>2026-06-02T01:22:14.589Z</atom:updated>
            <content:encoded><![CDATA[<p>Imagine you are heading up the stairs to your office every morning.</p><p>Your building has <strong>n steps</strong> to reach your floor.</p><p>Now, you are a little quirky. You never climb one step at a time if you can help it, but you also never jump more than two steps at a time because — well, your knees are not what they used to be.</p><p>So every morning, you ask yourself:</p><blockquote>“How many different ways can I get to the top?”</blockquote><p>Some days you go <strong>1, 1, 1, 1…</strong> Some days you go <strong>2, 2, 2…</strong> Some days you mix it up: <strong>1, 2, 1, 2…</strong></p><p>That curiosity — counting all the distinct ways to reach the top — is exactly what this problem is about.</p><h3>Problem Statement</h3><p>You are climbing a staircase. It takes n steps to reach the top.</p><p>Each time you can either climb <strong>1</strong> or <strong>2</strong> steps.</p><p>In how many distinct ways can you climb to the top?</p><p><strong>Input:</strong></p><pre>n = 5</pre><p><strong>Output:</strong></p><pre>8</pre><h3>Understanding the Core Idea</h3><p>Let us think about it from the <strong>destination backwards</strong>.</p><p>If you are standing at the step n, You got there from either:</p><ul><li>Step n - 1 (by taking 1 step), or</li><li>Step n - 2 (by taking 2 steps)</li></ul><p>So the number of ways to reach the step n is:</p><pre>ways(n) = ways(n-1) + ways(n-2)</pre><p>Does this formula look familiar?</p><p>It is the <strong>Fibonacci sequence</strong>.</p><p>Let us verify with small values:</p><p>1 Step -&gt; 1 Way to Climb</p><p>2 Steps -&gt; 2 Ways to Climb ( 1+1 or 2)</p><p>3 Steps -&gt; 3 Ways to Climb ( 1+1+1 or 1+2 or 2+1)</p><p>4 Steps -&gt; 5 ways to Climb</p><p>(1 + 1 + 1 + 1 or 1 + 1 + 2 or 1 + 2 + 1 or 2 + 1 + 1 or 2 + 2)</p><p>5 Steps -&gt; 8 Ways to Climb</p><p>f(5) = f(4) + f(3)<br> = 5 + 3<br> = 8</p><h3>Why it become Fibonacci</h3><p>Notice the sequence:</p><pre>Steps: 1  2  3  4  5  6  7<br>Ways : 1  2  3  5  8 13 21</pre><p>Each number is the sum of the previous two:</p><pre>3 = 2 + 1<br>5 = 3 + 2<br>8 = 5 + 3<br>13 = 8 + 5</pre><p>So the climbing-stairs problem is essentially the Fibonacci sequence shifted by one position:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/774/1*IdfFfJ6vSSdrzQgjaCks1Q.png" /></figure><p>0, 1, 1, 2, 3, 5, 8, 13, ...</p><p>That’s why solutions to the Climbing Stairs problem often use Fibonacci-style dynamic programming.</p><h3>Brute Force Approach (Recursion)</h3><p>The most natural first instinct is to recurse.</p><p>At every step, branch into two choices: take 1 step or take 2 steps.</p><p>python</p><pre>class Solution:<br>    def climbStairs(self, n: int) -&gt; int:<br>        if n == 1:<br>            return 1<br>        if n == 2:<br>            return 2<br>        return self.climbStairs(n - 1) + self.climbStairs(n - 2)</pre><p><strong>The Problem:</strong></p><p>This recalculates the same subproblems repeatedly.</p><p>For example, climbStairs(5) calls climbStairs(4) and climbStairs(3). But climbStairs(4) also calls climbStairs(3).</p><p>We compute climbStairs(3) <strong>twice</strong> — and it gets worse the larger n gets.</p><p><strong>Time Complexity:</strong> O(2ⁿ) — exponential. Too slow.</p><h3>Optimal Approach: Dynamic Programming</h3><p>We can eliminate redundant computation by <strong>storing results we have already computed</strong>.</p><h3>Top-Down (Memoization)</h3><p>python</p><pre>class Solution:<br>    def climbStairs(self, n: int) -&gt; int:<br>        memo = {}<br>        <br>        def dp(i):<br>            if i &lt;= 2:<br>                return i<br>            if i in memo:<br>                return memo[i]<br>            memo[i] = dp(i - 1) + dp(i - 2)<br>            return memo[i]<br>        return dp(n)</pre><p>We store each result in a dictionary. If we have already calculated dp(i), we return it immediately without recomputing.</p><h3>Bottom-Up (Tabulation) — Most Efficient</h3><p>python</p><pre>class Solution:<br>    def climbStairs(self, n: int) -&gt; int:<br>        if n == 1:<br>            return 1<br><br>        dp = [0] * (n + 1)<br>        dp[1] = 1<br>        dp[2] = 2<br>        for i in range(3, n + 1):<br>            dp[i] = dp[i - 1] + dp[i - 2]<br>        return dp[n]</pre><p>We build the solution <strong>from the ground up</strong>, starting at the base cases and working our way to n.</p><h3>Space-Optimised Bottom-Up</h3><p>Notice that at any step, we only need the <strong>last two values</strong> — not the entire array.</p><p>python</p><pre>class Solution:<br>    def climbStairs(self, n: int) -&gt; int:<br>        if n == 1:<br>            return 1<br>        <br>        prev2 = 1  # ways to reach step 1<br>        prev1 = 2  # ways to reach step 2<br>        for i in range(3, n + 1):<br>            curr = prev1 + prev2<br>            prev2 = prev1<br>            prev1 = curr<br>        return prev1</pre><p><strong>Driver Code:</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0dXqV9zoePsNTCMD_vSSbA.png" /></figure><p>python</p><pre>sol = Solution()</pre><pre>print(sol.climbStairs(1))   # Output: 1<br>print(sol.climbStairs(2))   # Output: 2<br>print(sol.climbStairs(5))   # Output: 8<br>print(sol.climbStairs(10))  # Output: 89</pre><p><strong>Output:</strong></p><pre>1<br>2<br>8<br>89</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bt-1vofanftgsv1-CD7ESQ.png" /></figure><p>The space-optimised approach is the best: <strong>O(n) time and O(1) space</strong>.</p><h3>Key Takeaways</h3><ul><li>This problem is a disguised <strong>Fibonacci sequence</strong>.</li><li>The recurrence relation is: ways(n) = ways(n-1) + ways(n-2).</li><li>Brute force recursion is exponentially slow due to overlapping subproblems.</li><li>Dynamic programming eliminates redundancy by caching or building from the bottom.</li><li>The space-optimised solution needs only two variables at any point.</li><li>This is one of the most classic entry-level DP problems asked in interviews at companies like Google, Amazon, and Meta.</li></ul><p>Read my other solutions with thought process.</p><p>Thanks for reading.</p><p>— <em>Shruti Mandaokar</em></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d8deaf98c70f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-leetcode-grind/leetcode-33-climbing-stairs-d8deaf98c70f">Leetcode #33: Climbing Stairs</a> was originally published in <a href="https://medium.com/the-leetcode-grind">The Leetcode Grind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode #32: Minimum Element After Replacement with Digit Sum]]></title>
            <link>https://medium.com/the-leetcode-grind/leetcode-32-minimum-element-after-replacement-with-digit-sum-4d3a35e32888?source=rss----1daa19c719ea---4</link>
            <guid isPermaLink="false">https://medium.com/p/4d3a35e32888</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Shruti Mandaokar]]></dc:creator>
            <pubDate>Sun, 31 May 2026 18:23:08 GMT</pubDate>
            <atom:updated>2026-05-31T18:23:09.594Z</atom:updated>
            <content:encoded><![CDATA[<p>Imagine a teacher checking student roll numbers, but instead of looking at the full number, she only cares about the <strong>energy score</strong> of each roll number.</p><p>How does she calculate it?</p><p>She adds all the digits together.</p><p>For example:</p><p>Roll Number and Energy Score</p><p>101 + 0 = 1</p><p>121 + 2 = 3</p><p>131 + 3 = 4</p><p>141 + 4 = 5</p><p>Now, instead of comparing the original roll numbers,</p><p>she compares these smaller “energy scores” and picks the minimum.</p><p>That is exactly what this problem asks us to do.</p><p>We transform every number into the <strong>sum of its digits</strong> and then find the smallest transformed value.</p><h3>Problem Statement</h3><p>You are given an integer array nums.</p><p>Replace every element in the array with the <strong>sum of its digits</strong> and return the <strong>minimum element</strong> after all replacements.</p><h3>Input</h3><pre>nums = [10, 12, 13, 14]</pre><h4>Updated array with Sum of Elements:</h4><pre>[1, 3, 4, 5]</pre><h4>Output: Maximum of Updated Array</h4><pre>1</pre><h3>Understanding the Core Idea</h3><p>We need to:</p><ol><li>Traverse every number in the array.</li><li>Find the sum of its digits.</li><li>Replace the original number with the digit sum.</li><li>Return the minimum value from the updated array.</li></ol><h3>Brute Force Approach</h3><p>The brute-force idea is straightforward:</p><ul><li>Convert each number into digits.</li><li>Add all digits manually.</li><li>Store the result back.</li><li>Finally, return the minimum.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*WkX_pzP_X8fdP4p6qi3VNg.png" /></figure><h3>1. Modulus Operator (%)</h3><p>Used to get the last digit.</p><p>Example:</p><pre>12 % 10 = 2</pre><h3>2. Floor Division (//)</h3><p>Used to remove the last digit.</p><p>Example:</p><pre>12 // 10 = 1</pre><h3>Optimal Python Solution</h3><pre>from typing import List<br>tclass Solution:<br>    def minElement(self, nums: List[int]) -&gt; int:<br>        <br>        # Traverse every element<br>        for i in range(len(nums)):<br>            <br>            digit_sum = 0<br>            n = nums[i]<br>            <br>            # Calculate digit sum<br>            while n &gt; 0:<br>                digit_sum += n % 10<br>                n //= 10<br>            <br>            # Replace original element<br>            nums[i] = digit_sum<br>        <br>        # Return minimum element<br>        return min(nums)</pre><pre># Driver Code<br>nums = [10, 12, 13, 14]</pre><pre>sol = Solution()</pre><pre>print(sol.minElement(nums))</pre><h3>Output</h3><pre>1</pre><h3>Time Complexity Analysis</h3><p>Let:</p><ul><li>n = number of elements</li><li>d = number of digits in each number</li></ul><p>For every number, we process all digits once.</p><p>Operation and Complexity</p><p>Traversing array — O(n)</p><p>Digit processing — O(d)</p><p>TotalO(n × d)</p><h3>Space Complexity</h3><p>Complexity — Extra SpaceO(1)</p><p>We only use a few variables.</p><p>No extra array or data structure is used.</p><h3>Alternative Pythonic Solution</h3><pre>class Solution:<br>    def minElement(self, nums):<br>        return min(sum(int(d) for d in str(num)) for num in nums)</pre><h3>Key Takeaways</h3><ul><li>% 10 extracts the last digit.</li><li>// 10 removes the last digit.</li><li>The while loop processes digits one by one.</li><li>Overall complexity is efficient: O(n × d).</li><li>This is a classic digit manipulation problem commonly asked in coding interviews.</li></ul><p>Read my other solutions with thought process.</p><p>Thanks for reading.</p><ul><li>Shruti Mandaokar</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=4d3a35e32888" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-leetcode-grind/leetcode-32-minimum-element-after-replacement-with-digit-sum-4d3a35e32888">Leetcode #32: Minimum Element After Replacement with Digit Sum</a> was originally published in <a href="https://medium.com/the-leetcode-grind">The Leetcode Grind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode #31 : Move Zeros]]></title>
            <link>https://medium.com/the-leetcode-grind/leetcode-31-move-zeros-3fe255cd3e5f?source=rss----1daa19c719ea---4</link>
            <guid isPermaLink="false">https://medium.com/p/3fe255cd3e5f</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[work]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Shruti Mandaokar]]></dc:creator>
            <pubDate>Mon, 30 Jun 2025 00:56:47 GMT</pubDate>
            <atom:updated>2025-06-30T00:56:47.790Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leetcode #31 : Move Zeros</h3><p>Imagine you’re cleaning up a spreadsheet where some rows are empty (0), and you want to shift them to the bottom while keeping all the useful data (non-zero entries) in the same order.</p><p>That’s basically what the <strong>“Move Zeroes”</strong> problem is all about.</p><h3>🧩 Problem Statement</h3><blockquote><em>Given an array of integers, move all the 0’s to the end while maintaining the </em><strong><em>relative order of the non-zero elements</em></strong><em>.</em></blockquote><blockquote><em>You </em><strong><em>must do this in-place</em></strong><em> (without using a copy of the array).</em></blockquote><h3>✏️ Examples</h3><pre>Input:  [0, 1, 0, 3, 12]<br>Output: [1, 3, 12, 0, 0]<br><br>Input:  [0]<br>Output: [0]</pre><h3>✅ Optimal Strategy: Two Pointers</h3><h3>🔄 Key Idea:</h3><p>Use two pointers:</p><ul><li>One (i) goes through every element</li><li>Another (last_non_zero) keeps track of where to put the next non-zero element</li></ul><p>We <strong>swap non-zero elements forward</strong> and naturally push zeros to the end.</p><pre>class Solution(object):<br>    def moveZeroes(self, nums):<br>        &quot;&quot;&quot;<br>        :type nums: List[int]<br>        :rtype: None. Modifies nums in-place.<br>        &quot;&quot;&quot;<br>        last_non_zero = 0<br><br>        for i in range(len(nums)):<br>            if nums[i] != 0:<br>                nums[last_non_zero], nums[i] = nums[i], nums[last_non_zero]<br>                last_non_zero += 1</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B-9_NezF43lY9OGfxer8zA.png" /></figure><h3>🧠 Why Is This Interview-Worthy?</h3><p>This problem tests:</p><ul><li>In-place array manipulation</li><li>Efficient use of two-pointer technique</li><li>Understanding of stable ordering (preserving original order)</li></ul><p>You’ll see it come up in companies like <strong>Google</strong>, <strong>Amazon</strong>, or <strong>Meta</strong>.</p><h3>🎓 Summary</h3><ul><li>Use two pointers to move non-zeroes forward</li><li>Push zeros to the end naturally</li><li>Do everything in a single pass without extra space</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3fe255cd3e5f" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-leetcode-grind/leetcode-31-move-zeros-3fe255cd3e5f">Leetcode #31 : Move Zeros</a> was originally published in <a href="https://medium.com/the-leetcode-grind">The Leetcode Grind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode #30 : Longest Common Prefix]]></title>
            <link>https://medium.com/the-leetcode-grind/leetcode-30-longest-common-prefix-3f1cd347f0b2?source=rss----1daa19c719ea---4</link>
            <guid isPermaLink="false">https://medium.com/p/3f1cd347f0b2</guid>
            <category><![CDATA[bootcamp]]></category>
            <category><![CDATA[work]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Shruti Mandaokar]]></dc:creator>
            <pubDate>Mon, 30 Jun 2025 00:56:28 GMT</pubDate>
            <atom:updated>2025-06-30T00:56:28.202Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leetcode #30 : Longest Common Prefix</h3><p>Have you ever typed a word in Google Search and watched it autocomplete as you typed? Behind the scenes, one of the things it might be doing is finding the <strong>longest common prefix</strong> between your input and some suggestions.</p><p>Today, we’ll solve a classic coding problem:</p><blockquote><strong><em>Find the longest common prefix string among a list of strings.</em></strong></blockquote><h3>📘 Problem Statement</h3><blockquote><em>Given an array of strings, return the </em><strong><em>longest common prefix</em></strong><em> (LCP) string.</em></blockquote><blockquote><em>If there is no common prefix, return an empty string “”.</em></blockquote><pre>Input:  [&quot;flower&quot;, &quot;flow&quot;, &quot;flight&quot;]<br>Output: &quot;fl&quot;</pre><h3>Another Example:</h3><pre>Input:  [&quot;dog&quot;, &quot;racecar&quot;, &quot;car&quot;]<br>Output: &quot;&quot;</pre><p>No common prefix in the second example, so we return an empty string.</p><h3>🧠 Intuition</h3><ol><li>The longest common prefix must be a <strong>prefix of every string</strong> in the list.</li><li>Start by <strong>assuming the first string is the prefix</strong>.</li><li>Compare this prefix with each subsequent string.</li><li><strong>Trim the prefix</strong> from the end until it matches the beginning of the current word.</li><li>If at any point, the prefix becomes empty → return “”.</li></ol><pre>class Solution(object):<br>    def longestCommonPrefix(self, strs):<br>        &quot;&quot;&quot;<br>        :type strs: List[str]<br>        :rtype: str<br>        &quot;&quot;&quot;<br>        if not strs:<br>            return &quot;&quot;<br><br>        # Assume the first string is the common prefix<br>        prefix = strs[0]<br><br>        # Loop through all other strings<br>        for s in strs[1:]:<br>            # While current word doesn&#39;t start with the prefix<br>            while not s.startswith(prefix):<br>                # Remove the last character from the prefix<br>                prefix = prefix[:-1]<br>                # If prefix becomes empty, return &quot;&quot;<br>                if not prefix:<br>                    return &quot;&quot;<br><br>        return prefix</pre><h3>🧪 Let’s Walk Through an Example</h3><pre>Input: [&quot;flower&quot;, &quot;flow&quot;, &quot;flight&quot;]</pre><ul><li>Start with prefix = “flower”</li><li>Compare with “flow” → doesn’t match → shorten to “flow”</li><li>Compare with “flight” → still doesn’t match → shorten to “flo”, then “fl”</li><li>“flight” starts with “fl” → done ✅</li></ul><h3>💡 Real-World Applications</h3><ul><li><strong>Autocomplete</strong> in search engines or code editors</li><li><strong>Finding common patterns</strong> in DNA sequences or logs</li><li><strong>Sorting UI elements</strong> based on shared labels</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3f1cd347f0b2" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-leetcode-grind/leetcode-30-longest-common-prefix-3f1cd347f0b2">Leetcode #30 : Longest Common Prefix</a> was originally published in <a href="https://medium.com/the-leetcode-grind">The Leetcode Grind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode #29 : Roman to Integer]]></title>
            <link>https://medium.com/the-leetcode-grind/leetcode-29-roman-to-integer-96989176c0fc?source=rss----1daa19c719ea---4</link>
            <guid isPermaLink="false">https://medium.com/p/96989176c0fc</guid>
            <category><![CDATA[study]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[bootcamp]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Shruti Mandaokar]]></dc:creator>
            <pubDate>Sat, 28 Jun 2025 10:48:45 GMT</pubDate>
            <atom:updated>2025-06-28T10:48:45.555Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leetcode #29 : Roman to Integer</h3><p>If you’ve ever wondered how ancient Romans wrote numbers — and how to convert those numbers back into modern digits — you’re in the right place.</p><p>Today, we’re going to decode <strong>Roman numerals</strong> and convert them to regular integers using Python. This is a <strong>classic interview problem</strong> and a great way to sharpen your string manipulation and logic skills.</p><h3>🧠 Understanding Roman Numerals</h3><p>Roman numerals use <strong>seven symbols</strong>:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/820/1*3lk4BfZ4Pr7HXo6BhaSliA.png" /></figure><h3>❗ Special Subtraction Cases</h3><p>Sometimes, smaller values come <strong>before</strong> larger values. That means we subtract instead of add.</p><p>Here are the six special cases:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/756/1*rbl0aqECBb56gs6ehgDKQw.png" /></figure><pre>class Solution(object):<br>    def romanToInt(self, s):<br>        &quot;&quot;&quot;<br>        :type s: str<br>        :rtype: int<br>        &quot;&quot;&quot;<br>        roman = {<br>            &#39;I&#39;: 1,<br>            &#39;V&#39;: 5,<br>            &#39;X&#39;: 10,<br>            &#39;L&#39;: 50,<br>            &#39;C&#39;: 100,<br>            &#39;D&#39;: 500,<br>            &#39;M&#39;: 1000<br>        }<br><br>        total = 0<br>        prev = 0<br><br>        # Loop through the Roman numeral from right to left<br>        for char in reversed(s):<br>            value = roman[char]<br>            if value &lt; prev:<br>                total -= value  # Subtract if smaller than previous<br>            else:<br>                total += value  # Add otherwise<br>            prev = value  # Update previous value for next comparison<br><br>        return total</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1018/1*tVRg3JRympupvMxqNxJHcQ.png" /></figure><p>We only loop through the string once, and the dictionary is constant-sized. so time complexity is O(n) and space complexity is O(1)</p><h3>🧠 Takeaways</h3><ul><li>Think <strong>backwards</strong> when subtraction is involved.</li><li>Compare each character to the one <strong>after it</strong> to know whether to subtract or add.</li></ul><p>This is a great problem to practice <strong>string traversal</strong>, <strong>conditional logic</strong>, and <strong>dictionary lookups</strong>.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96989176c0fc" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-leetcode-grind/leetcode-29-roman-to-integer-96989176c0fc">Leetcode #29 : Roman to Integer</a> was originally published in <a href="https://medium.com/the-leetcode-grind">The Leetcode Grind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode #28 : Summary Ranges]]></title>
            <link>https://medium.com/the-leetcode-grind/leetcode-28-summary-ranges-b16df5544eaf?source=rss----1daa19c719ea---4</link>
            <guid isPermaLink="false">https://medium.com/p/b16df5544eaf</guid>
            <category><![CDATA[python]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[work]]></category>
            <dc:creator><![CDATA[Shruti Mandaokar]]></dc:creator>
            <pubDate>Sat, 28 Jun 2025 10:48:20 GMT</pubDate>
            <atom:updated>2025-06-28T10:48:20.214Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leetcode #28 : Summary Ranges</h3><p>Have you ever looked at a list of numbers and thought — “That’s a lot of typing for something that could just be a range!”?</p><p>That’s exactly what the <strong>Summary Ranges</strong> problem is all about. Given a sorted list of unique integers, you’re asked to summarize consecutive sequences in a <strong>compact range format</strong>.</p><p>Let’s break it down and solve it step-by-step.</p><h3>🧠 Problem Statement</h3><blockquote><em>You are given a </em><strong><em>sorted</em></strong><em> unique integer array nums.</em></blockquote><blockquote><em>Return the </em><strong><em>smallest sorted list of ranges</em></strong><em> that cover all the numbers in the array exactly.</em></blockquote><p>Each range should be:</p><ul><li>“a-&gt;b” if a ≠ b</li><li>“a” if a == b (a single number)</li></ul><pre>Input:  nums = [0, 1, 2, 4, 5, 7]<br>Output: [&quot;0-&gt;2&quot;, &quot;4-&gt;5&quot;, &quot;7&quot;]</pre><ul><li>0, 1, 2 → becomes “0-&gt;2”</li><li>4, 5 → becomes “4-&gt;5”</li><li>7 → stands alone as “7”</li></ul><h3>🚶‍♀️ Step-by-Step Plan</h3><p>Here’s how we’ll solve this:</p><ol><li>Loop through the list of numbers.</li><li>Track the start of each range.</li><li>Whenever we detect a <strong>gap</strong> (i.e., numbers are no longer consecutive), we close the current range.</li><li>At the end, we also add the <strong>final range</strong>.</li><li>Format each range:</li></ol><ul><li>“a” for single elements</li><li>“a-&gt;b” for consecutive ranges</li></ul><pre>class Solution(object):<br>    def summaryRanges(self, nums):<br>        &quot;&quot;&quot;<br>        :type nums: List[int]<br>        :rtype: List[str]<br>        &quot;&quot;&quot;<br>        result = []<br>        if not nums:<br>            return result<br><br>        start = nums[0]  # Start of current range<br><br>        for i in range(1, len(nums)):<br>            # If current number is NOT consecutive<br>            if nums[i] != nums[i - 1] + 1:<br>                end = nums[i - 1]<br>                # Format the range depending on whether start == end<br>                if start == end:<br>                    result.append(str(start))<br>                else:<br>                    result.append(str(start) + &#39;-&gt;&#39; + str(end))<br>                start = nums[i]  # Start a new range<br><br>        # Add the last range<br>        end = nums[-1]<br>        if start == end:<br>            result.append(str(start))<br>        else:<br>            result.append(str(start) + &#39;-&gt;&#39; + str(end))<br><br>        return result</pre><p>Time and Space complexity both are O(n)</p><h3>1. result = []</h3><p>We create an empty list to store our final output.</p><h3>2. if not nums: return result</h3><p>Always good to handle the edge case where the list is empty.</p><h3>3. start = nums[0]</h3><p>We begin tracking our range from the first number.</p><h3>4. Loop through numbers:</h3><pre>for i in range(1, len(nums)):</pre><h3>5. Detect a break in the sequence:</h3><pre>if nums[i] != nums[i - 1] + 1:</pre><p>When two numbers aren’t consecutive, we close the range.</p><h3>6. Format the range:</h3><ul><li>If start == end: it’s just one number → “a”</li><li>If not: we format it as “a-&gt;b”</li></ul><h3>7. Update the start:</h3><p>We begin a new range from the current number.</p><h3>8. Final touch:</h3><p>After the loop ends, we still need to close the <strong>last range</strong>.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eaVFCoQG8zN4QKQAo02cGg.png" /></figure><h3>🙌 Conclusion</h3><p>The Summary Ranges problem teaches us how to <strong>recognize patterns</strong>, <strong>group data</strong>, and format it in a useful way — something you’ll often do in real-world data tasks.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b16df5544eaf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-leetcode-grind/leetcode-28-summary-ranges-b16df5544eaf">Leetcode #28 : Summary Ranges</a> was originally published in <a href="https://medium.com/the-leetcode-grind">The Leetcode Grind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode #27 : Majority Element]]></title>
            <link>https://medium.com/the-leetcode-grind/leetcode-27-majority-element-f5d8a280f41e?source=rss----1daa19c719ea---4</link>
            <guid isPermaLink="false">https://medium.com/p/f5d8a280f41e</guid>
            <category><![CDATA[mathematics]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <dc:creator><![CDATA[Shruti Mandaokar]]></dc:creator>
            <pubDate>Sat, 28 Jun 2025 10:48:03 GMT</pubDate>
            <atom:updated>2025-06-28T10:48:03.278Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leetcode #27 : Majority Element</h3><p>Have you ever been in a situation where one choice was just <em>way more popular</em> than the others? That’s what this coding problem is all about.</p><p>Let’s say you’re given a list of votes, and you’re guaranteed that one person got <strong>more than half</strong> of the total votes. Your job? Figure out who.</p><p>Sounds simple — and it is — if you know what to look for.</p><h3>🔍 The Problem</h3><blockquote><strong><em>Given</em></strong><em> an array of integers nums, return the </em><strong><em>majority element</em></strong><em> — the one that appears </em><strong><em>more than ⌊n / 2⌋ times</em></strong><em>.</em></blockquote><h3>🧾 Example:</h3><pre>Input:  nums = [3, 2, 3]<br>Output: 3<br><br>Input:  nums = [2, 2, 1, 1, 1, 2, 2]<br>Output: 2</pre><p>One key thing: <strong>you’re guaranteed</strong> that a majority element exists.</p><h3>🧠 The Idea: Count and Compare</h3><p>If we can <strong>count how many times</strong> each number appears, we can easily find the one that shows up more than n / 2 times.</p><p>Let’s use a Python dictionary (dict) to help us count.</p><pre>class Solution(object):<br>    def majorityElement(self, nums):<br>        &quot;&quot;&quot;<br>        :type nums: List[int]<br>        :rtype: int<br>        &quot;&quot;&quot;<br>        count = {}  # Dictionary to store how many times each number appears<br>        n = len(nums)  # Length of the array<br><br>        for num in nums:<br>            if num in count:<br>                count[num] += 1  # If we&#39;ve seen it before, add 1 to its count<br>            else:<br>                count[num] = 1  # If it&#39;s the first time, set count to 1<br><br>            # Check if this number is now the majority<br>            if count[num] &gt; n // 2:<br>                return num  # Return as soon as we find the majority</pre><h3>🧠 Final Thought</h3><p>The Majority Element problem teaches an important lesson:</p><blockquote><em>Before optimizing, just try to </em><strong><em>understand the problem simply</em></strong><em>.</em></blockquote><blockquote><em>Most problems have a “count and compare” version that’s good enough.</em></blockquote><p>If you master this version, then you’ll feel confident moving on to more efficient solutions like Boyer-Moore or divide-and-conquer.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f5d8a280f41e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-leetcode-grind/leetcode-27-majority-element-f5d8a280f41e">Leetcode #27 : Majority Element</a> was originally published in <a href="https://medium.com/the-leetcode-grind">The Leetcode Grind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode #26 : Single Number]]></title>
            <link>https://medium.com/the-leetcode-grind/leetcode-26-single-number-50903c10d9cf?source=rss----1daa19c719ea---4</link>
            <guid isPermaLink="false">https://medium.com/p/50903c10d9cf</guid>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[leetcode]]></category>
            <dc:creator><![CDATA[Shruti Mandaokar]]></dc:creator>
            <pubDate>Fri, 27 Jun 2025 09:02:54 GMT</pubDate>
            <atom:updated>2025-06-27T09:02:54.437Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leetcode #26 : Single Number</h3><p>You’re given a list of integers where every number appears twice — except one.</p><p>Can you find the number that appears only once?</p><p>Sounds like a good job for a hash map or sorting, right?</p><p>But what if I told you this could be solved in <strong>O(n) time</strong> and <strong>O(1) space</strong> — using just a single line of math magic?</p><p>Let’s explore this brilliant little trick.</p><h3>🧩 The Problem</h3><blockquote><em>Given a non-empty array of integers, where every element appears twice except for one, return that single one.</em></blockquote><p>Input: nums = [2, 3, 5, 4, 5, 3, 4]<br>Output: 2</p><h3>🧠 Why Does This Work?</h3><p>It comes down to how <strong>XOR (^) behaves</strong>:</p><ul><li>a ^ a = 0: any number XOR itself is 0</li><li>a ^ 0 = a: any number XOR 0 is itself</li><li>XOR is <strong>commutative and associative</strong> → order doesn’t matter</li></ul><h3>🎯 XOR in One Sentence:</h3><blockquote><strong><em>XOR flips the bit. If it’s 0, it becomes 1. If it’s 1, it becomes 0.</em></strong></blockquote><p>But only when both numbers are <strong>not the same</strong>.</p><h3>🎮 Try This With Hands</h3><p>Let’s say:</p><ul><li>result = 0 (your empty hand)</li><li>You XOR with 2 → now result = 2 (you’re holding 2)</li><li>XOR again with 2 → now result = 0 (you dropped 2)</li></ul><pre>class Solution(object):<br>    def singleNumber(self, nums):<br>        &quot;&quot;&quot;<br>        :type nums: List[int]<br>        :rtype: int<br>        &quot;&quot;&quot;<br>        result = 0<br>        for num in nums:<br>            result ^= num  # XOR each number<br>        return result</pre><p>So if every number appears <strong>twice</strong> except one, XORing them all <strong>cancels out the duplicates</strong> and leaves the unique number.</p><h3>✅ Summary (In Easy Words)</h3><ul><li>XOR gives you 1 when the two bits are <strong>different</strong>.</li><li>XOR is great at <strong>canceling duplicates</strong>.</li><li>It helps solve problems <strong>without using extra space</strong>.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=50903c10d9cf" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-leetcode-grind/leetcode-26-single-number-50903c10d9cf">Leetcode #26 : Single Number</a> was originally published in <a href="https://medium.com/the-leetcode-grind">The Leetcode Grind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode #25 : Pascal’s Triangle II]]></title>
            <link>https://medium.com/the-leetcode-grind/leetcode-25-pascals-triangle-ii-17b55223686a?source=rss----1daa19c719ea---4</link>
            <guid isPermaLink="false">https://medium.com/p/17b55223686a</guid>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[pascal]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[coding]]></category>
            <dc:creator><![CDATA[Shruti Mandaokar]]></dc:creator>
            <pubDate>Fri, 27 Jun 2025 09:02:35 GMT</pubDate>
            <atom:updated>2025-06-27T09:02:35.382Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leetcode #25 : Pascal’s Triangle II</h3><p>Pascal’s Triangle is one of those classic patterns in mathematics that shows up in many places — from binomial expansions to coding interviews.</p><p>In this post, we’ll learn how to generate <strong>any row</strong> of Pascal’s Triangle, using a Python approach that’s clean, efficient, and beginner-friendly.</p><h3>🌟 The Problem</h3><blockquote><em>Given an integer rowIndex, return the </em><strong><em>rowIndex-th</em></strong><em> row of Pascal’s Triangle.</em></blockquote><p><strong>Note:</strong> The triangle is 0-indexed. That means:</p><ul><li>Row 0 is [1]</li><li>Row 1 is [1, 1]</li><li>Row 2 is [1, 2, 1]</li><li>And so on…</li></ul><h3>🔺 What Is Pascal’s Triangle?</h3><p>Pascal’s Triangle starts with a 1, and every row builds from the previous one using this rule:</p><blockquote><strong><em>Each number is the sum of the two numbers directly above it.</em></strong></blockquote><p>Let’s visualize the first few rows:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*yxUZANafvDs4Jcsh9mn8Vw.png" /></figure><h3>🧠 Intuition</h3><p>Instead of building the entire triangle, we just want a <strong>specific row</strong>. That means we can build each row <strong>one at a time</strong>, updating as we go.</p><p>The trick: we <strong>update the list in place from right to left</strong>, so we don’t overwrite numbers we still need.</p><pre>class Solution(object):<br>    def getRow(self, rowIndex):<br>        &quot;&quot;&quot;<br>        :type rowIndex: int<br>        :rtype: List[int]<br>        &quot;&quot;&quot;<br>        row = [1]  # Start with the first row: [1]<br><br>        for i in range(1, rowIndex + 1):<br>            # Update the current row from right to left<br>            for j in range(len(row) - 1, 0, -1):<br>                row[j] = row[j] + row[j - 1]<br>            row.append(1)  # Add 1 at the end<br><br>        return row</pre><h3>🔍 Line-by-Line Breakdown</h3><h3>1. row = [1]</h3><p>We begin with the very first row of Pascal’s Triangle — just a single 1.</p><h3>2. for i in range(1, rowIndex + 1):</h3><p>We’ll build each row from 1 to rowIndex. Ex — rowIndex = 4</p><h3>3. for j in range(len(row) — 1, 0, -1):</h3><p>We iterate <strong>from right to left</strong>, so we don’t mess up the values while updating.</p><h3>4. row[j] = row[j] + row[j — 1]</h3><p>Each number in the row is the <strong>sum of the two numbers above it</strong>.</p><h3>5. row.append(1)</h3><p>Every Pascal’s Triangle row ends with 1, so we add it at the end.</p><h3>⏱ Time &amp; Space Complexity</h3><p>Time Complexity : O(n²)</p><p>Space Complexity : O(n)</p><ul><li>We only store one row at a time.</li><li>But since we update it in place with nested loops, it’s O(n²).</li></ul><h3><strong>Output</strong></h3><p>Input: rowIndex = 4<br>Output: [1, 4, 6, 4, 1]</p><h3>🔚 Final Thoughts</h3><p>This problem is a great example of how you can <strong>build complex patterns step by step</strong>, using smart iteration.</p><p>By learning to update the list <strong>from right to left</strong>, we avoid bugs and keep our code efficient — a handy trick that applies in many other coding scenarios too.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=17b55223686a" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-leetcode-grind/leetcode-25-pascals-triangle-ii-17b55223686a">Leetcode #25 : Pascal’s Triangle II</a> was originally published in <a href="https://medium.com/the-leetcode-grind">The Leetcode Grind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Leetcode #24 : Pascal’s Triangle]]></title>
            <link>https://medium.com/the-leetcode-grind/leetcode-24-pascals-triangle-499fa10406af?source=rss----1daa19c719ea---4</link>
            <guid isPermaLink="false">https://medium.com/p/499fa10406af</guid>
            <category><![CDATA[leetcode]]></category>
            <category><![CDATA[computer-science]]></category>
            <category><![CDATA[coding]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[arrays]]></category>
            <dc:creator><![CDATA[Shruti Mandaokar]]></dc:creator>
            <pubDate>Fri, 27 Jun 2025 09:02:16 GMT</pubDate>
            <atom:updated>2025-06-27T09:02:16.222Z</atom:updated>
            <content:encoded><![CDATA[<h3>Leetcode #24 : Pascal’s Triangle</h3><p>Have you ever looked at Pascal’s Triangle and wondered, “How do I build that with code?”</p><p>If you’re new to Python or learning dynamic programming, the <strong>Pascal’s Triangle problem</strong> is a fantastic way to practice nested loops, list manipulation, and recursion-like thinking.</p><p>In this post, we’ll go step-by-step to <strong>generate Pascal’s Triangle</strong> using Python, understand how it works, and break down the logic into very simple terms.</p><h3>🧠 What is Pascal’s Triangle?</h3><p>Pascal’s Triangle is a triangle of numbers with these rules:</p><ul><li>The first and last numbers of each row are always <strong>1</strong>.</li><li>Every number in between is the <strong>sum of the two numbers above it</strong>.</li><li>Here’s what the first 5 rows look like:</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/574/1*yxUZANafvDs4Jcsh9mn8Vw.png" /></figure><h3>🎯 The Problem</h3><blockquote><strong><em>Given an integer numRows, return the first numRows of Pascal’s Triangle.</em></strong></blockquote><h3>Example:</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/334/1*4BsH-g8uvXz2Sbv4hhagUg.png" /></figure><pre>class Solution(object):<br>    def generate(self, numRows):<br>        &quot;&quot;&quot;<br>        :type numRows: int<br>        :rtype: List[List[int]]<br>        &quot;&quot;&quot;<br>        # Each number = sum of the two numbers directly above it<br>        result = []  # This will store all the rows<br><br>        for i in range(numRows):<br>            row = [1] * (i + 1)  # Start each row with all 1s<br><br>            # Update the middle values (not the first and last)<br>            for j in range(1, i):<br>                row[j] = result[i - 1][j - 1] + result[i - 1][j]<br><br>            result.append(row)  # Add this row to the result<br><br>        return result</pre><h3>🔍 Step-by-Step Walkthrough</h3><p>Let’s walk through how this works for numRows = 4:</p><p>Iteration 0:</p><pre>row = [1]<br>→ result = [[1]]</pre><p>Iteration 2:</p><pre>row = [1, _, 1]<br>row[1] = result[1][0] + result[1][1] = 1 + 1 = 2<br>→ row = [1, 2, 1]<br>→ result = [[1], [1, 1], [1, 2, 1]]</pre><p>Iteration 3:</p><pre>row = [1, _, _, 1]<br>row[1] = result[2][0] + result[2][1] = 1 + 2 = 3<br>row[2] = result[2][1] + result[2][2] = 2 + 1 = 3<br>→ row = [1, 3, 3, 1]<br>→ result = [[1], [1, 1], [1, 2, 1], [1, 3, 3, 1]]</pre><h3>🤔 What Does This Line Do?</h3><p>row[j] = result[i — 1][j — 1] + result[i — 1][j]</p><p>This is where the magic happens.</p><ul><li>It adds the <strong>two numbers directly above</strong> the current position j.</li><li>This follows Pascal’s Triangle rule.</li></ul><h3>🧮 Time &amp; Space Complexity</h3><ul><li><strong>Time:</strong> O(numRows²) — because each row can be as long as the row number.</li><li><strong>Space:</strong> O(numRows²) — we store all the rows in a list.</li></ul><h3>🎓 What You Learn from This</h3><ul><li>How to use <strong>nested loops</strong> to fill patterns.</li><li>How to <strong>build on previous results</strong> using a simple formula.</li><li>The importance of <strong>initializing and updating arrays carefully</strong>.</li></ul><p>This problem is a classic for a reason — it teaches you clean logic and elegant iteration.</p><h3>✅ Final Output for numRows = 5</h3><pre>[<br> [1],<br> [1, 1],<br> [1, 2, 1],<br> [1, 3, 3, 1],<br> [1, 4, 6, 4, 1]<br>]</pre><h3>🧠 Final Tip</h3><p>If you ever get confused by the math, just <strong>draw the triangle on paper</strong> and follow the rule:</p><blockquote><em>Each middle number = sum of two numbers above it.</em></blockquote><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=499fa10406af" width="1" height="1" alt=""><hr><p><a href="https://medium.com/the-leetcode-grind/leetcode-24-pascals-triangle-499fa10406af">Leetcode #24 : Pascal’s Triangle</a> was originally published in <a href="https://medium.com/the-leetcode-grind">The Leetcode Grind</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>