<?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: Ashley Nguyen</title>
    <description>The latest articles on DEV Community by Ashley Nguyen (@diepanh04).</description>
    <link>https://dev.to/diepanh04</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%2F1094962%2F16631521-11ef-4bc3-a2e1-24c436d3695e.jpeg</url>
      <title>DEV Community: Ashley Nguyen</title>
      <link>https://dev.to/diepanh04</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/diepanh04"/>
    <language>en</language>
    <item>
      <title>LeetCode #155 - Min Stack</title>
      <dc:creator>Ashley Nguyen</dc:creator>
      <pubDate>Thu, 08 Jun 2023 17:45:08 +0000</pubDate>
      <link>https://dev.to/diepanh04/leetcode-155-min-stack-30h1</link>
      <guid>https://dev.to/diepanh04/leetcode-155-min-stack-30h1</guid>
      <description>&lt;p&gt;**Design a stack that supports push, pop, top, and retrieving the minimum element in constant time.&lt;/p&gt;

&lt;p&gt;Implement the MinStack class:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;MinStack() initializes the stack object.&lt;/li&gt;
&lt;li&gt;void push(int val) pushes the element val onto the stack.&lt;/li&gt;
&lt;li&gt;void pop() removes the element on the top of the stack.&lt;/li&gt;
&lt;li&gt;int top() gets the top element of the stack.&lt;/li&gt;
&lt;li&gt;int getMin() retrieves the minimum element in the stack.
You must implement a solution with O(1) time complexity for each function.**&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Another designing stack question - another version of the painful tricky Implementing queue from stack problem. The major difference here is that the stack will possess all functions like a normal stack - push, pop, top (peek), except that there must be another data structure to store and update the minimum value of the whole stack, and return it when getMin() is called.&lt;/p&gt;

&lt;p&gt;My initial approach was rather focused on getMin(), in which I would dynamically change the position of elements in the main stack to make sure they are in ascending order from the top down. However, this approach will prove problematic and complicated regarding other operations. &lt;/p&gt;

&lt;p&gt;So let's get started.&lt;/p&gt;

&lt;p&gt;First, I initialize two empty stacks. The main stack is minStack, and the supplementary stack storing min value is stack (please excuse the naming).&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--VlgqXYmn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bx0y5oz8j36p6wjdrcxy.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--VlgqXYmn--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bx0y5oz8j36p6wjdrcxy.png" alt="Image description" width="549" height="104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Push&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Since minStack acts like a normal stack, we simply push the element to minStack. With stack, we only add the element whenever it is less than stack's top element. This way, we can make sure that stack's top is always the current minimum element of the minStack.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--hkbfWwoY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztiqyda22ytwwfd0kjhm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--hkbfWwoY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ztiqyda22ytwwfd0kjhm.png" alt="Image description" width="549" height="104"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Pop&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Again, simply pop the head of minStack. If that value is equal to the head of stack, we pop the head of stack as well. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--SuUq8FXY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/645w03fttdw64lo14ymh.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--SuUq8FXY--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/645w03fttdw64lo14ymh.png" alt="Image description" width="523" height="131"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Top&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Simply return the head of minStack, in which I use Stack.peek() operation. stack is not touched in this operation.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--vjc0K98a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19lw6hjkqoj2cj6s9j00.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--vjc0K98a--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/19lw6hjkqoj2cj6s9j00.png" alt="Image description" width="553" height="78"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;getMin&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;After tons of new additions and removal from our main stack, we are guaranteed that only smaller elements are added to stack. Therefore, return stack's head element.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--asndvENh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9whc7ulfcbd4nou4h469.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--asndvENh--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9whc7ulfcbd4nou4h469.png" alt="Image description" width="590" height="75"&gt;&lt;/a&gt;&lt;/p&gt;

</description>
    </item>
    <item>
      <title>LeetCode #128 - Longest Consecutive Sequence</title>
      <dc:creator>Ashley Nguyen</dc:creator>
      <pubDate>Thu, 08 Jun 2023 17:44:40 +0000</pubDate>
      <link>https://dev.to/diepanh04/leetcode-128-longest-consecutive-sequence-c98</link>
      <guid>https://dev.to/diepanh04/leetcode-128-longest-consecutive-sequence-c98</guid>
      <description>&lt;p&gt;A medium LeetCode problem after days of only easy or no LeetCode at all. I got all test cases passed after four trials (the time gaps were due to my dinner..), and all of them were necessary to solve my careless presumptions in understanding the problem - really reminding me to spend more time thoroughly digging into edge cases and the underlying purpose. &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%2F5hj939vlpr6w86bmn8rr.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%2F5hj939vlpr6w86bmn8rr.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;_Given an unsorted array of integers nums, return the length of the longest consecutive elements sequence.&lt;/p&gt;

&lt;p&gt;You must write an algorithm that runs in O(n) time.&lt;/p&gt;

&lt;p&gt;Input: nums = [100,4,200,1,3,2]&lt;br&gt;
Output: 4&lt;br&gt;
Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4&lt;/p&gt;

&lt;p&gt;Input: nums = [0,3,7,2,5,8,4,6,0,1]&lt;br&gt;
Output: 9&lt;br&gt;
_&lt;br&gt;
&lt;strong&gt;Understand the problem&lt;/strong&gt;&lt;br&gt;
The problem statement was quite straightforward (poor assumption!) and the given examples made it even more self-explanatory: we need to find out in the given array the longest sequence that contains consecutive numbers. It looks to me that we can sort the array for this question and then it becomes much easier: all we need to do left is to identify the longest subarray that contains consecutive integers.&lt;/p&gt;

&lt;p&gt;What about edge cases? In the case that the array is empty or has only one element, the result will be the length of the array itself. &lt;/p&gt;

&lt;p&gt;**Match the problem&lt;/p&gt;

&lt;p&gt;One thing I could think of in the first place after sorting the array is that there can be multiple &lt;em&gt;relevant&lt;/em&gt; subarrays in the sorted array. Therefore, we need to find all those subarrays, keep a variable that represent the subarray length, and keep updating the variable whenever we find a longer &lt;em&gt;relevant&lt;/em&gt; sequence. This sounded to me like a &lt;strong&gt;sliding window&lt;/strong&gt; problem.&lt;/p&gt;

&lt;p&gt;The first thing to do after handling the edge case and sorting the array is creating two variables representing the start and end of the sliding window. I declare them both at 0.&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%2Fsaoq09jtl8avmpbrp1zf.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%2Fsaoq09jtl8avmpbrp1zf.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;One question at this point might be: Why do we declare maxLen at 1, not 0 or any other number? The reason is that we can consider each integer a &lt;em&gt;relevant&lt;/em&gt; sequence to return; therefore, even if the array does not contain any more than 2-integer &lt;em&gt;relevant&lt;/em&gt; sequence, the result returned will always be 1. &lt;/p&gt;

&lt;p&gt;Another question is : Why don't we declare left at 0 and right at 1 - we need to compare two consecutive numbers, don't we? The answer is that we will not use both left and right variables to compare, but the left variable will rather be "static" while the right variable continues to increment in case we identify a possible &lt;em&gt;relevant&lt;/em&gt; sequence. &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%2F1fq91uvcuf8h7es1i9lq.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%2F1fq91uvcuf8h7es1i9lq.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The right variable is actually compared to its next [right + 1]. If their difference is 0, we continue to increment right and keep an eye for maxLen. If otherwise, that means we need to start considering a new sequence starting at where right currently is, so we summon left to represent the start of a new sequence. &lt;/p&gt;

&lt;p&gt;BUT ...&lt;/p&gt;

&lt;p&gt;One case I did not encounter for is.. what if the sorted array looks like this: [0,1,1,2]. The answer for this case should be 3, not 2, because the problem is not asking for a sequence of consecutive numbers that lie next to each other, but that the numbers only need to be in the array. So in case there is duplicates, we can simply count them as one and move on.&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%2Fiw6wln5w08b12gkqs9ty.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%2Fiw6wln5w08b12gkqs9ty.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Final code (after rearrange if else statements): &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%2Fwon6pt6yt3llc1p3eqbi.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%2Fwon6pt6yt3llc1p3eqbi.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The time complexity of Arrays.sort is O(nlogn), and the time complexity of the rest of the code is O(n) (since we iterate through all elements). Therefore the overall time complexity is O(nlogn). &lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
