<?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: Shiki Endou</title>
    <description>The latest articles on DEV Community by Shiki Endou (@shikky).</description>
    <link>https://dev.to/shikky</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%2F852854%2F9004d26f-27fe-4276-b242-4404f572fad0.jpg</url>
      <title>DEV Community: Shiki Endou</title>
      <link>https://dev.to/shikky</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/shikky"/>
    <language>en</language>
    <item>
      <title>Solving the 'Container With Most Water' on LeetCode with C++</title>
      <dc:creator>Shiki Endou</dc:creator>
      <pubDate>Thu, 10 Aug 2023 02:32:27 +0000</pubDate>
      <link>https://dev.to/shikky/solving-the-container-with-most-water-on-leetcode-with-c-igl</link>
      <guid>https://dev.to/shikky/solving-the-container-with-most-water-on-leetcode-with-c-igl</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/container-with-most-water/description/"&gt;https://leetcode.com/problems/container-with-most-water/description/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Answer
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    int maxArea(vector&amp;lt;int&amp;gt;&amp;amp; height) {
        int maxArea = 0;
        int left = 0;
        int right = height.size() - 1;

        while (left &amp;lt; right) {
            int h = min(height[left], height[right]);
            int w = right - left;
            maxArea = max(maxArea, h * w);

            if (height[left] &amp;lt; height[right]) {
                left++;
            } else {
                right--;
            }
        }

        return maxArea;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's consider a solution to this problem.&lt;/p&gt;

&lt;p&gt;We want to find a container that can hold the most water. This is calculated as the maximum value derived from multiplying the height and width (between two bars).&lt;/p&gt;

&lt;p&gt;How do we find the height and width?&lt;/p&gt;

&lt;p&gt;The maximum width (distance between two bars) is between the first and last bars.&lt;/p&gt;

&lt;p&gt;For example, in the array [3, 2, 4, 2, 5], the first bar (index 0) and the last bar (index 4) offer the maximum width.&lt;/p&gt;

&lt;p&gt;So we begin finding a container with the most water from both ends.&lt;/p&gt;

&lt;p&gt;We then move to the next index. However, we are unsure which index to move to.&lt;/p&gt;

&lt;p&gt;We move the index with the smaller height to the next one. This is because we aim to find a container with the most water, hence we should continue selecting a taller height.&lt;/p&gt;

&lt;p&gt;For example, in the array [3, 2, 4, 2, 5], the first bar (index 0) and last bar (index 4) provide the maximum width. The container size is 12 (height 3 * width 4), and the height used is determined by the shorter bar.&lt;/p&gt;

&lt;p&gt;Then, the indices are 1 (height 2) and 4 (height 5) because we should continue to select a taller height. The container size is 6 (height 2 * width 3).&lt;/p&gt;

&lt;p&gt;The next indices are 2 (height 4) and 4 (height 5). The container size is 8 (height 4 * width 2).&lt;/p&gt;

&lt;p&gt;The final indices are 3 (height 2) and 4 (height 5). The container size is 2 (height 2 * width 1).&lt;/p&gt;

&lt;p&gt;The container that holds the most water has a size of 12 (when indices are 0 and 4).&lt;/p&gt;

&lt;p&gt;We converts the above algorithm to code.&lt;br&gt;
I repost the answer code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    int maxArea(vector&amp;lt;int&amp;gt;&amp;amp; height) {
        int maxArea = 0;
        int left = 0;
        int right = height.size() - 1;

        while (left &amp;lt; right) {
            int h = min(height[left], height[right]);
            int w = right - left;
            maxArea = max(maxArea, h * w);

            if (height[left] &amp;lt; height[right]) {
                left++;
            } else {
                right--;
            }
        }

        return maxArea;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        int maxArea = 0;
        int left = 0;
        int right = height.size() - 1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code starts from the first and last indices because the width is at its maximum.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        while (left &amp;lt; right) {

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

&lt;/div&gt;



&lt;p&gt;We continue comparing containers until the left index is less than or equal to the right index.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        while (left &amp;lt; right) {
            int h = min(height[left], height[right]);
            int w = right - left;
            maxArea = max(maxArea, h * w);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The 'h' variable takes the smaller height from the left or right index, because the container height is determined by the shorter bar.&lt;/p&gt;

&lt;p&gt;The 'w' variable is obtained by subtracting the left index from the right one.&lt;/p&gt;

&lt;p&gt;The 'maxArea' variable stores the larger area between the old and the new one.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            if (height[left] &amp;lt; height[right]) {
                left++;
            } else {
                right--;
            }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After calculating the area from the height and width, we move the index that has the smaller height because a larger height could yield a larger area.&lt;/p&gt;

&lt;p&gt;That's all.&lt;/p&gt;

</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Solving the 'Palindrome Number' on LeetCode with C++</title>
      <dc:creator>Shiki Endou</dc:creator>
      <pubDate>Wed, 02 Aug 2023 09:08:18 +0000</pubDate>
      <link>https://dev.to/shikky/solving-the-palindrome-number-on-leetcode-with-c-4jf3</link>
      <guid>https://dev.to/shikky/solving-the-palindrome-number-on-leetcode-with-c-4jf3</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/palindrome-number/"&gt;https://leetcode.com/problems/palindrome-number/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Answer
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    bool isPalindrome(int x) {
        if (x &amp;lt; 0) return false;

        long reversedX = 0;
        int originalX = x;
        while (x != 0) {
            reversedX = reversedX * 10 + x % 10;
            x /= 10;
        }

        return originalX == reversedX;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's consider a solution to this problem.&lt;/p&gt;

&lt;p&gt;We want to know whether an integer is a palindrome or not.&lt;/p&gt;

&lt;p&gt;It's simple. If an integer and its reversed form are the same, we know the integer is a palindrome.&lt;/p&gt;

&lt;p&gt;Let's analyze the solution as code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        if (x &amp;lt; 0) return false;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Firstly, if the integer is negative, it's not a palindrome.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        long reversedX = 0;
        int originalX = x;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We define a long variable, reversedX, because it might exceed -2^31 or 2^31.&lt;/p&gt;

&lt;p&gt;We define an int variable, originalX, because the value of x will change during the subsequent processes.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        while (x != 0) {
            reversedX = reversedX * 10 + x % 10;
            x /= 10;
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We want to reverse x. x % 10 gets the last digit of the integer.&lt;br&gt;
For example, if x is 123, 123 % 10 gives us 3.&lt;br&gt;
Then, we don't need the 3 of 123 and remove it using x /= 10.&lt;/p&gt;

&lt;p&gt;This process continues until x is 0.&lt;/p&gt;

&lt;p&gt;In the second loop, reversedX already has a value, so we need to add reversedX * 10 to x % 10.&lt;br&gt;
For example, if x is 12 and reversedX is 3, 3 * 10 + 12 % 10 equals 30 + 2 = 32.&lt;/p&gt;

&lt;p&gt;If we continue this process until x is 0, we will have reversed the integer.&lt;/p&gt;

&lt;p&gt;Lastly, we can determine whether the original integer is a palindrome by comparing reversedX and originalX.&lt;/p&gt;

</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Solving the 'String to Integer (atoi)' on LeetCode with C++</title>
      <dc:creator>Shiki Endou</dc:creator>
      <pubDate>Tue, 25 Jul 2023 10:50:17 +0000</pubDate>
      <link>https://dev.to/shikky/solving-the-string-to-integer-atoi-on-leetcode-with-c-9g4</link>
      <guid>https://dev.to/shikky/solving-the-string-to-integer-atoi-on-leetcode-with-c-9g4</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/string-to-integer-atoi/"&gt;https://leetcode.com/problems/string-to-integer-atoi/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Answer
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;string&amp;gt;
#include &amp;lt;climits&amp;gt;

class Solution {
public:
    int myAtoi(string s) {
        int i = 0, sign = 1, result = 0;

        // Skip whitespaces
        while (i &amp;lt; s.size() &amp;amp;&amp;amp; s[i] == ' ') {
            i++;
        }

        // Check the sign
        if (i &amp;lt; s.size() &amp;amp;&amp;amp; (s[i] == '-' || s[i] == '+')) {
            sign = (s[i++] == '-') ? -1 : 1;
        }

        // Read in the digits and check for overflow/underflow condition
        while (i &amp;lt; s.size() &amp;amp;&amp;amp; isdigit(s[i])) {
            if (result &amp;gt; INT_MAX / 10 || (result == INT_MAX / 10 &amp;amp;&amp;amp; s[i] - '0' &amp;gt; INT_MAX % 10)) {
                return (sign == 1) ? INT_MAX : INT_MIN;
            }
            result = result * 10 + (s[i++] - '0');
        }

        return result * sign;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's analyze the solution to the problem.&lt;/p&gt;

&lt;p&gt;Assume the input is " -45string".&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;1.Read in and ignore any leading whitespace.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We need to convert " -45string" to "-45string", resulting in "-45string".&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;2.Check if the next character (if not already at the end of the string) is '-' or '+'. Read this character in if it is either. This determines if the final result is negative or positive respectively. Assume the result is positive if neither is present.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We need to check whether a sign is positive or negative.&lt;br&gt;
The '-' sign is temporarily stored and the input becomes "45string".&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Read in next the characters until the next non-digit character or the end of the input is reached. The rest of the string is ignored.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We check each character in order, until a non-digit character is encountered.&lt;/p&gt;

&lt;p&gt;The input becomes "45".&lt;/p&gt;

&lt;p&gt;Finally, we convert the string to an integer and add the sign (positive or negative).&lt;/p&gt;

&lt;p&gt;The final output is -45.&lt;/p&gt;

&lt;p&gt;Additionally, we must ensure that the integer does not exceed the limits of INT_MAX and INT_MIN.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;To adhere to the above constraints, we add conditions to the code.&lt;/p&gt;

&lt;p&gt;Let's break down the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;#include &amp;lt;string&amp;gt;
#include &amp;lt;climits&amp;gt;

class Solution {
public:
    int myAtoi(string s) {
        int i = 0, sign = 1, result = 0;

        // Skip whitespaces
        while (i &amp;lt; s.size() &amp;amp;&amp;amp; s[i] == ' ') {
            i++;
        }

        // Check the sign
        if (i &amp;lt; s.size() &amp;amp;&amp;amp; (s[i] == '-' || s[i] == '+')) {
            sign = (s[i++] == '-') ? -1 : 1;
        }

        // Read in the digits and check for overflow/underflow condition
        while (i &amp;lt; s.size() &amp;amp;&amp;amp; isdigit(s[i])) {
            if (result &amp;gt; INT_MAX / 10 || (result == INT_MAX / 10 &amp;amp;&amp;amp; s[i] - '0' &amp;gt; INT_MAX % 10)) {
                return (sign == 1) ? INT_MAX : INT_MIN;
            }
            result = result * 10 + (s[i++] - '0');
        }

        return result * sign;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        while (i &amp;lt; s.size() &amp;amp;&amp;amp; s[i] == ' ') {
            i++;
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;blockquote&gt;
&lt;p&gt;Only the space character ' ' is considered a whitespace character.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We skip white spaces using &lt;code&gt;s[i] == ' '&lt;/code&gt; because the above constrain.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        if (i &amp;lt; s.size() &amp;amp;&amp;amp; (s[i] == '-' || s[i] == '+')) {
            sign = (s[i++] == '-') ? -1 : 1;
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the sign is stored as 1 or -1 and then multiplied by the result. This way, the sign is reflected in the result.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        while (i &amp;lt; s.size() &amp;amp;&amp;amp; isdigit(s[i])) {
            if (result &amp;gt; INT_MAX / 10 || (result == INT_MAX / 10 &amp;amp;&amp;amp; s[i] - '0' &amp;gt; INT_MAX % 10)) {
                return (sign == 1) ? INT_MAX : INT_MIN;
            }
            result = result * 10 + (s[i++] - '0');
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The while loop condition checks if i is less than s.size() and if s[i] is a digit.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If the integer is out of the 32-bit signed integer range [-231, 231 - 1], then clamp the integer so that it remains in the range. Specifically, integers less than -231 should be clamped to -231, and integers greater than 231 - 1 should be clamped to 231 - 1.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;We need to return INT_MAX or INT_MIN if result exceeds these limits.&lt;/p&gt;

&lt;p&gt;The condition result &amp;gt; INT_MAX / 10 will return INT_MAX or INT_MIN if the result is greater than INT_MAX / 10. This is due to the multiplication of result and 10 in the last line of the condition.&lt;/p&gt;

&lt;p&gt;The second condition result == INT_MAX / 10 &amp;amp;&amp;amp; s[i] - '0' &amp;gt; INT_MAX % 10 will return INT_MAX or INT_MIN if the result equals INT_MAX / 10 and s[i] is greater than INT_MAX % 10. This is because result * 10 is increased by s[i++] - '0'.&lt;/p&gt;

&lt;p&gt;Let's simplify the explanation:&lt;/p&gt;

&lt;p&gt;The condition result == INT_MAX / 10 &amp;amp;&amp;amp; s[i] - '0' &amp;gt; INT_MAX % 10 is checking if the current number (result) is about to exceed the maximum limit for an integer (INT_MAX).&lt;/p&gt;

&lt;p&gt;INT_MAX is the maximum value an integer can hold, which is 2147483647.&lt;/p&gt;

&lt;p&gt;The condition result == INT_MAX / 10 is checking if result is equal to the largest possible integer divided by 10, which is 214748364.&lt;/p&gt;

&lt;p&gt;The condition s[i] - '0' &amp;gt; INT_MAX % 10 is checking if the current digit of the string (where '0' is subtracted to convert from ASCII to numeric value) is greater than the remainder of INT_MAX divided by 10, which is 7.&lt;/p&gt;

&lt;p&gt;If result equals 214748364 (INT_MAX / 10) and the next digit is greater than 7, adding that digit to result would cause it to exceed the maximum value for an integer.&lt;/p&gt;

&lt;p&gt;For example, if result is 214748364 and the next digit is 8:&lt;/p&gt;

&lt;p&gt;Multiplying result by 10 gives 2147483640.&lt;/p&gt;

&lt;p&gt;Adding 8 (the next digit) gives 2147483648.&lt;/p&gt;

&lt;p&gt;2147483648 is greater than INT_MAX (2147483647), so it would exceed the limit.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;`result == INT_MAX / 10 &amp;amp;&amp;amp; s[i] - '0' &amp;gt; INT_MAX % 10`

result == INT_MAX / 10 
result == INT_MAX(2147483647) / 10 
result == 214748364

s[i] - '0' &amp;gt; INT_MAX % 10` 
s[i] - '0' &amp;gt; INT_MAX(2147483647) % 10 
s[i] - '0' &amp;gt; 7

The result will exceed INT_MAX if `result` equals INT_MAX / 10 (214748364) and `s[i] - '0'` is greater than 7.

For instance:
result = 214748364
s[i] = '8'

result * 10 + s[i++] - '0'
2147483640 + 8 
2147483648

The result 2147483648 exceeds INT_MAX.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Solving the 'Reverse Integer' on LeetCode with C++</title>
      <dc:creator>Shiki Endou</dc:creator>
      <pubDate>Thu, 20 Jul 2023 10:48:05 +0000</pubDate>
      <link>https://dev.to/shikky/solving-the-reverse-integer-on-leetcode-with-c-1nob</link>
      <guid>https://dev.to/shikky/solving-the-reverse-integer-on-leetcode-with-c-1nob</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/reverse-integer/description/"&gt;https://leetcode.com/problems/reverse-integer/description/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Answer
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    int reverse(int x) {
        long long result = 0;
        while(x) {
            result = result * 10 + x % 10;
            if (result &amp;gt; INT_MAX || result &amp;lt; INT_MIN) {
                return 0;
            }
            x /= 10;
        }
        return result;
    }
};

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

&lt;/div&gt;



&lt;p&gt;Let's examine the solution to the problem.&lt;br&gt;
For an example, consider the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: x = -123
Output: -321
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We solve the problem by taking the last digit in order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: x = -123
1: -3
2: -32
3: -321

Output: -321
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, let's dissect the code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    int reverse(int x) {
        long long result = 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;long long result = 0&lt;/code&gt; is used to store the answer. It might exceed the int range because we're reversing &lt;code&gt;x&lt;/code&gt;. Therefore, we use &lt;code&gt;long long&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        while(x) {
            result = result * 10 + x % 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As mentioned before, we solve the problem by taking the last digit in order.&lt;/p&gt;

&lt;p&gt;We use &lt;code&gt;x % 10&lt;/code&gt; to get the last digit. The variable &lt;code&gt;result&lt;/code&gt; will have a value starting from the second iteration. We use &lt;code&gt;result * 10&lt;/code&gt; to shift the digits one place to the left as we add the new digit to the rightmost place.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = -123
result = 0
Step 1: result = result * 10 + x % 10 = 0 * 10 + -123 % 10 = -3
x = -12 // explained later in this blog post.
result = -3
Step 2: result = result * 10 + x % 10 = - 3 * 10 + -12 % 10 = -32
x = -1
result = -32
Step 3: result = result * 10 + x % 10 = - 32 * 10 + -1 % 10 = -321
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            if (result &amp;gt; INT_MAX || result &amp;lt; INT_MIN) {
                return 0;
            }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;result&lt;/code&gt; exceeds the range of int, we must return 0. The condition &lt;code&gt;result &amp;gt; INT_MAX || result &amp;lt; INT_MIN&lt;/code&gt; checks for that.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            x /= 10;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use &lt;code&gt;x /= 10&lt;/code&gt; to eliminate the last digit.&lt;/p&gt;

&lt;p&gt;For instance,&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;x = - 123

Step 1: 
x = -123
// result = 0
result = result * 10 + x % 10 = -3

Step 2: 
x = -12
// result = -3 
result = result * 10 + x % 10 = -3
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to eliminate the rightmost digit, so we use &lt;code&gt;x /= 10&lt;/code&gt; to shift the digits one place to the right.&lt;/p&gt;

&lt;p&gt;We continue this process until &lt;code&gt;x&lt;/code&gt; becomes 0.&lt;/p&gt;

</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Solving the 'Zigzag Conversion' on LeetCode with C++</title>
      <dc:creator>Shiki Endou</dc:creator>
      <pubDate>Tue, 11 Jul 2023 10:39:49 +0000</pubDate>
      <link>https://dev.to/shikky/solving-the-zigzag-conversion-on-leetcode-with-c-2kbl</link>
      <guid>https://dev.to/shikky/solving-the-zigzag-conversion-on-leetcode-with-c-2kbl</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/zigzag-conversion/"&gt;https://leetcode.com/problems/zigzag-conversion/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Answer
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    string convert(string s, int numRows) {
        if(numRows == 1) {
            return s;
        }

        vector&amp;lt;string&amp;gt; rows(min(numRows, int(s.size())));
        int currentRow = 0;
        bool goingDown = false;

        int firstRow = 0
        int lastRow = numRows - 1;

        for(char c : s) {
            rows[currentRow] += c;
            if(currentRow == firstRow || currentRow == lastRow) {
                goingDown = !goingDown;
            }
            currentRow += goingDown ? 1 : -1;
        }

        string result;
        for(string row : rows) {
            result += row;
        }

        return result;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's consider the solution this problem.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: s = "PAYPALISHIRING", numRows = 4
Output: "PAHNAPLSIIGYIR"

P   A   H   N
A P L S I I G
Y   I   R
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We ask for the answer to combine the strings of each row from the first to the last row.&lt;/p&gt;

&lt;p&gt;The first row is "PAHN".&lt;br&gt;
The second row is "APLSIIG".&lt;br&gt;
The third row is "YIR".&lt;/p&gt;

&lt;p&gt;"PAHN" + "APLSIIG" + "YIR" = "PAHNAPLSIIGYIR"&lt;/p&gt;

&lt;p&gt;Let's write the above algorithm as code.&lt;br&gt;
I repost the answer code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    string convert(string s, int numRows) {
        if(numRows == 1) {
            return s;
        }

        vector&amp;lt;string&amp;gt; rows(min(numRows, int(s.size())));
        int currentRow = 0;
        bool goingDown = false;

        int firstRow = 0
        int lastRow = numRows - 1;

        for(char c : s) {
            rows[currentRow] += c;
            if(currentRow == firstRow || currentRow == lastRow) {
                goingDown = !goingDown;
            }
            currentRow += goingDown ? 1 : -1;
        }

        string result;
        for(string row : rows) {
            result += row;
        }

        return result;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's break down the above code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    string convert(string s, int numRows) {
        if(numRows == 1) {
            return s;
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We should return &lt;code&gt;s&lt;/code&gt; if &lt;code&gt;numRows&lt;/code&gt; is 1 because it's one row.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        vector&amp;lt;string&amp;gt; rows(min(numRows, int(s.size())));
        int currentRow = 0;
        bool goingDown = false;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to take the minimum of &lt;code&gt;numRows&lt;/code&gt; or the size of input &lt;code&gt;s&lt;/code&gt;. Because if &lt;code&gt;numRows&lt;/code&gt; is 4 and &lt;code&gt;s&lt;/code&gt; has a size of 3, not all rows will be used.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;goingDown&lt;/code&gt; indicates the moving direction, whether up or down in rows.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        for(char c : s) {
            rows[currentRow] += c;
            if(currentRow == firstRow || currentRow == lastRow) {
                goingDown = !goingDown;
            }
            currentRow += goingDown ? 1 : -1;
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;rows[currentRow] += c;&lt;/code&gt; creates a string for each row.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;if(currentRow == firstRow || currentRow == lastRow) {
                goingDown = !goingDown;
            }
            currentRow += goingDown ? 1 : -1;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It reverses &lt;code&gt;goingDown&lt;/code&gt; if &lt;code&gt;currentRow&lt;/code&gt; is the first row or &lt;code&gt;currentRow&lt;/code&gt; is the last row.&lt;br&gt;
It moves &lt;code&gt;currentRow&lt;/code&gt; to the next row if &lt;code&gt;goingDown&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt;.&lt;br&gt;
It moves &lt;code&gt;currentRow&lt;/code&gt; to the previous row if &lt;code&gt;goingDown&lt;/code&gt; is false`&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
The number of rows is 3.&lt;/p&gt;

&lt;p&gt;The start &lt;code&gt;currentRow&lt;/code&gt; is 0. &lt;code&gt;goingDown&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; because rows move from top to bottom.&lt;br&gt;
The next &lt;code&gt;currentRow&lt;/code&gt; is 1. &lt;code&gt;goingDown&lt;/code&gt; is &lt;code&gt;true&lt;/code&gt; and the row still moves from top to bottom because the &lt;code&gt;currentRow&lt;/code&gt; is not yet the last row.&lt;br&gt;
The next &lt;code&gt;currentRow&lt;/code&gt; is 2. &lt;code&gt;goingDown&lt;/code&gt; changes from &lt;code&gt;true&lt;/code&gt; to &lt;code&gt;false&lt;/code&gt; because the &lt;code&gt;currentRow&lt;/code&gt; is the last row. The next &lt;code&gt;currentRow&lt;/code&gt; needs to be 1.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;&lt;/code&gt;&lt;code&gt;&lt;br&gt;
        string result;&lt;br&gt;
        for(string row : rows) {&lt;br&gt;
            result += row;&lt;br&gt;
        }&lt;br&gt;
&lt;/code&gt;&lt;code&gt;&lt;/code&gt;&lt;br&gt;
It combines the strings of each row.&lt;/p&gt;

</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Solving the 'Longest Palindromic Substring' on LeetCode with C++</title>
      <dc:creator>Shiki Endou</dc:creator>
      <pubDate>Tue, 04 Jul 2023 10:28:10 +0000</pubDate>
      <link>https://dev.to/shikky/solving-the-longest-palindromic-substring-on-leetcode-with-c-ga5</link>
      <guid>https://dev.to/shikky/solving-the-longest-palindromic-substring-on-leetcode-with-c-ga5</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/longest-palindromic-substring/"&gt;https://leetcode.com/problems/longest-palindromic-substring/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Answer
&lt;/h2&gt;

&lt;h3&gt;
  
  
  O(N^3)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution { 
private: 
    bool check(string &amp;amp;subString, int subStringSize){
        int i = 0;

        while(i &amp;lt; j) {
            if(subString[i] != subString[j]) {
                return false;
            }
            i++;
            j--;
        }
        return true;
    }

public:
    string longestPalindrome(string s) {
        int inputStringSize = s.size();
        vector&amp;lt;string&amp;gt; substrings;

        for (int i = 0; i &amp;lt; inputStringSize; i++) {
            string tmp = "";

            for (int j = i; j &amp;lt; inputStringSize; j++) {
                tmp += s[j];
                substrings.push_back(tmp);
            }
        }

        int maxLength = 0;
        string longestSubstring = substrings[0];
        int subStringsSize = substrings.size();

        for (int i = 0; i &amp;lt; subStringsSize; i++) {
            int subStringSize = substrings[i].size();

            if(check(substrings[i], subStringSize - 1)){
                if(subStringSize &amp;gt; maxLength){
                    maxLength = subStringSize;
                    longestSubstring = substrings[i];
                }
            }       
        }
        return longestSubstring;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's consider the solution to this problem.&lt;/p&gt;

&lt;p&gt;The problem can be solved by checking all substrings. We determine whether a substring is palindromic by checking if the characters from the head of the substring and the end of the substring, moving towards the middle, are the same.&lt;/p&gt;

&lt;p&gt;For instance, let's split the string babd into all possible substrings:&lt;br&gt;
&lt;code&gt;b&lt;/code&gt;, &lt;code&gt;ba&lt;/code&gt;, &lt;code&gt;bab&lt;/code&gt;, &lt;code&gt;babd&lt;/code&gt;&lt;br&gt;
&lt;code&gt;ab&lt;/code&gt;, &lt;code&gt;abd&lt;/code&gt;, &lt;code&gt;bd&lt;/code&gt;, &lt;code&gt;d&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now we'll check whether the characters from the head of the substring and the end of the substring are the same, up to the middle of the substring.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;code&gt;b&lt;/code&gt; is a palindromic substring because the first (&lt;code&gt;b&lt;/code&gt;) and last (&lt;code&gt;b&lt;/code&gt;) characters are the same.&lt;br&gt;
&lt;code&gt;ba&lt;/code&gt; is not a palindromic substring because the first (&lt;code&gt;b&lt;/code&gt;) and last (&lt;code&gt;a&lt;/code&gt;) characters are not the same.&lt;br&gt;
&lt;code&gt;bab&lt;/code&gt; is a palindromic substring because the first (&lt;code&gt;b&lt;/code&gt;) and last (&lt;code&gt;b&lt;/code&gt;) characters are the same.&lt;br&gt;
&lt;code&gt;babd&lt;/code&gt; is not a palindromic substring because the first (&lt;code&gt;b&lt;/code&gt;) and last (&lt;code&gt;d&lt;/code&gt;) characters are not the same.&lt;br&gt;
We continue this process for all substrings.&lt;/p&gt;

&lt;p&gt;However, this solution has suboptimal performance because it has a time complexity of O(n^2) for creating all substrings and O(n) for checking whether a substring is palindromic.&lt;/p&gt;

&lt;p&gt;There is a solution with time complexity O(n^2) only. Let's explore this solution.&lt;/p&gt;
&lt;h3&gt;
  
  
  O(N^2)
&lt;/h3&gt;


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
private: 
    bool solve(vector&amp;lt;vector&amp;lt;bool&amp;gt;&amp;gt; &amp;amp;dp, int startIndex, int endIndex, string &amp;amp;inputString){
        if(startIndex == endIndex){
            return dp[startIndex][endIndex] = true;
        }
        if(endIndex - startIndex == 1){
            if(s[startIndex] == s[endIndex]){
                return dp[startIndex][endIndex] = true;
            }
            else{
                return dp[startIndex][endIndex] = false;
            }
        }
        if(s[startIndex] == s[endIndex] &amp;amp;&amp;amp; dp[startIndex + 1][endIndex - 1] == true){
            return dp[startIndex][endIndex] = true;
        } else {
            return dp[startIndex][endIndex] = false;
        }
    }
public:
    string longestPalindrome(string s) {
        int inputStringSize = s.size();
        int startIndexOfMaxLength = 0; int maxLength = 0;
        vector&amp;lt;vector&amp;lt;bool&amp;gt;&amp;gt; dp(inputStringSize, vector&amp;lt;bool&amp;gt;(inputStringSize, false));
        for(int g = 0; g &amp;lt; inputStringSize; g++){
            for(int startIndex = 0, endIndex = g; endIndex &amp;lt; inputStringSize; startIndex++, endIndex++){
                solve(dp, startIndex, endIndex, s);
                if(dp[startIndex][endIndex] == true){
                    if(endIndex - startIndex +　1 &amp;gt; maxLength){
                        startIndexOfMaxLength = startIndex;
                        maxLength = endIndex - startIndex + 1;
                    }
                }
            }
        }
        return inputStringSize.substr(startIndexOfMaxLength, maxLength);
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;This solution use dynamic programming.&lt;br&gt;
It solve a problem that is split to small problem and preserve answer of small problem as memo.&lt;/p&gt;

&lt;p&gt;Let's see the inside solution.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    string longestPalindrome(string s) {
        int inputStringSize = s.size();
        int startIndex = 0; int maxLength = 0;
        vector&amp;lt;vector&amp;lt;bool&amp;gt;&amp;gt; dp(inputStringSize, vector&amp;lt;bool&amp;gt;(inputStringSize, false));
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;inputStringSize variable is length of s that is an argument.&lt;br&gt;
startIndex variable is head index of target string.&lt;br&gt;
maxLength variable is the longest palindrome string.&lt;br&gt;
dp variable is an answer that is preserved small programing as memo and it is preserved an answer as two dimencional array.&lt;/p&gt;

&lt;p&gt;Next let's see the solve function that is used in double loop.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    bool solve(vector&amp;lt;vector&amp;lt;bool&amp;gt;&amp;gt; &amp;amp;dp, int startIndex, int endIndex, string &amp;amp;inputString){
        if(startIndex == endIndex){
            return dp[startIndex][endIndex] = true;
        }
        if(endIndex - startIndex == 1){
            if(s[startIndex] == s[endIndex]){
                return dp[startIndex][endIndex] = true;
            }
            else{
                return dp[startIndex][endIndex] = false;
            }
        }
        if(s[startIndex] == s[endIndex] &amp;amp;&amp;amp; dp[startIndex + 1][endIndex - 1] == true){
            return dp[startIndex][endIndex] = true;
        } else {
            return dp[startIndex][endIndex] = false;
        }
    }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It assignments &lt;code&gt;true&lt;/code&gt; to dp[startIndex][endIndex] if startIndex equals endIndex.&lt;br&gt;
For example:&lt;br&gt;
If &lt;code&gt;startIndex&lt;/code&gt; is 0 and &lt;code&gt;endIndex&lt;/code&gt; is 0, &lt;code&gt;inputString&lt;/code&gt; is one word. It equals palindromic string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        if(endIndex - startIndex == 1){
            if(s[startIndex] == s[endIndex]){
                return dp[startIndex][endIndex] = true;
            }
            else{
                return dp[startIndex][endIndex] = false;
            }
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It assignments &lt;code&gt;true&lt;/code&gt; to dp[startIndex][endIndex] if endIndex - startIndex equals 1 and s[StartIndex] equals s[endIndex].&lt;br&gt;
For example:&lt;br&gt;
If &lt;code&gt;s&lt;/code&gt; is &lt;code&gt;aa&lt;/code&gt;, &lt;code&gt;startIndex&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;endIndex&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt;, endIndex(1) - startIndex(0) equals &lt;code&gt;1&lt;/code&gt; and s[0] is &lt;code&gt;a&lt;/code&gt; and s[1] is &lt;code&gt;a&lt;/code&gt; that equal each other.&lt;br&gt;
It's palindromic string.&lt;/p&gt;

&lt;p&gt;It assignments &lt;code&gt;false&lt;/code&gt; to dp[startIndex][endIndex] if endIndex - startIndex equals 1 and s[StartIndex] doesn't equal s[endIndex].&lt;br&gt;
For example:&lt;br&gt;
If &lt;code&gt;s&lt;/code&gt; is &lt;code&gt;ab&lt;/code&gt;, &lt;code&gt;startIndex&lt;/code&gt; is &lt;code&gt;0&lt;/code&gt; and &lt;code&gt;endIndex&lt;/code&gt; is &lt;code&gt;1&lt;/code&gt;, endIndex(1) - startIndex(0) equals &lt;code&gt;1&lt;/code&gt; and s[0] is &lt;code&gt;a&lt;/code&gt; and s[1] is &lt;code&gt;b&lt;/code&gt; that doesn't equal each other.&lt;br&gt;
It's not palindromic string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        if(s[startIndex] == s[endIndex] &amp;amp;&amp;amp; dp[startIndex + 1][endIndex - 1] == true){
            return dp[startIndex][endIndex] = true;
        } else {
            return dp[startIndex][endIndex] = false;
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It assignments true to dp[startIndex][endIndex] if s[startIndex] and s[endIndex] equal and dp[startIndex + 1][endIndex - 1] is true.&lt;br&gt;
For example:&lt;br&gt;
If s is aba, startIndex is 0 and endIndex is 2, s[startIndex] and s[endIndex] equal.&lt;br&gt;
If s is aba and dp[0 + 1][2 - 1] is true, It's palindromic string.&lt;br&gt;
dp[1][1] already is calculated because it is calculated from small substring. dp[1][1] is &lt;code&gt;true&lt;/code&gt; because dp[1][1] is &lt;code&gt;b&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It assignments false to dp[startIndex][endIndex] if s[startIndex] and s[endIndex] don't equal or dp[startIndex + 1][endIndex - 1] is not true.&lt;br&gt;
For example:&lt;br&gt;
If s is abcd, startIndex is 0, endIndex is 3 and dp[0 + 1][3 - 1] is false, It's not palindromic string.&lt;br&gt;
dp[1][2] already is calculated because it is calculated from small substring. dp[1][2] is &lt;code&gt;false&lt;/code&gt; because dp[1][2] is &lt;code&gt;bc&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        for(int g = 0; g &amp;lt; inputStringSize; g++){
            for(int i = 0, j = g; j &amp;lt; inputStringSize; i++, j++){
                solve(dp, i, j, s);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The loop perform the solve function to all substring from one word to all words.&lt;br&gt;
For example:&lt;br&gt;
&lt;code&gt;inputStringSize&lt;/code&gt; is &lt;code&gt;aba&lt;/code&gt;.&lt;br&gt;
First, &lt;code&gt;solve(dp, 0, 0, aba)&lt;/code&gt;. dp[0][0] is true because substring is &lt;code&gt;a&lt;/code&gt;.&lt;br&gt;
Second, &lt;code&gt;solve(dp, 1, 1, aba)&lt;/code&gt;. dp[1][1] is true because substring is &lt;code&gt;b&lt;/code&gt;.&lt;br&gt;
Third, &lt;code&gt;solve(dp, 2, 2, aba)&lt;/code&gt;. dp[2][2] is true because substring is &lt;code&gt;a&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;solve(dp, 0, 1, aba)&lt;/code&gt;. dp[0][1] is false because substring is &lt;code&gt;ab&lt;/code&gt;.&lt;br&gt;
&lt;code&gt;solve(dp, 1, 2, aba)&lt;/code&gt;. dp[1][2] is false because substring is &lt;code&gt;ba&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;solve(dp, 0, 2, aba)&lt;/code&gt;. dp[0][2] is true because substring is &lt;code&gt;aba&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;                if(dp[startIndex][endIndex] == true){
                    if(endIndex - startIndex +　1 &amp;gt; maxLength){
                        startIndexOfMaxLength = startIndex;
                        maxLength = endIndex - startIndex + 1;
                    }
                }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It reassignment startIndex to startIndexOfMaxLength and &lt;code&gt;endIndex - startIndex + 1&lt;/code&gt; to maxLength if dp[startIndex][endIndex] that is calculated in the solve function is true and &lt;code&gt;endIndex - startIndex + 1&lt;/code&gt; that is maxLength is bigger than current maxLength.&lt;/p&gt;

&lt;p&gt;Finally, longestPalindrome function returns inputStringSize.substr(startIndexOfMaxLength, maxLength) that is the longest palindromic substring.&lt;/p&gt;

</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Solving the 'Longest Substring Without Repeating Characters' problem on LeetCode with C++</title>
      <dc:creator>Shiki Endou</dc:creator>
      <pubDate>Tue, 27 Jun 2023 10:19:27 +0000</pubDate>
      <link>https://dev.to/shikky/solving-the-longest-substring-without-repeating-characters-problem-on-leetcode-with-c-1np9</link>
      <guid>https://dev.to/shikky/solving-the-longest-substring-without-repeating-characters-problem-on-leetcode-with-c-1np9</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/longest-substring-without-repeating-characters/"&gt;https://leetcode.com/problems/longest-substring-without-repeating-characters/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Answer
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    int lengthOfLongestSubstring(std::string s) {
        std::unordered_map&amp;lt;char, int&amp;gt; map;
        int maxLength = 0;
        int left = 0;

        for (int right = 0; right &amp;lt; s.length(); right++) {
            if (map.find(s[right]) != map.end()) {
                left = std::max(left, map[s[right]] + 1);
            }

            map[s[right]] = right;
            maxLength = std::max(maxLength, right - left + 1);
        }

        return maxLength;
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Let's consider the solution to this problem.&lt;/p&gt;

&lt;p&gt;The answer is 4 (&lt;code&gt;sapd&lt;/code&gt;) when the input is &lt;code&gt;asapda&lt;/code&gt;.&lt;br&gt;
We will examine a character from the beginning.&lt;br&gt;
When the first &lt;code&gt;a&lt;/code&gt; appears, the length of the longest&lt;br&gt;
substring is 1 (&lt;code&gt;a&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Next, &lt;code&gt;s&lt;/code&gt; appears for the first time, extending the length of the longest substring to 2 (as).&lt;br&gt;
Then &lt;code&gt;a&lt;/code&gt; appears for the second time. As &lt;code&gt;a&lt;/code&gt; already appeared at the beginning of the string, we can't include it in the count.&lt;/p&gt;

&lt;p&gt;We are tasked with finding the length of the longest substring without repeating characters. &lt;code&gt;asa&lt;/code&gt; would not be the correct answer.&lt;/p&gt;

&lt;p&gt;Next we have to start counting characters from the position of &lt;code&gt;s&lt;/code&gt;. &lt;br&gt;
This implies that if we encounter a character for the second time, we should start counting from the character following the first occurrence of that character.&lt;/p&gt;

&lt;p&gt;Next, &lt;code&gt;p&lt;/code&gt; appears for the first time, extending the length of the longest substring to 3 (&lt;code&gt;sap&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Next, &lt;code&gt;d&lt;/code&gt; also appears for the first time, extending the length of the longest substring to 4 (&lt;code&gt;sapd&lt;/code&gt;).&lt;br&gt;
Last, &lt;code&gt;a&lt;/code&gt; appears for the second time, we don't count it.&lt;/p&gt;

&lt;p&gt;So the answer is 4 (&lt;code&gt;sapd&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;Now, let's consider the above solution in terms of code.&lt;br&gt;
The crucial point is that when a character reappears, we must start counting from the next character in the position in which it last appeared.&lt;/p&gt;

&lt;p&gt;We will store the index of a character when we encounter it.&lt;br&gt;
For example with &lt;code&gt;asapda&lt;/code&gt;, a is at index 0, s is at index 1.&lt;/p&gt;

&lt;p&gt;When &lt;code&gt;a&lt;/code&gt; reappears, we need to start counting from the next index after the first &lt;code&gt;a&lt;/code&gt; and update the index of &lt;code&gt;a&lt;/code&gt; with the new &lt;code&gt;a&lt;/code&gt; index. &lt;br&gt;
We don't need to store the index of the same character more than once.&lt;/p&gt;

&lt;p&gt;A Map is suitable for this problem because it allows us to store unique key-value pairs. We store a character as the key and its index as the value.&lt;/p&gt;

&lt;p&gt;We need to have two variables: the first variable indicates where to start counting and the second variable indicates where to finish counting.&lt;/p&gt;

&lt;p&gt;Let's consider the answer code in detail.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        std::unordered_map&amp;lt;char, int&amp;gt; map;
        int maxLength = 0;
        int left = 0;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We declare &lt;code&gt;map&lt;/code&gt; as a Map data structure to store a character as the key and its index as the value. The maxLength variable counts the length of the longest substring, and the left variable is indicating where to start counting.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        for (int right = 0; right &amp;lt; s.length(); right++) {
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;right&lt;/code&gt; variable is that counts characters forward.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            if (map.find(s[right]) != map.end()) {
                left = std::max(left, map[s[right]] + 1);
            }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If we encounter the same character for the second time, we update &lt;code&gt;left&lt;/code&gt;, which is the position where we start counting.&lt;/p&gt;

&lt;p&gt;However &lt;code&gt;left&lt;/code&gt; must not go back from its current position, reassign &lt;code&gt;left&lt;/code&gt; to the greater of the current &lt;code&gt;left&lt;/code&gt; and the original &lt;code&gt;left&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            map[s[right]] = right;
            maxLength = std::max(maxLength, right - left + 1);
        }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;map[s[right]] = right&lt;/code&gt; stores a character as the key and its index as the value.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;std::max(maxLength, right - left + 1)&lt;/code&gt; updates &lt;code&gt;maxLength&lt;/code&gt; with the larger of the current &lt;code&gt;maxLength&lt;/code&gt; or &lt;code&gt;right&lt;/code&gt; (the forward counter) minus &lt;code&gt;left&lt;/code&gt; (the start counting position).&lt;/p&gt;

&lt;p&gt;We add 1 to ensure accurate character counting because index start from 0.&lt;/p&gt;

</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Solving the 'Add Two Numbers' problem on LeetCode with C++</title>
      <dc:creator>Shiki Endou</dc:creator>
      <pubDate>Thu, 22 Jun 2023 10:39:05 +0000</pubDate>
      <link>https://dev.to/shikky/solving-the-add-two-numbers-problem-on-leetcode-with-c-246g</link>
      <guid>https://dev.to/shikky/solving-the-add-two-numbers-problem-on-leetcode-with-c-246g</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/add-two-numbers/description/"&gt;https://leetcode.com/problems/add-two-numbers/description/&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Answer
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */

class Solution {
public:
    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* dummy = new ListNode(); 
        ListNode* temp = dummy;
        int carry = 0; 

        while(l1 != NULL || l2 != NULL || carry){ 
            int sum = 0; 
            if(l1 != NULL){
                sum += l1-&amp;gt;val; 
                l1 = l1-&amp;gt;next; 
            }
            if(l2 != NULL){
                sum += l2-&amp;gt;val; 
                l2 = l2-&amp;gt;next; 
            }
            sum += carry; 
            carry = sum / 10; 
            ListNode* newnode = new ListNode(sum % 10); 
            temp-&amp;gt;next = newnode;
            temp = temp-&amp;gt;next; 
        }
        return dummy-&amp;gt;next; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Firstly, let's consider the expected output.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: l1 = [2,4,3], l2 = [5,6,4]
Output: [7,0,8]
Explanation: 342 + 465 = 807.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can obtain the output by summing the numbers from l1 and l2 in order.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 + 5 = 7
4 + 6 = 10 -&amp;gt; 0 // 4 + 6 = 10. Carry 1 to the next digit and truncate the remaining digits. 
3 + 4 = 7 -&amp;gt; 7 + 1 = 8 // Add 1 carried over from the previous digit.

Output: 7, 0, 8
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now we need to represent this operation in the code.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    ListNode* addTwoNumbers(ListNode* l1, ListNode* l2) {
        ListNode* dummy = new ListNode(); 
        ListNode* temp = dummy;
        int carry = 0; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We define &lt;code&gt;dummy&lt;/code&gt; as a variable of type &lt;code&gt;ListNode*&lt;/code&gt; because the function result is &lt;code&gt;ListNode*&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We define the &lt;code&gt;temp&lt;/code&gt; variable to reference the dummy pointer, because we need to reference each node to sum each digit.&lt;/p&gt;

&lt;p&gt;We define the &lt;code&gt;carry&lt;/code&gt; variable to store the carried over number.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;        while(l1 != NULL || l2 != NULL || carry){ 
            int sum = 0; 
            if(l1 != NULL){
                sum += l1-&amp;gt;val; 
                l1 = l1-&amp;gt;next; 
            }
            if(l2 != NULL){
                sum += l2-&amp;gt;val; 
                l2 = l2-&amp;gt;next; 
            }
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need to calculate all &lt;code&gt;l1&lt;/code&gt; and &lt;code&gt;l2&lt;/code&gt; numbers.&lt;br&gt;
This means we must continue calculation until &lt;code&gt;l1&lt;/code&gt; and &lt;code&gt;l2&lt;/code&gt; are &lt;code&gt;NULL&lt;/code&gt; and &lt;code&gt;carry&lt;/code&gt; is 0.&lt;/p&gt;

&lt;p&gt;If &lt;code&gt;l1&lt;/code&gt; is not &lt;code&gt;NULL&lt;/code&gt;, we add the value of &lt;code&gt;l1&lt;/code&gt; to the sum variable.&lt;br&gt;
We move the &lt;code&gt;l1&lt;/code&gt; pointer to the next pointer because the current &lt;code&gt;l1&lt;/code&gt; value has been calculated.&lt;/p&gt;

&lt;p&gt;We do the same for &lt;code&gt;l2&lt;/code&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            sum += carry; 
            carry = sum / 10; 
            ListNode* newnode = new ListNode(sum % 10); 
            temp-&amp;gt;next = newnode;
            temp = temp-&amp;gt;next; 
        }
        return dummy-&amp;gt;next; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If there's a &lt;code&gt;carry&lt;/code&gt; from a previous digit, we need to add it to the sum variable and update &lt;code&gt;carry&lt;/code&gt; with the new value that needs to be carried over to the next digit.&lt;/p&gt;

&lt;p&gt;The first digit of the sum should be assigned to a new Node, and this new Node should be assigned to &lt;code&gt;temp-&amp;gt;next&lt;/code&gt;.&lt;br&gt;
This adds the new Node to the dummy ListNode, but we don't assign the new Node to &lt;code&gt;dummy-&amp;gt;next&lt;/code&gt; directly.&lt;/p&gt;

&lt;p&gt;Because &lt;code&gt;dummy&lt;/code&gt; and &lt;code&gt;temp&lt;/code&gt; have same pointer.&lt;/p&gt;

&lt;p&gt;The reason for this is that we need to return the head of the new ListNode at the end of the function.&lt;/p&gt;

&lt;p&gt;Here, &lt;code&gt;temp&lt;/code&gt; is a pointer used for calculating &lt;code&gt;l1&lt;/code&gt; and &lt;code&gt;l2&lt;/code&gt;, and &lt;code&gt;dummy&lt;/code&gt; is a pointer used to return the head of the new ListNode.&lt;/p&gt;

</description>
      <category>leetcode</category>
    </item>
    <item>
      <title>Understanding the Record Type Implementation in TypeScript</title>
      <dc:creator>Shiki Endou</dc:creator>
      <pubDate>Wed, 21 Jun 2023 09:35:08 +0000</pubDate>
      <link>https://dev.to/shikky/understanding-the-record-type-implementation-in-typescript-3fg2</link>
      <guid>https://dev.to/shikky/understanding-the-record-type-implementation-in-typescript-3fg2</guid>
      <description>&lt;h2&gt;
  
  
  What is the Record Type in TypeScript?
&lt;/h2&gt;

&lt;p&gt;Firstly, let's talk about the Record feature in TypeScript.&lt;/p&gt;

&lt;p&gt;You can find an explanation of the Record type at the following URL:&lt;br&gt;
&lt;a href="https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type"&gt;https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Record type maps one type to another by accepting two types. &lt;/p&gt;

&lt;p&gt;For instance:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type RGBColors = Record&amp;lt;'red' | 'green' | 'blue', number&amp;gt;;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the Record type maps the number type (the second argument) to the union of "red", "green", and "blue" (the first argument). &lt;/p&gt;

&lt;p&gt;The result is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type RGBColors = {
  "red": number;
  "green": number;
  "blue": number;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Understanding the Record Type Implementation in TypeScript
&lt;/h2&gt;

&lt;p&gt;You can examine the implementation of the Record type at the following URL:&lt;br&gt;
&lt;a href="https://github.com/microsoft/TypeScript/blob/5d8ef4bdcb8bdbd4307c74a07b01e3bdf6e04b6a/lib/lib.es5.d.ts#L1590"&gt;https://github.com/microsoft/TypeScript/blob/5d8ef4bdcb8bdbd4307c74a07b01e3bdf6e04b6a/lib/lib.es5.d.ts#L1590&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * Construct a type with a set of properties K of type T
 */
type Record&amp;lt;K extends keyof any, T&amp;gt; = {
    [P in K]: T;
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The Record type accepts two arguments:&lt;/p&gt;

&lt;p&gt;The first argument is &lt;code&gt;K extends keyof any&lt;/code&gt;, which corresponds to &lt;code&gt;"string" | "number" | "symbol"&lt;/code&gt;. Hence, the first argument can be of &lt;code&gt;"string" | "number" | "symbol"&lt;/code&gt; type.&lt;br&gt;
The second argument, &lt;code&gt;T&lt;/code&gt;, is a generic argument. It can accept all types.&lt;/p&gt;

&lt;p&gt;Now, let's take a look into the Record type. &lt;br&gt;
The key is &lt;code&gt;[P in K]&lt;/code&gt;. The &lt;code&gt;in&lt;/code&gt; keyword is used to iterate over &lt;code&gt;K&lt;/code&gt; to map &lt;code&gt;P&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;For example, when &lt;code&gt;K&lt;/code&gt; is &lt;code&gt;"red" | "green" | "blue"&lt;/code&gt;, &lt;code&gt;[P in K]&lt;/code&gt; generates keys that are &lt;code&gt;"red" | "green" | "blue"&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Let's revisit the RGBColors type from earlier:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;type RGBColors = Record&amp;lt;'red' | 'green' | 'blue', number&amp;gt;;

// This is equivalent to the above RGBColors.
type RGBColors = {
  "red": number;
  "green": number;
  "blue": number;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With this explanation, we should now have a better understanding of the Record type in TypeScript.&lt;/p&gt;

</description>
      <category>typescript</category>
    </item>
    <item>
      <title>Solving the 'Two Sum' problem on LeetCode with C++</title>
      <dc:creator>Shiki Endou</dc:creator>
      <pubDate>Thu, 15 Jun 2023 14:42:48 +0000</pubDate>
      <link>https://dev.to/shikky/solving-the-two-sum-problem-on-leetcode-with-c-4656</link>
      <guid>https://dev.to/shikky/solving-the-two-sum-problem-on-leetcode-with-c-4656</guid>
      <description>&lt;h2&gt;
  
  
  Problem
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://leetcode.com/problems/two-sum/description/?envType=featured-list&amp;amp;envId=top-interview-questions"&gt;https://leetcode.com/problems/two-sum/description/?envType=featured-list&amp;amp;envId=top-interview-questions&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Answer (Brute Force)
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    vector&amp;lt;int&amp;gt; twoSum(vector&amp;lt;int&amp;gt;&amp;amp; nums, int target) {
        for (int i = 0; i &amp;lt; nums.size() - 1; i++) {
            for (int j = i + 1; j &amp;lt; nums.size(); j++) {
                if (nums[i] + nums[j] == target) {
                    return {i, j};
                } 
            }
        }

        return {};
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To solve this problem, it is necessary to sum each pair of elements in the nums array.&lt;/p&gt;

&lt;p&gt;For instance:&lt;br&gt;
nums = [1, 2, 3, 4]&lt;br&gt;
target = 5&lt;/p&gt;

&lt;p&gt;1 + 2 = 3;&lt;br&gt;
2 + 3 = 5;&lt;br&gt;
3 + 4 = 7;&lt;/p&gt;

&lt;p&gt;As a result, we need to use a double for loop.&lt;br&gt;
The first loop iterates over the nums array up to the second to last element, because we need to sum two elements and thus the last element doesn't need to be included in the first loop.&lt;br&gt;
The second loop starts at position i + 1 to avoid repeating the same combination twice.&lt;/p&gt;

&lt;p&gt;Lastly, we return the indices of the two elements in the form of a vector when the sum of these two elements equals the target argument.&lt;/p&gt;

&lt;p&gt;In the statement return {i, j}, {i, j} creates a vector containing i and j elements.&lt;/p&gt;

</description>
      <category>leetcode</category>
    </item>
  </channel>
</rss>
