Thursday, July 20, 2023

Sliding window | Leetcode

 https://leetcode.com/tag/sliding-window/

Sliding Window

You have solved 29 / 92 problems.

 #TitleAcceptanceDifficultyFrequency
22022.3%Hard
215622.4%Hard
86225.9%Hard
104430.5%Hard
3031.4%Hard
278132.1%Hard
252833.0%Hard
239833.2%Hard
210635.2%Hard
68337.0%Hard
161037.3%Hard
48040.4%Hard
252440.4%Hard
7641.0%Hard
170342.7%Hard
72743.1%Hard
149945.1%Hard
23946.1%Hard
142547.7%Hard
99551.3%Hard
230253.1%Hard
99255.3%Hard
63261.5%Hard
244461.8%Hard
274731.3%Medium
265331.7%Medium
255532.0%Medium
333.9%Medium
246134.0%Medium
251634.4%Medium
273034.5%Medium
277935.2%Medium
147736.6%Medium
123437.4%Medium
165837.6%Medium
188839.2%Medium
276239.6%Medium
183839.7%Medium
241142.4%Medium
90443.7%Medium
56744.2%Medium
115644.7%Medium
39544.8%Medium
83745.4%Medium
169645.9%Medium
71346.2%Medium
20946.4%Medium
65846.9%Medium
97847.2%Medium
18747.4%Medium
34048.3%Medium
209048.3%Medium
143848.6%Medium
183948.8%Medium
253749.0%Medium
120849.3%Medium
240149.7%Medium
48749.8%Medium
43850.3%Medium
226050.8%Medium
71851.2%Medium
213451.4%Medium
129751.8%Medium
42452.3%Medium
142352.3%Medium
191852.6%Medium
93053.6%Medium
15954.0%Medium
210755.8%Medium
105257.1%Medium
169557.6%Medium
145658.2%Medium
103159.6%Medium
115160.8%Medium
124862.3%Medium
100463.0%Medium
135864.0%Medium
149366.2%Medium
202467.3%Medium
134368.0%Medium
185271.0%Medium
110074.6%Medium
274377.3%Medium
276028.2%Easy
21943.0%Easy
64343.5%Easy
117652.9%Easy
198455.0%Easy
226957.9%Easy
237957.9%Easy
176360.9%Easy
187671.7%Easy

Find minimum substring given unique characters

 C# | Sliding window | July 2023 | Test cases | Warm up

774
7
20 hours ago
C#

Intuition


Approach


Complexity

  • Time complexity:

O(m + n), m is length of string, n is length of pattern string.

  • Space complexity:

Code

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _76_minimum_window_substring_review
{
    class Program
    {
        static void Main(string[] args)
        {
            var testResult = GetShortestUniqueSubstring(new char[] { 'a', 'b', 'c' }, "aaaefbcgaxy");
            Debug.Assert(testResult.CompareTo("bcga") == 0);
        }

        /// <summary>
        /// code review on July 19, 2023
        /// https://codereview.stackexchange.com/questions/176769/find-the-smallest-substring-that-contains-some-given-subset-of-characters
        /// </summary>
        /// <param name="search"></param>
        /// <param name="source"></param>
        /// <returns></returns>
        public static string GetShortestUniqueSubstring(char[] search, string source)
        {
            if (search == null || search.Length == 0 || string.IsNullOrEmpty(source))
            {
                return "";
            }

            // put unique chars in search string to the dictionary
            var map = new Dictionary<char, int>();
            var set = new HashSet<char>(search);

            foreach (var item in search)
            {
                map.Add(item, 1);
            }           

            // iterate the string and find match, and also keep track of minimum 
            var left = 0;
            var length = source.Length;

            var smallestLength = length + 1;
            var smallestSubstring = "";

            for (int index = 0; index < length; index++)
            {
                var visit = source[index];

                //var inMap = map.ContainsKey(visit);
                //var needOne = inMap && map[visit] > 0;

                if (map.ContainsKey(visit))
                {
                    map[visit]--;                   
                }

                set.Remove(visit);
                
                if (set.Count > 0)
                {
                    continue;
                }

                // move left point forward
                // extra one -> value in hashmap has negative value 
                while (left <= index && (!map.ContainsKey(source[left]) || map[source[left]] < 0))
                {
                    var removeChar = source[left];
                                     
                    if (map.ContainsKey(removeChar))
                    {
                        map[removeChar]++;
                    }

                    left++;
                }

                // added on July 19, 2023 - index-out-of-range concern
                if (left == length)
                {
                    break;
                }

                var currentLength = index - left + 1;                
                if (currentLength < smallestLength)
                {
                    smallestLength = currentLength;
                    smallestSubstring = source.Substring(left, currentLength);
                }

                var removed = source[left];
                map[removed]++;
                set.Add(removed);  // match map 

                left = left + 1;                                                               
            }

            return smallestLength == (length + 1) ? "" : smallestSubstring;            
        }
    }
}

Tuesday, July 18, 2023

Collar Example

Here is the article. 

The Collar Options Strategy Explained in Simple Terms


Collar Example

Assume an investor is long 1,000 shares of stock ABC at a price of $80 per share, and the stock is currently trading at $87 per share. The investor wants to temporarily hedge the position due to the increase in the overall market's volatility.

The investor purchases 10 put options (one option contract is 100 shares) with a strike price of $77 and a premium of $3.00 and writes 10 call options with a strike price of $97 with a premium of $4.50.

  • Cost to implement collar (Buy $77 strike Put & write $97 strike call) is a net credit of $1.50 / share.
  • Breakeven point = $80 + $1.50 = $81.50 / share.

The maximum profit is $15,500, or 10 contracts x 100 shares x (($97 - $1.50) - $80). This scenario occurs if the stock prices goes to $97 or above.

Conversely, the maximum loss is $4,500, or 10 x 100 x ($80 - ($77 - $1.50)). This scenario occurs if the stock price drops to $77 or below.

Flatten a Dictionary | C# | Warmup

 #Flatten a Dictionary

Given a dictionary, write a function to flatten it. Consider the following input/output scenario for better understanding:

Input:

{ 'Key1': '1', 'Key2': { 'a' : '2', 'b' : '3', 'c' : { 'd' : '3', 'e' : '1' } } } Output:

{ 'Key1': '1', 'Key2.a': '2', 'Key2.b' : '3', 'Key2.c.d' : '3', 'Key2.c.e' : '1' }


July 18, 2023
My C# practice
https://gist.github.com/jianminchen/98e6b3175a6d01d35c18b2f6f05011be

Hengrui Zhang

 Hengrui Zhang has 3 years of experience in public equity investment research & analysis with a focus on US and Asian markets. In his current role at Acuity Knowledge Partners, Hengrui supports investment management for a large Asia private equity fund, covering a wide spectrum of industries including but unlimited to healthcare, education, consumers, and real estate. Hengrui holds a bachelor’s degree in economics from the University of Toronto.

Author's Contribution

A collar strategy

 A collar is an options strategy that involves buying a downside put and selling an upside call that is implemented to protect against large losses, but that also limits large upside gains. The protective collar strategy involves two strategies known as a protective put and covered call.

Monday, July 17, 2023

Victor Sperandeo

 Victor Sperandeo, known as “Trader Vic”, is a US trader, index developer, and financial commentator based in Grapevine, Texas, United States. He serves as the President and CEO of Alpha Financial Technologies, LLC (AFT), is a founding partner of EAM Partners L.P., and serves as the President and CEO of its general partner, EAM Corporation.

Sperandeo traded in commodities, particularly in the energy and metals sectors. He is renowned for having 'predicted' the stock market crash of 1987 during an extensive interview in the September 21 issue of Barron's; on October 16, one trading session prior to Black Monday, Sperandeo shorted the Dow and made 300% during a day the DJIA fell by over 20%.[2]