Wednesday, October 7, 2020

Leetcode discuss: Number of Atoms

 Here is the link. 

Oct. 7, 2020
Introduction
It is time for me to start to work on 50 hard level algorithms in next four weeks. I came cross this algorithm, and I like to review my last practice two year eight months ago.

Code review
I will write a quick and short review for my last practice. Stay tuned.

public class Solution {
    public string CountOfAtoms(string formula) {
      
            string result = string.Empty;
            // "K4(ON(SO3)2)2"
            int open_brackets = 0;
            var d = new Dictionary<string, int>();
            var name_stack = new Stack<string>();
            var value_stack = new Stack<int>();

            for (int i = 0; i < formula.Length; i++)
            {
                string atom = string.Empty;
                //(H2O)2
                if (formula[i] == '(')
                {
                    open_brackets++;
                    name_stack.Push("(");
                    value_stack.Push(-1);
                }
                else if (formula[i] == ')')
                {
                    open_brackets--;

                    i++;
                    while (i < formula.Length && char.IsDigit(formula[i]))
                    {
                        atom += formula[i];
                        i++;
                    }

                    i--;
                    int mul = atom.Length > 0 ? int.Parse(atom) : 1;

                    if (open_brackets > 0)
                    {
                        var temp_name_stack = new Stack<string>();
                        var temp_value_stack = new Stack<int>();

                        while (name_stack.Count > 0 && name_stack.Peek() != "(")
                        {
                            temp_name_stack.Push(name_stack.Pop());
                            temp_value_stack.Push(value_stack.Pop() * mul);
                        }

                        name_stack.Pop();
                        value_stack.Pop();

                        while (temp_name_stack.Count > 0)
                        {
                            name_stack.Push(temp_name_stack.Pop());
                            value_stack.Push(temp_value_stack.Pop());
                        }
                    }
                    else
                    {
                        while (name_stack.Count > 0 && name_stack.Peek() != "(")
                        {
                            if (d.ContainsKey(name_stack.Peek()))
                            {
                                d[name_stack.Pop()] += mul * value_stack.Pop();
                            }
                            else
                            {
                                d.Add(name_stack.Pop(), mul * value_stack.Pop());
                            }
                        }

                        name_stack.Pop();
                        value_stack.Pop();
                    }
                }


                else if (char.IsLetter(formula[i]) && char.IsUpper(formula[i]))
                {
                    atom += formula[i];
                    i++;

                    while (i < formula.Length && char.IsLower(formula[i]))
                    {
                        atom += formula[i];
                        i++;
                    }

                    string value = "";

                    while (i < formula.Length && char.IsDigit(formula[i]))
                    {
                        value += formula[i];
                        i++;
                    }

                    i--;
                    int m = value.Length > 0 ? int.Parse(value) : 1;
                    if (open_brackets > 0)
                    {
                        name_stack.Push(atom);
                        value_stack.Push(m);
                    }
                    else
                    {
                        if (d.ContainsKey(atom))
                        {
                            d[atom] += m;
                        }
                        else
                        {
                            d.Add(atom, m);
                        }
                    }
                }
            }

            while (name_stack.Count > 0)
            {
                if (d.ContainsKey(name_stack.Peek()))
                {
                    d[name_stack.Pop()] += value_stack.Pop();
                }
                else
                {
                    d.Add(name_stack.Pop(), value_stack.Pop());
                }
            }

            d.OrderBy((x) => x.Key).ToList().ForEach(y =>
            {
                result += y.Key;
                if (y.Value > 1)
                {
                    result += y.Value;
                }
            });

            return result;
        }
}

1531. String Compression II

 Oct. 7, 2020

Introduction

I came cross this algorithm as a Google phone screen algorithm on Leetcode discussion post, so I like to spend time to work on. 

A few good ideas

I like to read carefully the following two discussion post. 

One is in Chinese. Another one is written in C#. 

Here is the solution I like to learn from. The player is top 2000 in Leetcode weekly contest. 



ENBL stock: Hold less than two weeks - too short to be an investor

INTC stock: Do not try to beat the market!

 Oct. 22, 2020

It is easy for me to come out my intel stock purchase history. As an investor, I have to learn so many lessons. I have so many money management issues, all those shows up in my management of Intel stocks. 

My purchase history

I started to purchase one time 170 shares. It is almost 50% of my IRA Ameritrade.com account balance. It went down 10% next 30 days. I was patient to wait and started to read more about Intel as a business. 



NCLH stock: Beginner mistake - one week to hold, two weeks 10% up


 NCLH has potential to bring into $3/ share capital gains from Sept. 18 to Oct. 9, less than one month. I did purchase 240 shares, but I was in panic since pullback. I sold all 240 shares at price of $15.82 on Sept. 26, 2020. Otherwise I would have capital gains of near $720 dollars in less than one month. 

I do believe that the article talking about re-sail of NCLH after restriction of coronavirus helped a lot of retail investors to bet on stock for short term return. 

I should stay calm and then expect possible big gain instead of selling those stocks purchased at lowest price. 

3 weeks 30% gains - NCLH - Sept. 2020


Sept. 24, 2020 NCLH price was at $14.35. Now it is more than 30% gain in less than three weeks. Oct. 9 close price is $18.73. 


Tuesday, October 6, 2020

Leetcode patterns: Google - hard level

 Here are 26 algorithms I like to go over at least once. 

First Missing Positivehttps://leetcode.com/problems/first-missing-positive/
Longest Consecutive Sequencehttps://leetcode.com/problems/longest-consecutive-sequence/
Merge k Sorted Listshttps://leetcode.com/problems/merge-k-sorted-lists/
Smallest Range Covering Elements from K Listshttps://leetcode.com/problems/smallest-range-covering-elements-from-k-lists/
Insert Intervalhttps://leetcode.com/problems/insert-interval/




WWR stock: From $2.00/ share to $14/ share in less than 8 business days

 WWR stock 

- I like to learn more about WWR stock, and how to understand WWR stock as an investor. 



逆序对问题的基础算法

 Here is the article. 

现在终于可以介绍树状数组(Binary Indexed Tree)。树状数组这个中文名称比英文Binary Indexed Tree好很多,因为树状数组用起来跟普通数组差不多,而对tree进行索引访问很奇怪,如tree[2]=10。树状数组有两个独特性质:

  1. 树状数组设置值的复杂度为O(log n)。
  2. 对树状数组任意区域求和的复杂度为O(log n)。

基于以上特性,我一般在代码中按其功能写成QuickSumArray,而不是Binary Indexed Tree或中文树状数组。


493. Reverse Pairs

 Here is the discussion link I read after I thought about the solution by myself over 10 minutes. 

Hard level algorithms: Life is tough to work with those hard level algorithms

 Oct. 6, 2020

Introduction

I like to invest time to learn 50 hard level algorithms on Leetcode.com. What I like to do first is to learn ideas how to solve them, and seek some good ideas to build strong analytical skills first. 

Hard level algorithms

I have limit time to work on those hard level algorithms, so I like to figure out how to learn better. 


AMWELL stock: IPO in Sept 2020

 My friend advised us to purchase IPO stock in Sept. Now it is up 50%. 

I should look into the stock early. Be positive and buy some IPO stocks. 


PPSI stock: Gambling 10x more it's capital value in a few hours

 Oct. 6, 2020

Introduction

One of us in wechat group told us that she made $20,000 US dollars on PPSI stock, and get in and out six times. I like to do a short case study on this stock. 

PPSI stock

Capital value is 41.885 Million dollars, today it went up 300%. Original the value is around $15.00 million dollars. The volume is around 319,981,400. More than 10 times more total capital value. 

So many players rushed to buy and sell. 



Website project: Html script extra close div - Remove a menu causing crash

 Oct. 6, 2020

Introduction

It was two weeks ago that I was so busy to make so many changes on the website, I was asked to remove uplight from the website, and then after a while, I was told that website was not responding. 

Nervousness

I was so nervous since I did not have any issue over last 12 months. I went back to check IIS event view log, and also I tried to rollback every change to see which one to cause the problem. 

My coworker reminded me that it happened just after I removed the uplight. 

Code change

I rollbacked the change to remove a row in the table WebsiteMainMenu. I turned off the cache of the table, and Monday morning I went to work, the coworker told me the page was really slow. 

I quickly fixed the cache issue. I also rewrote the code for Main Menu setup written in C#. 

Here is my project folder. 

I like to work on my project management skills, and also I have to review my own code and take some time to make it more robust. 

Stocks with 10% increase: Oct 2 - Oct. 6, 2020

 Oct. 6, 2020

Introduction

I missed 10% increase from Oct. 2 to Oct. 6, 2020. I like to write about 10% increase. 

10% increase

I bet on MRO stock 1000 share, but I failed to get rebound and sold with $180 dollars loss. And MRO price went down last Friday. I thought about purchase back, actually it is time. 

For gambling, it is bet to have a rebound. I tried so many times, and I thought about blackswan, and another 20% pullback, so I did not try to recover my loss. 

Today I checked the following stocks all having 10% rebound:

SU, MRO, OXY, NCLH, AAL, SQ

I think that it is gambling which makes stock market always attractive. It is easy to get addicted. I did try to catch rebound so many times after August 31, 2020, but I failed all the time. I decided to quit, and then it has 10% rebound. 

Behavior problems

I was panic since the market will have another 20% to 30% pullback. In my Ameritrade.com account, all my gains of $3000 from May 2019 to June 8 2020 are gone. I am betting on my capital. 

It takes some risk to make 10% profit. 

I lost over $500 dollars on INTC stock, if I just hold another month, today it went back around $52.20 dollars. I should learn from the lesson to hold longer. 

I did not make profit on 240 shares of NCLH, but if I hold until today at $17.78, then I will make profit $1.1 for each share. I sold too early. Even if it is short term investment, I also need to learn to take risk to hold longer. I should learn sentimental in the market, and take loss and hold longer those position. To be successful, I also need to learn more about renting out those shares if needed.