<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Sachin Yadav</title>
    <description>The latest articles on DEV Community by Sachin Yadav (@sachin_yadav_01b3080e2538).</description>
    <link>https://dev.to/sachin_yadav_01b3080e2538</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2095884%2F279193a7-bd61-4f75-a36e-feea39ef3c16.jpeg</url>
      <title>DEV Community: Sachin Yadav</title>
      <link>https://dev.to/sachin_yadav_01b3080e2538</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/sachin_yadav_01b3080e2538"/>
    <language>en</language>
    <item>
      <title>Bag of Tokens (Greedy Two Pointer Technique)</title>
      <dc:creator>Sachin Yadav</dc:creator>
      <pubDate>Sun, 01 Feb 2026 16:46:02 +0000</pubDate>
      <link>https://dev.to/sachin_yadav_01b3080e2538/bag-of-tokens-2b55</link>
      <guid>https://dev.to/sachin_yadav_01b3080e2538/bag-of-tokens-2b55</guid>
      <description>&lt;p&gt;The approach I used here is greedy I always want my score to increase so I will always go with the first condition that is if the power is greater than the current token then increment the count also if there is need or my power is less than the current token then I will always want to increase my power with the biggest token number. Therefore this gives the intuition that &lt;strong&gt;we need to sort the array of tokens in the ascending order&lt;/strong&gt; so that when I am &lt;u&gt;increasing my score then I pick from the left&lt;/u&gt; because it will be less than my power and if I want to &lt;u&gt;increase my power then I will always choose from the right&lt;/u&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * @param {number[]} tokens
 * @param {number} power
 * @return {number}
 */
var bagOfTokensScore = function (tokens, power) {
    tokens.sort((a, b) =&amp;gt; a - b)
    let score = 0
    let maxScore = 0
    let i = 0
    let j = tokens.length - 1
    while (i &amp;lt;= j) {
        if (power &amp;gt;= tokens[i]) {
            power -= tokens[i]
            score++
            i++
            maxScore = Math.max(score, maxScore)
        } else if (score &amp;gt;= 1) {
            power = power + tokens[j]
            j--
            score--
        } else {
            return maxScore
        }
    }
    return maxScore
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>greedy</category>
      <category>webdev</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Leetcode 2705. Compact Object</title>
      <dc:creator>Sachin Yadav</dc:creator>
      <pubDate>Sat, 27 Sep 2025 11:26:25 +0000</pubDate>
      <link>https://dev.to/sachin_yadav_01b3080e2538/leetcode-2705-compact-object-34f0</link>
      <guid>https://dev.to/sachin_yadav_01b3080e2538/leetcode-2705-compact-object-34f0</guid>
      <description>&lt;p&gt;View the dry run live here &lt;a href="https://leetcode-compact-object.netlify.app/" rel="noopener noreferrer"&gt;https://leetcode-compact-object.netlify.app/&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * @param {Object|Array} obj
 * @return {Object|Array}
 */
var compactObject = function (obj) {
    if (typeof obj !== 'object' || obj == null) {
        return obj
    }
    const isArray = Array.isArray(obj)
    const result = Array.isArray(obj) ? [] : {}
    const keys = Object.keys(obj)
    for (const key of keys) {
        let value = obj[key]
        let compactedValue = compactObject(value)
        if (Boolean(compactedValue)) {
            if (isArray) {
                result.push(compactedValue)
            } else {
                result[key] = compactedValue
            }
        }
    }
    return result
};


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>dsa</category>
      <category>leetcode</category>
      <category>javascript</category>
    </item>
    <item>
      <title>Leetcode 1493 Longest Subarray of 1's After Deleting One Element</title>
      <dc:creator>Sachin Yadav</dc:creator>
      <pubDate>Sun, 24 Aug 2025 18:29:30 +0000</pubDate>
      <link>https://dev.to/sachin_yadav_01b3080e2538/leetcode-1493-longest-subarray-of-1s-after-deleting-one-element-2f7k</link>
      <guid>https://dev.to/sachin_yadav_01b3080e2538/leetcode-1493-longest-subarray-of-1s-after-deleting-one-element-2f7k</guid>
      <description>&lt;p&gt;This problem is based on the sliding window technique. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Trick To Solve :-&lt;/strong&gt; Instead of looking at this problem like deleting one element, this problem can actually be viewed as solving the &lt;strong&gt;longest subarray of 1 having at most one zero&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Solve using the at most one zero approach and then return maxLen-1 and not maxLen because you have solved for at most one zero. &lt;/p&gt;

&lt;p&gt;One benefit of solving this problem is that, The exact same to same code can be used for another leetcode problem named &lt;em&gt;Max Consecutive Ones III&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Please note in &lt;a href="https://leetcode.com/problems/max-consecutive-ones-iii/" rel="noopener noreferrer"&gt;Max Consecutive Ones III&lt;/a&gt; we don't return maxLen-1 rather we return maxLen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * @param {number[]} nums
 * @return {number}
 */
var longestSubarray = function (nums) {
    let i = 0
    let j = 0
    let n = nums.length
    let maxLen = 0
    let zeroCount = 0
    while (j &amp;lt; n) {
        let num = nums[j]
        if (num == 0) {
            zeroCount++
        }
        while (zeroCount &amp;gt; 1) {
            if (nums[i] == 0) {
                zeroCount--
            }
            i++
        }
        maxLen = Math.max(maxLen, j - i + 1)
        j++
    }
    return maxLen - 1
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>dsa</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>Leetcode 1493 Longest Subarray of 1's After Deleting One Element</title>
      <dc:creator>Sachin Yadav</dc:creator>
      <pubDate>Sun, 24 Aug 2025 18:29:30 +0000</pubDate>
      <link>https://dev.to/sachin_yadav_01b3080e2538/leetcode-1493-longest-subarray-of-1s-after-deleting-one-element-2j22</link>
      <guid>https://dev.to/sachin_yadav_01b3080e2538/leetcode-1493-longest-subarray-of-1s-after-deleting-one-element-2j22</guid>
      <description>&lt;p&gt;This problem is based on the sliding window technique. &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Trick To Solve :-&lt;/strong&gt; Instead of looking at this problem like deleting one element, this problem can actually be viewed as solving the &lt;strong&gt;longest subarray of 1 having at most one zero&lt;/strong&gt;. &lt;/p&gt;

&lt;p&gt;Solve using the at most one zero approach and then return maxLen-1 and not maxLen because you have solved for at most one zero. &lt;/p&gt;

&lt;p&gt;One benefit of solving this problem is that, The exact same to same code can be used for another leetcode problem named &lt;em&gt;Max Consecutive Ones III&lt;/em&gt;. &lt;/p&gt;

&lt;p&gt;Please note in &lt;a href="https://leetcode.com/problems/max-consecutive-ones-iii/" rel="noopener noreferrer"&gt;Max Consecutive Ones III&lt;/a&gt; we don't return maxLen-1 rather we return maxLen.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * @param {number[]} nums
 * @return {number}
 */
var longestSubarray = function (nums) {
    let i = 0
    let j = 0
    let n = nums.length
    let maxLen = 0
    let zeroCount = 0
    while (j &amp;lt; n) {
        let num = nums[j]
        if (num == 0) {
            zeroCount++
        }
        while (zeroCount &amp;gt; 1) {
            if (nums[i] == 0) {
                zeroCount--
            }
            i++
        }
        maxLen = Math.max(maxLen, j - i + 1)
        j++
    }
    return maxLen - 1
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>dsa</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>Leetcode 678 :- Valid Parenthesis String</title>
      <dc:creator>Sachin Yadav</dc:creator>
      <pubDate>Thu, 19 Sep 2024 08:51:56 +0000</pubDate>
      <link>https://dev.to/sachin_yadav_01b3080e2538/leetcode-678-valid-parenthesis-string-59na</link>
      <guid>https://dev.to/sachin_yadav_01b3080e2538/leetcode-678-valid-parenthesis-string-59na</guid>
      <description>&lt;p&gt;Today I solved the question LC 678 &lt;a href="https://leetcode.com/problems/valid-parenthesis-string/description/" rel="noopener noreferrer"&gt;Valid Parenthesis String&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Intuition
&lt;/h2&gt;

&lt;p&gt;Since we have 3 choices to make for when we encounter a * we can solve this recursively.&lt;/p&gt;

&lt;h2&gt;
  
  
  Approach
&lt;/h2&gt;

&lt;p&gt;Since the recursive solution would lead to a TLE for when s="**********************" therefore we can optimize the recursive calls using Dynamic Programming&lt;/p&gt;

&lt;h2&gt;
  
  
  Complexity
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Time complexity:
For the recursive code :- At the worst possible scenario we are making 3 branches when we encounter an * so the TC is 3^n&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F06q6xce0y0od7ums2yyz.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F06q6xce0y0od7ums2yyz.jpg" alt="Recursive Tree" width="800" height="1117"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbyddyl6zkhdh6s6txz1m.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbyddyl6zkhdh6s6txz1m.jpg" alt="Code" width="800" height="1040"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Code
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//👇👇 Recursive Code
 var checkValidString = function (s, index = 0, count = 0) {
     if (count &amp;lt; 0) return false
     if (index === s.length) {
         return count === 0
     }
     if (s[index] === '(') {
         return checkValidString(s, index + 1, count + 1)
     } else if (s[index] === ')') {
         return checkValidString(s, index + 1, count - 1)
     } else {
         return checkValidString(s, index + 1, count + 1) || checkValidString(s, index + 1, count - 1) || checkValidString(s, index + 1, count)
     }
 };
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// 👇👇DP Code
var checkValidString = function (s, index = 0, count = 0, memo = {}) {
    if (count &amp;lt; 0) return false;
    if (index === s.length) {
        return count === 0;
    }
    // Create a unique key for the current state
    const key = `${index},${count}`;
    if (key in memo) {
        return memo[key]; // Return cached result
    }
    let result;
    if (s[index] === '(') {
        result = checkValidString(s, index + 1, count + 1, memo);
    } else if (s[index] === ')') {
        result = checkValidString(s, index + 1, count - 1, memo);
    } else { // s[index] === '*'
        result = checkValidString(s, index + 1, count + 1, memo) || // Treat '*' as '('
                 checkValidString(s, index + 1, count - 1, memo) || // Treat '*' as ')'
                 checkValidString(s, index + 1, count, memo);       // Treat '*' as empty
    }
    memo[key] = result; // Store the result in the cache
    return result;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>datastructures</category>
      <category>recursion</category>
    </item>
  </channel>
</rss>
