<?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: Ahmad Ra'fat</title>
    <description>The latest articles on DEV Community by Ahmad Ra'fat (@luffy_14).</description>
    <link>https://dev.to/luffy_14</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%2F90767%2F7f705147-5559-4d90-b659-319369a6e4c7.jpg</url>
      <title>DEV Community: Ahmad Ra'fat</title>
      <link>https://dev.to/luffy_14</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/luffy_14"/>
    <language>en</language>
    <item>
      <title>Make GitHub Contribution Chart fancier</title>
      <dc:creator>Ahmad Ra'fat</dc:creator>
      <pubDate>Tue, 06 Feb 2024 09:23:55 +0000</pubDate>
      <link>https://dev.to/luffy_14/make-github-contribution-chart-fancier-1mgi</link>
      <guid>https://dev.to/luffy_14/make-github-contribution-chart-fancier-1mgi</guid>
      <description>&lt;p&gt;Hello, fellow developers and the GoLang community!&lt;/p&gt;

&lt;p&gt;It always bothered me when I opened my GitHub profile and saw my contribution charts empty, while I did commit every day to work on GitLab.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3smofa4tp0ydmx4kftgt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F3smofa4tp0ydmx4kftgt.png" alt="GitHub Contribution Chart" width="800" height="178"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;So, I thought why not bring my GitLab commits to Github and make my contribution chart look a bit fancy? ✨✨&lt;/p&gt;

&lt;p&gt;As you know you master a programming language by building things with it, so I thought of building a very basic/simple tool using GoLang that does exactly that, fetch my commits from GitLab, specifically the dates of the commits, then push those to a private GitHub repo as an empty commits reserving their dates for history records.&lt;/p&gt;

&lt;p&gt;The tool doesn't get any information from GitLab except the dates and pushes them to GitHub as empty commits with a standard commit message.&lt;/p&gt;

&lt;p&gt;And my chart transformed like this! 🌟🌟&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9d6o6i93v0rh1lkges5.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fi9d6o6i93v0rh1lkges5.png" alt="Improved GitHub Contribution Chart" width="800" height="100"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The tool is designed in a way to support multiple different repository management tools such as BitBucket, but if you need that your contribution is more than welcome!&lt;br&gt;
Currently, only GitLab &amp;amp; GitHub.&lt;/p&gt;

&lt;p&gt;Find it here (Make sure to read the ReadMe file): &lt;a href="https://github.com/AhmedRaafat14/universal-commits-migrator"&gt;https://github.com/AhmedRaafat14/universal-commits-migrator&lt;/a&gt;&lt;br&gt;
Very basic/intuitive docs: &lt;a href="https://ahmedraafat14.github.io/universal-commits-migrator/"&gt;https://ahmedraafat14.github.io/universal-commits-migrator/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Thanks&lt;/p&gt;

</description>
      <category>go</category>
      <category>terminal</category>
    </item>
    <item>
      <title>Unveiling PHP Library Development 101: from Basics to Advanced</title>
      <dc:creator>Ahmad Ra'fat</dc:creator>
      <pubDate>Sun, 04 Feb 2024 22:27:10 +0000</pubDate>
      <link>https://dev.to/luffy_14/unveiling-php-library-development-101-from-basics-to-advanced-47c3</link>
      <guid>https://dev.to/luffy_14/unveiling-php-library-development-101-from-basics-to-advanced-47c3</guid>
      <description>&lt;p&gt;🚀 PHP Library Development 101 book is out for &lt;strong&gt;FREE&lt;/strong&gt; 🚀&lt;/p&gt;

&lt;p&gt;Hello, DEV Community!&lt;/p&gt;

&lt;p&gt;I'm excited to offer my book, &lt;strong&gt;PHP Library Development 101: From Basics to Advanced&lt;/strong&gt; for &lt;strong&gt;FREE&lt;/strong&gt;. With nearly a decade of working experience in PHP, I've distilled my learning and discoveries into this book to help others embark on their journey in building PHP libraries.&lt;/p&gt;

&lt;p&gt;This book was reviewed by expert PHP developers to ensure the accuracy of the content.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why This Book?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This book is not just about understanding PHP libraries; it's a comprehensive guide aimed at shaping you into a proficient PHP developer. Whether you're just starting or are an experienced developer, this book is tailored to enhance your skills deepen your understanding of PHP library development, and put you on the right path in the PHP library development world.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What You'll Learn:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The foundational concepts of PHP libraries.&lt;/li&gt;
&lt;li&gt;Advanced topics like Namespaces, Autoloading, Exceptions, and more.&lt;/li&gt;
&lt;li&gt;Practical approaches to write reusable code and contribute to the open-source community.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;My Learning Philosophy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I believe in learning by sharing and writing. This book embodies that philosophy, serving as a practical starting point for those looking to master PHP libraries and advanced PHP subjects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Be Part of This Journey&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As we approach the release of the book, I invite you to subscribe for exclusive early insights and chapter previews. Your feedback and participation would be invaluable to this journey.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Get The book&lt;/strong&gt;&lt;br&gt;
You get:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Two PDF files one in Dark and the other in Light themes.&lt;/li&gt;
&lt;li&gt;Two directories contain the code of the library built through the book.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Find it here: &lt;a href="https://ahmadraafat.gumroad.com/l/php-library-development-book"&gt;https://ahmadraafat.gumroad.com/l/php-library-development-book&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;And subscribe to my newsletter directly: &lt;a href="https://ahmadraafat.gumroad.com/subscribe"&gt;https://ahmadraafat.gumroad.com/subscribe&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Unveiling PHP Library Development 101: from Basics to Advanced</title>
      <dc:creator>Ahmad Ra'fat</dc:creator>
      <pubDate>Thu, 04 Jan 2024 11:22:04 +0000</pubDate>
      <link>https://dev.to/luffy_14/unveiling-php-library-development-101-from-basics-to-advanced-4pf4</link>
      <guid>https://dev.to/luffy_14/unveiling-php-library-development-101-from-basics-to-advanced-4pf4</guid>
      <description>&lt;p&gt;🌟 &lt;strong&gt;Early Insights Alert: PHP Library Development 101&lt;/strong&gt; 🌟&lt;/p&gt;

&lt;p&gt;Hello, DEV Community!&lt;/p&gt;

&lt;p&gt;I'm excited to offer an exclusive sneak peek into my upcoming book, &lt;strong&gt;PHP Library Development 101: From Basics to Advanced&lt;/strong&gt;. With nearly a decade of working experience in PHP, I've distilled my learning and discoveries into this book to help others embark on their journey in building PHP libraries.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Why This Book?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This book is not just about understanding PHP libraries; it's a comprehensive guide aimed at shaping you into a proficient PHP developer. Whether you're just starting or are an experienced developer, this book is tailored to enhance your skills deepen your understanding of PHP library development, and put you on the right path in the PHP library development world.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What You'll Learn:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The foundational concepts of PHP libraries.&lt;/li&gt;
&lt;li&gt;Advanced topics like Namespaces, Autoloading, Exceptions, and more.&lt;/li&gt;
&lt;li&gt;Practical approaches to write reusable code and contribute to the open-source community.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;My Learning Philosophy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;I believe in learning by sharing and writing. This book embodies that philosophy, serving as a practical starting point for those looking to master PHP libraries and advanced PHP subjects.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Be Part of This Journey&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;As we approach the release of the book, I invite you to subscribe for exclusive early insights and chapter previews. Your feedback and participation would be invaluable to this journey.&lt;/p&gt;

&lt;p&gt;🔗 &lt;strong&gt;Sneak Peek and Subscription&lt;/strong&gt;&lt;br&gt;
Get a glimpse of the content and be among the first to access the book by subscribing: &lt;a href="https://ahmadraafat.gumroad.com/l/php-library-development-101-book"&gt;https://ahmadraafat.gumroad.com/l/php-library-development-101-book&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Or subscribe to my newsletter directly: &lt;a href="https://ahmadraafat.gumroad.com/subscribe"&gt;https://ahmadraafat.gumroad.com/subscribe&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Happy Coding!&lt;/p&gt;

</description>
      <category>php</category>
      <category>webdev</category>
      <category>symfony</category>
      <category>laravel</category>
    </item>
    <item>
      <title>30-Day LeetCoding Challenge: Product of Array Except Self</title>
      <dc:creator>Ahmad Ra'fat</dc:creator>
      <pubDate>Thu, 16 Apr 2020 11:24:19 +0000</pubDate>
      <link>https://dev.to/luffy_14/30-day-leetcoding-challenge-product-of-array-except-self-36ic</link>
      <guid>https://dev.to/luffy_14/30-day-leetcoding-challenge-product-of-array-except-self-36ic</guid>
      <description>&lt;h3&gt;
  
  
  &lt;a href="https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/530/week-3/3300/"&gt;The problem&lt;/a&gt;
&lt;/h3&gt;




&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;The problem asks us to replace each element in an array with the product of the other array elements and also asks us to do it in &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So, for example if we have this array &lt;code&gt;arr = [1, 2, 3]&lt;/code&gt;, the result should be &lt;code&gt;ans = [6, 3, 2]&lt;/code&gt; do we replaced the &lt;code&gt;1&lt;/code&gt; with &lt;code&gt;2 * 3 = 6&lt;/code&gt; and so on.&lt;/p&gt;

&lt;p&gt;To do this in &lt;strong&gt;O(n)&lt;/strong&gt; should make you think a little bit about how we can do that! I can think of that we have two arrays one we put for each element the product of the elements before it (left) and another one do the same but from the end (right) some kind of DP. Then we do another loop to multiple for each element the &lt;code&gt;left[i] * right[i]&lt;/code&gt; and that will be our answer.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;try to do this solution yourself :).&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Then I saw that they asking for a follow-up to do it in constant space without considering our output array.&lt;/p&gt;

&lt;p&gt;So, I thought of simulating the two array approach but instead of using two arrays, I will just use two pointers one of them will call it &lt;code&gt;left_product&lt;/code&gt; and this one will go from the left to right and another pointer called &lt;code&gt;right_product&lt;/code&gt; which will go from the right to the left.&lt;/p&gt;

&lt;p&gt;The only thing when I do from the left I use index &lt;code&gt;i = 0, 1,..n&lt;/code&gt; but from the right we need the invert (~) of this number so we can get it from the right &lt;code&gt;i = -1,-2,...-n&lt;/code&gt;, and we keep updating the two pointers with the latest product we find until we finish with the whole array.&lt;/p&gt;

&lt;p&gt;Let's do an example to make it more clear.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;Define &lt;code&gt;arr = [1, 2, 3]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;We will be using &lt;code&gt;left_product = 1&lt;/code&gt;, &lt;code&gt;right_product = 1&lt;/code&gt; we initialise them with &lt;code&gt;1&lt;/code&gt; as we do product.&lt;/li&gt;
&lt;li&gt;We will initialise as well our answer array by the same size of our &lt;code&gt;arr&lt;/code&gt; but will be filled with ones &lt;code&gt;ans = [1, 1, 1]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Start with &lt;code&gt;idx = 0&lt;/code&gt; so our &lt;code&gt;num = arr[idx] = 1&lt;/code&gt; now will find our &lt;code&gt;ans[idx]&lt;/code&gt; by multiple it by the latest &lt;code&gt;left_product&lt;/code&gt; we have so: &lt;code&gt;ans[0] = ans[0] * left_product = 1 * 1 = 1&lt;/code&gt;, our new answer is &lt;code&gt;ans = [1, 1, 1]&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, we calculate the new &lt;code&gt;left_product&lt;/code&gt; by multiple it with the current &lt;code&gt;arr[idx]&lt;/code&gt; so it will be &lt;code&gt;left_product = left_product * arr[0] = 1 * 1 = 1&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Do the right as we said we need to use the invert so &lt;code&gt;idx = - (idx + 1)&lt;/code&gt; or we can just use the invert operator &lt;code&gt;idx = ~idx&lt;/code&gt;, and find our new &lt;code&gt;ans[~idx] = ans[-1]&lt;/code&gt; will multiple it by the right_product so &lt;code&gt;ans[-1] = ans[-1] * right_product = 1 * 1 = 1&lt;/code&gt;, and our new answer is &lt;code&gt;ans = [1, 1, 1]&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Calculate the &lt;code&gt;right_product&lt;/code&gt; by multiple it's value with the &lt;code&gt;arr[~idx]&lt;/code&gt; so it will be &lt;code&gt;right_product = right_product * arr[-1] = 1 * 3 = 3&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Well, keep doing the above procedure over and over until you will reach the final answer array which will be &lt;code&gt;ans = [6, 3, 2]&lt;/code&gt;. Do it on paper so you can get the idea very clear.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pseudocode:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. define ans = [1] * len(arr)
2. define our pointers left_product,right_product = 1, 1
3. loop over the arr from i = 0 to len(arr)
4.    calculate ans[i] = ans[i] * left_product
5.    calculate left_product = left_product * arr[i]
6.    calculate ans[~i] = ans[~i] * right_product
7.    calculate right_product = right_product * arr[~i]
8. return our ans
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Time Complexity: it is so clear we do &lt;strong&gt;O(n)&lt;/strong&gt; one loop over the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Space Complexity: taking away the answer array it will be constant space &lt;strong&gt;O(1)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/AhmedRaafat14/Leetcode-problems/blob/master/30-Day-LeetCoding-Challenge/product-of-array-except-self/Solution.py"&gt;Solution in python&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>leetcode</category>
      <category>coding</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>How many requests can the 4-cores server that run PHP app take at the same time?</title>
      <dc:creator>Ahmad Ra'fat</dc:creator>
      <pubDate>Wed, 15 Apr 2020 09:57:46 +0000</pubDate>
      <link>https://dev.to/luffy_14/how-many-requests-can-the-4-cores-server-take-at-the-same-time-16ef</link>
      <guid>https://dev.to/luffy_14/how-many-requests-can-the-4-cores-server-take-at-the-same-time-16ef</guid>
      <description>&lt;p&gt;If you have a server with 4-cores and 8GB ram, how many requests can this server take at the same time and how many child processes you can run on it?&lt;br&gt;
I'm taking about PHP and if you are running a PHP application there.&lt;/p&gt;

</description>
      <category>php</category>
      <category>discuss</category>
      <category>webdev</category>
    </item>
    <item>
      <title>30-Day LeetCoding Challenge: Perform String Shifts</title>
      <dc:creator>Ahmad Ra'fat</dc:creator>
      <pubDate>Wed, 15 Apr 2020 07:56:19 +0000</pubDate>
      <link>https://dev.to/luffy_14/30-day-leetcoding-challenge-perform-string-shifts-4eih</link>
      <guid>https://dev.to/luffy_14/30-day-leetcoding-challenge-perform-string-shifts-4eih</guid>
      <description>&lt;h3&gt;
  
  
  &lt;a href="https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3299/"&gt;The problem&lt;/a&gt;
&lt;/h3&gt;




&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;The problem is asking us to do some shifts on a string but doing that with some rules in considerations, it says you will have a string &lt;code&gt;s&lt;/code&gt; and an array called &lt;code&gt;shifts&lt;/code&gt; that will hold a list of lists (array of arrays) each item will have two elements the first element is the direction of the shift and the second one is the amount to move.&lt;/p&gt;

&lt;p&gt;The directions are &lt;code&gt;0&lt;/code&gt; for left shift and &lt;code&gt;1&lt;/code&gt; for the right shift.&lt;/p&gt;

&lt;p&gt;So, if the shifts array like this &lt;code&gt;shifts = [[0, 1], [1, 2]]&lt;/code&gt;, so we will shift the string twice the first shift with &lt;code&gt;direction = 0&lt;/code&gt; and &lt;code&gt;amount = 1&lt;/code&gt; means do left shift by the &lt;code&gt;amount = 1&lt;/code&gt; chars so remove the first letter and append it to the end, and the right shift &lt;code&gt;1&lt;/code&gt; is the opposite remove from the end and put it first.&lt;/p&gt;

&lt;p&gt;let's do an example to make it more clear.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;define &lt;code&gt;s = 'abc'&lt;/code&gt; and &lt;code&gt;shifts = [[0, 1], [1, 2]]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So with the first &lt;code&gt;shift = [0, 1]&lt;/code&gt; we see our direction &lt;code&gt;direction = 0&lt;/code&gt; and our amount &lt;code&gt;amount = 1&lt;/code&gt;, so we remove from the beginning and append to the end, so remove &lt;code&gt;a&lt;/code&gt; and add to the end &lt;code&gt;s = 'bca'&lt;/code&gt; and decrease our amount by 1 &lt;code&gt;amount = 1 - 1 = 0&lt;/code&gt; so we are done with this shift.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Now, the second &lt;code&gt;shift = [1, 2]&lt;/code&gt; the &lt;code&gt;direction = 1&lt;/code&gt; says do right shift which means we will move the last &lt;code&gt;amount = 2&lt;/code&gt; to the beginning of the strings:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;First we move the &lt;code&gt;a&lt;/code&gt; from &lt;code&gt;s = 'bca'&lt;/code&gt; to beginning it will be &lt;code&gt;s = 'abc'&lt;/code&gt;, and our &lt;code&gt;amount = 2 - 1 = 1&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Move the &lt;code&gt;c&lt;/code&gt; from &lt;code&gt;s = 'abc'&lt;/code&gt; to the beginning it will be &lt;code&gt;s = 'cab'&lt;/code&gt; and the &lt;code&gt;amount = 1 - 1 = 0&lt;/code&gt; so we are done.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We don't have any other shifts so we stop here with the final answer as &lt;code&gt;s = 'cab'&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you look more closely to the approach and how we reached our answer you will find a pattern for example to do the left shift by &lt;code&gt;one&lt;/code&gt; letter we moved &lt;code&gt;s[0]&lt;/code&gt; to the end we can simulate it to be &lt;code&gt;s = s[0+1:] + s[0] = s[1:] + s[0] = s[1:] + s[:1]&lt;/code&gt; right from the 1st index to the end plus our one single letter if we look very well that the one in &lt;code&gt;s[1:] &amp;amp; s[:1]&lt;/code&gt; is exactly our amount so we can say I need to bring every char after the &lt;code&gt;amount&lt;/code&gt; to the beginning and put every char from beginning to the &lt;code&gt;amount&lt;/code&gt; at the end so it will be: &lt;code&gt;s = s[amount:] + s[:amount]&lt;/code&gt; that's how we do the left shift.&lt;/p&gt;

&lt;p&gt;For the right shift we do from the end so in Python to call the string from the end we use the &lt;code&gt;minus (-)&lt;/code&gt; sign, for example, indexes from beginning for &lt;code&gt;s = 'abc'&lt;/code&gt; is &lt;code&gt;0, 1, 2&lt;/code&gt; but from the tail (end) is &lt;code&gt;-1, -2, -3&lt;/code&gt; so we can use the same equation with just adding the &lt;code&gt;-&lt;/code&gt; to our amount so move from the end to beginning using &lt;code&gt;s = s[-amount:] + s[:-amount]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pseudocode:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. loop over the shifts for each shift
2.    define direction = shift[0] and amount = shift[1]
3.    check if direction is 0 so do left shift using s[amount:] + s[:amount]
4.    otherwise direction is 1 do the right shift using s[-amount:] + s[:-amount]
5. return our final s
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Time Complexity: So we have a loop over the shifts which will be &lt;strong&gt;O(n)&lt;/strong&gt; where &lt;code&gt;n&lt;/code&gt; is the number of shifts we do, in each shift we do modification on the string and that will cost us &lt;strong&gt;O(c)&lt;/strong&gt; where &lt;code&gt;c&lt;/code&gt; is the number of chars in the string so our overall time will be &lt;strong&gt;O(n . c)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Space Complexity: While modifying the string we keep the old &amp;amp; the new string in memory so that will cost us the length of the string which will be &lt;strong&gt;O(c)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/AhmedRaafat14/Leetcode-problems/blob/master/30-Day-LeetCoding-Challenge/perform-string-shifts/Solution.py"&gt;Solution in python&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>leetcode</category>
      <category>coding</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>30-Day LeetCoding Challenge: Contiguous Array</title>
      <dc:creator>Ahmad Ra'fat</dc:creator>
      <pubDate>Tue, 14 Apr 2020 13:51:27 +0000</pubDate>
      <link>https://dev.to/luffy_14/30-day-leetcoding-challenge-contiguous-array-488b</link>
      <guid>https://dev.to/luffy_14/30-day-leetcoding-challenge-contiguous-array-488b</guid>
      <description>&lt;h3&gt;
  
  
  &lt;a href="https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3298/"&gt;The problem&lt;/a&gt;
&lt;/h3&gt;




&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;The problem asking us to find the maximum length of the subarray that contains equal numbers of 0 and 1.&lt;/p&gt;

&lt;p&gt;We can say if we have zero counter and one counter our contiguous subarray with an equal number of 0 &amp;amp; 1 will happen when the counters are equal right!&lt;/p&gt;

&lt;p&gt;So let's define one counter called &lt;code&gt;count = 0&lt;/code&gt; and when we see &lt;code&gt;0&lt;/code&gt; we decrease the count by &lt;code&gt;1&lt;/code&gt; and when we see &lt;code&gt;1&lt;/code&gt; we increase it with &lt;code&gt;1&lt;/code&gt; and when the &lt;code&gt;count == 0&lt;/code&gt; that means we have our subarray so calculate the length.&lt;/p&gt;

&lt;p&gt;To be able to find our answer which is the &lt;code&gt;maximum length&lt;/code&gt; we need to store this counts somewhere associated with their indexes and when we encounter the same count again then we subtract the new index with the stored index and find our answer.&lt;/p&gt;

&lt;p&gt;I can't think of any data structure to help us do that besides the &lt;strong&gt;hash-map&lt;/strong&gt; so we will be using it.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;define &lt;code&gt;arr = [0, 0, 1, 0, 0, 0, 1, 1]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;define &lt;code&gt;start with map = {0: -1}&lt;/code&gt; to hold our counts associated with indexes&lt;/li&gt;
&lt;li&gt;define &lt;code&gt;count = 0&lt;/code&gt; to count the number of zeros and ones&lt;/li&gt;
&lt;li&gt;&lt;p&gt;define &lt;code&gt;max_length = 0&lt;/code&gt; to hold our answer&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When we encounter first &lt;code&gt;arr[0] = 0&lt;/code&gt; so we decrease the count by &lt;code&gt;1&lt;/code&gt;, &lt;code&gt;count -= 1 = -1&lt;/code&gt; do we have count &lt;code&gt;-1&lt;/code&gt; in our &lt;code&gt;map&lt;/code&gt; so so store it with the index: &lt;code&gt;map[count] = arr[0]&lt;/code&gt; =&amp;gt; &lt;code&gt;map = {0: -1, -1: 0}&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When &lt;code&gt;arr[1] = 0&lt;/code&gt; so our new count will be &lt;code&gt;count = -2&lt;/code&gt; not in the map so add it: &lt;code&gt;map = {0: -1, -1: 0, -2: 1}&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;When &lt;code&gt;arr[2] = 1&lt;/code&gt; now increase our count by one so it will be &lt;code&gt;count = -2 + 1 = -1&lt;/code&gt; do we have this in our &lt;code&gt;map&lt;/code&gt; yes we have it so we subtract the current index &lt;code&gt;2&lt;/code&gt; from the one stored in the &lt;code&gt;map&lt;/code&gt; ==&amp;gt; &lt;code&gt;0&lt;/code&gt;, so it will be &lt;code&gt;tmp = 2 - 0 = 2&lt;/code&gt; compare it with our &lt;code&gt;max_length&lt;/code&gt; and choose the max: &lt;code&gt;max_length = max(max_length, tmp) = max(0, 2) = 2&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Then we continue doing this until the end we will get our final answer which will be &lt;code&gt;max_length = 6&lt;/code&gt;, do it on paper to get the idea.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Pseudocode:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. define our count_idx_map = {0: -1}
2. define our max_length = 0
3. define count = 0
4. loop over the arr from i = 0 to len(arr)
5.    add 1 to count if arr[i] = 1 and decrease 1 if it is = 0
6.    if count in count_idx_map find our max_length = max(max_length, i - count_idx_map[count])
7.    otherwise add the count to the map count_idx_map[count] = i
8. at end return out max_length
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Time Complexity: We do one loop over our array items &lt;strong&gt;(n)&lt;/strong&gt; and inside the loop, we do check if the &lt;code&gt;count&lt;/code&gt; in the map or not but this takes only &lt;strong&gt;O(1)&lt;/strong&gt; as it is hash-map, so overall will be &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Space Complexity: Our hash-map in the worst case will be filled with counts for all array items if there is no 0 or 1 so we can say it is &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/AhmedRaafat14/Leetcode-problems/blob/master/30-Day-LeetCoding-Challenge/contiguous-array/Solution.py"&gt;Solution in python&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>leetcode</category>
      <category>coding</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>30-Day LeetCoding Challenge: Last Stone Weight</title>
      <dc:creator>Ahmad Ra'fat</dc:creator>
      <pubDate>Tue, 14 Apr 2020 12:49:19 +0000</pubDate>
      <link>https://dev.to/luffy_14/30-day-leetcoding-challenge-last-stone-weight-3iej</link>
      <guid>https://dev.to/luffy_14/30-day-leetcoding-challenge-last-stone-weight-3iej</guid>
      <description>&lt;h3&gt;
  
  
  &lt;a href="https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3297/"&gt;The problem&lt;/a&gt;
&lt;/h3&gt;




&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;The problem says that we have an array of stones weights and we need to keep smashing them until we only have one weight remains and return it.&lt;/p&gt;

&lt;p&gt;With some conditions which are:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;we choose the two &lt;strong&gt;heaviest&lt;/strong&gt; weights &lt;code&gt;x &amp;amp; y&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;if the two weights are equal we smash both of them entirely &lt;code&gt;smash x &amp;amp; y&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;if the two weights are not equal we destroy &lt;code&gt;x&lt;/code&gt; and update the &lt;code&gt;y&lt;/code&gt; weight to be &lt;code&gt;y = y - x&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In the end, we will have only one stone left and that will be our answer.&lt;/p&gt;

&lt;p&gt;If we think about the first step we need to pick the &lt;strong&gt;heaviest&lt;/strong&gt; two stones with an un-sorted array that is not easy it will cost us a lot of time to find it each time we need to pick a new two stones, so this a perfect use case of &lt;strong&gt;max-heap&lt;/strong&gt; whenever you call it returns the maximum element it has it use tree underlaying to do it or an array or whatever doesn't matter. We need to focus only on the use of it.&lt;/p&gt;

&lt;p&gt;So, we use max-heap and call it each time to get the heaviest stones.&lt;/p&gt;

&lt;p&gt;Let's do an example so it can be more clear.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;We have list of stones: &lt;code&gt;stones = [2,7,4,1,8,1]&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We will create &lt;code&gt;max-heap&lt;/code&gt; with the &lt;code&gt;stones&lt;/code&gt; array so let's assume it will look like this underlaying: &lt;code&gt;stones_heap = [1,1,2,4,7,8]&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We need to pick the heaviest two stones so will pop from the heap: &lt;code&gt;stone_1 = stones_heap.pop() = 8&lt;/code&gt; =&amp;gt; &lt;code&gt;stones_heap = [1,1,2,4,7]&lt;/code&gt; and &lt;code&gt;stone_2 = stones_heap.pop() = 7&lt;/code&gt; ==&amp;gt; &lt;code&gt;stones_heap = [1,1,2,4]&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now, we check is &lt;code&gt;stone_1 == stone_2&lt;/code&gt; no they are not. So, we destroy &lt;code&gt;stone_1&lt;/code&gt; and update our &lt;code&gt;stone_2 = stone_2 - stone_1 = 8 - 7 = 1&lt;/code&gt;, then we add it to our heap &lt;code&gt;stones_heap.push(stone_2) = stones_heap.push(1)&lt;/code&gt; ==&amp;gt; &lt;code&gt;stones_heap = [1,1,1,2,4]&lt;/code&gt; of course the heap takes care of putting the new weight in the correct place.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We keep doing this process again and again until we will have only one item in our heap &lt;code&gt;stones_heap = [1]&lt;/code&gt; and this will be our answer to return. &lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Do it to the end on paper to get the full idea.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;We need to keep in mind in some languages like python it only implements &lt;strong&gt;min-heap&lt;/strong&gt; so to be able to use a &lt;strong&gt;max-heap&lt;/strong&gt; we need to transform all the numbers to be negative and transform it back to it is original when we return our answer.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;As I am solving in &lt;code&gt;Python&lt;/code&gt; I will write the following in the same steps as it is implemented in python.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. Loop over the stones array from i = 0 to len(stones)
2.    stones[i] *= -1 so we can use the max-heap
3. put the stones in heap: heapq.heapify(stones)
4. loop until the len(stones) is equal to 1
5.    stone_1 = stones.heappop()
6.    stone_2 = stones.heappop()
7.    if stone_1 != stone_2
8.        we need to add the new stone weight to the heap: heapq.heappush(stones, stone_1 - stone_2)
9. if the heap is empty return 0
10. otherwise, return -heapq.heappop(stones)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you are using language that has &lt;code&gt;max-heap&lt;/code&gt; discard the steps &lt;code&gt;1 &amp;amp; 2&lt;/code&gt; and in the step &lt;code&gt;#10&lt;/code&gt; remove the &lt;code&gt;minus (-)&lt;/code&gt; from the beginning.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Time Complexity: To transform the list to heap we will take &lt;strong&gt;O(n)&lt;/strong&gt; where &lt;code&gt;n&lt;/code&gt; is the number of stones we have, and the loop will take only &lt;strong&gt;O(n - 1) = O(n)&lt;/strong&gt; and the operations to push &amp;amp; pop will take &lt;strong&gt;O(log(n))&lt;/strong&gt; so overall will take: &lt;strong&gt;O(n log(n))&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Space Complexity: In our python solution you should now that it modifies the list in-place and we don't use any extra space so our overall space will be &lt;strong&gt;O(1)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/AhmedRaafat14/Leetcode-problems/blob/master/30-Day-LeetCoding-Challenge/last-stone-weight/Solution.py"&gt;Solution in python&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>leetcode</category>
      <category>coding</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>30-Day LeetCoding Challenge: Diameter of Binary Tree</title>
      <dc:creator>Ahmad Ra'fat</dc:creator>
      <pubDate>Tue, 14 Apr 2020 11:53:28 +0000</pubDate>
      <link>https://dev.to/luffy_14/30-day-leetcoding-challenge-diameter-of-binary-tree-3124</link>
      <guid>https://dev.to/luffy_14/30-day-leetcoding-challenge-diameter-of-binary-tree-3124</guid>
      <description>&lt;h3&gt;
  
  
  &lt;a href="https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3293/"&gt;The problem&lt;/a&gt;
&lt;/h3&gt;




&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;It asks us to find a binary tree diameter, it also explains to us that is the diameter of a binary tree is &lt;strong&gt;the length of the longest path between any two nodes in a tree.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;It is also indicated that doesn't matter if the path passes by the root or not.&lt;/p&gt;

&lt;p&gt;So, we need to calculate the depth of each node in the tree and then find the longest path between the nodes.&lt;/p&gt;

&lt;p&gt;First the depth of node can be found by &lt;code&gt;node.depth = max(node.left.depth, node.right.depth) + 1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Also, the path always will be the depth of the left subtree + the depth of the right subtree, so on each node, we need to choose the maximum value that takes us to the final answer so our answer will be: &lt;code&gt;diameter = max(diameter, left_depth + right_depth)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, how we can calculate that if you look to the illustration above we see that we need to go deep to the tree to get one node depth which means we need to use &lt;code&gt;depth-first search DFS&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We use DFS to find the left subtree depth then find the right subtree depth and later choose between them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pseudocode:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;diameterOfBinaryTree(root)&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. define our answer
2. call find_depth_helper(root) with the root to find the longest path.
3. return our answer
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;find_depth_helper(node)&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. if the node is None return 0
2. find_depth_helper(node.left) --&amp;gt; left_depth
3. find_depth_helper(node.right) --&amp;gt; right_depth
4. answer = max(answer, left_depth + right_depth)
5. return the current node depth max(left_depth, right_depth) + 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Complexity:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Time Complexity: it is so clear that we go over all our nodes so our time will be &lt;strong&gt;O(n)&lt;/strong&gt; where &lt;strong&gt;n&lt;/strong&gt; is the number of nodes in the tree.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Space Complexity: we use recursion stack as we do recursion calls and this stack will be equal to the number of nodes we have so our overall space will be &lt;strong&gt;O(n)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/AhmedRaafat14/Leetcode-problems/blob/master/30-Day-LeetCoding-Challenge/diameter-of-binary-tree/Solution.py"&gt;Solution in python&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>leetcode</category>
      <category>coding</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>30-Day LeetCoding Challenge: Min Stack</title>
      <dc:creator>Ahmad Ra'fat</dc:creator>
      <pubDate>Tue, 14 Apr 2020 10:08:23 +0000</pubDate>
      <link>https://dev.to/luffy_14/30-day-leetcoding-challenge-min-stack-20l7</link>
      <guid>https://dev.to/luffy_14/30-day-leetcoding-challenge-min-stack-20l7</guid>
      <description>&lt;h3&gt;
  
  
  &lt;a href="https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3292/"&gt;The problem&lt;/a&gt;
&lt;/h3&gt;




&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;The question is asking us to build &lt;code&gt;Stack&lt;/code&gt; but a special one that returns the minimum value in it in constant time &lt;strong&gt;O(1)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We all now very clear that Stack is working with the &lt;strong&gt;LIFO&lt;/strong&gt; principle which means &lt;strong&gt;Last In First Out&lt;/strong&gt;, and why this is important to think about because we know pushing item to stack takes &lt;strong&gt;O(1)&lt;/strong&gt; and also removing an item from stack takes &lt;strong&gt;O(1)&lt;/strong&gt;, and getting the top item of the stack is &lt;strong&gt;O(1)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;We all know this very clear so it is easy to build stack class using list (array) to store the items.&lt;/p&gt;

&lt;p&gt;But here we need a special function called &lt;code&gt;getMin()&lt;/code&gt; which should return to us the minimum item in the stack in &lt;strong&gt;O(1)&lt;/strong&gt; if we think about it ooh that is not possible I have to sort the stack first or I should do a loop over the list to find that minimum item and of course that is not &lt;strong&gt;O(1)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So how we gonna fix that okay let's see we no whenever we put an element to the stack we will not be able to remove it until we remove the one above it, imagine a stack of plates you can not remove the plate in the middle without moving the top one's first right! So we can apply the same principle by adjusting our stack a little bit to not just store our item no we will store with each one the minimum value we found so far.&lt;/p&gt;

&lt;p&gt;So, for example, let's say we are creating our stack and the first item we push is &lt;code&gt;3&lt;/code&gt; will ask is the stack empty yes it is so that means that &lt;code&gt;3&lt;/code&gt; is our minimum value we know so far!&lt;/p&gt;

&lt;p&gt;Our stack will look like this &lt;code&gt;[(item, min_item)] = [(3, 3)]&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So if we add now a new item let's say &lt;code&gt;2&lt;/code&gt;, so we check again the stack now it is not empty so we check is our new item (&lt;code&gt;2&lt;/code&gt;) smaller than the minimum one stored with our top item in the stack which is &lt;code&gt;3&lt;/code&gt;, so yes &lt;code&gt;2 &amp;lt; 3&lt;/code&gt; so we push to our stack our new item associated with the new minimum value.&lt;/p&gt;

&lt;p&gt;Our new stack looks &lt;code&gt;[(3, 3), (2, 2)]&lt;/code&gt;, now if we want to call &lt;code&gt;getMin()&lt;/code&gt; it will just return the second element in the top pair in the stack :).&lt;/p&gt;

&lt;p&gt;Now, the &lt;code&gt;getMin()&lt;/code&gt; will cost &lt;strong&gt;O(1)&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Try to simulate it on paper with more items.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pseudocode:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;push(number)&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    1. if stack is empty
    2.    stack.append((number, number))
    3. otherwise:
    4.    define smallest_num = min(number, stack[-1][-1]) compare with the top
    5.    stack.append((number, smallest_num))
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;pop()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    1. always get &amp;amp; remove the first number in the top pair: stack.pop()[0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;top()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    1. always get the first number in the top pair: stack[-1][0]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;getMin()&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;  1. always get the second number in the top pair: stack [-1][1]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Complexity:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Time Complexity: for all functions will be &lt;strong&gt;O(1)&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Space Complexity: we use a list to store the elements as a stack which will be &lt;strong&gt;O(n)&lt;/strong&gt; where n is the number of items, we also have a modified stack that stores the numbers in pairs so it will be &lt;strong&gt;2.O(n) == O(n)&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/AhmedRaafat14/Leetcode-problems/blob/master/30-Day-LeetCoding-Challenge/min-stack/Solution.py"&gt;Solution in python&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>leetcode</category>
      <category>coding</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>30-Day LeetCoding Challenge: Backspace String Compare</title>
      <dc:creator>Ahmad Ra'fat</dc:creator>
      <pubDate>Fri, 10 Apr 2020 12:49:03 +0000</pubDate>
      <link>https://dev.to/luffy_14/30-day-leetcoding-challenge-backspace-string-compare-2k0j</link>
      <guid>https://dev.to/luffy_14/30-day-leetcoding-challenge-backspace-string-compare-2k0j</guid>
      <description>&lt;h3&gt;
  
  
  &lt;a href="https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3291/"&gt;The problem&lt;/a&gt;
&lt;/h3&gt;




&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;The problem is so easy if you read it carefully it asks if string &lt;strong&gt;S&lt;/strong&gt; and string &lt;strong&gt;T&lt;/strong&gt; are equal consider you typing them in a text editor (notepad), but it is not that easy you will have a special character called &lt;strong&gt;#&lt;/strong&gt; it means &lt;strong&gt;backspace&lt;/strong&gt; so whenever you see it assume you clicked on the backspace button in your keyboard.&lt;/p&gt;

&lt;p&gt;So, if you have a string like this &lt;code&gt;ab#c&lt;/code&gt; if we follow the rule we mentioned above, you first type &lt;code&gt;a&lt;/code&gt; then you find &lt;code&gt;b&lt;/code&gt; not a &lt;code&gt;#&lt;/code&gt; so type it and it will be &lt;code&gt;ab&lt;/code&gt; now we see &lt;code&gt;#&lt;/code&gt; and it means backspace so click on the backspace button will remove the &lt;code&gt;b&lt;/code&gt; and leave us with &lt;code&gt;a&lt;/code&gt; then the last one is &lt;code&gt;c&lt;/code&gt; type it &lt;code&gt;ac&lt;/code&gt; and this our final result.&lt;/p&gt;

&lt;p&gt;It is so obvious now whenever we see the backspace &lt;code&gt;#&lt;/code&gt; char we remove the last typed char and this takes to a data structure called &lt;strong&gt;&lt;a href="https://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm"&gt;Stack&lt;/a&gt;&lt;/strong&gt; as the stack works in &lt;strong&gt;LIFO&lt;/strong&gt; manner which means &lt;code&gt;last-in-first-out&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So, we will go through the two strings to build a stack from their characters and in the end, we compare both of them if they are equal or not.&lt;/p&gt;

&lt;p&gt;Let us do an example.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;S = "ab#c"&lt;/code&gt;&lt;br&gt;
&lt;code&gt;T = "ad#c"&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;We will do each string separately and then combine all elements in the stack into one string and then compare them.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;We will use stack called &lt;code&gt;typed_chars = []&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Start with &lt;code&gt;S = "ab#c"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Starting with &lt;code&gt;ch = 'a'&lt;/code&gt; is our &lt;code&gt;ch == '#' # backspace&lt;/code&gt;  no it is not so add it to the stack &lt;code&gt;typed_chars = ['a']&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;With &lt;code&gt;ch = 'b'&lt;/code&gt; is &lt;code&gt;ch == '#'&lt;/code&gt; no so add it to the stack &lt;code&gt;typed_chars = ['a', 'b']&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;With &lt;code&gt;ch = '#'&lt;/code&gt; is &lt;code&gt;ch == '#'&lt;/code&gt; yes it is so we simulate the click on the &lt;code&gt;backspace #&lt;/code&gt; button by removing the last typed character &lt;code&gt;typed_chars.pop() ==&amp;gt; 'b'&lt;/code&gt; so our new stack is &lt;code&gt;typed_chars = ['a']&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;With our last &lt;code&gt;S&lt;/code&gt; char &lt;code&gt;ch = 'c'&lt;/code&gt; is &lt;code&gt;ch == '#'&lt;/code&gt; no it is not so add to the stack &lt;code&gt;typed_chars = ['a', 'c']&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Build final string from the &lt;code&gt;typed_chars&lt;/code&gt; to be &lt;code&gt;new_s = 'ac'&lt;/code&gt; remember it.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Now will do the same with the &lt;code&gt;T = "ad#c"&lt;/code&gt; it will be exactly the same process and will end-up with &lt;code&gt;new_t = 'ac'&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If we compare both &lt;code&gt;new_s == new_t = 'ac' == 'ac' = True&lt;/code&gt;, and this is our answer.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pseudocode:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1. define helper function called helper that takes one string: helper(s)
2.      define typed_chars = []
3.      loop for ch in s:
4.          check if ch != '#':
5.              typed_chars.append(ch)
5.          if ch is equal to # and typed_chars not empty:
6.              typed_chars.pop()
7.      return "".join(typed_chars) to return one string
8. check if helper(S) is equal to helper(T) or not and return the result.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ul&gt;
&lt;li&gt;We can see that we loop two times to build our stacks the first one is for &lt;strong&gt;O(n)&lt;/strong&gt; where &lt;strong&gt;n&lt;/strong&gt; is our first-string length, and the second time is for &lt;strong&gt;O(m)&lt;/strong&gt; where &lt;strong&gt;m&lt;/strong&gt; is our second string length, so in total it will be &lt;strong&gt;O(n + m)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;

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

&lt;ul&gt;
&lt;li&gt;We store our chars in stack two times of course for our first string and our second one, so in total it will be &lt;strong&gt;O(n + m)&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/AhmedRaafat14/Leetcode-problems/blob/master/30-Day-LeetCoding-Challenge/backspace-string-compare/Solution.py"&gt;Solution in python&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>leetcode</category>
      <category>coding</category>
      <category>algorithms</category>
    </item>
    <item>
      <title>30-Day LeetCoding Challenge: Middle of the Linked List</title>
      <dc:creator>Ahmad Ra'fat</dc:creator>
      <pubDate>Thu, 09 Apr 2020 13:08:37 +0000</pubDate>
      <link>https://dev.to/luffy_14/30-day-leetcoding-challenge-middle-of-the-linked-list-31k</link>
      <guid>https://dev.to/luffy_14/30-day-leetcoding-challenge-middle-of-the-linked-list-31k</guid>
      <description>&lt;h3&gt;
  
  
  &lt;a href="https://leetcode.com/explore/challenge/card/30-day-leetcoding-challenge/529/week-2/3290/"&gt;The Problem&lt;/a&gt;
&lt;/h3&gt;




&lt;h2&gt;
  
  
  Solution:
&lt;/h2&gt;

&lt;p&gt;The problem is so straightforward and so clear it asks us to return the middle node, with one condition if there is two middle numbers return the second one.&lt;/p&gt;

&lt;p&gt;If we think about it for a minute the two middle numbers only happen when the length of the array is &lt;strong&gt;even&lt;/strong&gt;, but the fact is if we do integer division for &lt;code&gt;len(arr) // 2&lt;/code&gt; to get the middle number it will always give you the correct middle number, let's see it:&lt;/p&gt;

&lt;p&gt;If we have &lt;code&gt;arr = [1,2,3,4,5]&lt;/code&gt; then the middle number will be at &lt;code&gt;len(arr) // 2 = 5 // 2 = 2&lt;/code&gt; so it will be &lt;strong&gt;&lt;code&gt;arr[2] = 3&lt;/code&gt;&lt;/strong&gt; which is the correct middle number.&lt;/p&gt;

&lt;p&gt;What if it is even &lt;code&gt;arr = [1,2,3,4,5,6]&lt;/code&gt; so it will be &lt;code&gt;len(arr) // 2 = 6 // 2 = 3&lt;/code&gt; so ... &lt;strong&gt;&lt;code&gt;arr[3] == 4&lt;/code&gt;&lt;/strong&gt; which is the second middle number.&lt;/p&gt;

&lt;p&gt;This should make the problem now easier so what we will do now based on the above we just want to put all our &lt;strong&gt;linked-list nodes&lt;/strong&gt; in an &lt;strong&gt;array&lt;/strong&gt; then return the middle node in that array (&lt;code&gt;return nodes[len(nodes) // 2]&lt;/code&gt;).&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Example:&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;code&gt;lked_list = 1 --&amp;gt; 2 --&amp;gt; 3 --&amp;gt; 4 --&amp;gt; 5&lt;/code&gt;&lt;br&gt;
&lt;code&gt;head -&amp;gt; 1&lt;/code&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;First we will have &lt;code&gt;nodes = []&lt;/code&gt; array to store our nodes there.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;We need to start from the &lt;code&gt;head&lt;/code&gt; and keep looping until the end and keep pushing to the array the elements&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;So, at end our nodes array will be like this: &lt;br&gt;&lt;br&gt;
&lt;code&gt;nodes = [{val: 1, next: -&amp;gt;}, {val: 2, next: -&amp;gt;}, {val: 3, next: -&amp;gt;}, {val: 4, next: -&amp;gt;}, {val: 5, next: None}]&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;No our answer will be &lt;code&gt;ans = nodes[len(nodes) // 2] = nodes[5 // 2] = ndes[2] = {val: 3, next: -&amp;gt;}&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Pseudocode:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt; 1. define nodes = []
 2. loop as long as head != None
 3.   nodes.append(head)
 4.   head = head.next
 5. return nodes[len(nodes) // 2]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Complexity:&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Time Complexity: It is so clear we only do one loop to build our &lt;code&gt;nodes&lt;/code&gt; array so it will be &lt;strong&gt;O(n)&lt;/strong&gt; where &lt;strong&gt;n&lt;/strong&gt; is the number of nodes in the list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Space Complexity: it is clear that we use extra space to store all our list nodes in an array and this will cost us &lt;strong&gt;O(n)&lt;/strong&gt; where &lt;strong&gt;n&lt;/strong&gt; is the number of nodes in the list.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;a href="https://github.com/AhmedRaafat14/Leetcode-problems/blob/master/30-Day-LeetCoding-Challenge/middle-of-linked-list/Solution.py"&gt;Solution in python&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;There is an optimal space solution try to find it out yourself :).&lt;/p&gt;
&lt;/blockquote&gt;

</description>
      <category>python</category>
      <category>leetcode</category>
      <category>coding</category>
      <category>algorithms</category>
    </item>
  </channel>
</rss>
