Tuesday, June 7, 2022

Leetcode: Google tagged | Hard level algorithms | Last six months | 55 algorithm problems

 Hard level - 55 algorithms to work on 


1284
1944
428
1096
847
1597
2158
296
1948
778
632
302
272
753
1240
425
410
1793
1235
920
465
679
1263
588
642
772
407
1499
499
815
1345
818
727
843
631
60
315
1397
1606
2188
224
460
354
358
1610
871
68
269
803
2092
2242
2132
564
420


Save time | Six months | $50,000 US dollars | SABR 6700 shares

June 7, 2022

I like to try to figure out if this idea will work out perfectly or not. That is to invest in bear market, and then the plan is to save time, invest one stock, $50,000 US dollars and then do not trade in next six months.

Investing and my plan

It is hard for me to learn and also be a good investor. I have tried so many things in last two years, but I could not learn how to stay in the market and choose good stocks for me in the long run and good price to get in. 

I did make choice to purchase SABR stock recently, after I cut loss around $11,000 US dollars. I like to learn one business, software company SABRE. 

Lessons learned from CVE, MRO

I like to learn from my own lessons. Stay in the market. Do not try to time the market. Invest long term. 

Insider trading log



3 ideas to think about as an investor


  1. There is less than 3% interest rate in saving account; 
  2. I should be super patient to be a smart investor. Read more articles about investing in Chinese; 
  3. Do not trade too often. 
  4. Try to make a long term plan.
  5. Learn to take some risks. 
  6. Stay positive no matter what. 


Leetcode discuss: 127. Word Ladder

June 7, 2022

Here is the link. 


BFS - C# - Backtracking - Tips

Dec. 7, 2020
Introduction
Based on the comment from Google | Onsite | Find Most Similar Path In Graph, I had chance to review this algorithm and practice second time.

BFS algorithm
I like to take down a few notes in the following:

  1. beginWord may not be in the dictionary;
  2. BFS algorithm - avoid deadloop, mark visited for each word is important, here is to remove word from HashSet.
  3. Time complexity: O(M * M * N), M is length of word, N is length of dictionary.
  4. For each char in the word, replace it using 'a' to 'z', at most 26 choices. So total choice is 26 * M.
  5. C# string.ToCharArray() is easy to back track.
  6. put depth++; statement before for loop since it is easy to make mistake to put in wrong places with more than one '}'.
public class Solution {
    public int LadderLength(string beginWord, string endWord, IList<string> wordList) {
        if (beginWord == null || endWord == null || wordList == null || wordList.Count == 0)
            {
                return 0;
            }

            var dict = new HashSet<string>(wordList);
            if (!dict.Contains(endWord))
            {
                return 0;
            }

            var queue = new Queue<string>();

            queue.Enqueue(beginWord);

            // mark visit
            dict.Remove(beginWord);

            int depth = 0;

            while (queue.Count > 0)
            {
                int levelCount = queue.Count;
                depth++;
                // Check each adjacent string
                for (int i = 0; i < levelCount; i++)
                {
                    var current = queue.Dequeue();
                    var charArray = current.ToCharArray(); 

                    // base case
                    if(current.CompareTo(endWord) == 0)
                    {
                        return depth; 
                    }

                    // go over each char in the word first                    
                    for (int index = 0; index < charArray.Length; index++)
                    {
                        // replace by all possible neighbors
                        for (char c = 'a'; c <= 'z'; c++)
                        {
                            if (c == current[index])
                            {
                                continue;
                            }

                            var tmp = charArray[index];
                            charArray[index] = c;
                            var next = new string(charArray);                             

                            if (dict.Contains(next))
                            {
                                queue.Enqueue(next);
                                dict.Remove(next);
                            }

                            //backtracking
                            charArray[index] = tmp;
                        }
                    }                    
                }                
            }

            return 0;
    }
}

Comment

Leetcode discuss: 127. Word Ladder

 June 7, 2022

Here is the link. 

C# BFS practice in 2019

It is the graph problem and I like to apply breadth first search algorithm in my practice.

Here are highlights:

  1. Understand how to construct a graph from startWord to endWord with distance one between each connected words, and all connected words should be in given dictionary;
  2. Understand breadth first search, count the queue's size before visiting each node in the queue;
  3. Mark visited node in the graph, using tip to remove word from dictionary. Also argue that it should be OK for different paths to mark visited node.
  4. Work on coding style, make sure that code is readable and the result is correct.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _127_word_ladder
{
    class Program
    {
        static void Main(string[] args)
        {
        }

        /// <summary>
        /// code review on 2019 July 2
        /// </summary>
        /// <param name="beginWord"></param>
        /// <param name="endWord"></param>
        /// <param name="wordDict"></param>
        /// <returns></returns>
        public int LadderLength(string beginWord, string endWord, IList<string> wordDict)
        {
            if (beginWord == null || endWord == null || wordDict == null || wordDict.Count == 0)
            {
                return 0;
            }

            var dict = new HashSet<string>(wordDict);
            if ( !dict.Contains(endWord))
                return 0; 

            var queue = new Queue<string>();

            queue.Enqueue(beginWord);
            
            // mark visit
            dict.Remove(beginWord); 

            int depth = 1;

            while (queue.Count > 0)
            {
                int count = queue.Count;
                depth++; 

                // Check each adjacent string
                for (int layer = 0; layer < count; layer++) 
                {
                    var current = queue.Dequeue();

                    // go over each char in the word first                    
                    for (int index = 0; index < current.Length; index++)
                    {
                        // replace by all possible neighbors
                        for (char c = 'a'; c <= 'z'; c++)
                        {
                            if (c == current[index])
                            {
                                continue;
                            }

                            var neighbor = stringReplaceAt(current, index, c); // C# string is immutable

                            // find endWord
                            if (neighbor.CompareTo(endWord) == 0)
                            {
                                return depth;
                            }

                            if (dict.Contains(neighbor))
                            {
                                queue.Enqueue(neighbor);
                                dict.Remove(neighbor);
                            }
                        }
                    }
                }                
            }

            return 0;
        }

        /// <summary>
        /// write my own function
        /// </summary>
        /// <param name="s"></param>
        /// <param name="index"></param>
        /// <param name="c"></param>
        /// <returns></returns>
        private static String stringReplaceAt(String s, int index, char c)
        {
            var array = s.ToCharArray();
            array[index] = c;

            return new String(array);
        }
    }
}

Read More

@jianminchen I have been following your answers for quite some time, It's nice to see how you improve over the time and revisit the problems and improve on top of it.
Any advise for the fellow mate on how you keep track of older problems to revisit ?

0
Hide 1 reply
Reply
Share
Report
jianminchen's avatar
Read More

Any advise for the fellow mate on how you keep track of older problems to revisit ?
I think that it is so hard to find time to revisit problems solved before. I solved around 692 problems. My goal is to learn from the best, like votrubac - https://leetcode.com/votrubac/, so recently I chose top voted discussion posts from votrubac, and then review top voted 60 algorithms. I had chance to review some algorithms, and I did compare my performance to votrubac.

I also tried to use Leetcode -> My List, and then create a few lists to document my practice to prepare for onsite interviews. Last two year, I also paid Leetcode premium, so that I can read solutions provided by Leetcode for premium users, and also work on mock interview from Amazon, Microsoft, Google, Facebook.

I also spend time to write some discuss post to list algorithms I practice, even though it is hard for players to find the page, I try my best to get feedbacks from peers. https://leetcode.com/discuss/interview-question/1914695/30-days-to-meta-onsite-4th-facebook-onsite-system-design-daily-update-day-29

One more discuss post: https://leetcode.com/discuss/general-discussion/994399/leetcode-600-questions-beyond-strategies-and-tips

0
Reply
Share
Edit
Delete
venendroid's avatar
Read More

@jianminchen Nice solution.