Tuesday, July 4, 2023

Leetcode | 2022 practice

 10 algorithms practiced most challenge:

    1. Step-By-Step Directions From a Binary Tree Node to Another
      C# | Post order | Study discuss post | Solve TLE problem
    1. Expression Add Operators (Hard level)
      C# | Excellent learning opportunity
      C# | Learning opportunities
    1. Reorder Routes to Make All Paths Lead to the City Zero
      C# | Graph problem | Quick study | Warmup
    1. Shortest Subarray with Sum at Least K
      C# | Design a deque | Review Array - Subarray | 2022 March

Jan. 17, 2022
I came back to work on algorithm since I had to prepare for Microsoft Vancouver online assessment.


257. Binary Tree Paths
C# | Warmup | Backtracking | StringBuilder | Jan 16, 2022

1647. Minimum Deletions to Make Character Frequencies Unique
C# | Copy the idea and warm up | Keep it simple using one hashset and one array | Jan 16 2022

151. Reverse Words in a String
C# | Warmup for onsite | Define start and end char in a word | 2022 Jan 19
C# | Define start and end index of a word | C# StringBuilder Insert API | Insert the word right away


210. Course Schedule II
C# | DFS algorithm | Study Leetcode discuss C# code | Jan. 26, 2022
C# | DFS solution | Java code -> C# code | Jan. 26, 2022

874. Walking Robot Simulation
C# | Warmup algorithm and short review | 2018 Oct. 14

692. Top K Frequent Words
C# | Warmup practice | Sort once | Using simple data structure

636. Exclusive Time of Functions
C# | Opportunistic learning | Study C# code
C# | Lessons learned | My failed practice 54/120 test cases in 2020

263. Ugly Number
C# | Practice in Sept. 14, 2018
Feb. 8, 2022

1405. Longest Happy String
C# | Failed test case 24/35 | Think more carefully in design
C# | Greedy on char with largest count only | Longest is not easy

545. Boundary of Binary Tree
C# | Definition of left boundary | Reasoning behind | Feb. 2022

827. Making A Large Island
C# | Map each island using integer | Hashmap | DFS | Failed a few times

424. Longest Repeating Character Replacement
C# | Sliding window technique | mock interview

98. Validate Binary Search Tree
C# | Mock interview practice | Interviewer with MSFT, Amazon, Oracle, Meta | 2022 Feb. 23

March 4, 2022
Microsoft Vancouver onsite - Four rounds of algorithm interviews
I passed online code assessment in Dec. 2021, but I preferred to have more practice before I had onsite interview. I chose not to move fast because I did not practice very often from May to Dec. 2021. That is a big mistake, I should not stop practice Leetcode algorithms. It takes time to warm up and also it takes time to build habit to think hard on Leetcode algorithms, and also patience is a skill to learn again after six months break.

I continued to practice Leetcode algorithms to prepare Meta phone screen on March 21, 2022..

March 2022, monthly review
March 1 - March 7
It takes self-discipline to work on Leetcode algorithms continuously. I only have less than 200 submissions last 12 months, compared to Dr. Lai (https://leetcode.com/justyy/), over 1500 submissions, I like to learn from him. Hard work is most important, and I should discipline myself and then I can improve my crafting skills and problem solving skills.
Algorithms I worked on, 408, 1650, 1762, 314, 498, 162, 523, 282
I started to work on algorithms from votrubac, top-voted solutions: Another 11 algorithms, 1162, 1553, 992, 934, 1249, 1092, 1062, 959, 983, 1544

March 6, 2022
408. Valid Word Abbreviation
C# | learn from failed test cases


1650. Lowest Common Ancestor of a Binary Tree III
C# | Space complexity O(1)

1762. Buildings With an Ocean View
C# | Using stack

314. Binary Tree Vertical Order Traversal
C# | Preorder traversal | Record total count, horizontal, level in the tree
March 7, 2022
498. Diagonal Traverse
C# | Automatically direction change

162. Find Peak Element
C# | Binary search algorithm

523. Continuous Subarray Sum
C# | Learn from failed test cases
March 5, 2022
Algorithm to plan to work on
I chose to work on Leetcode with tag Facebook, last six month most frequent algorithms.
Algorithm 314, 1762, 1570, 1650, 528, 408, 339, 71, 31, 670;
10 more: 498, 162, 523, 282, 1891, 1011, 708, 1382, 1539, 2060;
10 more: 266, 286, 1868, 1216, 348, 778, 536, 270, 1428, 825;
10 more: 269, 658, 16, 2056, 43, 1060, 380, 1344, 1522, 1944;
10 more: 1644, 2076, 689, 1424, 983, 224, 298, 742, 767, 333;
10 more: 2019, 2033, 238, 1541, 1209, 2071, 296, 1244, 2065, 2081;
10 more: 406, 271, 1498, 735, 163, 1245, 1559, 247, 2025, 1197;
723, 2092, 62, 540, 1985
work hard | Hard work beats talent
Do not think too much. Just work on more algorithms. Solve as many algorithms as I can. Once I solve another 20 algorithms, stop and review what I have learned, and then continue to solve another 20 algorithms.
I wrote a blog to document my plan on March 5, 2022. Good planning is important for success for any project.
I understood that I could not solve all those algorithms, I tried to go over some algorithms quickly, and found those most intersting and practice one or two, and I tried different ideas to learn from others from discuss post.

March 8, 2022
282. Expression Add Operators (Hard level)
C# | Excellent learning opportunity
C# | Learning opportunities
March 16, 2022
39. Combination Sum
C# | Warmup practice | DFS | Backtracking | 2022

128. Longest Consecutive Sequence
C# | Time complexity: O(N) | Python code converted to C#

C# | Time complexity: O(N), N is length of the array
Leetcode 78 subsets, 90 Subset II, 77 combinations, 40 Combinations Sum II, 377 Combination Sum IV
Plan to type those python code from Li Yin's book, and then convert those code into C# code.

March 17, 2022

15. 3Sum
C# | Time complexity: O(N^2) | Review code written in 2016
C# | Warmup in 2022 | Using HashSet to remove duplicate
C# | Time complexity O(N^2) | Remove duplicate by sorting, no use of HashSet
C# | Time complexity O(N^2) | Use HashSet

18. 4Sum
C# | Sorting, sliding window, skip duplicate | Time: O(N^3)

454. 4Sum II
C# | Optimal time complexity: O(N^2), not O(N^3)

March 21, 2022

2096. Step-By-Step Directions From a Binary Tree Node to Another

C# | Preorder | TLE error 287/332
The above practice shows that I have weakness in terms of design recursive function, use post order traversal or preorder, TLE issue.

C# | Post order | Study discuss post | Solve TLE problem

C# | Post order traversal | Solve TLE problem
I practice near 10 algorithms last weekend, and I chose to study discussion post written by votrubac top-voted discuss posts.
I found so many issues and learned how to solve a few more algorithms related to DP, tree and graph problems.
March 21, 2022

1162. As Far from Land as Possible
C# | BFS | All land nodes into Queue | Search together

1553. Minimum Number of Days to Eat N Oranges
C# | Understand the subproblems

992. Subarrays with K Different Integers
C# | At most K | Two subproblems -> difference

934. Shortest Bridge
C# | Apply DFS to visit all nodes in each island | Brute force two nodes' distance

1249. Minimum Remove to Make Valid Parentheses
C# | Using stack | * to replace unmatched parentheses


1092. Shortest Common Supersequence
C# | LCS DP solution first | Learn C# code

1062. Longest Repeating Substring
C# | DP solution | Time complexity: O(N^2)

959. Regions Cut By Slashes
C# | DFS | Understand the problem

983. Minimum Cost For Tickets
C# | DP solution | learn 20 algorithms from one player

1524. Number of Sub-arrays With Odd Sum
C# | New idea to find algorithm to work on | Solutions tag

March 22, 2022
1675. Minimize Deviation in Array
C# | Learn the analysis first | Learn C# code

March 31, 2022
1466. Reorder Routes to Make All Paths Lead to the City Zero
C# | Graph problem | Quick study | Warmup

1320. Minimum Distance to Type a Word Using Two Fingers
C# | DP solution | Need more time

April 1, 2022
1371. Find the Longest Substring Containing Vowels in Even Counts
C# | "aeiou" -> one integer, 5 bits, 32 variations | Warmup for Meta onsite

1721. Swapping Nodes in a Linked List
C# | One pass | Kth node and kth to Last | Secret - one follower starts kth node

My plan | From my blog | April1, 2022 | Leetcode algorithm practice: votrubac | Top voted algorithms | Another 15 algorithms
Quickly go over 15 algorithms, think about five minutes for each algorithm;
Sort algorithms by priority, work on high priority ones first;
Work on challenging ones first.
Think about how to reward myself for the hard work
Time start: April 1, 2022 6:43 PM

I did code two algorithms, first one I was not lucky and I coded a brute force solution, and I learned from two comparison solution from votrubac.
1547. Minimum Cost to Cut a Stick
C# | Hard level | DFS + Memo | Opportunistic algorithm review for Meta onsite

Next I worked on a hard level algorithm, and I studied C# solution and wrote my own solution.

Next I followed advice from votrubac, reviewd a few algorithms, 312, 1000, 1039. I came cross my discuss post written in 2019 with 6 up-votes, I just could not believe that I did such great job to write something for this hard level algorithm in 2019. I was three year younger, and passion about algorithms, specially on DP solutions.

  1. Minimum Cost to Merge Stones
    C# dynamic programming solution bottom up - how to hack one?

I just could not believe that I could have same amount time to work on the hard level algorithm again this time. I quickly reviewed and skipped to next algorithm.

  1. Ways to Split Array Into Three Subarrays
    C# | Linear solution | First divide position any, second one is in a range

It is much better to work on algorithms for a few hours, so that I could go over at least 10 algorithms and learn so many things during my study and practice. I started to code for an easy algorithm first, and then go back to write again using a simple solution, and learn lesson for my reading skills, and then I have chance to work on hard level algorithms, and review related algorithms. This experience definitely is best I can do to prepare.
Total hours: 4+ hours algorithm practice

April 12, 2022
I worked on a hard level algorithm, 1542. Find Longest Awesome Substring, my two practices:

  1. Failed practice, C# | First position | Bit manipulation | Failed: "9498331" | Lessons learned;
  2. Study code, C# | Last position | Bit manipulation | Study code

36. Valid sudoku
C# | Encode 1-9 digits to each row and each column and 9 3 x 3 matrix
C# | Encode string - three ideas to map same row, same column, same 3 x 3 matrix
C# | Validation process | HashSet


295. Find Median from Data Stream (Hard level)
I chose to write a different solution to make my life easy. I like to have simple function, easy to follow. So, I wrote down my approch, and then wrote C# code and played with the idea.
C# | Minimum heap and maximum heap | SortedSet<Tuple<int, int>>

  1. Game of Life
    C# | in-place algorithm using module 10

Day 29

  1. Find K Pairs with Smallest Sums
    C# | Minimum heap | SortedSet<Tuple<int, int, int> | Two neighbors: right and bottom

May 19, 2022
1632. Rank Transform of a Matrix
C# | Quick learner | Union find algorithm | Illustration using diagrams

  1. Maximum Number of Consecutive Values You Can Make
    C# | Quick learner | I tried 10 minutes to figure out something first

May 17, 2022
1855. Maximum Distance Between a Pair of Values
C# | Two sorted arrays | Two pointer technique | Linear time complexity

  1. Largest Multiple of Three
    C# | Quick learner | At most remove two digits

  2. Maximum Sum of Two Non-Overlapping Subarrays
    C# | Quick learner | 30 minutes to study

May 12, 2022
907. Sum of Subarray Minimums
C# | Quick learner | Using stack O(N) time complexity | Play for fun

  1. The k Strongest Values in an Array
    C# | Quick learner | two pointers

May 5, 2022
1915. Number of Wonderful Substrings
C# | Quick learner | 10 bits to represent a - j count as odd or even

  1. Min Cost to Connect All Points
    C# | Quick learner | Union find algorithm

May 4, 2022
2062. Count Vowel Substrings of a String
C# | Quick learner | Sliding window

May 3, 2022
984. String Without AAA or BBB
C# | Quick learner | greedy idea

  1. Queue Reconstruction by Height
    C# | Quick learner from an example

May 2, 2022
255. Verify Preorder Sequence in Binary Search Tree
C# | 15 minutes as a quick learner | Recursive solution

  1. Number of Digit One
    C# | 30 minutes to work on hard level

  2. Find K Pairs with Smallest Sums
    C# | Minimum heap | SortedSet<Tuple<int, int, int> | Two neighbors: right and bottom

  3. Game of Life
    C# | in-place algorithm using module 10

  4. Number of Matching Subsequences
    C# | Group words by starting char | Avoid TLE error

  5. Binary Watch
    C# | Bit manipulation | DFS algorithm

April 26, 2022
458. Poor Pigs
C# | Hard level | One pig can test multiple buckets a time

April 25, 2022
234. Palindrome Linked List
C# | Optimal solution O(N) time complexity O(1) space
C# | Review of my practice in July 2018

  1. Wiggle Sort II
    C# | Using Sorting to find median value

April 22, 2022
295. Find Median from Data Stream
C# | Minimum heap and maximum heap | SortedSet<Tuple<int, int>>

April 19, 2022
11. Container With Most Water
C# | Two pointers technique | Time complexity: O(N)

#MetaOnsite #onsite #Facebook #SystemDesign


No comments:

Post a Comment