<?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: Kensuke</title>
    <description>The latest articles on DEV Community by Kensuke (@niitsken).</description>
    <link>https://dev.to/niitsken</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%2F1173176%2F0dbca3c7-b07c-41df-8127-dc638ae209c3.png</url>
      <title>DEV Community: Kensuke</title>
      <link>https://dev.to/niitsken</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/niitsken"/>
    <language>en</language>
    <item>
      <title>【Video】896. Monotonic Array - Python, JavaScript, Java and C++</title>
      <dc:creator>Kensuke</dc:creator>
      <pubDate>Fri, 29 Sep 2023 14:02:18 +0000</pubDate>
      <link>https://dev.to/niitsken/video-896-monotonic-array-python-javascript-java-and-c-50kk</link>
      <guid>https://dev.to/niitsken/video-896-monotonic-array-python-javascript-java-and-c-50kk</guid>
      <description>&lt;p&gt;Welcome to my article! This article starts with "How we think about a solution". In other words, that is my thought process to solve the question. This article explains how I get to my solution instead of just posting solution codes or out of blue algorithms. I hope this article is helpful for someone.&lt;/p&gt;

&lt;h1&gt;
  
  
  Intuition
&lt;/h1&gt;

&lt;p&gt;Use two flags to check this is increasing or decreasing.&lt;/p&gt;




&lt;h1&gt;
  
  
  Solution Video
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://youtu.be/UVEDSDDSP-U"&gt;896. Monotonic Array Solution Video&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  ⭐️⭐️ Don't forget to subscribe to my channel! ⭐️⭐️
&lt;/h3&gt;

&lt;p&gt;&lt;strong&gt;■ Subscribe URL&lt;/strong&gt;&lt;br&gt;
&lt;a href="http://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1"&gt;http://www.youtube.com/channel/UC9RMNwYTL3SXCP6ShLWVFww?sub_confirmation=1&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Subscribers: 2,533&lt;br&gt;
My initial goal is 10,000&lt;br&gt;
Thank you for your support!&lt;/p&gt;


&lt;h1&gt;
  
  
  Approach
&lt;/h1&gt;
&lt;h3&gt;
  
  
  How we think about a solution.
&lt;/h3&gt;

&lt;p&gt;At first, I solved this question with two loops simply like this.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def isMonotonic(self, nums: List[int]) -&amp;gt; bool:
        n = len(nums)
        if n == 1: return True

        # check if this is increasing        
        for i in range(1, n):
            if nums[i] &amp;lt; nums[i-1]:
                break

            if i == n - 1:
                return True

        # check if this is decreasing
        for i in range(1, n):
            if nums[i] &amp;gt; nums[i-1]:
                break

            if i == n - 1:
                return True

        return False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This solution passed all test cases and beated about 60%... not bad. but I realized that we can check both simultaneously if we have &lt;code&gt;two flags&lt;/code&gt;.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;The &lt;code&gt;two flags&lt;/code&gt; indicates that input is increasing or decreasing.&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;The both flags start with &lt;code&gt;True&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: nums = [1,2,2,3]

is_inc = True
is_dec = True
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;First of all, check length of input and if length of input is &lt;code&gt;1&lt;/code&gt; return &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Next, we start from &lt;code&gt;index 1&lt;/code&gt;. Compare two numbers at &lt;code&gt;index 1&lt;/code&gt; and &lt;code&gt;index 0&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;index 0: 1
index 1: 2

is_inc = True
is_dec = False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is increasing case, so in the end the &lt;code&gt;two flags&lt;/code&gt; should be above.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;index 1: 2
index 2: 2

is_inc = True
is_dec = False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We can consider the same numbers as both cases, so this is increasing case because &lt;code&gt;is_dec&lt;/code&gt; was already &lt;code&gt;false&lt;/code&gt;, so in the end the &lt;code&gt;two flags&lt;/code&gt; should be the same.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;index 2: 2
index 3: 3

is_inc = True
is_dec = False
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is increasing case, the &lt;code&gt;both flag&lt;/code&gt; should be the same. Then finish looping.&lt;/p&gt;

&lt;p&gt;After that, check both flags and if one of them is still &lt;code&gt;true&lt;/code&gt;, that means we have &lt;code&gt;Monotonic Array&lt;/code&gt;, so return &lt;code&gt;true&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Of course, during looping, if both flags are &lt;code&gt;false&lt;/code&gt;, we can immediately return &lt;code&gt;false&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;And one more thing&lt;/p&gt;

&lt;p&gt;I use &lt;code&gt;or&lt;/code&gt; at the last step in the codes. It covers cases like &lt;code&gt;[1,3,2]&lt;/code&gt;. In this case, loop 2 times and &lt;code&gt;is_dec&lt;/code&gt; turn into &lt;code&gt;false&lt;/code&gt; in the first loop and &lt;code&gt;is_inc&lt;/code&gt; turn into &lt;code&gt;false&lt;/code&gt; in the second loop.&lt;/p&gt;

&lt;p&gt;So now both flags are &lt;code&gt;false&lt;/code&gt; and finish looping. &lt;/p&gt;

&lt;p&gt;What I'm trying to say is that &lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Finishing loop is not always true case.&lt;/strong&gt; &lt;/p&gt;




&lt;p&gt;That's why I check &lt;code&gt;both flags&lt;/code&gt; with &lt;code&gt;or&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Be careful.&lt;/p&gt;

&lt;p&gt;Let's see a real algorithm!&lt;/p&gt;

&lt;p&gt;Algorithm Overview:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialize variables &lt;code&gt;is_inc&lt;/code&gt; and &lt;code&gt;is_dec&lt;/code&gt; to &lt;code&gt;True&lt;/code&gt; to track whether the sequence is increasing or decreasing.&lt;/li&gt;
&lt;li&gt;Iterate through the input list &lt;code&gt;nums&lt;/code&gt; starting from the second element.&lt;/li&gt;
&lt;li&gt;Update &lt;code&gt;is_inc&lt;/code&gt; to &lt;code&gt;False&lt;/code&gt; if the current element is less than the previous element.&lt;/li&gt;
&lt;li&gt;Update &lt;code&gt;is_dec&lt;/code&gt; to &lt;code&gt;False&lt;/code&gt; if the current element is greater than the previous element.&lt;/li&gt;
&lt;li&gt;If both &lt;code&gt;is_inc&lt;/code&gt; and &lt;code&gt;is_dec&lt;/code&gt; are &lt;code&gt;False&lt;/code&gt;, return &lt;code&gt;False&lt;/code&gt; as the sequence is neither strictly increasing nor strictly decreasing.&lt;/li&gt;
&lt;li&gt;Return &lt;code&gt;True&lt;/code&gt; if either &lt;code&gt;is_inc&lt;/code&gt; or &lt;code&gt;is_dec&lt;/code&gt; is &lt;code&gt;True&lt;/code&gt;, indicating that the sequence is either strictly increasing or strictly decreasing.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Detailed Explanation:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Initialize variables &lt;code&gt;is_inc&lt;/code&gt; and &lt;code&gt;is_dec&lt;/code&gt; to &lt;code&gt;True&lt;/code&gt; to indicate that the sequence is initially assumed to be both increasing and decreasing.&lt;/li&gt;
&lt;li&gt;Iterate through the input list &lt;code&gt;nums&lt;/code&gt; starting from the second element (index 1).&lt;/li&gt;
&lt;li&gt;Check if the sequence is not both increasing and decreasing (i.e., both &lt;code&gt;is_inc&lt;/code&gt; and &lt;code&gt;is_dec&lt;/code&gt; are &lt;code&gt;False&lt;/code&gt;). If so, return &lt;code&gt;False&lt;/code&gt; since the sequence is neither strictly increasing nor strictly decreasing.&lt;/li&gt;
&lt;li&gt;Check if the current element (&lt;code&gt;nums[i]&lt;/code&gt;) is less than the previous element (&lt;code&gt;nums[i-1]&lt;/code&gt;). If true, update &lt;code&gt;is_inc&lt;/code&gt; to &lt;code&gt;False&lt;/code&gt; since the sequence is no longer strictly increasing.&lt;/li&gt;
&lt;li&gt;Check if the current element (&lt;code&gt;nums[i]&lt;/code&gt;) is greater than the previous element (&lt;code&gt;nums[i-1]&lt;/code&gt;). If true, update &lt;code&gt;is_dec&lt;/code&gt; to &lt;code&gt;False&lt;/code&gt; since the sequence is no longer strictly decreasing.&lt;/li&gt;
&lt;li&gt;After iterating through the entire list, if either &lt;code&gt;is_inc&lt;/code&gt; or &lt;code&gt;is_dec&lt;/code&gt; is &lt;code&gt;True&lt;/code&gt;, return &lt;code&gt;True&lt;/code&gt; indicating that the sequence is either strictly increasing or strictly decreasing. If both are &lt;code&gt;False&lt;/code&gt;, return &lt;code&gt;False&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;




&lt;h1&gt;
  
  
  Complexity
&lt;/h1&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Time complexity: O(n)&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Space complexity: O(1)&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Python&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution:
    def isMonotonic(self, nums: List[int]) -&amp;gt; bool:
        n = len(nums)
        if n == 1: return True

        is_inc = True
        is_dec = True

        for i in range(1, n):
            if not is_inc and not is_dec:
                return False

            if nums[i] &amp;lt; nums[i-1]:
                is_inc = False
            if nums[i] &amp;gt; nums[i-1]:
                is_dec = False

        return is_inc or is_dec
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;JavaScript&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;/**
 * @param {number[]} nums
 * @return {boolean}
 */
var isMonotonic = function(nums) {
    const n = nums.length;
    if (n === 1) return true;

    let isInc = true;
    let isDec = true;

    for (let i = 1; i &amp;lt; n; i++) {
        if (!isInc &amp;amp;&amp;amp; !isDec) {
            return false;
        }

        if (nums[i] &amp;lt; nums[i - 1]) {
            isInc = false;
        }
        if (nums[i] &amp;gt; nums[i - 1]) {
            isDec = false;
        }
    }

    return isInc || isDec;    
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Java&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
    public boolean isMonotonic(int[] nums) {
        int n = nums.length;
        if (n == 1) return true;

        boolean isInc = true;
        boolean isDec = true;

        for (int i = 1; i &amp;lt; n; i++) {
            if (!isInc &amp;amp;&amp;amp; !isDec) {
                return false;
            }

            if (nums[i] &amp;lt; nums[i - 1]) {
                isInc = false;
            }
            if (nums[i] &amp;gt; nums[i - 1]) {
                isDec = false;
            }
        }

        return isInc || isDec;        
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;C++&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Solution {
public:
    bool isMonotonic(vector&amp;lt;int&amp;gt;&amp;amp; nums) {
        int n = nums.size();
        if (n == 1) return true;

        bool isInc = true;
        bool isDec = true;

        for (int i = 1; i &amp;lt; n; i++) {
            if (!isInc &amp;amp;&amp;amp; !isDec) {
                return false;
            }

            if (nums[i] &amp;lt; nums[i - 1]) {
                isInc = false;
            }
            if (nums[i] &amp;gt; nums[i - 1]) {
                isDec = false;
            }
        }

        return isInc || isDec;        
    }
};
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>leetcode</category>
      <category>python</category>
      <category>javascript</category>
      <category>java</category>
    </item>
  </channel>
</rss>
