<?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: Riyaz</title>
    <description>The latest articles on DEV Community by Riyaz (@riyu).</description>
    <link>https://dev.to/riyu</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%2F813636%2Ffc75576b-8271-4ef5-aa89-d17fb51e9b56.jpg</url>
      <title>DEV Community: Riyaz</title>
      <link>https://dev.to/riyu</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/riyu"/>
    <language>en</language>
    <item>
      <title>Find minimum in rotated sorted array in Python(code and explanation)</title>
      <dc:creator>Riyaz</dc:creator>
      <pubDate>Mon, 21 Feb 2022 10:42:02 +0000</pubDate>
      <link>https://dev.to/riyu/find-minimum-in-rotated-sorted-array-in-pythoncode-and-explanation-hm4</link>
      <guid>https://dev.to/riyu/find-minimum-in-rotated-sorted-array-in-pythoncode-and-explanation-hm4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem statement&lt;/strong&gt;&lt;br&gt;
Suppose an array of length n sorted in ascending order is rotated between 1 and n times. For example, the array nums = [0,1,2,4,5,6,7] might become:&lt;/p&gt;

&lt;p&gt;[4,5,6,7,0,1,2] if it was rotated 4 times.&lt;br&gt;
[0,1,2,4,5,6,7] if it was rotated 7 times.&lt;br&gt;
Notice that rotating an array [a[0], a[1], a[2], ..., a[n-1]] 1 time results in the array [a[n-1], a[0], a[1], a[2], ..., a[n-2]].&lt;/p&gt;

&lt;p&gt;Given the sorted rotated array nums of unique elements, return the minimum element of this array.&lt;/p&gt;

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

&lt;p&gt;Example 1:&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 = [3,4,5,1,2]
Output: 1
Explanation: The original array was [1,2,3,4,5] rotated 3 times.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Intuition&lt;/strong&gt;&lt;br&gt;
From the question we can easily understand that it is a Binary Search question(Hints - &lt;em&gt;Sorted and expectedTime Complexity-logn&lt;/em&gt;).&lt;br&gt;
But we cannot directly apply it.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementation&lt;/strong&gt;&lt;br&gt;
So if the array is rotated then we can say that now the entire array is divided into two different sorted arrays.&lt;br&gt;
for eg.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[6, 7, 8, 1, 4]
0 1 2 3 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;from index 0 to 2 the array is sorted and from index 3 to 3 the array is sorted.&lt;br&gt;
But from index 2 to 3 the array is not sorted.&lt;br&gt;
Also we can say that if any number from array is greater than its first element than it is sorted.Then we will look for our number in right part of array i.e&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And if any number is less than its first element than it is not sorted from first index to mid.Then we will look for our number in left part of array i.e&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Also in the array the smallest number will be the number that is smaller than both its previous and next element&lt;br&gt;
Hence we will check&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
 &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&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;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
         &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Solution&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;findMin&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="bp"&gt;self&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;List&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;int&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

        &lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;h&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="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
        &lt;span class="k"&gt;while&lt;/span&gt; &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
            &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;l&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;//&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;

            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&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="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
            &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&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;lt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&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="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;mid&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;nums&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;]:&lt;/span&gt;
                &lt;span class="n"&gt;l&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
            &lt;span class="k"&gt;else&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
                &lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;mid&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Time Complexity&lt;/strong&gt;&lt;br&gt;
O(logn) as we are using binary search itself with some changes.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Space Complexity&lt;/strong&gt;&lt;br&gt;
O(1) No extra space required similar to binary search.&lt;/p&gt;

</description>
      <category>python</category>
      <category>algorithms</category>
      <category>leetcode</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Path Sum(code and Explanation)</title>
      <dc:creator>Riyaz</dc:creator>
      <pubDate>Wed, 16 Feb 2022 14:31:37 +0000</pubDate>
      <link>https://dev.to/riyu/path-sumcode-and-explanation-5fa</link>
      <guid>https://dev.to/riyu/path-sumcode-and-explanation-5fa</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem Statement&lt;/strong&gt;&lt;br&gt;
Q.)Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum.&lt;br&gt;
Binary tree&lt;br&gt;
Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22&lt;br&gt;
Output: [[5,4,11,2],[5,8,4,5]]&lt;br&gt;
Explanation: There are two paths whose sum equals targetSum:&lt;br&gt;
5 + 4 + 11 + 2 = 22&lt;br&gt;
5 + 8 + 4 + 5 = 22&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Intuition&lt;/strong&gt;&lt;br&gt;
Lets say you know this question requires you to use DFS but how are you going to store the individual values of all the node and sum them to find if they are equal to targetSum.&lt;br&gt;
For a complete beginner it may not come handy unless you have practiced a lot of such questions.&lt;br&gt;
Now in the iterative version of DFS you know that you have to use stack to remember visited nodes and traverse further.So you are aware that stack is used to store the nodes.So instead of storing only the nodes in the stack you can also store a tuple inside the stack along with the node’s sum of all the previous node value.Something like this&lt;br&gt;
stack&lt;br&gt;
This little trick is hard to come up with your own,but once you know it you can solve many questions regarding the DFS of Binary Tree i.e now you know that you can use the stack itself to store more data about the binary tree.&lt;br&gt;
&lt;strong&gt;SOLUTION:&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;def pathSum(root):
    if not root: return None
    stack = [(root,root.val,[root.val])]
    ans = []
    while stack:
        node,val,li = stack.pop()
        if not node.left and not node.right and val == targetSum:
            ans.append(li)
        if node.right:
            stack.append((node.right,val+node.right.val,li+[node.right.val]))
        if node.left:
            stack.append((node.left,val+node.left.val,li+[node.left.val]))

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

&lt;/div&gt;



&lt;p&gt;This trick of storing using tuples in stack will help you solve a lot more questions&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>python</category>
      <category>leetcode</category>
      <category>dsa</category>
    </item>
    <item>
      <title>Remove Stones to minimize the Total(code and Explanation)</title>
      <dc:creator>Riyaz</dc:creator>
      <pubDate>Mon, 14 Feb 2022 14:32:09 +0000</pubDate>
      <link>https://dev.to/riyu/remove-stones-to-minimize-the-totalcode-and-explanation-2o0g</link>
      <guid>https://dev.to/riyu/remove-stones-to-minimize-the-totalcode-and-explanation-2o0g</guid>
      <description>&lt;p&gt;&lt;strong&gt;PROBLEM STATEMENT&lt;/strong&gt;&lt;br&gt;
You are given a 0-indexed integer array piles, where piles[i] represents the number of stones in the ith pile, and an integer k. You should apply the following operation exactly k times:&lt;/p&gt;

&lt;p&gt;Choose any piles[i] and remove floor(piles[i] / 2) stones from it.&lt;br&gt;
Notice that you can apply the operation on the same pile more than once.&lt;/p&gt;

&lt;p&gt;Return the minimum possible total number of stones remaining after applying the k operations.&lt;/p&gt;

&lt;p&gt;floor(x) is the greatest integer that is smaller than or equal to x (i.e., rounds x down).&lt;br&gt;
'''&lt;br&gt;
'''&lt;br&gt;
Example 1:&lt;/p&gt;

&lt;p&gt;Input: piles = [5,4,9], k = 2&lt;br&gt;
Output: 12&lt;br&gt;
Explanation: Steps of a possible scenario are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Apply the operation on pile 2. The resulting piles are [5,4,5].&lt;/li&gt;
&lt;li&gt;Apply the operation on pile 0. The resulting piles are [3,4,5].
The total number of stones in [3,4,5] is 12.
Example 2:&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Input: piles = [4,3,6,7], k = 3&lt;br&gt;
Output: 12&lt;br&gt;
Explanation: Steps of a possible scenario are:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Apply the operation on pile 2. The resulting piles are [4,3,3,7].&lt;/li&gt;
&lt;li&gt;Apply the operation on pile 3. The resulting piles are [4,3,3,4].&lt;/li&gt;
&lt;li&gt;Apply the operation on pile 0. The resulting piles are [2,3,3,4].
The total number of stones in [2,3,3,4] is 12.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;EXPLANATION&lt;/strong&gt;&lt;br&gt;
Everytime you want the maximum value from the pile to be divided.So we will use a maxheap to store the elements in &lt;br&gt;
greatest to least order and after dividing we push the resultant value again inside the heap&lt;br&gt;
&lt;strong&gt;CODE&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;from heapq import *
class Solution:
    def minStoneSum(self, piles: List[int], k: int) -&amp;gt; int:
        maxHeap = []
        for i in range(len(piles)):
            heappush(maxHeap, -piles[i])

        i = k
        while i &amp;gt; 0:
            temp = heappop(maxHeap)
            x = floor(temp/2)
            heappush(maxHeap,x)
            i -= 1
        return -sum(maxHeap)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Time Complexity&lt;/em&gt;&lt;br&gt;
O(KlogN)&lt;br&gt;
&lt;em&gt;Space Complexity&lt;/em&gt;&lt;br&gt;
O(N) for storing N elements in heap&lt;/p&gt;

</description>
      <category>python</category>
      <category>programming</category>
      <category>algorithms</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>'K' Closest Numbers(code and explanation)</title>
      <dc:creator>Riyaz</dc:creator>
      <pubDate>Sun, 13 Feb 2022 16:09:07 +0000</pubDate>
      <link>https://dev.to/riyu/k-closest-numbers-2eji</link>
      <guid>https://dev.to/riyu/k-closest-numbers-2eji</guid>
      <description>&lt;p&gt;&lt;strong&gt;Problem Statement&lt;/strong&gt; &lt;br&gt;
Given a number array and two integers ‘K’ and ‘X’, find ‘K’ closest numbers to ‘X’ in the array. &lt;br&gt;
Return the numbers in the sorted order. ‘X’ is not necessarily present in the array.&lt;br&gt;
Example 1:&lt;br&gt;
Input: [5, 6, 7, 8, 9], K = 3, X = 7&lt;br&gt;
Output: [6, 7, 8]&lt;br&gt;
Example 2:&lt;br&gt;
Input: [2, 4, 5, 6, 9], K = 3, X = 6&lt;br&gt;
Output: [4, 5, 6]&lt;br&gt;
&lt;strong&gt;SOLUTION&lt;/strong&gt;&lt;br&gt;
For finding the closest numbers to X we can simple calculate the absolute difference between X and &lt;br&gt;
all the values from the array.Now we store these values in a pair using tuple (abs diff, value) with the abs difference&lt;br&gt;
along with the value itself&lt;br&gt;
So, for example, [5, 6, 7, 8, 9], K = 3, X = 7&lt;br&gt;
the tuple values would be (2,5), (1,6), (0,7), (1,8), (2,9)&lt;br&gt;
So the value the first value in the tuple, the closest the element is from X &lt;br&gt;
Now we want the top k(3) values from this tuple that are closest to X(7)&lt;br&gt;
Now we can utilize the heap data structure to do so&lt;br&gt;
We can use min heap which will store the tuples in smallest to greatest values&lt;br&gt;
So the heap will look like&lt;/p&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;            (0,7)
           /     \
         (1,6)   (1,8)
        /    \
      (2,5)  (2,9)     
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;p&gt;Now we can easily remove the top K elements from this heap which will give the closest numbers&lt;br&gt;
Look at the below code for more clarity&lt;br&gt;
&lt;strong&gt;CODE&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;from heapq import *
def findClosestElements(A,K,X):
    answer = []

    minHeap1 = []
    for num in A:
        heappush(minHeap1, (abs(num-X),num)) #Creates a min heap with tuple values as discussed

    minHeap2 = [] #Remove the top K elements from minHeap1 and store in minHeap2 as we have to maintain order
    n = K
    while n &amp;gt; 0:
        a = heappop(minHeap1)
        heappush(minHeap2, a[1]) #Append top K elements values from the minHeap1 and store the second value 
        n -= 1                   # from the tuple i.e (0,7) --&amp;gt; 7 as '7' is the actual element

    while minHeap2:
        b = heappop(minHeap2) #Remove all the elements from the minHeap2 one by one and store them in answer 
        answer.append(b)

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

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Time Complexity&lt;/em&gt;&lt;br&gt;
The first step of creating minHeap1 will take O(N)&lt;br&gt;
Creating minHeap2 will take O(KlogK)&lt;br&gt;
So overall time complexity is O(KLogK)&lt;br&gt;
&lt;em&gt;Space Complexity&lt;/em&gt;&lt;br&gt;
O(N) for storing N elements in heap&lt;/p&gt;

</description>
      <category>algorithms</category>
      <category>python</category>
      <category>leetcode</category>
      <category>programming</category>
    </item>
    <item>
      <title>Connect Ropes(code and explanation)</title>
      <dc:creator>Riyaz</dc:creator>
      <pubDate>Sun, 13 Feb 2022 16:04:28 +0000</pubDate>
      <link>https://dev.to/riyu/connect-ropes-9m2</link>
      <guid>https://dev.to/riyu/connect-ropes-9m2</guid>
      <description>&lt;p&gt;&lt;strong&gt;Yet another question on a mixture on Greedy approach utilizing heap data structure&lt;/strong&gt;&lt;br&gt;
Problem Statement &lt;br&gt;
Given ‘N’ ropes with different lengths, we need to connect these ropes into one big rope with minimum cost. &lt;br&gt;
The cost of connecting two ropes is equal to the sum of their lengths.&lt;br&gt;
Example 1:&lt;br&gt;
Input: [1, 3, 11, 5]&lt;br&gt;
Output: 33&lt;br&gt;
Explanation: First connect 1+3(=4), then 4+5(=9), and then 9+11(=20). So the total cost is 33 (4+9+20)&lt;br&gt;
Example 2:&lt;br&gt;
Input: [3, 4, 5, 6]&lt;br&gt;
Output: 36&lt;br&gt;
Explanation: First connect 3+4(=7), then 5+6(=11), 7+11(=18). Total cost is 36 (7+11+18)&lt;br&gt;
&lt;strong&gt;SOLUTION&lt;/strong&gt;&lt;br&gt;
This question is a perfect example of how heap data structure can be utilized in solving a question that requires greedy approach&lt;br&gt;
In this case, everytime you have to add the smallest two numbers.You can even do this question using a auxillary array that will store all the prefix sums&lt;br&gt;
but here we will be utilizing an heap to do so&lt;br&gt;
&lt;strong&gt;ALGORITHM&lt;/strong&gt;&lt;br&gt;
1.Make a min heap&lt;br&gt;
2.Push all the elements in the heap&lt;br&gt;
3.Remove top two elements from the heap and store its sum in another variable&lt;br&gt;
4.Push this sum again in the heap&lt;br&gt;
5.Continue till heap is empty&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from heapq import *
def connectRopes(ropeLengths):
    minHeap = []
    for length in ropeLengths: #push all the lengths in heap
        heappush(minHeap, length)

    finalLength = 0
    while len(minHeap) &amp;gt; 1:
        temp = heappop(minHeap) + heappop(minHeap) #pop the first two elements
        finalLength += temp #Calculate its sum and store in another variable 
        heappush(minHeap, temp) #Push the current sum again in heap for further calculations
    return finalLength
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;Time Complexity&lt;/em&gt;&lt;br&gt;
Inserting all the elements inside a heap will take O(NlogN)&lt;br&gt;
Removing top two elements and again push in heap will take a maximum of O(NlogN) time&lt;br&gt;
So overall time complexity is O(NlogN)&lt;br&gt;
&lt;em&gt;Space Complexity&lt;/em&gt;&lt;br&gt;
minHeap will require a space of O(N)&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>algorithms</category>
      <category>python</category>
      <category>beginners</category>
    </item>
    <item>
      <title>How to start Leetcode grind</title>
      <dc:creator>Riyaz</dc:creator>
      <pubDate>Fri, 11 Feb 2022 17:55:40 +0000</pubDate>
      <link>https://dev.to/riyu/how-to-start-leetcode-grind-2f00</link>
      <guid>https://dev.to/riyu/how-to-start-leetcode-grind-2f00</guid>
      <description>&lt;p&gt;Since you have click on this blog i may assume you are a complete beginner in leetcode and want to start your leetcode journey to get into top tech companies or change companies.&lt;/p&gt;

&lt;p&gt;By leetcode grind , I do not recommened only using leetcode but also other websites such as GeeksForGeeks, HackerRank etc. to get more exposure to different questions.&lt;/p&gt;

&lt;p&gt;But before you jump into solving questions of any type I recommend you to understand how the Data Structures are implemented and some theory around them.After thats down lets move on to actual coding.&lt;/p&gt;

&lt;p&gt;You might have heard of many patterns of programming like Blind75, Sean Prashad, Grokking the coding interview etc which are great(I use them too) but only doing that questions but guarantee you get good in coding.These questions cover all the patterns that are important but only doing them will not guarantee you can do all the similar questions related to them.&lt;/p&gt;

&lt;p&gt;For Example, Lets say you are solving questions on binary Trees and you come across the Depth First Search of Binary Tree.Now you know how to do a level order traversal but you might not always be able to solve all the questions around Depth First Search unless you have solved plenty of them.For example, lets say the question is&lt;/p&gt;

&lt;p&gt;Q.)Given the root of a binary tree and an integer targetSum, return all root-to-leaf paths where the sum of the node values in the path equals targetSum.&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%2Fvz5nbheipvulbcmzje6p.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%2Fvz5nbheipvulbcmzje6p.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Input: root = [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum = 22
Output: [[5,4,11,2],[5,8,4,5]]
Explanation: There are two paths whose sum equals targetSum:
5 + 4 + 11 + 2 = 22
5 + 8 + 4 + 5 = 22
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Lets say you know this question requires you to use DFS but how are you going to store the individual values of all the node and sum them to find if they are equal to targetSum.&lt;/p&gt;

&lt;p&gt;For a complete beginner it may not come handy unless you have practiced a lot of such questions.&lt;/p&gt;

&lt;p&gt;Now in the iterative version of DFS you know that you have to use stack to remember visited nodes and traverse further.So you are aware that stack is used to store the nodes.So instead of storing only the nodes in the stack you can also store a tuple inside the stack along with the node’s sum of all the previous node value.Something like this&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%2F77p8skmc793vhgeuecr8.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%2F77p8skmc793vhgeuecr8.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This little trick is hard to come up with your own,but once you know it you can solve many questions regarding the DFS of Binary Tree i.e now you know that you can use the stack itself to store more data about the binary tree.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def pathSum(root):
    if not root: return None
    stack = [(root,root.val,[root.val])]
    ans = []
    while stack:
        node,val,li = stack.pop()
        if not node.left and not node.right and val == targetSum:
            ans.append(li)
        if node.right:
            stack.append((node.right,val+node.right.val,li+[node.right.val]))
        if node.left:
            stack.append((node.left,val+node.left.val,li+[node.left.val]))

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

&lt;/div&gt;



&lt;p&gt;This trick of storing using tuples in stack will help you solve a lot more questions.&lt;/p&gt;

&lt;p&gt;So my point is after reading one pattern, solve as many questions related to it as you can.Another important thing to consider is to not give a question more than 5–7 minutes of your time in the beginning for easy questions.The reason for it is if you can’t come up with a solution for easy question in 5 minutes then maybe you are lacking some concepts regarding it.&lt;/p&gt;

&lt;p&gt;One more important thing to remember is spaced repetition.You might not remember everything you do.So it is better to revise them from time to time to completely grasp the concept or the question.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Some Motivation&lt;/strong&gt;&lt;br&gt;
‘‘The reason to win the game is so that you can be free of it.’’ — Naval Ravikant&lt;br&gt;
&lt;strong&gt;HAPPY GRINDING&lt;/strong&gt;&lt;/p&gt;

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