<?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: MD ARIFUL HAQUE</title>
    <description>The latest articles on DEV Community by MD ARIFUL HAQUE (@mdarifulhaque).</description>
    <link>https://dev.to/mdarifulhaque</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.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F944478%2F148ec474-230c-4c52-8b8e-5aba51bc6d2e.jpeg</url>
      <title>DEV Community: MD ARIFUL HAQUE</title>
      <link>https://dev.to/mdarifulhaque</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mdarifulhaque"/>
    <language>en</language>
    <item>
      <title>2574. Left and Right Sum Differences</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sat, 06 Jun 2026 17:55:55 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2574-left-and-right-sum-differences-b08</link>
      <guid>https://dev.to/mdarifulhaque/2574-left-and-right-sum-differences-b08</guid>
      <description>&lt;p&gt;2574. Left and Right Sum Differences&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Easy&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Mid Level&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Prefix Sum&lt;/code&gt;, &lt;code&gt;Weekly Contest 334&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given a &lt;strong&gt;0-indexed&lt;/strong&gt; integer array &lt;code&gt;nums&lt;/code&gt; of size &lt;code&gt;n&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Define two arrays &lt;code&gt;leftSum&lt;/code&gt; and &lt;code&gt;rightSum&lt;/code&gt; where:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;leftSum[i]&lt;/code&gt; is the sum of elements to the left of the index &lt;code&gt;i&lt;/code&gt; in the array &lt;code&gt;nums&lt;/code&gt;. If there is no such element, &lt;code&gt;leftSum[i] = 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;rightSum[i]&lt;/code&gt; is the sum of elements to the right of the index &lt;code&gt;i&lt;/code&gt; in the array &lt;code&gt;nums&lt;/code&gt;. If there is no such element, &lt;code&gt;rightSum[i] = 0&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return an integer array &lt;code&gt;answer&lt;/code&gt; of size &lt;code&gt;n&lt;/code&gt; where &lt;code&gt;answer[i] = |leftSum[i] - rightSum[i]|&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [10,4,8,3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [15,1,11,22]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The array leftSum is [0,10,14,22] and the array rightSum is [15,11,3,0].&lt;/li&gt;
&lt;li&gt;The array answer is [|0 - 15|,|10 - 11|,|14 - 3|,|22 - 0|] = [15,1,11,22].&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [0]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;The array leftSum is [0] and the array rightSum is [0].&lt;/li&gt;
&lt;li&gt;The array answer is [|0 - 0|] = [0].&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 1000&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums[i] &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For each index &lt;code&gt;i&lt;/code&gt;, maintain two variables &lt;code&gt;leftSum&lt;/code&gt; and &lt;code&gt;rightSum&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Iterate on the range &lt;code&gt;j: [0 … i - 1]&lt;/code&gt; and add &lt;code&gt;nums[j]&lt;/code&gt; to the &lt;code&gt;leftSum&lt;/code&gt; and similarly iterate on the range &lt;code&gt;j: [i + 1 … nums.length - 1]&lt;/code&gt; and add &lt;code&gt;nums[j]&lt;/code&gt; to the &lt;code&gt;rightSum&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Similar Questions:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/000724-find-pivot-index" rel="noopener noreferrer"&gt;724. Find Pivot Index&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/001991-find-the-middle-index-in-array" rel="noopener noreferrer"&gt;1991. Find the Middle Index in Array&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/002670-find-the-distinct-difference-array" rel="noopener noreferrer"&gt;2670. Find the Distinct Difference Array&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003179-find-the-n-th-value-after-k-seconds" rel="noopener noreferrer"&gt;3179. Find the N-th Value After K Seconds&lt;/a&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The solution computes the absolute difference between the sum of elements to the left and the sum of elements to the right for each index in the array. It uses prefix and suffix sums to achieve this efficiently without nested loops.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use prefix sums to calculate &lt;code&gt;leftSum[i]&lt;/code&gt; as the sum of all elements before index &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use suffix sums to calculate &lt;code&gt;rightSum[i]&lt;/code&gt; as the sum of all elements after index &lt;code&gt;i&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each index &lt;code&gt;i&lt;/code&gt;, compute the absolute difference between &lt;code&gt;leftSum[i]&lt;/code&gt; and &lt;code&gt;rightSum[i]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/002574-left-and-right-sum-differences" rel="noopener noreferrer"&gt;2574. Left and Right Sum Differences&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums
 * @return Integer[]
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;leftRightDifference&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;leftRightDifference&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// Output: [15,1,11,22]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;leftRightDifference&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// Output: [0]&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Create arrays &lt;code&gt;leftSum&lt;/code&gt; and &lt;code&gt;rightSum&lt;/code&gt; of the same length as &lt;code&gt;nums&lt;/code&gt;, initialized to &lt;code&gt;0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;First pass from left to right:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;leftSum[i] = leftSum[i-1] + nums[i-1]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;This accumulates sums of elements before the current index.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Second pass from right to left:

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;rightSum[i] = rightSum[i+1] + nums[i+1]&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;This accumulates sums of elements after the current index.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Third pass through indices:

&lt;ul&gt;
&lt;li&gt;Compute &lt;code&gt;answer[i] = |leftSum[i] - rightSum[i]|&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Return the &lt;code&gt;answer&lt;/code&gt; array.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; — each element is processed three times (once for left sum, once for right sum, once for answer).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; — two extra arrays of size &lt;code&gt;n&lt;/code&gt; are used for left and right sums.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3753. Total Waviness of Numbers in Range II</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Fri, 05 Jun 2026 15:48:08 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3753-total-waviness-of-numbers-in-range-ii-2g4c</link>
      <guid>https://dev.to/mdarifulhaque/3753-total-waviness-of-numbers-in-range-ii-2g4c</guid>
      <description>&lt;p&gt;3753. Total Waviness of Numbers in Range II&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Hard&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Senior Staff&lt;/code&gt;, &lt;code&gt;Math&lt;/code&gt;, &lt;code&gt;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Biweekly Contest 170&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given two integers &lt;code&gt;num1&lt;/code&gt; and &lt;code&gt;num2&lt;/code&gt; representing an &lt;strong&gt;inclusive&lt;/strong&gt; range &lt;code&gt;[num1, num2]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;waviness&lt;/strong&gt; of a number is defined as the total count of its &lt;strong&gt;peaks&lt;/strong&gt; and &lt;strong&gt;valleys&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A digit is a &lt;strong&gt;peak&lt;/strong&gt; if it is &lt;strong&gt;strictly greater&lt;/strong&gt; than both of its immediate neighbors.&lt;/li&gt;
&lt;li&gt;A digit is a &lt;strong&gt;valley&lt;/strong&gt; if it is &lt;strong&gt;strictly less&lt;/strong&gt; than both of its immediate neighbors.&lt;/li&gt;
&lt;li&gt;The first and last digits of a number &lt;strong&gt;cannot&lt;/strong&gt; be peaks or valleys.&lt;/li&gt;
&lt;li&gt;Any number with fewer than 3 digits has a waviness of 0.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return the total sum of waviness for all numbers in the range &lt;code&gt;[num1, num2]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; num1 = 120, num2 = 130&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;In the range &lt;code&gt;[120, 130]&lt;/code&gt;:&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;120&lt;/code&gt;: middle digit 2 is a peak, waviness = 1.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;121&lt;/code&gt;: middle digit 2 is a peak, waviness = 1.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;130&lt;/code&gt;: middle digit 3 is a peak, waviness = 1.&lt;/li&gt;
&lt;li&gt;All other numbers in the range have a waviness of 0.&lt;/li&gt;
&lt;li&gt;Thus, total waviness is &lt;code&gt;1 + 1 + 1 = 3&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; num1 = 198, num2 = 202&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;In the range &lt;code&gt;[198, 202]&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;198&lt;/code&gt;: middle digit 9 is a peak, waviness = 1.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;201&lt;/code&gt;: middle digit 0 is a valley, waviness = 1.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;202&lt;/code&gt;: middle digit 0 is a valley, waviness = 1.&lt;/li&gt;
&lt;li&gt;All other numbers in the range have a waviness of 0.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Thus, total waviness is &lt;code&gt;1 + 1 + 1 = 3&lt;/code&gt;.&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; num1 = 4848, num2 = 4848&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Number &lt;code&gt;4848&lt;/code&gt;: the second digit 8 is a peak, and the third digit 4 is a valley, giving a waviness of 2.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 4:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; num1 = 63, num2 = 101&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= num1 &amp;lt;= num2 &amp;lt;= 10¹⁵&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use digit dynamic programming&lt;/li&gt;
&lt;li&gt;Build a digit-DP state &lt;code&gt;(position, tight, lastDigit, secondLastDigit)&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This solution calculates the &lt;strong&gt;total sum of waviness&lt;/strong&gt; for all numbers in a given inclusive range &lt;code&gt;[num1, num2]&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
Waviness is defined as the count of &lt;strong&gt;peaks&lt;/strong&gt; and &lt;strong&gt;valleys&lt;/strong&gt; in the digit sequence of a number (excluding first and last digits).&lt;br&gt;&lt;br&gt;
The approach uses &lt;strong&gt;digit dynamic programming (digit DP)&lt;/strong&gt; to efficiently process ranges up to &lt;code&gt;10^15&lt;/code&gt;, avoiding brute-force enumeration.&lt;br&gt;&lt;br&gt;
The function &lt;code&gt;totalWaviness(num1, num2)&lt;/code&gt; computes the result as &lt;code&gt;solve(num2) - solve(num1 - 1)&lt;/code&gt;, where &lt;code&gt;solve(n)&lt;/code&gt; returns the total waviness for &lt;code&gt;[1, n]&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Digit DP&lt;/strong&gt; – We recursively process digits from most significant to least significant.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;State&lt;/strong&gt; – &lt;code&gt;(position, tight, started, secondLast, last)&lt;/code&gt; tracks:

&lt;ul&gt;
&lt;li&gt;current index in the number&lt;/li&gt;
&lt;li&gt;whether we are bound by the prefix of &lt;code&gt;n&lt;/code&gt; (&lt;code&gt;tight&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;whether we have started placing non-leading digits (&lt;code&gt;started&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;the previous two digits (&lt;code&gt;secondLast&lt;/code&gt;, &lt;code&gt;last&lt;/code&gt;) to detect peaks/valleys&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Memoization&lt;/strong&gt; – For &lt;code&gt;!tight&lt;/code&gt; states, we store results to reuse across different numbers.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Contribution counting&lt;/strong&gt; – When we place a new digit:

&lt;ul&gt;
&lt;li&gt;If we have at least 3 digits so far (&lt;code&gt;secondLast !== -1&lt;/code&gt;), we check if the middle digit (&lt;code&gt;last&lt;/code&gt;) is a peak or valley.&lt;/li&gt;
&lt;li&gt;If yes, we add &lt;code&gt;1 × (number of valid suffixes)&lt;/code&gt; to the waviness sum.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Base case&lt;/strong&gt; – When all digits are placed (&lt;code&gt;pos == len&lt;/code&gt;), return &lt;code&gt;count = 1, waviness = 0&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Range subtraction&lt;/strong&gt; – &lt;code&gt;totalWaviness(num1, num2) = solve(num2) - solve(num1 - 1)&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003753-total-waviness-of-numbers-in-range-ii" rel="noopener noreferrer"&gt;3753. Total Waviness of Numbers in Range II&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer $num1
 * @param Integer $num2
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;totalWaviness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$num2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * @param int $n
 * @return int|mixed
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;sumWavinessUpTo&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$n&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;mixed&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;totalWaviness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;130&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;totalWaviness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;198&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;202&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;totalWaviness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4848&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4848&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// Output: 2&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;totalWaviness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;63&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Peak &amp;amp; Valley definition&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;A digit &lt;code&gt;d&lt;/code&gt; is a &lt;strong&gt;peak&lt;/strong&gt; if &lt;code&gt;prev &amp;lt; d &amp;gt; next&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;A digit &lt;code&gt;d&lt;/code&gt; is a &lt;strong&gt;valley&lt;/strong&gt; if &lt;code&gt;prev &amp;gt; d &amp;lt; next&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;First and last digits are ignored.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Digit DP state&lt;/strong&gt; We need the &lt;strong&gt;last two digits&lt;/strong&gt; to know if the current position (as the second last) is a peak/valley when the next digit is chosen.&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Transition&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;At position &lt;code&gt;pos&lt;/code&gt;, we try digits &lt;code&gt;0..limit&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;If not started and digit is 0, stay in &lt;code&gt;not started&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If not started and digit &amp;gt; 0, start the number, store it as &lt;code&gt;last&lt;/code&gt;, &lt;code&gt;secondLast = -1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If started, compute if &lt;code&gt;last&lt;/code&gt; (which is the middle digit between &lt;code&gt;secondLast&lt;/code&gt; and &lt;code&gt;digit&lt;/code&gt;) is peak/valley, and add to waviness accordingly.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Memoization&lt;/strong&gt; The result for a state &lt;code&gt;(pos, started, secondLast, last)&lt;/code&gt; with &lt;code&gt;tight = 0&lt;/code&gt; can be reused across different higher prefixes.&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Complexity&lt;/strong&gt; State count is &lt;code&gt;O(len × 2 × 10 × 10)&lt;/code&gt; with &lt;code&gt;len ≤ 16&lt;/code&gt; → very fast.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(len × 2 × 10 × 10 × 10) ≈ O(16 × 1000) = O(16000)&lt;/strong&gt;&lt;/em&gt; per &lt;code&gt;solve(n)&lt;/code&gt;, which is trivial for input sizes up to &lt;code&gt;10¹⁵&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(len × 2 × 10 × 10)&lt;/strong&gt;&lt;/em&gt; for memoization table.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3751. Total Waviness of Numbers in Range I</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Thu, 04 Jun 2026 17:55:07 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3751-total-waviness-of-numbers-in-range-i-4kon</link>
      <guid>https://dev.to/mdarifulhaque/3751-total-waviness-of-numbers-in-range-i-4kon</guid>
      <description>&lt;p&gt;3751. Total Waviness of Numbers in Range I&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Senior&lt;/code&gt;, &lt;code&gt;Math&lt;/code&gt;, &lt;code&gt;Dynamic Programming&lt;/code&gt;, &lt;code&gt;Enumeration&lt;/code&gt;, &lt;code&gt;Biweekly Contest 170&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given two integers &lt;code&gt;num1&lt;/code&gt; and &lt;code&gt;num2&lt;/code&gt; representing an &lt;strong&gt;inclusive&lt;/strong&gt; range &lt;code&gt;[num1, num2]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The &lt;strong&gt;waviness&lt;/strong&gt; of a number is defined as the total count of its &lt;strong&gt;peaks&lt;/strong&gt; and &lt;strong&gt;valleys&lt;/strong&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A digit is a &lt;strong&gt;peak&lt;/strong&gt; if it is &lt;strong&gt;strictly greater&lt;/strong&gt; than both of its immediate neighbors.&lt;/li&gt;
&lt;li&gt;A digit is a &lt;strong&gt;valley&lt;/strong&gt; if it is &lt;strong&gt;strictly less&lt;/strong&gt; than both of its immediate neighbors.&lt;/li&gt;
&lt;li&gt;The first and last digits of a number &lt;strong&gt;cannot&lt;/strong&gt; be peaks or valleys.&lt;/li&gt;
&lt;li&gt;Any number with fewer than 3 digits has a waviness of 0.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return the total sum of waviness for all numbers in the range &lt;code&gt;[num1, num2]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; num1 = 120, num2 = 130&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;In the range [120, 130]:&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;120&lt;/code&gt;: middle digit 2 is a peak, waviness = 1.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;121&lt;/code&gt;: middle digit 2 is a peak, waviness = 1.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;130&lt;/code&gt;: middle digit 3 is a peak, waviness = 1.&lt;/li&gt;
&lt;li&gt;All other numbers in the range have a waviness of 0.&lt;/li&gt;
&lt;li&gt;Thus, total waviness is &lt;code&gt;1 + 1 + 1 = 3&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; num1 = 198, num2 = 202&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;In the range [198, 202]:&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;198&lt;/code&gt;: middle digit 9 is a peak, waviness = 1.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;201&lt;/code&gt;: middle digit 0 is a valley, waviness = 1.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;202&lt;/code&gt;: middle digit 0 is a valley, waviness = 1.&lt;/li&gt;
&lt;li&gt;All other numbers in the range have a waviness of 0.Thus,&lt;/li&gt;
&lt;li&gt;total waviness is &lt;code&gt;1 + 1 + 1 = 3&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; num1 = 4848, num2 = 4848&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 2&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; Number &lt;code&gt;4848&lt;/code&gt;: the second digit 8 is a peak, and the third digit 4 is a valley, giving a waviness of 2.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= num1 &amp;lt;= num2 &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use bruteforce&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We compute the waviness of each number in the inclusive range from &lt;code&gt;num1&lt;/code&gt; to &lt;code&gt;num2&lt;/code&gt; and sum the results. Waviness counts internal digits that are strictly greater than both neighbors (peaks) or strictly less than both neighbors (valleys). Numbers with fewer than 3 digits automatically have 0 waviness.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Iterate through every number in the range.&lt;/li&gt;
&lt;li&gt;Convert each number to a string to access its digits.&lt;/li&gt;
&lt;li&gt;Skip numbers with fewer than 3 digits.&lt;/li&gt;
&lt;li&gt;Check each interior digit (excluding first and last) for peak or valley conditions.&lt;/li&gt;
&lt;li&gt;Sum the waviness for the number.&lt;/li&gt;
&lt;li&gt;Accumulate the total over all numbers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003751-total-waviness-of-numbers-in-range-i" rel="noopener noreferrer"&gt;3751. Total Waviness of Numbers in Range I&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer $num1
 * @param Integer $num2
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;totalWaviness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$num1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$num2&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="cd"&gt;/**
 * @param $num
 * @return int
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;wavinessOfNumber&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$num&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;totalWaviness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;120&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;130&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;totalWaviness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;198&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;202&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;totalWaviness&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4848&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4848&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// Output: 2&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Brute force enumeration&lt;/strong&gt; is feasible here because the range is at most 100,000 numbers.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;wavinessOfNumber()&lt;/code&gt; handles the logic for a single number:

&lt;ul&gt;
&lt;li&gt;Split digits into an array.&lt;/li&gt;
&lt;li&gt;Loop from index 1 to &lt;code&gt;len-2&lt;/code&gt; (inclusive) to avoid first and last digits.&lt;/li&gt;
&lt;li&gt;Compare each digit with its immediate left and right neighbors.&lt;/li&gt;
&lt;li&gt;Increment waviness if the digit is a peak (&lt;code&gt;mid &amp;gt; left &amp;amp;&amp;amp; mid &amp;gt; right&lt;/code&gt;) or a valley (&lt;code&gt;mid &amp;lt; left &amp;amp;&amp;amp; mid &amp;lt; right&lt;/code&gt;).&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;The main function &lt;code&gt;totalWaviness()&lt;/code&gt; loops from &lt;code&gt;num1&lt;/code&gt; to &lt;code&gt;num2&lt;/code&gt;, calling the helper and adding to the total.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(N * D)&lt;/strong&gt;&lt;/em&gt; where N = range size (num2 - num1 + 1) and D = max digit length (≤ 6 for numbers ≤ 10⁵).

&lt;ul&gt;
&lt;li&gt;Here, D is small, so effectively &lt;em&gt;&lt;strong&gt;O(N)&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(D)&lt;/strong&gt;&lt;/em&gt;, as only a few variables are used.&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3635. Earliest Finish Time for Land and Water Rides II</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Wed, 03 Jun 2026 15:29:17 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3635-earliest-finish-time-for-land-and-water-rides-ii-4e89</link>
      <guid>https://dev.to/mdarifulhaque/3635-earliest-finish-time-for-land-and-water-rides-ii-4e89</guid>
      <description>&lt;p&gt;3635. Earliest Finish Time for Land and Water Rides II&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Staff&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Two Pointers&lt;/code&gt;, &lt;code&gt;Binary Search&lt;/code&gt;, &lt;code&gt;Greedy&lt;/code&gt;, &lt;code&gt;Sorting&lt;/code&gt;, &lt;code&gt;Biweekly Contest 162&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given two categories of theme park attractions: land rides and water rides.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Land rides&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;landStartTime[i]&lt;/code&gt; – the earliest time the &lt;code&gt;iᵗʰ&lt;/code&gt; land ride can be boarded.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;landDuration[i]&lt;/code&gt; – how long the &lt;code&gt;iᵗʰ&lt;/code&gt; land ride lasts.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Water rides&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;waterStartTime[j]&lt;/code&gt; – the earliest time the &lt;code&gt;jᵗʰ&lt;/code&gt; water ride can be boarded.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;waterDuration[j]&lt;/code&gt; – how long the &lt;code&gt;jᵗʰ&lt;/code&gt; water ride lasts.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;A tourist must experience &lt;strong&gt;exactly one&lt;/strong&gt; ride from &lt;strong&gt;each&lt;/strong&gt; category, in &lt;strong&gt;either order&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A ride may be started at its opening time or &lt;strong&gt;any later moment&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If a ride is started at time &lt;code&gt;t&lt;/code&gt;, it finishes at time &lt;code&gt;t + duration&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Immediately after finishing one ride the tourist may board the other (if it is already open) or wait until it opens.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return the &lt;strong&gt;earliest possible time&lt;/strong&gt; at which the tourist can finish both rides.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; landStartTime = [2,8], landDuration = [4,1], waterStartTime = [6], waterDuration = [3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 9&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Explanation:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Plan A (land ride 0 → water ride 0):

&lt;ul&gt;
&lt;li&gt;Start land ride 0 at time &lt;code&gt;landStartTime[0] = 2&lt;/code&gt;. Finish at &lt;code&gt;2 + landDuration[0] = 6&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Water ride 0 opens at time &lt;code&gt;waterStartTime[0] = 6&lt;/code&gt;. Start immediately at &lt;code&gt;6&lt;/code&gt;, finish at &lt;code&gt;6 + waterDuration[0] = 9&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Plan B (water ride 0 → land ride 1):

&lt;ul&gt;
&lt;li&gt;Start water ride 0 at time &lt;code&gt;waterStartTime[0] = 6&lt;/code&gt;. Finish at &lt;code&gt;6 + waterDuration[0] = 9&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Land ride 1 opens at &lt;code&gt;landStartTime[1] = 8&lt;/code&gt;. Start at time &lt;code&gt;9&lt;/code&gt;, finish at &lt;code&gt;9 + landDuration[1] = 10&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Plan C (land ride 1 → water ride 0):

&lt;ul&gt;
&lt;li&gt;Start land ride 1 at time &lt;code&gt;landStartTime[1] = 8&lt;/code&gt;. Finish at &lt;code&gt;8 + landDuration[1] = 9&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Water ride 0 opened at &lt;code&gt;waterStartTime[0] = 6&lt;/code&gt;. Start at time &lt;code&gt;9&lt;/code&gt;, finish at &lt;code&gt;9 + waterDuration[0] = 12&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Plan D (water ride 0 → land ride 0):

&lt;ul&gt;
&lt;li&gt;Start water ride 0 at time &lt;code&gt;waterStartTime[0] = 6&lt;/code&gt;. Finish at &lt;code&gt;6 + waterDuration[0] = 9&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Land ride 0 opened at &lt;code&gt;landStartTime[0] = 2&lt;/code&gt;. Start at time &lt;code&gt;9&lt;/code&gt;, finish at &lt;code&gt;9 + landDuration[0] = 13&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Plan A gives the earliest finish time of 9.&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; landStartTime = [5], landDuration = [3], waterStartTime = [1], waterDuration = [10]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 14&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Plan A (water ride 0 → land ride 0):

&lt;ul&gt;
&lt;li&gt;Start water ride 0 at time &lt;code&gt;waterStartTime[0] = 1&lt;/code&gt;. Finish at &lt;code&gt;1 + waterDuration[0] = 11&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Land ride 0 opened at &lt;code&gt;landStartTime[0] = 5&lt;/code&gt;. Start immediately at &lt;code&gt;11&lt;/code&gt; and finish at &lt;code&gt;11 + landDuration[0] = 14&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Plan B (land ride 0 → water ride 0):

&lt;ul&gt;
&lt;li&gt;Start land ride 0 at time &lt;code&gt;landStartTime[0] = 5&lt;/code&gt;. Finish at &lt;code&gt;5 + landDuration[0] = 8&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Water ride 0 opened at &lt;code&gt;waterStartTime[0] = 1&lt;/code&gt;. Start immediately at &lt;code&gt;8&lt;/code&gt; and finish at &lt;code&gt;8 + waterDuration[0] = 18&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Plan A provides the earliest finish time of 14.&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= n, m &amp;lt;= 5 * 10⁴&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;landStartTime.length == landDuration.length == n&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;waterStartTime.length == waterDuration.length == m&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Sort each ride list by opening time and build a prefix minimum of ride durations and a suffix minimum of ride finish times (&lt;code&gt;start + duration&lt;/code&gt;).&lt;/li&gt;
&lt;li&gt;Try both orders, land then water and water then land. For each ride in the first list compute &lt;code&gt;finish1 = start1 + duration1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Binary‑search the second list (sorted by start) to split rides into those with &lt;code&gt;start &amp;lt;= finish1&lt;/code&gt; and those with &lt;code&gt;start &amp;gt; finish1&lt;/code&gt;. Use the prefix minimum duration on the early group to get an earliest finish of &lt;code&gt;finish1 + minDuration&lt;/code&gt; and the suffix minimum finish time on the late group.&lt;/li&gt;
&lt;li&gt;For each pairing take the smaller finish time and track the overall minimum.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The problem requires finding the earliest finish time for a tourist who must take exactly one land ride and one water ride, in either order.&lt;br&gt;&lt;br&gt;
The solution sorts both lists by start time, then uses &lt;strong&gt;prefix minimum durations&lt;/strong&gt; and &lt;strong&gt;suffix minimum finish times&lt;/strong&gt; combined with &lt;strong&gt;binary search&lt;/strong&gt; to efficiently compute the best finish time for each possible first ride.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Two Orders&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Consider both possible sequences:&lt;/strong&gt; land ride first then water ride, and water ride first then land ride.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sorting&lt;/strong&gt; Sort both ride lists by start time to enable binary search and efficient preprocessing.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Preprocessing&lt;/strong&gt;
For the second category (the one taken after the first ride), compute:

&lt;ul&gt;
&lt;li&gt;Prefix minimum of durations (for rides that can start before the first ride ends)&lt;/li&gt;
&lt;li&gt;Suffix minimum of finish times (for rides that start after the first ride ends)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Binary Search&lt;/strong&gt; After finishing the first ride at time &lt;code&gt;finish1&lt;/code&gt;, binary search in the second list to split rides into:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Group A&lt;/strong&gt;: &lt;code&gt;start &amp;lt;= finish1&lt;/code&gt; → use prefix min duration to get earliest finish&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Group B&lt;/strong&gt;: &lt;code&gt;start &amp;gt; finish1&lt;/code&gt; → use suffix min finish time (start immediately)&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Optimization&lt;/strong&gt; Take the minimum finish time over all splits and both orders.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003635-earliest-finish-time-for-land-and-water-rides-ii" rel="noopener noreferrer"&gt;3635. Earliest Finish Time for Land and Water Rides II&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $landStartTime
 * @param Integer[] $landDuration
 * @param Integer[] $waterStartTime
 * @param Integer[] $waterDuration
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;earliestFinishTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$landStartTime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$landDuration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$waterStartTime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$waterDuration&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;earliestFinishTime&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 9&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;earliestFinishTime&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 14&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Step 1 – Pair and sort&lt;/strong&gt; Each ride is stored as &lt;code&gt;[startTime, duration]&lt;/code&gt;. Both lists are sorted by start time to allow binary search.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Step 2 – Preprocess second category&lt;/strong&gt; For water rides (if taken second):

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;prefixMinDuration[i]&lt;/code&gt; = minimum duration among first &lt;code&gt;i+1&lt;/code&gt; water rides&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;suffixMinFinish[i]&lt;/code&gt; = minimum finish time among water rides from &lt;code&gt;i&lt;/code&gt; to end
&lt;/li&gt;
&lt;li&gt;Same preprocessing for land rides when taken second.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Step 3 – Binary search split&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Given &lt;code&gt;finish1&lt;/code&gt; from the first ride, find first index &lt;code&gt;pos&lt;/code&gt; where &lt;code&gt;start[pos] &amp;gt; finish1&lt;/code&gt;.
&lt;/li&gt;
&lt;li&gt;This splits second rides into those starting before/after the end of the first ride.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Step 4 – Compute best finish for this first ride&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;For Group A: earliest finish = &lt;code&gt;finish1 + minDuration&lt;/code&gt; (start immediately after first ride ends, since all start ≤ finish1)&lt;/li&gt;
&lt;li&gt;For Group B: earliest finish = &lt;code&gt;minFinish[pos]&lt;/code&gt; (start at their own start time, since first ride ended earlier)&lt;/li&gt;
&lt;li&gt;Take the smaller of these two as the best for this pairing.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Step 5 – Minimize over all first rides and both orders&lt;/strong&gt; Track the global minimum finish time.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n log n + m log m)&lt;/strong&gt;&lt;/em&gt; for sorting, plus &lt;code&gt;O(n log m + m log n)&lt;/code&gt; for binary search per first ride → &lt;strong&gt;O((n + m) * log(n + m))&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n + m)&lt;/strong&gt;&lt;/em&gt; for storing arrays of start times, durations, finish times, prefix/suffix arrays.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3633. Earliest Finish Time for Land and Water Rides I</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Tue, 02 Jun 2026 17:32:57 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3633-earliest-finish-time-for-land-and-water-rides-i-28mh</link>
      <guid>https://dev.to/mdarifulhaque/3633-earliest-finish-time-for-land-and-water-rides-i-28mh</guid>
      <description>&lt;p&gt;3633. Earliest Finish Time for Land and Water Rides I&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Easy&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Mid Level&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Two Pointers&lt;/code&gt;, &lt;code&gt;Binary Search&lt;/code&gt;, &lt;code&gt;Greedy&lt;/code&gt;, &lt;code&gt;Sorting&lt;/code&gt;, &lt;code&gt;Biweekly Contest 162&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given two categories of theme park attractions: land rides and water rides.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Land rides&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;landStartTime[i]&lt;/code&gt; – the earliest time the &lt;code&gt;ith&lt;/code&gt; land ride can be boarded.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;landDuration[i]&lt;/code&gt; – how long the &lt;code&gt;ith&lt;/code&gt; land ride lasts.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Water rides&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;waterStartTime[j]&lt;/code&gt; – the earliest time the &lt;code&gt;jth&lt;/code&gt; water ride can be boarded.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;waterDuration[j]&lt;/code&gt; – how long the &lt;code&gt;jth&lt;/code&gt; water ride lasts.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;A tourist must experience &lt;strong&gt;exactly one&lt;/strong&gt; ride from &lt;strong&gt;each&lt;/strong&gt; category, in &lt;strong&gt;either order&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A ride may be started at its opening time or &lt;strong&gt;any later moment&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;If a ride is started at time &lt;code&gt;t&lt;/code&gt;, it finishes at time &lt;code&gt;t + duration&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Immediately after finishing one ride the tourist may board the other (if it is already open) or wait until it opens.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return the &lt;strong&gt;earliest possible time&lt;/strong&gt; at which the tourist can finish both rides.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; landStartTime = [2,8], landDuration = [4,1], waterStartTime = [6], waterDuration = [3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 9&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Plan A (land ride 0 → water ride 0):

&lt;ul&gt;
&lt;li&gt;Start land ride 0 at time &lt;code&gt;landStartTime[0] = 2&lt;/code&gt;. Finish at &lt;code&gt;2 + landDuration[0] = 6&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Water ride 0 opens at time &lt;code&gt;waterStartTime[0] = 6&lt;/code&gt;. Start immediately at &lt;code&gt;6&lt;/code&gt;, finish at &lt;code&gt;6 + waterDuration[0] = 9&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Plan B (water ride 0 → land ride 1):

&lt;ul&gt;
&lt;li&gt;Start water ride 0 at &lt;code&gt;time waterStartTime[0] = 6&lt;/code&gt;. Finish at &lt;code&gt;6 + waterDuration[0] = 9&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Land ride 1 opens at &lt;code&gt;landStartTime[1] = 8&lt;/code&gt;. Start at time &lt;code&gt;9&lt;/code&gt;, finish at &lt;code&gt;9 + landDuration[1] = 10&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Plan C (land ride 1 → water ride 0):

&lt;ul&gt;
&lt;li&gt;Start land ride 1 at time &lt;code&gt;landStartTime[1] = 8&lt;/code&gt;. Finish at &lt;code&gt;8 + landDuration[1] = 9&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Water ride 0 opened at &lt;code&gt;waterStartTime[0] = 6&lt;/code&gt;. Start at time &lt;code&gt;9&lt;/code&gt;, finish at &lt;code&gt;9 + waterDuration[0] = 12&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Plan D (water ride 0 → land ride 0):

&lt;ul&gt;
&lt;li&gt;Start water ride 0 at time &lt;code&gt;waterStartTime[0] = 6&lt;/code&gt;. Finish at &lt;code&gt;6 + waterDuration[0] = 9&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Land ride 0 opened at &lt;code&gt;landStartTime[0] = 2&lt;/code&gt;. Start at time &lt;code&gt;9&lt;/code&gt;, finish at &lt;code&gt;9 + landDuration[0] = 13&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Plan A gives the earliest finish time of 9.&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; landStartTime = [5], landDuration = [3], waterStartTime = [1], waterDuration = [10]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 14&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Plan A (water ride 0 → land ride 0):

&lt;ul&gt;
&lt;li&gt;Start water ride 0 at time &lt;code&gt;waterStartTime[0] = 1&lt;/code&gt;. Finish at &lt;code&gt;1 + waterDuration[0] = 11&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Land ride 0 opened at &lt;code&gt;landStartTime[0] = 5&lt;/code&gt;. Start immediately at &lt;code&gt;11&lt;/code&gt; and finish at &lt;code&gt;11 + landDuration[0] = 14&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Plan B (land ride 0 → water ride 0):

&lt;ul&gt;
&lt;li&gt;Start land ride 0 at time &lt;code&gt;landStartTime[0] = 5&lt;/code&gt;. Finish at &lt;code&gt;5 + landDuration[0] = 8&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Water ride 0 opened at &lt;code&gt;waterStartTime[0] = 1&lt;/code&gt;. Start immediately at &lt;code&gt;8&lt;/code&gt; and finish at &lt;code&gt;8 + waterDuration[0] = 18&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Plan A provides the earliest finish time of 14.&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= n, m &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;landStartTime.length == landDuration.length == n&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;waterStartTime.length == waterDuration.length == m&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= landStartTime[i], landDuration[i], waterStartTime[j], waterDuration[j] &amp;lt;= 1000&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Use brute force&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The problem requires calculating the earliest possible finish time after riding exactly one land ride and one water ride, in either order, with start times that may be delayed until the ride opens. The solution uses brute‑force iteration over all pairs of rides, computes both possible orders for each pair, and tracks the minimum finish time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Iterate through every combination of a land ride and a water ride.&lt;/li&gt;
&lt;li&gt;For each pair, compute the finish time for both sequences:

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Land first&lt;/strong&gt; → then water.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Water first&lt;/strong&gt; → then land.&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt;The start time of the second ride is the maximum of the finish time of the first ride and the start time of the second ride (must wait if it isn’t open yet).&lt;/li&gt;
&lt;li&gt;Keep the global minimum finish time across all pairs and orders.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003633-earliest-finish-time-for-land-and-water-rides-i" rel="noopener noreferrer"&gt;3633. Earliest Finish Time for Land and Water Rides I&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $landStartTime
 * @param Integer[] $landDuration
 * @param Integer[] $waterStartTime
 * @param Integer[] $waterDuration
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;earliestFinishTime&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$landStartTime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$landDuration&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$waterStartTime&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$waterDuration&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;earliestFinishTime&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 9&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;earliestFinishTime&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 14&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;The brute‑force method is feasible because the number of rides in each category is at most 100, so checking all (n × m) pairs is fine.&lt;/li&gt;
&lt;li&gt;For each pair &lt;code&gt;(land[i], water[j])&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Land first&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Land finishes at &lt;code&gt;landStartTime[i] + landDuration[i]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Water starts at &lt;code&gt;max(landFinishTime, waterStartTime[j])&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Water finishes at &lt;code&gt;waterStart + waterDuration[j]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Water first&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Water finishes at &lt;code&gt;waterStartTime[j] + waterDuration[j]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Land starts at &lt;code&gt;max(waterFinishTime, landStartTime[i])&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Land finishes at &lt;code&gt;landStart + landDuration[i]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;li&gt;The answer is the smallest finish time from all orders and all pairs.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n × m)&lt;/strong&gt;&lt;/em&gt; — loops through all land–water pairs, each with constant work.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; — only a few scalar variables used, independent of input size.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>2144. Minimum Cost of Buying Candies With Discount</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Mon, 01 Jun 2026 16:21:16 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2144-minimum-cost-of-buying-candies-with-discount-a4p</link>
      <guid>https://dev.to/mdarifulhaque/2144-minimum-cost-of-buying-candies-with-discount-a4p</guid>
      <description>&lt;p&gt;2144. Minimum Cost of Buying Candies With Discount&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Easy&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Mid Level&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Greedy&lt;/code&gt;, &lt;code&gt;Sorting&lt;/code&gt;, &lt;code&gt;Biweekly Contest 70&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;A shop is selling candies at a discount. For &lt;strong&gt;every two&lt;/strong&gt; candies sold, the shop gives a &lt;strong&gt;third&lt;/strong&gt; candy for &lt;strong&gt;free&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;The customer can choose &lt;strong&gt;any&lt;/strong&gt; candy to take away for free as long as the cost of the chosen candy is less than or equal to the &lt;strong&gt;minimum&lt;/strong&gt; cost of the two candies bought.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;For example, if there are &lt;code&gt;4&lt;/code&gt; candies with costs &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;2&lt;/code&gt;, &lt;code&gt;3&lt;/code&gt;, and &lt;code&gt;4&lt;/code&gt;, and the customer buys candies with costs &lt;code&gt;2&lt;/code&gt; and &lt;code&gt;3&lt;/code&gt;, they can take the candy with cost &lt;code&gt;1&lt;/code&gt; for free, but not the candy with cost &lt;code&gt;4&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Given a &lt;strong&gt;0-indexed&lt;/strong&gt; integer array &lt;code&gt;cost&lt;/code&gt;, where &lt;code&gt;cost[i]&lt;/code&gt; denotes the cost of the &lt;code&gt;iᵗʰ&lt;/code&gt; candy, return the &lt;strong&gt;minimum cost&lt;/strong&gt; of buying &lt;strong&gt;all&lt;/strong&gt; the candies.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; cost = [1,2,3]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 5&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;We buy the candies with costs 2 and 3, and take the candy with cost 1 for free.&lt;/li&gt;
&lt;li&gt;The total cost of buying all candies is 2 + 3 = 5. This is the only way we can buy the candies.&lt;/li&gt;
&lt;li&gt;Note that we cannot buy candies with costs 1 and 3, and then take the candy with cost 2 for free.&lt;/li&gt;
&lt;li&gt;The cost of the free candy has to be less than or equal to the minimum cost of the purchased candies.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; cost = [6,5,7,9,2,2]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 23&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;The way in which we can get the minimum cost is described below:&lt;/li&gt;
&lt;li&gt;Buy candies with costs 9 and 7&lt;/li&gt;
&lt;li&gt;Take the candy with cost 6 for free&lt;/li&gt;
&lt;li&gt;We buy candies with costs 5 and 2&lt;/li&gt;
&lt;li&gt;Take the last remaining candy with cost 2 for free&lt;/li&gt;
&lt;li&gt;Hence, the minimum cost to buy all candies is 9 + 7 + 5 + 2 = 23.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; cost = [5,5]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 10&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;Since there are only 2 candies, we buy both of them. There is not a third candy we can take for free.&lt;/li&gt;
&lt;li&gt;Hence, the minimum cost to buy all candies is 5 + 5 = 10.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= cost.length &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= cost[i] &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If we consider costs from high to low, what is the maximum cost of a single candy that we can get for free?&lt;/li&gt;
&lt;li&gt;How can we generalize this approach to maximize the costs of the candies we get for free?&lt;/li&gt;
&lt;li&gt;Can “sorting” the array help us find the minimum cost?&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The solution reduces total cost by sorting candy prices in descending order and taking every third candy for free, because the discount allows a free candy whose price is ≤ the cheaper of the two purchased candies. Sorting descending and skipping every third item maximizes the value of free candies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Sort the array in &lt;strong&gt;descending order&lt;/strong&gt; so the most expensive candies are considered first.&lt;/li&gt;
&lt;li&gt;Iterate through the sorted list, adding the cost of each candy to the total &lt;strong&gt;except&lt;/strong&gt; for every third candy.&lt;/li&gt;
&lt;li&gt;The index pattern (0, 1, 3, 4, 6, 7, …) ensures that after buying two candies, the next one is free.&lt;/li&gt;
&lt;li&gt;Sum all purchased candies and return the total.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/002144-minimum-cost-of-buying-candies-with-discount" rel="noopener noreferrer"&gt;2144. Minimum Cost of Buying Candies With Discount&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $cost
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;minimumCost&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$cost&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumCost&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;               &lt;span class="c1"&gt;// Output: 5&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumCost&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;9&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 23&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minimumCost&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                 &lt;span class="c1"&gt;// Output: 10&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sorting descending&lt;/strong&gt; ensures that when we take a free candy, it’s the smallest possible among the remaining items.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Index check &lt;code&gt;(i % 3 != 2)&lt;/code&gt;&lt;/strong&gt; means we skip adding the price for the third candy in every group of three.&lt;/li&gt;
&lt;li&gt;Example: costs sorted → 9, 7, 6, 5, 2, 2

&lt;ul&gt;
&lt;li&gt;Buy 9 (index 0), buy 7 (index 1), skip 6 (index 2), buy 5 (index 3), buy 2 (index 4), skip 2 (index 5)&lt;/li&gt;
&lt;li&gt;Total = 9 + 7 + 5 + 2 = 23&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;This greedy method works because taking a free candy from the smallest available prices is always optimal.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n log n)&lt;/strong&gt;&lt;/em&gt; — due to sorting.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; — ignoring the input storage, only a few variables are used.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>2126. Destroying Asteroids</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sun, 31 May 2026 17:40:45 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/2126-destroying-asteroids-ek</link>
      <guid>https://dev.to/mdarifulhaque/2126-destroying-asteroids-ek</guid>
      <description>&lt;p&gt;2126. Destroying Asteroids&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Staff&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Greedy&lt;/code&gt;, &lt;code&gt;Sorting&lt;/code&gt;, &lt;code&gt;Weekly Contest 274&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an integer &lt;code&gt;mass&lt;/code&gt;, which represents the original mass of a planet. You are further given an integer array &lt;code&gt;asteroids&lt;/code&gt;, where &lt;code&gt;asteroids[i]&lt;/code&gt; is the mass of the &lt;code&gt;iᵗʰ&lt;/code&gt; asteroid.&lt;/p&gt;

&lt;p&gt;You can arrange for the planet to collide with the asteroids in &lt;strong&gt;any arbitrary order&lt;/strong&gt;. If the mass of the planet is &lt;strong&gt;greater than or equal to&lt;/strong&gt; the mass of the asteroid, the asteroid is &lt;strong&gt;destroyed&lt;/strong&gt; and the planet &lt;strong&gt;gains&lt;/strong&gt; the mass of the asteroid. Otherwise, the planet is destroyed.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;&lt;code&gt;true&lt;/code&gt; if &lt;strong&gt;all&lt;/strong&gt; asteroids can be destroyed. Otherwise, return &lt;code&gt;false&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; mass = 10, asteroids = [3,9,19,5,21]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; true&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; 

&lt;ul&gt;
&lt;li&gt;One way to order the asteroids is [9,19,5,3,21]:&lt;/li&gt;
&lt;li&gt;The planet collides with the asteroid with a mass of 9. New planet mass: 10 + 9 = 19&lt;/li&gt;
&lt;li&gt;The planet collides with the asteroid with a mass of 19. New planet mass: 19 + 19 = 38&lt;/li&gt;
&lt;li&gt;The planet collides with the asteroid with a mass of 5. New planet mass: 38 + 5 = 43&lt;/li&gt;
&lt;li&gt;The planet collides with the asteroid with a mass of 3. New planet mass: 43 + 3 = 46&lt;/li&gt;
&lt;li&gt;The planet collides with the asteroid with a mass of 21. New planet mass: 46 + 21 = 67&lt;/li&gt;
&lt;li&gt;All asteroids are destroyed.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; mass = 5, asteroids = [4,9,23,4]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; false&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;The planet cannot ever gain enough mass to destroy the asteroid with a mass of 23.&lt;/li&gt;
&lt;li&gt;After the planet destroys the other asteroids, it will have a mass of 5 + 4 + 9 + 4 = 22.&lt;/li&gt;
&lt;li&gt;This is less than 23, so a collision would not destroy the last asteroid.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= mass &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= asteroids.length &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= asteroids[i] &amp;lt;= 10⁵&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choosing the asteroid to collide with can be done greedily.&lt;/li&gt;
&lt;li&gt;If an asteroid will destroy the planet, then every bigger asteroid will also destroy the planet.&lt;/li&gt;
&lt;li&gt;You only need to check the smallest asteroid at each collision. If it will destroy the planet, then every other asteroid will also destroy the planet.&lt;/li&gt;
&lt;li&gt;Sort the asteroids in non-decreasing order by mass, then greedily try to collide with the asteroids in that order.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The solution determines whether a planet with a given starting mass can destroy all asteroids by colliding with them in an optimal order.&lt;br&gt;&lt;br&gt;
It does this by sorting the asteroids in ascending order and then greedily absorbing them one by one, ensuring the planet’s mass never drops below the next asteroid’s mass. If at any point the planet’s mass is insufficient, the answer is &lt;code&gt;false&lt;/code&gt;; otherwise, it’s &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Sorting the asteroids&lt;/strong&gt; in non-decreasing order ensures we always try to absorb the smallest possible asteroid next.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Greedy absorption&lt;/strong&gt; — if the planet’s current mass is at least the asteroid’s mass, it absorbs it and increases its mass.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Early exit&lt;/strong&gt; — if at any step the planet’s mass is less than the asteroid’s mass, all larger asteroids will also be impossible to absorb, so we return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Final check&lt;/strong&gt; — if all asteroids are processed successfully, return &lt;code&gt;true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/002126-destroying-asteroids" rel="noopener noreferrer"&gt;2126. Destroying Asteroids&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer $mass
 * @param Integer[] $asteroids
 * @return Boolean
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;asteroidsDestroyed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nv"&gt;$mass&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$asteroids&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;bool&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;asteroidsDestroyed&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// Output: true&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;asteroidsDestroyed&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                &lt;span class="c1"&gt;// Output: false&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Step 1:&lt;/strong&gt; Sort the &lt;code&gt;asteroids&lt;/code&gt; array so the smallest asteroids come first.&lt;br&gt;&lt;br&gt;
&lt;em&gt;Reason:&lt;/em&gt; Absorbing smaller asteroids first maximizes the chance of reaching a mass large enough to take on bigger ones.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Step 2:&lt;/strong&gt; Initialize &lt;code&gt;currentMass&lt;/code&gt; with the planet’s starting &lt;code&gt;mass&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;em&gt;Purpose:&lt;/em&gt; Track the planet’s mass dynamically as it absorbs asteroids.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Step 3:&lt;/strong&gt; Loop through each asteroid in the sorted list.&lt;br&gt;&lt;br&gt;
&lt;em&gt;Check:&lt;/em&gt; If &lt;code&gt;currentMass &amp;gt;= asteroid&lt;/code&gt;, the planet can destroy it — add the asteroid’s mass to &lt;code&gt;currentMass&lt;/code&gt;.&lt;br&gt;&lt;br&gt;
&lt;em&gt;Otherwise:&lt;/em&gt; Immediately return &lt;code&gt;false&lt;/code&gt; (planet is destroyed).&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Step 4:&lt;/strong&gt; If the loop completes without returning &lt;code&gt;false&lt;/code&gt;, return &lt;code&gt;true&lt;/code&gt; (all asteroids destroyed).&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n log n)&lt;/strong&gt;&lt;/em&gt; due to sorting, where &lt;em&gt;&lt;strong&gt;n&lt;/strong&gt;&lt;/em&gt; is the number of asteroids.
&lt;em&gt;Greedy traversal&lt;/em&gt; is &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; extra space (sorting may use &lt;em&gt;&lt;strong&gt;O(log n)&lt;/strong&gt;&lt;/em&gt; stack space in PHP’s &lt;code&gt;sort()&lt;/code&gt; function).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3161. Block Placement Queries</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Sun, 31 May 2026 15:00:00 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3161-block-placement-queries-2acj</link>
      <guid>https://dev.to/mdarifulhaque/3161-block-placement-queries-2acj</guid>
      <description>&lt;p&gt;3161. Block Placement Queries&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Hard&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Principal&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Binary Search&lt;/code&gt;, &lt;code&gt;Binary Indexed Tree&lt;/code&gt;, &lt;code&gt;Segment Tree&lt;/code&gt;, &lt;code&gt;Biweekly Contest 131&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;There exists an infinite number line, with its origin at 0 and extending towards the positive x-axis.&lt;/p&gt;

&lt;p&gt;You are given a 2D array &lt;code&gt;queries&lt;/code&gt;, which contains two types of queries:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For a query of type 1, &lt;code&gt;queries[i] = [1, x]&lt;/code&gt;. Build an obstacle at distance &lt;code&gt;x&lt;/code&gt; from the origin. It is guaranteed that there is no obstacle at distance &lt;code&gt;x&lt;/code&gt; when the query is asked.&lt;/li&gt;
&lt;li&gt;For a query of type 2, &lt;code&gt;queries[i] = [2, x, sz]&lt;/code&gt;. Check if it is possible to place a block of size &lt;code&gt;sz&lt;/code&gt; &lt;em&gt;anywhere&lt;/em&gt; in the range &lt;code&gt;[0, x]&lt;/code&gt; on the line, such that the block &lt;strong&gt;entirely&lt;/strong&gt; lies in the range &lt;code&gt;[0, x]&lt;/code&gt;. A block &lt;strong&gt;cannot&lt;/strong&gt; be placed if it intersects with any obstacle, but it may touch it. Note that you do &lt;strong&gt;not&lt;/strong&gt; actually place the block. Queries are separate.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Return a boolean array &lt;code&gt;results&lt;/code&gt;, where &lt;code&gt;results[i]&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; if you can place the block specified in the &lt;code&gt;iᵗʰ&lt;/code&gt; query of type 2, and &lt;code&gt;false&lt;/code&gt; otherwise.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; queries = [[1,2],[2,3,3],[2,3,1],[2,2,2]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [false,true,true]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&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%2Fsmaowxvz2lhtiocbd7yz.png" alt="example0block" width="309" height="129"&gt;
For query 0, place an obstacle at &lt;code&gt;x = 2&lt;/code&gt;. A block of size at most 2 can be placed before &lt;code&gt;x = 3&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; queries = [[1,7],[2,7,6],[1,2],[2,7,5],[2,7,6]]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [true,true,false]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&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%2Fwe355hlno5x1vtj9zwz8.png" alt="example1block" width="310" height="130"&gt;

&lt;ul&gt;
&lt;li&gt;Place an obstacle at &lt;code&gt;x = 7&lt;/code&gt; for query 0. A block of size at most 7 can be placed before &lt;code&gt;x = 7&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Place an obstacle at &lt;code&gt;x = 2&lt;/code&gt; for query 2. Now, a block of size at most 5 can be placed before &lt;code&gt;x = 7&lt;/code&gt;, and a block of size at most 2 before &lt;code&gt;x = 2&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= queries.length &amp;lt;= 15 * 10⁴&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;2 &amp;lt;= queries[i].length &amp;lt;= 3&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= queries[i][0] &amp;lt;= 2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= x, sz &amp;lt;= min(5 * 10⁴, 3 * queries.length)&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;The input is generated such that for queries of type 1, no obstacle exists at distance &lt;code&gt;x&lt;/code&gt; when the query is asked.&lt;/li&gt;
&lt;li&gt;The input is generated such that there is at least one query of type 2.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Let &lt;code&gt;d[x]&lt;/code&gt; be the distance of the next obstacle after &lt;code&gt;x&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For each query of type 2, we just need to check if &lt;code&gt;max(d[0], d[1], d[2], …d[x - sz]) &amp;gt; sz&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Use segment tree to maintain &lt;code&gt;d[x]&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The problem involves handling two types of queries on an infinite positive number line:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Type 1:&lt;/strong&gt; Place an obstacle at a given coordinate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type 2:&lt;/strong&gt; Check if a block of a given size can fit entirely within a range &lt;code&gt;[0, x]&lt;/code&gt; without intersecting any obstacles (touching is allowed).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The solution uses a &lt;strong&gt;segment tree&lt;/strong&gt; to maintain the largest gap starting at each position between obstacles, and a sorted list of obstacles to efficiently update and query gaps. It answers each query in &lt;strong&gt;O(log n)&lt;/strong&gt; time.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Segment tree&lt;/strong&gt; stores, for each position &lt;code&gt;i&lt;/code&gt;, the size of the gap starting at &lt;code&gt;i&lt;/code&gt; (i.e., the distance to the next obstacle to the right).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Obstacles list&lt;/strong&gt; is kept sorted to allow binary search for predecessor and successor.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sentinel obstacle&lt;/strong&gt; at &lt;code&gt;0&lt;/code&gt; simplifies gap tracking.&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;type 2 queries&lt;/strong&gt;, we first check the partial gap from the last obstacle to &lt;code&gt;x&lt;/code&gt;. If that fits, answer is &lt;code&gt;true&lt;/code&gt;. Otherwise, we query the segment tree for the maximum gap starting anywhere in &lt;code&gt;[0, x - sz]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;For &lt;strong&gt;type 1 queries&lt;/strong&gt;, we find predecessor and successor obstacles, update the gaps for both ends, and insert the new obstacle into the sorted list.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003161-block-placement-queries" rel="noopener noreferrer"&gt;3161. Block Placement Queries&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[][] $queries
 * @return Boolean[]
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getResults&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$queries&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;getResults&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                &lt;span class="c1"&gt;// Output: [false,true,true]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;getResults&lt;/span&gt;&lt;span class="p"&gt;([[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;],[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;]])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// Output: [true,true,false]&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Initialization:&lt;/strong&gt; Start with obstacle at &lt;code&gt;0&lt;/code&gt; and a large initial gap from &lt;code&gt;0&lt;/code&gt; to &lt;code&gt;maxVal&lt;/code&gt; (an upper bound slightly larger than any given coordinate).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Type 1 (add obstacle at &lt;code&gt;p&lt;/code&gt;):&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Find predecessor &lt;code&gt;L&lt;/code&gt; and successor &lt;code&gt;R&lt;/code&gt; using binary search on sorted obstacles list.&lt;/li&gt;
&lt;li&gt;Update segment tree at &lt;code&gt;L&lt;/code&gt; with new gap &lt;code&gt;p - L&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Update segment tree at &lt;code&gt;p&lt;/code&gt; with new gap &lt;code&gt;R - p&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Insert &lt;code&gt;p&lt;/code&gt; into sorted obstacles.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Type 2 (query for &lt;code&gt;sz&lt;/code&gt; in &lt;code&gt;[0, x]&lt;/code&gt;):&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;If &lt;code&gt;sz &amp;gt; x&lt;/code&gt;, immediately return &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Find last obstacle &lt;code&gt;lastObs ≤ x&lt;/code&gt; using binary search.&lt;/li&gt;
&lt;li&gt;If &lt;code&gt;x - lastObs ≥ sz&lt;/code&gt;, return &lt;code&gt;true&lt;/code&gt; (fits in rightmost gap).&lt;/li&gt;
&lt;li&gt;Otherwise, query segment tree for max gap in &lt;code&gt;[0, x - sz]&lt;/code&gt;. If that max gap ≥ &lt;code&gt;sz&lt;/code&gt;, return &lt;code&gt;true&lt;/code&gt;, else &lt;code&gt;false&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Segment tree operations:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;set(idx, val)&lt;/code&gt; updates position &lt;code&gt;idx&lt;/code&gt; with new gap size.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;max(l, r)&lt;/code&gt; returns largest gap size in range &lt;code&gt;[l, r]&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Type 1: Binary search + segment tree updates → &lt;strong&gt;O(log n)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Type 2: Binary search + segment tree query → &lt;strong&gt;O(log n)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Total: &lt;strong&gt;O(q log n)&lt;/strong&gt; where &lt;code&gt;q&lt;/code&gt; is number of queries, &lt;code&gt;n&lt;/code&gt; is range size.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Space Complexity&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Segment tree: &lt;strong&gt;O(n)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Obstacles list: &lt;strong&gt;O(q)&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Total: &lt;strong&gt;O(n + q)&lt;/strong&gt; where &lt;code&gt;n = maxVal&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3300. Minimum Element After Replacement With Digit Sum</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Fri, 29 May 2026 16:44:19 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3300-minimum-element-after-replacement-with-digit-sum-hal</link>
      <guid>https://dev.to/mdarifulhaque/3300-minimum-element-after-replacement-with-digit-sum-hal</guid>
      <description>&lt;p&gt;3300. Minimum Element After Replacement With Digit Sum&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Easy&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Mid Level&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;Math&lt;/code&gt;, &lt;code&gt;Biweekly Contest 140&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given an integer array &lt;code&gt;nums&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;You replace each element in &lt;code&gt;nums&lt;/code&gt; with the &lt;strong&gt;sum&lt;/strong&gt; of its digits.&lt;/p&gt;

&lt;p&gt;Return the &lt;strong&gt;minimum&lt;/strong&gt; element in &lt;code&gt;nums&lt;/code&gt; after all replacements.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [10,12,13,14]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[1, 3, 4, 5]&lt;/code&gt; after all replacements, with minimum element 1.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [1,2,3,4]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[1, 2, 3, 4]&lt;/code&gt; after all replacements, with minimum element 1.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; nums = [999,19,199]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 10&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; &lt;code&gt;nums&lt;/code&gt; becomes &lt;code&gt;[27, 10, 19]&lt;/code&gt; after all replacements, with minimum element 10.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums.length &amp;lt;= 100&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= nums[i] &amp;lt;= 10⁴&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Convert to string and calculate the sum for each element.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The solution replaces each element in the array with the sum of its digits, then returns the smallest resulting value. This is done without storing the transformed array, keeping memory usage low.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Iterate through each number in the input array.&lt;/li&gt;
&lt;li&gt;Compute the digit sum for the current number.&lt;/li&gt;
&lt;li&gt;Track the minimum digit sum found so far.&lt;/li&gt;
&lt;li&gt;Return the minimum value after processing all numbers.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003300-minimum-element-after-replacement-with-digit-sum" rel="noopener noreferrer"&gt;3300. Minimum Element After Replacement With Digit Sum&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param Integer[] $nums
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;minElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$nums&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minElement&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;13&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minElement&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;              &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;minElement&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="mi"&gt;999&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;19&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;199&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;           &lt;span class="c1"&gt;// Output: 10&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Digit sum calculation&lt;/strong&gt; is performed using repeated modulus and integer division.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No auxiliary array&lt;/strong&gt; is created — only the running minimum is stored.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Comparison and update&lt;/strong&gt; happen in the same loop, making the solution efficient.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n * d)&lt;/strong&gt;&lt;/em&gt; — where &lt;code&gt;n&lt;/code&gt; is the array length and &lt;code&gt;d&lt;/code&gt; is the number of digits in the largest number (max 5 digits since nums[i] ≤ 10⁴).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; — only a few integer variables are used.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3093. Longest Common Suffix Queries</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Thu, 28 May 2026 16:46:39 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3093-longest-common-suffix-queries-2pm2</link>
      <guid>https://dev.to/mdarifulhaque/3093-longest-common-suffix-queries-2pm2</guid>
      <description>&lt;p&gt;3093. Longest Common Suffix Queries&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Hard&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Senior Staff&lt;/code&gt;, &lt;code&gt;Array&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Trie&lt;/code&gt;, &lt;code&gt;Weekly Contest 390&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given two arrays of strings &lt;code&gt;wordsContainer&lt;/code&gt; and &lt;code&gt;wordsQuery&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For each &lt;code&gt;wordsQuery[i]&lt;/code&gt;, you need to find a string from &lt;code&gt;wordsContainer&lt;/code&gt; that has the &lt;strong&gt;longest common suffix&lt;/strong&gt; with &lt;code&gt;wordsQuery[i]&lt;/code&gt;. If there are two or more strings in &lt;code&gt;wordsContainer&lt;/code&gt; that share the longest common suffix, find the string that is the &lt;strong&gt;smallest&lt;/strong&gt; in length. If there are two or more such strings that have the &lt;strong&gt;same&lt;/strong&gt; smallest length, find the one that occurred &lt;strong&gt;earlier&lt;/strong&gt; in &lt;code&gt;wordsContainer&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return &lt;em&gt;an array of integers &lt;code&gt;ans&lt;/code&gt;, where &lt;code&gt;ans[i]&lt;/code&gt; is the index of the string in &lt;code&gt;wordsContainer&lt;/code&gt; that has the &lt;strong&gt;longest common suffix&lt;/strong&gt; with &lt;code&gt;wordsQuery[i]&lt;/code&gt;&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; wordsContainer = ["abcd","bcd","xbcd"], wordsQuery = ["cd","bcd","xyz"]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [1,1,1]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Let's look at each &lt;code&gt;wordsQuery[i]&lt;/code&gt; separately:

&lt;ul&gt;
&lt;li&gt;For &lt;code&gt;wordsQuery[0] = "cd"&lt;/code&gt;, strings from &lt;code&gt;wordsContainer&lt;/code&gt; that share the longest common suffix &lt;code&gt;"cd"&lt;/code&gt; are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;wordsQuery[1] = "bcd"&lt;/code&gt;, strings from &lt;code&gt;wordsContainer&lt;/code&gt; that share the longest common suffix &lt;code&gt;"bcd"&lt;/code&gt; are at indices 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.&lt;/li&gt;
&lt;li&gt;For &lt;code&gt;wordsQuery[2] = "xyz"&lt;/code&gt;, there is no string from &lt;code&gt;wordsContainer&lt;/code&gt; that shares a common suffix. Hence the longest common suffix is &lt;code&gt;""&lt;/code&gt;, that is shared with strings at index 0, 1, and 2. Among these, the answer is the string at index 1 because it has the shortest length of 3.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; wordsContainer = ["abcdefgh","poiuygh","ghghgh"], wordsQuery = ["gh","acbfgh","acbfegh"]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [2,0,2]&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt;

&lt;ul&gt;
&lt;li&gt;Let's look at each &lt;code&gt;wordsQuery[i]&lt;/code&gt; separately:

&lt;ul&gt;
&lt;li&gt;For &lt;code&gt;wordsQuery[0] = "gh"&lt;/code&gt;, strings from wordsContainer that share the longest common suffix &lt;code&gt;"gh"&lt;/code&gt; are at indices 0, 1, and 2. Among these, the answer is the string at index 2 because it has the shortest length of 6.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= wordsContainer.length, wordsQuery.length &amp;lt;= 10⁴&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= wordsContainer[i].length &amp;lt;= 5 * 10³&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= wordsQuery[i].length &amp;lt;= 5 * 10³&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;wordsContainer[i]&lt;/code&gt; consists only of lowercase English letters.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;wordsQuery[i]&lt;/code&gt; consists only of lowercase English letters.&lt;/li&gt;
&lt;li&gt;Sum of &lt;code&gt;wordsContainer[i].length&lt;/code&gt; is at most &lt;code&gt;5 * 1⁵&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Sum of &lt;code&gt;wordsQuery[i].length&lt;/code&gt; is at most &lt;code&gt;5 * 10⁵&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;If we reverse the strings, the problem changes to finding the longest common prefix.&lt;/li&gt;
&lt;li&gt;Build a Trie, each node is a letter and only saves the best word’s index in each node, based on the criteria.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The problem requires finding, for each query string, the index of a container string that shares the &lt;strong&gt;longest common suffix&lt;/strong&gt; with the query.&lt;br&gt;&lt;br&gt;
Tie-breaking rules:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Prefer the &lt;strong&gt;shortest&lt;/strong&gt; string among those with the longest common suffix.&lt;/li&gt;
&lt;li&gt;If lengths are equal, prefer the &lt;strong&gt;earliest&lt;/strong&gt; index in &lt;code&gt;wordsContainer&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The solution &lt;strong&gt;reverses all strings&lt;/strong&gt; and builds a &lt;strong&gt;trie&lt;/strong&gt; on the reversed container strings, storing the best (shortest length, earliest index) string at each node.&lt;br&gt;&lt;br&gt;
Each query is reversed and traversed in the trie; the last reachable node gives the best container index.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Reverse strings&lt;/strong&gt; to convert the problem into a &lt;strong&gt;longest common prefix&lt;/strong&gt; problem in a trie.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Build a trie&lt;/strong&gt; from reversed container strings.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Store in each trie node&lt;/strong&gt; the best container word index based on:

&lt;ul&gt;
&lt;li&gt;Primary: shortest length of original word&lt;/li&gt;
&lt;li&gt;Secondary: smallest index if lengths are equal&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Process each query&lt;/strong&gt; by reversing it and walking the trie as far as possible.&lt;/li&gt;
&lt;li&gt;The &lt;strong&gt;best index&lt;/strong&gt; at the final node is the answer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003093-longest-common-suffix-queries" rel="noopener noreferrer"&gt;3093. Longest Common Suffix Queries&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param String[] $wordsContainer
 * @param String[] $wordsQuery
 * @return Integer[]
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;stringIndices&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$wordsContainer&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt; &lt;span class="nv"&gt;$wordsQuery&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;array&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;


&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;stringIndices&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;"abcd"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"bcd"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"xbcd"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"cd"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"bcd"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"xyz"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                           &lt;span class="c1"&gt;// Output: [1,1,1]&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;stringIndices&lt;/span&gt;&lt;span class="p"&gt;([&lt;/span&gt;&lt;span class="s2"&gt;"abcdefgh"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"poiuygh"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"ghghgh"&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s2"&gt;"gh"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"acbfgh"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="s2"&gt;"acbfegh"&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;          &lt;span class="c1"&gt;// Output: [2,0,2]&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Step 1 — Reverse strings&lt;/strong&gt; Reversing turns "longest common suffix" into "longest common prefix" in a trie.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Step 2 — Initialize trie and root node&lt;/strong&gt; Root node’s best index/length are updated from all container words, since an empty suffix is always valid.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;&lt;strong&gt;Step 3 — Insert each container word (reversed) into trie&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;At each node (including root), check if the current word’s length is shorter than stored best length, or equal length but earlier index → update best.&lt;/li&gt;
&lt;li&gt;This ensures later queries can quickly get the best match for any prefix.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;&lt;strong&gt;Step 4 — Query processing&lt;/strong&gt;  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reverse query string, walk down trie character by character.
&lt;/li&gt;
&lt;li&gt;Stop when a character is missing in children.
&lt;/li&gt;
&lt;li&gt;The last successfully reached node’s stored best index is the answer.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Step 5 — Complexity handling&lt;/strong&gt; Total container + query length is large (up to ~5×10⁵), so trie traversal is efficient.&lt;/p&gt;&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Building trie: &lt;em&gt;&lt;strong&gt;O(∑ |wordsContainer[i]|)&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Query processing: &lt;em&gt;&lt;strong&gt;O(∑ |wordsQuery[i]|)&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Total: &lt;em&gt;&lt;strong&gt;O(total length of all strings)&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Space Complexity&lt;/strong&gt;:

&lt;ul&gt;
&lt;li&gt;Trie nodes: &lt;em&gt;&lt;strong&gt;O(∑ |wordsContainer[i]|)&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;li&gt;Each node stores children map and two integers.&lt;/li&gt;
&lt;li&gt;Total: &lt;em&gt;&lt;strong&gt;O(total length of all container strings)&lt;/strong&gt;&lt;/em&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3121. Count the Number of Special Characters II</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Wed, 27 May 2026 15:36:29 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3121-count-the-number-of-special-characters-ii-2bo3</link>
      <guid>https://dev.to/mdarifulhaque/3121-count-the-number-of-special-characters-ii-2bo3</guid>
      <description>&lt;p&gt;3121. Count the Number of Special Characters II&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Medium&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Senior&lt;/code&gt;, &lt;code&gt;Hash Table&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Weekly Contest 394&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given a string &lt;code&gt;word&lt;/code&gt;. A letter &lt;code&gt;c&lt;/code&gt; is called &lt;strong&gt;special&lt;/strong&gt; if it appears &lt;strong&gt;both&lt;/strong&gt; in lowercase and uppercase in &lt;code&gt;word&lt;/code&gt;, and &lt;code&gt;every&lt;/code&gt; lowercase occurrence of &lt;code&gt;c&lt;/code&gt; appears before the &lt;strong&gt;first&lt;/strong&gt; uppercase occurrence of &lt;code&gt;c&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return the number of &lt;strong&gt;special&lt;/strong&gt; letters in &lt;code&gt;word&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; word = "aaAbcBC"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The special characters are &lt;code&gt;'a'&lt;/code&gt;, &lt;code&gt;'b'&lt;/code&gt;, and &lt;code&gt;'c'&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; word = "abc"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There are no special characters in &lt;code&gt;word&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Input:&lt;/strong&gt; "AbBCab"&lt;br&gt;
&lt;strong&gt;Output:&lt;/strong&gt; [0,1]&lt;br&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; There are no special characters in &lt;code&gt;word&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= word.length &amp;lt;= 2 * 10&lt;sup&gt;5&lt;/sup&gt;&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;word&lt;/code&gt; consists of only lowercase and uppercase English letters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;For each character &lt;code&gt;c&lt;/code&gt;, store the first occurrence of its uppercase and the last occurrence of its lowercase.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The problem requires counting letters that appear in both lowercase and uppercase forms in a string, with the constraint that &lt;strong&gt;every lowercase occurrence&lt;/strong&gt; of a letter must appear before the &lt;strong&gt;first uppercase occurrence&lt;/strong&gt; of the same letter.&lt;br&gt;&lt;br&gt;
The solution tracks the &lt;strong&gt;first occurrence&lt;/strong&gt; of each uppercase letter and the &lt;strong&gt;last occurrence&lt;/strong&gt; of each lowercase letter, then compares their indices.&lt;/p&gt;
&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Use two arrays to store the &lt;strong&gt;first occurrence index&lt;/strong&gt; of uppercase letters and the &lt;strong&gt;last occurrence index&lt;/strong&gt; of lowercase letters.&lt;/li&gt;
&lt;li&gt;Iterate through the string once, updating these arrays.&lt;/li&gt;
&lt;li&gt;For each letter &lt;code&gt;a..z&lt;/code&gt;, check if:

&lt;ul&gt;
&lt;li&gt;Both its lowercase and uppercase forms exist in the string.&lt;/li&gt;
&lt;li&gt;The last lowercase occurrence occurs &lt;strong&gt;before&lt;/strong&gt; the first uppercase occurrence.&lt;/li&gt;
&lt;/ul&gt;
&lt;/li&gt;
&lt;li&gt;If both conditions are met, count it as a &lt;strong&gt;special character&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003121-count-the-number-of-special-characters-ii" rel="noopener noreferrer"&gt;3121. Count the Number of Special Characters II&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param String $word
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;numberOfSpecialChars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$word&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;numberOfSpecialChars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"aaAbcBC"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;numberOfSpecialChars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"abc"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 0&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;numberOfSpecialChars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"AbBCab"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;         &lt;span class="c1"&gt;// Output: 0&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;first&lt;/code&gt; array stores the first occurrence index for each character (both cases).&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;last&lt;/code&gt; array stores the last occurrence index for each character.&lt;/li&gt;
&lt;li&gt;During traversal:

&lt;ul&gt;
&lt;li&gt;If a character hasn’t been seen before, set &lt;code&gt;first[char]&lt;/code&gt; to current index.&lt;/li&gt;
&lt;li&gt;Always update &lt;code&gt;last[char]&lt;/code&gt; to current index.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;After traversal, check each lowercase letter &lt;code&gt;i&lt;/code&gt;:

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;last['a' + i]&lt;/code&gt; → last index of lowercase.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;first['A' + i]&lt;/code&gt; → first index of uppercase.&lt;/li&gt;
&lt;li&gt;Condition: &lt;code&gt;last[lower] &amp;lt; first[upper]&lt;/code&gt; ensures all lowercase occur before any uppercase.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Count how many satisfy this.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt; — single pass through the string, then &lt;code&gt;O(26)&lt;/code&gt; for checking letters.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt; — fixed-size arrays for ASCII characters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <item>
      <title>3120. Count the Number of Special Characters I</title>
      <dc:creator>MD ARIFUL HAQUE</dc:creator>
      <pubDate>Tue, 26 May 2026 16:30:47 +0000</pubDate>
      <link>https://dev.to/mdarifulhaque/3120-count-the-number-of-special-characters-i-248f</link>
      <guid>https://dev.to/mdarifulhaque/3120-count-the-number-of-special-characters-i-248f</guid>
      <description>&lt;p&gt;3120. Count the Number of Special Characters I&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Difficulty:&lt;/strong&gt; Easy&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Topics:&lt;/strong&gt; &lt;code&gt;Mid Level&lt;/code&gt;, &lt;code&gt;Hash Table&lt;/code&gt;, &lt;code&gt;String&lt;/code&gt;, &lt;code&gt;Weekly Contest 394&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;You are given a string &lt;code&gt;word&lt;/code&gt;. A letter is called &lt;strong&gt;special&lt;/strong&gt; if it appears &lt;strong&gt;both&lt;/strong&gt; in lowercase and uppercase in &lt;code&gt;word&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Return the number of &lt;strong&gt;special&lt;/strong&gt; letters in &lt;code&gt;word&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example 1:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; word = "aaAbcBC"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 3&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The special characters in &lt;code&gt;word&lt;/code&gt; are &lt;code&gt;'a'&lt;/code&gt;, &lt;code&gt;'b'&lt;/code&gt;, and &lt;code&gt;'c'&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 2:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; word = "abc"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 0&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; No character in &lt;code&gt;word&lt;/code&gt; appears in uppercase.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Example 3:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Input:&lt;/strong&gt; word = "abBCab"&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Output:&lt;/strong&gt; 1&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Explanation:&lt;/strong&gt; The only special character in &lt;code&gt;word&lt;/code&gt; is &lt;code&gt;'b'&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Constraints:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;1 &amp;lt;= word.length &amp;lt;= 50&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;word&lt;/code&gt; consists of only lowercase and uppercase English letters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Hint:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The constraints are small. For all 52 characters, check if they are present in &lt;code&gt;word&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Solution:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The solution determines how many English letters appear in both lowercase and uppercase forms within a given string. It uses two boolean arrays to track the presence of each letter (a–z) in lowercase and uppercase separately, then counts the letters that appear in both arrays.&lt;/p&gt;

&lt;h3&gt;
  
  
  Approach:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Track lowercase and uppercase letters separately&lt;/strong&gt; using two fixed-size boolean arrays (size 26).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Iterate through the string&lt;/strong&gt; and mark the appropriate array based on whether the character is lowercase or uppercase.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Count matches&lt;/strong&gt; by checking both arrays for the same letter index.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's implement this solution in PHP: &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php/tree/main/algorithms/003120-count-the-number-of-special-characters-i" rel="noopener noreferrer"&gt;3120. Count the Number of Special Characters I&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;?php&lt;/span&gt;
&lt;span class="cd"&gt;/**
 * @param String $word
 * @return Integer
 */&lt;/span&gt;
&lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;numberOfSpecialChars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt; &lt;span class="nv"&gt;$word&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="mf"&gt;...&lt;/span&gt;
    &lt;span class="cd"&gt;/**
     * go to ./solution.php
     */&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Test cases&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;numberOfSpecialChars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"aaAbcBC"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;            &lt;span class="c1"&gt;// Output: 3&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;numberOfSpecialChars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"abc"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;                &lt;span class="c1"&gt;// Output: 0&lt;/span&gt;
&lt;span class="k"&gt;echo&lt;/span&gt; &lt;span class="nf"&gt;numberOfSpecialChars&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;"abBCab"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="mf"&gt;.&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;             &lt;span class="c1"&gt;// Output: 1&lt;/span&gt;
&lt;span class="cp"&gt;?&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Explanation:
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Initialize two arrays&lt;/strong&gt; &lt;code&gt;$lower&lt;/code&gt; and &lt;code&gt;$upper&lt;/code&gt; of size 26, all set to &lt;code&gt;false&lt;/code&gt;. Index 0 represents &lt;code&gt;'a'&lt;/code&gt;/&lt;code&gt;'A'&lt;/code&gt;, index 1 represents &lt;code&gt;'b'&lt;/code&gt;/&lt;code&gt;'B'&lt;/code&gt;, etc.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Loop through each character&lt;/strong&gt; in the string:

&lt;ul&gt;
&lt;li&gt;If the character is lowercase, mark &lt;code&gt;$lower[index] = true&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;If the character is uppercase, mark &lt;code&gt;$upper[index] = true&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;After processing&lt;/strong&gt; the string, loop through indexes 0 to 25:

&lt;ul&gt;
&lt;li&gt;If both &lt;code&gt;$lower[i]&lt;/code&gt; and &lt;code&gt;$upper[i]&lt;/code&gt; are &lt;code&gt;true&lt;/code&gt;, increment the count.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;

&lt;strong&gt;Return the total count&lt;/strong&gt; of special characters.&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  Complexity
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(n)&lt;/strong&gt;&lt;/em&gt;, One pass through the string (n = length of &lt;code&gt;word&lt;/code&gt;) and one pass through the 26 letters → O(n + 26) → O(n).&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Space Complexity&lt;/strong&gt;: &lt;em&gt;&lt;strong&gt;O(1)&lt;/strong&gt;&lt;/em&gt;, Two fixed-size arrays of 26 booleans → constant space.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Contact Links&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you found this series helpful, please consider giving the &lt;strong&gt;&lt;a href="https://github.com/mah-shamim/leet-code-in-php" rel="noopener noreferrer"&gt;repository&lt;/a&gt;&lt;/strong&gt; a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me&lt;a href="https://chaindoorman.com/hzk8jsphf8?key=5ba736283dafd7f94a84865e3cc3d775" rel="noopener noreferrer"&gt;!&lt;/a&gt;&lt;br&gt;
&lt;a href="https://buymeacoffee.com/mah.shamim" rel="noopener noreferrer"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fcdn.buymeacoffee.com%2Fbuttons%2Fv2%2Fdefault-yellow.png" alt="Buy Me A Coffee" width="545" height="153"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;If you want more helpful content like this, feel free to follow me:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://www.linkedin.com/in/arifulhaque/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;&lt;a href="https://github.com/mah-shamim" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>php</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
  </channel>
</rss>
