<?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: Justin Chan</title>
    <description>The latest articles on DEV Community by Justin Chan (@justinc16).</description>
    <link>https://dev.to/justinc16</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%2F646642%2F9b5bb766-ff67-44c4-a5ef-a22bfe32c181.png</url>
      <title>DEV Community: Justin Chan</title>
      <link>https://dev.to/justinc16</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/justinc16"/>
    <language>en</language>
    <item>
      <title>Back-Tracking N-queen challenge</title>
      <dc:creator>Justin Chan</dc:creator>
      <pubDate>Mon, 26 Jul 2021 03:58:09 +0000</pubDate>
      <link>https://dev.to/justinc16/back-tracking-n-queen-challenge-mh2</link>
      <guid>https://dev.to/justinc16/back-tracking-n-queen-challenge-mh2</guid>
      <description>&lt;h1&gt;
  
  
  What is backtracking?
&lt;/h1&gt;

&lt;p&gt;Backtracking is a brute-force approach that finds all possible solution. What is brute-force? Brute-force exhaustively analyze the input and constraints to find a specific solution. Basically, backtracking is find the way of your way through a cornfield maze. &lt;/p&gt;

&lt;h1&gt;
  
  
  How does it relate to a maze in 4 steps?
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Enter the cornfield &lt;/li&gt;
&lt;li&gt;Take a path. Ex. Left, right&lt;/li&gt;
&lt;li&gt;if Dead end, trace back aka "backtrack" to the intersection and explore another path.&lt;/li&gt;
&lt;li&gt;Repeat steps 2 and 3 until you find exit.&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Explanation
&lt;/h1&gt;

&lt;p&gt;The maze in essence is related to backtracking because we traced back one step and made a different turn to find another possible solution.&lt;/p&gt;

&lt;h1&gt;
  
  
  More notes about backtracking
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Represented in a State-Space Tree&lt;/li&gt;
&lt;li&gt;Bounding function is used to add constraints to find the desired solutions. Note: We can have more than one solution in backtracking. &lt;/li&gt;
&lt;li&gt; DFS!!!! [DFS = forms a root to child "path" by "path"]&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  More notes about brute-force
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;DP is a brute-force approach that finds the maximum or minimum optimal solution.&lt;/li&gt;
&lt;li&gt;Branch and Bound is also a brute-force approach that is BFS[form the tree level by level].&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  Challenge: Solve the n-queen problem
&lt;/h1&gt;

&lt;h2&gt;
  
  
  Background
&lt;/h2&gt;

&lt;p&gt;Queen is most valuable piece on the board...actually, the king is most valuable because if you don't have a king, you lose. The queen is the 2nd most valuable piece on the chess board. This piece is able to move diagonally, horizontally, and vertically. Your job is to place 7 queens on a chess board where each queen does not pose any threat to each other.&lt;br&gt;
I have solved this challenge in 10 minutes. Can you beat it?&lt;/p&gt;

&lt;h1&gt;
  
  
  Steps
&lt;/h1&gt;

&lt;ol&gt;
&lt;li&gt;Navigate to the link below and have fun:
Click on me [&lt;a href="https://www.chess.com/analysis"&gt;https://www.chess.com/analysis&lt;/a&gt;]&lt;/li&gt;
&lt;li&gt;Click on 'Setup' and 'Trashcan' icon&lt;/li&gt;
&lt;li&gt;Have fun!&lt;/li&gt;
&lt;/ol&gt;

&lt;h1&gt;
  
  
  My Solution
&lt;/h1&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--L6K8PIJO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1c0253668qtdpxwkgbd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--L6K8PIJO--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1c0253668qtdpxwkgbd.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Reference: &lt;br&gt;
Introduction to Backtracking [&lt;a href="https://www.youtube.com/watch?v=DKCbsiDBN6c"&gt;https://www.youtube.com/watch?v=DKCbsiDBN6c&lt;/a&gt;]&lt;/p&gt;

</description>
      <category>leetcode</category>
      <category>backtracking</category>
    </item>
    <item>
      <title>Day 2-4: My Driver's Test </title>
      <dc:creator>Justin Chan</dc:creator>
      <pubDate>Wed, 07 Jul 2021 00:11:02 +0000</pubDate>
      <link>https://dev.to/justinc16/day-2-4-my-driver-s-test-4ble</link>
      <guid>https://dev.to/justinc16/day-2-4-my-driver-s-test-4ble</guid>
      <description>&lt;p&gt;I have been preparing for my driver's test for the past few days. I have practiced more than 50 hours on various maneuvers such as 3-point turns and parking. However, as you would later find out, it would not be enough. &lt;/p&gt;

&lt;p&gt;On the exam day,  my mother woke me up 1 hour before the big test. She said that we would have to report to the test site early since there were already other vehicles lining up. I hurried to brush my teeth, get dressed, grab my breakfast. Within 5 minutes, my mother and I were heading out of the door. &lt;/p&gt;

&lt;p&gt;When we arrived at the test site, other people were waiting for their turn. Based on my view, approximately six cars were waiting at the location. My mother parked the car behind the last queued car. We switched seats since I would be taking the road test.&lt;/p&gt;

&lt;p&gt;As time passed by, each vehicle left with the examiner. We slowly inched closer to the starting point. At some point, I was tired of starting my car and moving 3 feet closer for every car that leaves the test site. That was a big mistake on my part. I should have moved closer no matter what the circumstances. &lt;/p&gt;

&lt;p&gt;A lady was walking on the sidewalk and speaking on the phone simultaneously. On top of that, she glared at me. In my mind, I was like, "Why are you looking at me like that?" I was completely confused since I didn't know her. It looked like I did something which she would never forgive me. She slowly walked on the empty parking spot in front of me and walk back on the sidewalk. Then a car drove by and intended to park in that spot. In my mind, I had to inch closer to prevent this unforgivable action. Everybody is waiting patiently and these people have to cut the line. The following steps were the actions that I have taken: &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Turned on the car&lt;/li&gt;
&lt;li&gt;I thought I had the car in drive and pushed on the gas pedal, but the car engine just revved. &lt;/li&gt;
&lt;li&gt;Placed the car in Drive and moved forward to prevent the female driver from parking.&lt;/li&gt;
&lt;li&gt;The funny thing is these two ladies went out their way to coordinate, but she had to drive to the back of the line.🤣🤣🤣&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Back to the story.....&lt;br&gt;
When we were the first one on the line and the examiner approached the car, we gave the name[Justin] and time[8:30 AM]. I thought it was my turn to take the test; only in your dreams! The examiner told us to wait for the next batch of examiners. &lt;/p&gt;

&lt;p&gt;So we waited and waited.....&lt;/p&gt;

&lt;p&gt;And waited...&lt;/p&gt;

&lt;p&gt;A lady examiner approached our car and told us to move closer to the curb. Note that if the car hits the curb, I would fail. As a result, I mumbled to my mother, "Tell me when!" As I was moving closer to the curb, my mother was like "More, more." That gave the examiner a bad impression from the start.  She also asked us to wipe everything because of COVID. She stated her script and I stated without thinking, "Can I start?" That probably angered her even more! I feel like she intended to fail me from the start by instructing me to take the most difficult route[Note that no other driver took that route]. &lt;/p&gt;

&lt;p&gt;My route&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;All-way stop &lt;/li&gt;
&lt;li&gt;A left turn to the narrow street&lt;/li&gt;
&lt;li&gt;Right turn at an intersection&lt;/li&gt;
&lt;li&gt;Drive through the curvy road&lt;/li&gt;
&lt;li&gt;Parking&lt;/li&gt;
&lt;li&gt;Right turn &lt;/li&gt;
&lt;li&gt;Right turn&lt;/li&gt;
&lt;li&gt;3-point turn&lt;/li&gt;
&lt;li&gt;3 Right turns &lt;/li&gt;
&lt;li&gt;All-way stop&lt;/li&gt;
&lt;li&gt;Pull up to the curb
&lt;/li&gt;
&lt;li&gt;Done &lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;I received the envelope and scored a 70% on the road test. I passed! 🍾🥂 This day was an emotional roaster!&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--7fbaSON0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zrb63ne6qk2nldnktyqr.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--7fbaSON0--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zrb63ne6qk2nldnktyqr.png" alt="image"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What did I learn?&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;First Impression Counts!!&lt;/li&gt;
&lt;li&gt;Speed up to not impede traffic. I drive at 10 mph when the speed limit is 25 mph. &lt;/li&gt;
&lt;li&gt;Check all mirrors before any maneuvers. Ex. Parallel Parking&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>learning</category>
      <category>driving</category>
      <category>knowledge</category>
    </item>
    <item>
      <title>Day 1: My Leetcode Adventure[2]</title>
      <dc:creator>Justin Chan</dc:creator>
      <pubDate>Sun, 04 Jul 2021 10:10:32 +0000</pubDate>
      <link>https://dev.to/justinc16/day-1-my-leetcode-adventure-2-2h1n</link>
      <guid>https://dev.to/justinc16/day-1-my-leetcode-adventure-2-2h1n</guid>
      <description>&lt;p&gt;How many problems have I solved? Uno&lt;br&gt;
&lt;strong&gt;&lt;a href="https://leetcode.com/problems/build-array-from-permutation/"&gt;Leetcode 1920: Build Array from Permutation&lt;br&gt;
&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;*Return same-length zero-based permutation nums array[array of distinct integers from 0 to nums.length-1] in which ans[i] = nums[nums[i]] where 0&amp;lt;=i&amp;lt; nums.length. *&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;My Answer&lt;/strong&gt;&lt;br&gt;
This is the first problem I had for the Weekly Contest 248. On top of that, it has been a while since I visited this website. I am starting the grind to secure a job after graduation.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;newArr=[]
        for i in range(len(nums)):
            # Calculate the new val
            temp=nums[nums[i]]
            newArr.append(temp)
        return newArr
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Better 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; return [nums[nums[i]] for i in range(len(nums))]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Even BetterSolution&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; return [nums[i] for i in nums]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;What I learned?&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;List comprehensions can reduce code snippets&lt;/li&gt;
&lt;li&gt;Sometimes I am extra like finding the same element[Ex. nums[i]]
Instead of...
&lt;code&gt;newArr=[]
    for i in range(len(nums)):
        # Calculate the new val
        temp=nums[nums[i]]
        newArr.append(temp)
    return newArr&lt;/code&gt;
Do...
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;return [nums[i] for i in nums]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;a href="https://leetcode.com/problems/eliminate-maximum-number-of-monsters/"&gt;Leetcode 1921: Build Array from Permutation&lt;br&gt;
&lt;/a&gt;&lt;/strong&gt;&lt;br&gt;
*Given monsters arr and speed arr where monster i travels at speed j. You are allowed to kill any monsters starting from minute 0. Keep in mind that monsters are not allowed to be killed in the middle of a minute and only one monster can be killed at the start of the minute. If monster reaches city == start of the minute, you still lose. Return the max numbers of monsters you can kill before the monster reach your city. *&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Answer&lt;/strong&gt;&lt;br&gt;
I had trouble with this question because I was not able to solve it completely with 3 attempts. Every attempt got me closer to the answer. &lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Attempt 1 = 53/150 Cases passed&lt;/li&gt;
&lt;li&gt;Attempt 2 = 105/150 Cases &lt;/li&gt;
&lt;li&gt;Attempt 3 = 116/130 Cases&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Attempt 1&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;killed=0
        while dist or sum(1 for d in dist if d&amp;lt;0)&amp;gt;0:
            killed +=1 
            #Remove 1st monster
            dist.pop(0)
            speed.pop(0)
            # Subtract dist and speed 
            for i in range(len(dist)):
                dist[i]-=speed[i]
        return killed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;My first approach was to kill the first monster, but it is flawed because the other monsters could be faster. In addition, the second condition[sum(1 for d in dist if d&amp;lt;0)&amp;gt;0] doesn't make sense. It should stop iterating if monster reached the city.&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;Attempt 2&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;killed=0
        # print(sum(1 for d in dist if d&amp;lt;=0))
        while dist and sum(1 for d in dist if d&amp;lt;=0)==0:
            print(dist)
            print(speed)
            # print("Killing...")
            for i in range(len(dist)):
                dist[i]-=speed[i]
            print(dist)
            # Remove a monster 
            delMon=[ind for ind,elem in enumerate(dist) if elem &amp;lt;=0]
            delMonInd= delMon[0] if delMon else dist.index(min(dist))
            print("Killed #", killed,", Index:",delMonInd, ", Value:",dist[delMonInd] )
            dist.pop(delMonInd)
            speed.pop(delMonInd)

            killed +=1 
        return killed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;2nd approach was to dist-speed and then find all monster that have entered the city and remove 1st occurence. The problem is for the else condition where some monster can advance to the city faster so I can't just remove the monster with the shortest distance.&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;Attempt 3&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;killed=0
        while dist and sum(1 for d in dist if d&amp;lt;=0)==0:
            for i in range(len(dist)):
                dist[i]-=speed[i]
            # Remove a monster 
            delMon=[ind for ind,elem in enumerate(dist) if elem &amp;lt;=0]
            special=[]
            for i in range(len(dist)):
                special.append(int(dist[i]/speed[i]))
            # delMonInd= delMon[0] if delMon else self.turnsLeft(dist,speed)
            delMonInd= delMon[0] if delMon else special.index(min(special))
            dist.pop(delMonInd)
            speed.pop(delMonInd)
            killed +=1 

            # print("Killed #", killed,", Index:",delMonInd )
        return killed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;3rd approach was to take current result and perform division between distance and speed. This helps resolve the closest monster problem and this is the right approach. However, time exceeded for this approach.&lt;/em&gt;&lt;br&gt;
&lt;strong&gt;Better 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;arr=[]
            #Store the minute time for each monster
            for i in range(len(dist)):
                minute=math.ceil(dist[i]/speed[i] )
                arr.append(minute)
            arr.sort()
            killed=0
            #Count number of monster killed
            for m in range(len(arr)):
                if(arr[m]&amp;lt;=m):
                    return killed
             killed+=1
            return killed
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;This sorting approach is O(nlogn) which is simple and fast. First, it converts to comparable units. Then it sorts to decide which monster to remove during minute m. Remove monsters until monster has reached the city.  *&lt;br&gt;
**What I learned?&lt;/em&gt;*&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;How to check array for emptiness? if arr: T else F&lt;/li&gt;
&lt;li&gt;How to write a ternary operator in Python? "Y" if T else 'N'&lt;/li&gt;
&lt;li&gt;How to move elements from array? .pop()!!!&lt;/li&gt;
&lt;li&gt;Condition should be better&lt;/li&gt;
&lt;li&gt;math.ceil() vs math.floor()&lt;/li&gt;
&lt;li&gt;Reassigning func list has no effect on outside&lt;/li&gt;
&lt;li&gt;Include code to main flow--&amp;gt; Transfer to a function&lt;/li&gt;
&lt;li&gt;zip(lst1,lst2)--&amp;gt; Create a zip object of tuple pairs from both list&lt;/li&gt;
&lt;/ol&gt;

</description>
      <category>leetcode</category>
      <category>learning</category>
      <category>knowledge</category>
    </item>
  </channel>
</rss>
