<?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: Yaz</title>
    <description>The latest articles on DEV Community by Yaz (@yazdun).</description>
    <link>https://dev.to/yazdun</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%2F1253380%2Fc8c8b5f1-a980-457f-a123-8673ea66afdc.jpg</url>
      <title>DEV Community: Yaz</title>
      <link>https://dev.to/yazdun</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/yazdun"/>
    <language>en</language>
    <item>
      <title>Best Time to Buy and Sell Stock II - Leetcode 122 - TypeScript</title>
      <dc:creator>Yaz</dc:creator>
      <pubDate>Wed, 07 Feb 2024 19:05:54 +0000</pubDate>
      <link>https://dev.to/yazdun/best-time-to-buy-and-sell-stock-ii-leetcode-122-typescript-2eg3</link>
      <guid>https://dev.to/yazdun/best-time-to-buy-and-sell-stock-ii-leetcode-122-typescript-2eg3</guid>
      <description>&lt;p&gt;Here's the Leetcode 122 solution using TypeScript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/pGKj4iiABR4?si=GHoX085K8CRKYZiF"&gt;Watch on Youtube &lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Leetcode 122 - &lt;a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii"&gt;Best Time to Buy and Sell Stock II&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;You are given an integer array prices where &lt;code&gt;prices[i]&lt;/code&gt; is the price of a given stock on the ith day.&lt;/p&gt;

&lt;p&gt;On each day, you may decide to buy and/or sell the stock. You can only hold at most one share of the stock at any time. However, you can buy it then immediately sell it on the same day.&lt;/p&gt;

&lt;p&gt;Find and return the maximum profit you can achieve.&lt;/p&gt;

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

&lt;p&gt;Your goal is to find the maximum profit that can be obtained by multiple buy-and-sell transactions on a given stock. &lt;/p&gt;

&lt;p&gt;You can use a one-pass algorithm with two pointers, &lt;code&gt;l&lt;/code&gt; and &lt;code&gt;r&lt;/code&gt;, to track the buying and selling days.&lt;/p&gt;

&lt;p&gt;Initialize &lt;code&gt;l&lt;/code&gt; and &lt;code&gt;r&lt;/code&gt; to the first and second days, respectively.&lt;/p&gt;

&lt;p&gt;Then iterate through the array using a while loop. &lt;/p&gt;

&lt;p&gt;In each iteration, check if the price on day &lt;code&gt;l&lt;/code&gt; is less than the price on day &lt;code&gt;r&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;If true, then calculate the profit by subtracting the buying price (&lt;code&gt;prices[l]&lt;/code&gt;) from the selling price (&lt;code&gt;prices[r]&lt;/code&gt;). &lt;/p&gt;

&lt;p&gt;The calculated profit is added to the total profit (&lt;code&gt;max&lt;/code&gt;), and the &lt;code&gt;l&lt;/code&gt; pointer is moved to the current selling day (&lt;code&gt;r&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;If the buying price is not less than the selling price, it means a better buying opportunity is found, so the &lt;code&gt;l&lt;/code&gt; pointer is moved to the current selling day (&lt;code&gt;r&lt;/code&gt;) without adding any profit. &lt;/p&gt;

&lt;p&gt;This ensures that your algorithm considers multiple transactions for optimal profit.&lt;/p&gt;

&lt;p&gt;The final result is the maximum profit obtained by multiple buy-and-sell transactions on the stock.&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;maxProfit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[]):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="nx"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;profit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="nx"&gt;max&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="nx"&gt;profit&lt;/span&gt;
      &lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nx"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;
    &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;max&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Time complexity is &lt;code&gt;O(n)&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is the length of the input array prices. The algorithm iterates through the array once, and each iteration involves constant-time operations.&lt;/p&gt;

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

&lt;p&gt;Space complexity is &lt;code&gt;O(1)&lt;/code&gt; or constant. The algorithm uses only a constant amount of extra space for variables. &lt;/p&gt;

&lt;p&gt;It doesn't require additional memory that grows with the size of the input array.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>dsa</category>
    </item>
    <item>
      <title>My thoughts on building startups, inspired by Sam Altman.</title>
      <dc:creator>Yaz</dc:creator>
      <pubDate>Wed, 07 Feb 2024 02:45:48 +0000</pubDate>
      <link>https://dev.to/yazdun/my-thoughts-on-building-startups-inspired-by-sam-altman-4bof</link>
      <guid>https://dev.to/yazdun/my-thoughts-on-building-startups-inspired-by-sam-altman-4bof</guid>
      <description>&lt;p&gt;These are my notes and thoughts on Sam Altman's startup investor school presentation. This post may be valuable to anyone serious about building startups.&lt;/p&gt;

&lt;p&gt;I recently gave up a high-paying corporate job to pursue what I'm passionate about and fully focus on building startups. Sam Altman perfectly summarizes my feelings towards the entire situation:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;There's beauty in inexperience. People who are doing things for the first time will be doing things that anyone with a few more battle scars won’t try.&lt;/p&gt;

&lt;p&gt;Sam Altman&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;I feel happier when I'm around people with startup-oriented mindset. These people carry an endless sense of optimism about the future, a quality you likely won't find in any other industry.&lt;/p&gt;

&lt;p&gt;Sam mentions that investing in startups is a very humbling experience. You make mistakes and you get used to being wrong.&lt;/p&gt;

&lt;p&gt;I believe this concept applies to both angel investing and also building a startup. As a founder, you'll often find yourself making mistakes and it's inevitable. You must be willing to admit that you’ve made and error and try to avoid the same mistake next time.&lt;/p&gt;

&lt;p&gt;On the other hand, this should not impact your confidence. You should continue to make decisions, even though you're fully aware that you're prone to mistakes. This is a great quality to have as an individual.&lt;/p&gt;

&lt;p&gt;Sam mentions two major mistakes that angel investors often make. I've personally witnessed these two common mistakes throughout my career and it just clicked when Sam mentioned it. As a founder, it's important to understand how most investors think:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;People often outsource 80% of their decision-making based on other’s opinions. The issue is that this approach will cause trends where every investor wants to invest in a particular company without any solid reasoning, merely based on other investor’s behaviour&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Power law: Your most successful investment will yield a greater return than all your other investments combined. Likewise, your second-best investment will outperform the sum of your third and all subsequent investments. Most people new to angel investing wonder if they can succeed through a series of smaller, consistent investments. While this strategy may work with stocks, angel investing is about hitting home runs. Investors should always be on the lookout for potential home runs, and they often aren’t.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;blockquote&gt;
&lt;p&gt;It’s all about magnitude of your biggest success and it’s not about your failure rate.&lt;/p&gt;

&lt;p&gt;Sam Altman&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You can have 95% failure rate and one investment can return a billion dollar and you’ll be happy.&lt;/p&gt;

&lt;p&gt;YC has funded approximately 1700 companies. Their top 5 companies represent about 2/3 of the value they have created, while one top company accounts for 1/3 of this value.&lt;/p&gt;

&lt;p&gt;Other than that, many of these generation-defining companies are started by individuals who are not well-known and out of network. These are not the type of people who can launch a company and get bunch of press right away.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;First think how big could it be if it works? And only then think about the potential things that can go wrong.&lt;/p&gt;

&lt;p&gt;Sam Altman&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Maintaining a good reputation during challenging times is more valuable than extracting a small sum from a dying company.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Reputation of being good to work with goes a long way and people remember that for a long time.&lt;/p&gt;

&lt;p&gt;Sam Altman&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Starting a startup is a long-term commitment. If it's going to work, it will likely take more than a decade, and there may be times when you want to give up. Many people start startups thinking it's a quick path to become rich, but this is rarely the case.&lt;/p&gt;

&lt;p&gt;You want focused and mission-driven founders, your personal growth is just as important as your business growth. This helps you stay on track even during tough times.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;you have to have a deep sense of mission as a startup founder&lt;/p&gt;

&lt;p&gt;Sam Altman&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;You should favor a small, growing market over a large, existing one. If you pursue what has worked in the past, it will be harder to do for the second time. It's far more better to identify the next rapidly expanding market.&lt;/p&gt;

&lt;p&gt;To do that, you need to be an independent thinker, and you are not going to succeed by simply following others.&lt;/p&gt;

&lt;p&gt;If you are able to think independently, you will approach trends with skepticism.&lt;/p&gt;

&lt;p&gt;To identify genuine trends, look for instances where not many people may be talking about it, but those actually using the product are consistently talking about it.&lt;/p&gt;

&lt;p&gt;That's all I got. I hope you found this post valuable.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Best Time to Buy and Sell Stock - Leetcode 121 - TypeScript</title>
      <dc:creator>Yaz</dc:creator>
      <pubDate>Mon, 05 Feb 2024 20:47:51 +0000</pubDate>
      <link>https://dev.to/yazdun/best-time-to-buy-and-sell-stock-leetcode-121-typescript-1o2h</link>
      <guid>https://dev.to/yazdun/best-time-to-buy-and-sell-stock-leetcode-121-typescript-1o2h</guid>
      <description>&lt;p&gt;Here's the Leetcode 121 solution using TypeScript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/nMpwYY-EqYU?si=M4b-GhXIS2UzJ_Nj"&gt;Watch on Youtube&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Leetcode 121 - &lt;a href="https://leetcode.com/problems/best-time-to-buy-and-sell-stock/description/"&gt;Best Time to Buy and Sell Stock&lt;/a&gt;:
&lt;/h2&gt;

&lt;p&gt;You are given an array prices where &lt;code&gt;prices[i]&lt;/code&gt; is the price of a given stock on the &lt;code&gt;i&lt;/code&gt;th day.&lt;/p&gt;

&lt;p&gt;You want to maximize your profit by choosing a single day to buy one stock and choosing a different day in the future to sell that stock.&lt;/p&gt;

&lt;p&gt;Return the maximum profit you can achieve from this transaction. If you cannot achieve any profit, return &lt;code&gt;0&lt;/code&gt;.&lt;/p&gt;

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

&lt;p&gt;The goal is to find the maximum profit that you can obtain by buying and selling a stock given it's prices over time. &lt;/p&gt;

&lt;p&gt;You can use a simple one-pass algorithm with two pointers, &lt;code&gt;l&lt;/code&gt; and &lt;code&gt;r&lt;/code&gt;, and track the buying and selling days.&lt;/p&gt;

&lt;p&gt;Initialize &lt;code&gt;l&lt;/code&gt; and &lt;code&gt;r&lt;/code&gt; to the first and second days, respectively. &lt;/p&gt;

&lt;p&gt;Then iterate through the array using a &lt;code&gt;while&lt;/code&gt; loop. &lt;/p&gt;

&lt;p&gt;In each iteration, check if the price on day &lt;code&gt;l&lt;/code&gt; is less than the price on day &lt;code&gt;r&lt;/code&gt;. If true, calculate the profit by subtracting the buying price (&lt;code&gt;prices[l]&lt;/code&gt;) from the selling price (&lt;code&gt;prices[r]&lt;/code&gt;) and update the maximum profit (&lt;code&gt;max&lt;/code&gt;) only if the calculated profit is greater than the current maximum.&lt;/p&gt;

&lt;p&gt;On the other hand, if the buying price is not less than the selling price, it means a better buying opportunity is found, so move the &lt;code&gt;l&lt;/code&gt; pointer to the current selling day (&lt;code&gt;r&lt;/code&gt;). This way you can ensure that the algorithm always looks for the lowest buying price before searching for a selling opportunity.&lt;/p&gt;

&lt;p&gt;The final result is the maximum profit obtained by buying and selling the stock.&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%2Fxn1hdq98pdubneleldhj.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%2Fxn1hdq98pdubneleldhj.png" alt="Excalidraw" width="800" height="548"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://excalidraw.com/#json=ZRKj1Q4wJ1CUDfhHxaij6,AZ4A6hX7Yjf-2yNdTlyYlQ"&gt;View on Excalidraw&lt;/a&gt;&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;maxProfit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[]):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

  &lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;profit&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;prices&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;l&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="nx"&gt;max&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;profit&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="nx"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;r&lt;/span&gt;

    &lt;span class="nx"&gt;r&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;max&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;The time complexity is &lt;code&gt;O(n)&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is the length of the input array &lt;code&gt;prices&lt;/code&gt;. The algorithm iterates through the array once, and each iteration involves constant-time operations.&lt;/p&gt;

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

&lt;p&gt;The space complexity is &lt;code&gt;O(1)&lt;/code&gt; or constant. The algorithm uses only a constant amount of extra space for variables like &lt;code&gt;l&lt;/code&gt;, &lt;code&gt;r&lt;/code&gt;, &lt;code&gt;max&lt;/code&gt;, and &lt;code&gt;profit&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;It doesn't require additional memory that grows with the size of the input array.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Rotate Array - Leetcode 189 - TypeScript</title>
      <dc:creator>Yaz</dc:creator>
      <pubDate>Mon, 05 Feb 2024 18:03:42 +0000</pubDate>
      <link>https://dev.to/yazdun/rotate-array-leetcode-189-typescript-3522</link>
      <guid>https://dev.to/yazdun/rotate-array-leetcode-189-typescript-3522</guid>
      <description>&lt;p&gt;Here's the Leetcode 189 solution using TypeScript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/uEPV8rSrCyo?si=s7muRks-T4ffcFc6"&gt;Watch on Youtube&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Leetcode 189 - &lt;a href="https://leetcode.com/problems/rotate-array/description/"&gt;Rotate Array&lt;/a&gt;:
&lt;/h2&gt;

&lt;p&gt;Given an integer array nums, rotate the array to the right by &lt;code&gt;k&lt;/code&gt; steps, where &lt;code&gt;k&lt;/code&gt; is non-negative.&lt;/p&gt;

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

&lt;p&gt;The goal is to rotate an array &lt;code&gt;nums&lt;/code&gt; to the right by &lt;code&gt;k&lt;/code&gt; steps. You can use a cyclic rotation by repeatedly popping the last element and unshifting it to the beginning of the array in a loop.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;for&lt;/code&gt; loop runs for &lt;code&gt;k&lt;/code&gt; iterations, and in each iteration, the &lt;code&gt;pop&lt;/code&gt; method removes the last element from the array, and &lt;code&gt;unshift&lt;/code&gt; adds it to the beginning. This process is repeated &lt;code&gt;k&lt;/code&gt; times and it will rotate the array to the right.&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%2F83eew7mfvy1kusqqbnpp.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%2F83eew7mfvy1kusqqbnpp.png" alt="View on Excalidraw" width="800" height="721"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://excalidraw.com/#json=Zkzf7c8eAGbBaj8iMr97j,5kL57c8LJfFFHu0UOYCNuw"&gt;View on Excalidraw&lt;/a&gt;&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;rotate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;unshift&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;pop&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;Time complexity is &lt;code&gt;O(k * n)&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is the length of the input array &lt;code&gt;nums&lt;/code&gt;. In each iteration of the loop, both &lt;code&gt;pop&lt;/code&gt; and &lt;code&gt;unshift&lt;/code&gt; operations take O(n) time as they involve shifting elements. Therefore, the total time complexity is O(k * n).&lt;/p&gt;

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

&lt;p&gt;Space complexity is &lt;code&gt;O(1)&lt;/code&gt; or constant. The algorithm performs the rotation in-place, it modifies the input array directly without using additional memory that grows with the size of the input array.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Remove Duplicates from Sorted Array II - Leetcode 80 - TypeScript</title>
      <dc:creator>Yaz</dc:creator>
      <pubDate>Sat, 03 Feb 2024 12:24:31 +0000</pubDate>
      <link>https://dev.to/yazdun/remove-duplicates-from-sorted-array-ii-leetcode-80-typescript-4lkd</link>
      <guid>https://dev.to/yazdun/remove-duplicates-from-sorted-array-ii-leetcode-80-typescript-4lkd</guid>
      <description>&lt;p&gt;Here's the Leetcode 80 solution using TypeScript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/DxPL8tR_3zM?si=I641PXfoGjH3enh8" rel="noopener noreferrer"&gt;Watch on Youtube&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Leetcode 80 - &lt;a href="https://leetcode.com/problems/remove-duplicates-from-sorted-array-ii/description" rel="noopener noreferrer"&gt;Remove Duplicates from Sorted Array II&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Given an integer array nums sorted in &lt;strong&gt;non-decreasing&lt;/strong&gt; order, remove some duplicates &lt;strong&gt;in-place&lt;/strong&gt; such that each unique element appears at &lt;strong&gt;most twice&lt;/strong&gt;. The relative order of the elements should be kept the same.&lt;/p&gt;

&lt;p&gt;Since it is impossible to change the length of the array in some languages, you must instead have the result be placed in the first part of the array nums. More formally, if there are &lt;code&gt;k&lt;/code&gt; elements after removing the duplicates, then the first &lt;code&gt;k&lt;/code&gt; elements of &lt;code&gt;nums&lt;/code&gt; should hold the final result. It does not matter what you leave beyond the first &lt;code&gt;k&lt;/code&gt; elements.&lt;/p&gt;

&lt;p&gt;Return &lt;code&gt;k&lt;/code&gt; after placing the final result in the first &lt;code&gt;k&lt;/code&gt; slots of nums.&lt;/p&gt;

&lt;p&gt;Do not allocate extra space for another array. You must do this by modifying the input array in-place with &lt;code&gt;O(1)&lt;/code&gt; extra memory.&lt;/p&gt;

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

&lt;p&gt;The goal is to modify an array of numbers by allowing at most two duplicates of each element and return the new length of the modified array. &lt;/p&gt;

&lt;p&gt;You'll use two pointers: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;i&lt;/code&gt; to iterate through the array.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;k&lt;/code&gt; to keep track of the position where valid elements (remember you can only allow at most two duplicates) should be placed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You'll start the loop from the third element (index 2) since the first two elements are automatically considered as part of the modified array. &lt;/p&gt;

&lt;p&gt;Then you'll check if the current element is different from the element at position &lt;code&gt;k - 2&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;If it is, it means the current element is not a duplicate within the allowed limit, so you assign its value to the position indicated by the pointer &lt;code&gt;k&lt;/code&gt; and then increment &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;This approach will allow only two duplicates of each element in the modified portion of the array.&lt;/p&gt;

&lt;p&gt;Now &lt;code&gt;k&lt;/code&gt; represents the new length of the modified array.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ar3ahxm3rm4779f5zll.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0ar3ahxm3rm4779f5zll.png" alt="Excalidraw"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://excalidraw.com/#json=e8M9RgqusabVxzs0mwa1o,4qRapbvMej1RI_68iyC9dg" rel="noopener noreferrer"&gt;View on Excalidraw&lt;/a&gt;&lt;/p&gt;

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


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;removeDuplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[]):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;br&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;br&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

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

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Time Complexity&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;The time complexity is &lt;code&gt;O(n)&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is the length of the input array &lt;code&gt;nums&lt;/code&gt;. The algorithm iterates through each element in the array once, and the operations inside the loop take constant time.&lt;/p&gt;

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

&lt;p&gt;The space complexity is &lt;code&gt;O(1)&lt;/code&gt; or constant. The algorithm modifies the input array in-place, and only uses a constant amount of extra space for variables. It doesn't require additional memory that grows with the size of the input array.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Majority Element - Leetcode 169 - TypeScript</title>
      <dc:creator>Yaz</dc:creator>
      <pubDate>Thu, 01 Feb 2024 18:29:01 +0000</pubDate>
      <link>https://dev.to/yazdun/majority-element-leetcode-169-typescript-3lp5</link>
      <guid>https://dev.to/yazdun/majority-element-leetcode-169-typescript-3lp5</guid>
      <description>&lt;p&gt;Here's the Leetcode 169 solution using TypeScript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/FmWPpOC_Br4?si=P9TBRk8Qpt2SDm6H" rel="noopener noreferrer"&gt;Watch on Youtube&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Leetcode 169 - &lt;a href="https://leetcode.com/problems/majority-element/description" rel="noopener noreferrer"&gt;Majority Element&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;Given an array nums of size &lt;code&gt;n&lt;/code&gt;, return the majority element.&lt;/p&gt;

&lt;p&gt;The majority element is the element that appears more than &lt;code&gt;⌊n / 2⌋&lt;/code&gt; times. You may assume that the majority element always exists in the array.&lt;/p&gt;

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

&lt;p&gt;The goal is to find the majority element in an array, the majority is an element that appears more than &lt;code&gt;⌊n / 2⌋&lt;/code&gt; times (where &lt;code&gt;n&lt;/code&gt; is the length of the array). &lt;/p&gt;

&lt;p&gt;There are several approaches to solve this problem, but the easiest approach is sorting the array and finding the element at the middle index.&lt;/p&gt;

&lt;p&gt;After sorting, the majority element will always be at the middle index of the sorted array. &lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://excalidraw.com/#json=aJC0j28Q0uGYAXxgFnJ1Z,Y4hjr-HIU66Cu0gNUiGVcA" rel="noopener noreferrer"&gt;View on Excalidraw&lt;/a&gt;&lt;/p&gt;

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


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;majorityElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[]):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
  &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sort&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;br&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;midIdx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;floor&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;br&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;midIdx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

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

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Time Complexity&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;Time complexity is &lt;code&gt;O(n log n)&lt;/code&gt;, where n is the length of the input array nums. The dominating factor here is the sorting operation, which has a time complexity of O(n log n) for most sorting algorithms.&lt;/p&gt;

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

&lt;p&gt;Space complexity is &lt;code&gt;O(1)&lt;/code&gt; or constant. The sorting is done in-place, it means the algorithm modifies the input array directly without using additional memory that grows with the size of the input array. The space required for variables like &lt;code&gt;midIdx&lt;/code&gt; is constant regardless of the input size.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Remove Duplicates from Sorted Array - Leetcode 26 - TypeScript</title>
      <dc:creator>Yaz</dc:creator>
      <pubDate>Tue, 30 Jan 2024 12:00:12 +0000</pubDate>
      <link>https://dev.to/yazdun/remove-duplicates-from-sorted-array-leetcode-26-typescript-40m6</link>
      <guid>https://dev.to/yazdun/remove-duplicates-from-sorted-array-leetcode-26-typescript-40m6</guid>
      <description>&lt;p&gt;Here's the Leetcode 26 solution using TypeScript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/v4XsfNrnAT8?si=kwz5mhum-cWalNsx" rel="noopener noreferrer"&gt;Watch on Youtube&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Leetcode 26 - &lt;a href="https://leetcode.com/problems/remove-duplicates-from-sorted-array/description/" rel="noopener noreferrer"&gt;Remove Duplicates from Sorted Array&lt;/a&gt;:
&lt;/h2&gt;

&lt;p&gt;Given an integer array nums sorted in &lt;strong&gt;non-decreasing&lt;/strong&gt; order, remove the duplicates &lt;strong&gt;in-place&lt;/strong&gt; such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.&lt;/p&gt;

&lt;p&gt;Consider the number of unique elements of nums to be &lt;code&gt;k&lt;/code&gt;, to get accepted, you need to do the following things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Change the array nums such that the first &lt;code&gt;k&lt;/code&gt; elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;The goal is to modify an array nums by removing any duplicate elements and return the new length of the modified array. &lt;/p&gt;

&lt;p&gt;You should use two pointers: &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;(i)&lt;/code&gt; to iterate through the array. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;code&gt;(k)&lt;/code&gt; to keep track of the position where unique elements should be placed.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You'll loop through each element in the array and checks if the current element is less than the next element. &lt;/p&gt;

&lt;p&gt;If it is, it means the current element is unique, so we assign its value to the position indicated by the pointer &lt;code&gt;k&lt;/code&gt; and then increment &lt;code&gt;k&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;If the next element is undefined (which indicates the end of the array), you'll also add the current element to the modified portion of the array. &lt;/p&gt;

&lt;p&gt;The last element of the original array should be considered anyways, even if it is a duplicate.&lt;/p&gt;

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

&lt;p&gt;&lt;a href="https://excalidraw.com/#json=ozy2PM4woon13_eKEsQxO,tWouIgsmTJ5nhAB3sMT1GQ" rel="noopener noreferrer"&gt;View on Excalidraw&lt;/a&gt;&lt;/p&gt;

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


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;removeDuplicates&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[]):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;br&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;===&lt;/span&gt; &lt;span class="kc"&gt;undefined&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;br&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;br&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

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

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Time Complexity&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;The time complexity of this solution is &lt;code&gt;O(n)&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is the length of the input array nums. &lt;/p&gt;

&lt;p&gt;This is because the algorithm iterates through each element in the array once, and the operations inside the loop take constant time. &lt;/p&gt;

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

&lt;p&gt;The space complexity is &lt;code&gt;O(1)&lt;/code&gt; or constant, which means that the algorithm uses only a fixed amount of extra space regardless of the size of the input array. &lt;/p&gt;

&lt;p&gt;The space complexity is low because our algorithm modifies the input array in-place and only uses a constant number of variables.&lt;/p&gt;

</description>
      <category>typescript</category>
      <category>algorithms</category>
      <category>leetcode</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Remove Element - Leetcode 27 - TypeScript</title>
      <dc:creator>Yaz</dc:creator>
      <pubDate>Tue, 23 Jan 2024 04:11:25 +0000</pubDate>
      <link>https://dev.to/yazdun/remove-element-leetcode-27-typescript-15bg</link>
      <guid>https://dev.to/yazdun/remove-element-leetcode-27-typescript-15bg</guid>
      <description>&lt;p&gt;Here's the Leetcode 27 solution using TypeScript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/B4MQsbzRFdc?si=Nt-nsti54evFr2e7"&gt;Watch on Youtube&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Leetcode 27 - &lt;a href="https://leetcode.com/problems/remove-element/description/"&gt;Remove Element&lt;/a&gt;:
&lt;/h2&gt;

&lt;p&gt;Given an integer array &lt;code&gt;nums&lt;/code&gt; and an integer &lt;code&gt;val&lt;/code&gt;, remove all occurrences of &lt;code&gt;val&lt;/code&gt; in &lt;code&gt;nums&lt;/code&gt; in-place. The order of the elements may be changed. Then return the number of elements in nums which are not equal to val.&lt;/p&gt;

&lt;p&gt;Consider the number of elements in nums which are not equal to val be &lt;code&gt;k&lt;/code&gt;, to get accepted, you need to do the following things:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Change the array nums such that the first &lt;code&gt;k&lt;/code&gt; elements of nums contain the elements which are not equal to val. The remaining elements of &lt;code&gt;nums&lt;/code&gt; are not important as well as the size of &lt;code&gt;nums&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Return &lt;code&gt;k&lt;/code&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

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

&lt;p&gt;Your goal is to remove all occurrences of a specified value from an array &lt;code&gt;nums&lt;/code&gt; and return the new length of the modified array. &lt;/p&gt;

&lt;p&gt;You can use two pointers: one &lt;code&gt;i&lt;/code&gt; to iterate through the original array and another &lt;code&gt;k&lt;/code&gt; to keep track of the position where non-matching elements should be placed.&lt;/p&gt;

&lt;p&gt;You'll loop through each element in the array, and if the current element is not equal to the target value, it means you want to keep that element.&lt;/p&gt;

&lt;p&gt;In this case, we assign the value of the current element to the position indicated by the pointer &lt;code&gt;k&lt;/code&gt; and then increment &lt;code&gt;k&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;This effectively removes the occurrences of &lt;code&gt;val&lt;/code&gt; from the modified portion of the array.&lt;/p&gt;

&lt;p&gt;Then you'll have your modified array, and you can also return &lt;code&gt;k&lt;/code&gt; which is basically the length of the modified array.&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%2Fo1ffml70n55tcqqtvy53.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%2Fo1ffml70n55tcqqtvy53.png" alt="Image description" width="800" height="612"&gt;&lt;/a&gt;&lt;br&gt;
&lt;a href="https://excalidraw.com/#json=sRlcpjUtmW1TwPuAAbUGm,-erWnG5CvkW6lZJgG7VdUw"&gt;View on Excalidraw&lt;/a&gt;&lt;/p&gt;

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



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;removeElement&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;

  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;!==&lt;/span&gt; &lt;span class="nx"&gt;val&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
      &lt;span class="nx"&gt;k&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;

  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;k&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;p&gt;The time complexity of this solution is &lt;code&gt;O(n)&lt;/code&gt;, where &lt;code&gt;n&lt;/code&gt; is the length of the input array nums. &lt;/p&gt;

&lt;p&gt;This is because the algorithm iterates through each element in the array once, and the operations inside the loop take constant time. &lt;/p&gt;

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

&lt;p&gt;The space complexity is &lt;code&gt;O(1)&lt;/code&gt; or constant because the algorithm modifies the input array in-place,and we are using only a constant amount of extra space for variables. It doesn't require additional memory that scales with the size of the input array. &lt;/p&gt;

&lt;p&gt;In simpler terms, the time it takes to run the algorithm grows linearly with the size of the input array, and it doesn't use much extra memory regardless of the array's size.&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>typescript</category>
      <category>algorithms</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Merge Sorted Array - Leetcode 88 - TypeScript</title>
      <dc:creator>Yaz</dc:creator>
      <pubDate>Sun, 21 Jan 2024 08:33:44 +0000</pubDate>
      <link>https://dev.to/yazdun/merge-sorted-array-leetcode-88-typescript-58n9</link>
      <guid>https://dev.to/yazdun/merge-sorted-array-leetcode-88-typescript-58n9</guid>
      <description>&lt;p&gt;Here's the Leetcode 88 solution using TypeScript.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://youtu.be/p1Rul9s4XPE?si=UpXoUC3U7kFcqzzm" rel="noopener noreferrer"&gt;Watch on YouTube&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Leetcode 88 - &lt;a href="https://leetcode.com/problems/merge-sorted-array/description/" rel="noopener noreferrer"&gt;Merge Sorted Array&lt;/a&gt;
&lt;/h2&gt;

&lt;p&gt;You are given two integer arrays &lt;code&gt;nums1&lt;/code&gt; and &lt;code&gt;nums2&lt;/code&gt;, sorted in non-decreasing order, and two integers &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt;, representing the number of elements in &lt;code&gt;nums1&lt;/code&gt; and &lt;code&gt;nums2&lt;/code&gt; respectively.&lt;/p&gt;

&lt;p&gt;Merge &lt;code&gt;nums1&lt;/code&gt; and &lt;code&gt;nums2&lt;/code&gt; into a single array sorted in non-decreasing order.&lt;/p&gt;

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

&lt;p&gt;Since both arrays are sorted, you can use two pointers to compare elements from the end of both arrays.&lt;/p&gt;

&lt;p&gt;You'll use two pointers (&lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt;) initially set to the lengths of &lt;code&gt;nums1&lt;/code&gt; and &lt;code&gt;nums2&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Then you can compare the elements at these positions, and the larger element is placed at the end of &lt;code&gt;nums1&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;Then you'll decrement the pointers and the position in &lt;code&gt;nums1&lt;/code&gt; accordingly. &lt;/p&gt;

&lt;p&gt;You should continue this process until either of the arrays is fully merged. &lt;/p&gt;

&lt;p&gt;If there are remaining elements in &lt;code&gt;nums2&lt;/code&gt; after the first loop, you'll add them to the beginning of &lt;code&gt;nums1&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd2c0ls02i275wlopl5rk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fd2c0ls02i275wlopl5rk.png" alt="My excalidraw board which represents the solution of Leetcode 88"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://excalidraw.com/#json=bG4zx5lZ3O-OWXnk6sQt6,kv1BdCmknXaVXZKMWpYbSw" rel="noopener noreferrer"&gt;View on Excalidraw&lt;/a&gt;&lt;/p&gt;

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


&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight typescript"&gt;&lt;code&gt;

&lt;p&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;merge&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums1&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;nums2&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;[],&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kr"&gt;number&lt;/span&gt;&lt;span class="p"&gt;):&lt;/span&gt; &lt;span class="k"&gt;void&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
    &lt;span class="k"&gt;if &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;nums1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;nums2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;nums1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;m&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;br&gt;
    &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;nums1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;br&gt;
      &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;br&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;&lt;br&gt;
    &lt;span class="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;br&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

&lt;p&gt;&lt;span class="k"&gt;while &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;br&gt;
    &lt;span class="nx"&gt;nums1&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;last&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;nums2&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;br&gt;
    &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;br&gt;
    &lt;span class="nx"&gt;last&lt;/span&gt; &lt;span class="o"&gt;-=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;br&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;br&gt;
&lt;span class="p"&gt;}&lt;/span&gt;&lt;/p&gt;

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

&lt;/div&gt;
&lt;h2&gt;
&lt;br&gt;
  &lt;br&gt;
  &lt;br&gt;
  Time Complexity&lt;br&gt;
&lt;/h2&gt;

&lt;p&gt;This solution has the time complexity of &lt;code&gt;O(m + n)&lt;/code&gt;, where &lt;code&gt;m&lt;/code&gt; and &lt;code&gt;n&lt;/code&gt; are the lengths of the input arrays &lt;code&gt;nums1&lt;/code&gt; and &lt;code&gt;nums2&lt;/code&gt;. &lt;/p&gt;

&lt;p&gt;The algorithm iterates through the arrays once, and at each step, it compares and places elements in the correct position. &lt;/p&gt;

&lt;p&gt;Since the length of the merged array is &lt;code&gt;m + n&lt;/code&gt;, the time complexity is linear with respect to the combined length of both input arrays.&lt;/p&gt;

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

&lt;p&gt;The space complexity is &lt;code&gt;O(1)&lt;/code&gt; or constant. &lt;/p&gt;

&lt;p&gt;The algorithm performs the merging in-place without using any additional space that scales with the input size. &lt;/p&gt;

&lt;p&gt;We use a constant amount of extra space for variables like last, m, n, and the loop control variables. &lt;/p&gt;

&lt;p&gt;This makes the algorithm memory-efficient, as the space required to perform this algorithm remains constant regardless of the input size.&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>typescript</category>
      <category>algorithms</category>
      <category>dsa</category>
    </item>
  </channel>
</rss>
