Monday, July 17, 2023

Leetcode 18: 4 sum | Editorial solution study

Here is the link.

 C# | k sum problem | Time complexity: O(N^(k-1)) | Editorial | July 2023

774
0
19 minutes ago
C#

Intuition

It is a good idea to write a solution based on editorial solution, which is written for Leetcode premium account. The idea is to solve a k sum problem using time complexity O(N^(k - 1)).

Approach

C# solution highlights:

  1. Sort the array
  2. Base case: Two sum problem using two pointer technique, O(N) time complexity
  3. Keep unique quadruplet - remove duplicate
  4. C# List APIs: AddRange, Insert(index, int)
  5. Compare editorial Java solution, and understand difference between Java and C# related to List and APIs.

Complexity

  • Time complexity:
  • Space complexity:

Code

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

namespace _18_4sum_study
{
    class Program
    {
        static void Main(string[] args)
        {
            var test = new Program();

            var result = test.FourSum(new int[]{1, 0, -1, 0, -2, 2}, 0); 
            // output 
            // [[-2,-1,1,2],[-2,0,0,2],[-1,0,0,1]]
        }

        /// <summary>
        /// Code study on July 17, 2023
        /// </summary>
        /// <param name="nums"></param>
        /// <param name="target"></param>
        /// <returns></returns>
        public IList<IList<int>> FourSum(int[] nums, int target)
        {
            Array.Sort(nums);
            return kSum(nums, target, 0, 4);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="nums"></param>
        /// <param name="target"></param>
        /// <param name="start"></param>
        /// <param name="k"></param>
        /// <returns></returns>
        private IList<IList<int>> kSum(int[] nums, long target, int start, int k)
        {
            var result = new List<IList<int>>();

            var length = nums.Length; 

            if (start == length)
            {
                return result; 
            }

            long averageValue = target / k;

            if (averageValue < nums[start] || averageValue > nums[length - 1])
            {
                return result; 
            }

            // base case
            if (k == 2)
            {
                return twoSum(nums, target, start);
            }

            for (int i = start; i < length; i++)
            {
                // pruning - remove duplicate ones - check previous element in sorted array
                if (i == start || nums[i - 1] != nums[i])
                {
                    var current = nums[i];
                    var kResult = kSum(nums, target - current, i + 1, k - 1);

                    for (int j = 0; j < kResult.Count; j++)
                    {
                        kResult[j].Insert(0, current);
                    }

                    result.AddRange(kResult);                            
                }
            }

            return result; 
        }

        /// <summary>
        /// Two sum classical algorithm
        /// Remove duplicate - check previous
        /// low -> compare to low - 1
        /// high -> compare to high + 1
        /// </summary>
        /// <param name="nums"></param>
        /// <param name="target"></param>
        /// <param name="start"></param>
        /// <returns></returns>
        private IList<IList<int>> twoSum(int[] nums, long target, int start)
        {
            var result = new List<IList<int>>();
            int low = start;
            var length = nums.Length; 
            int high = length - 1;

            while (low < high)
            {
                int sum = nums[low] + nums[high];

                if (sum < target || (low > start && nums[low] == nums[low - 1]))
                {
                    low++;
                }
                else if(sum > target || (high < length - 1 && nums[high] == nums[high + 1]))
                {
                    high--; 
                }
                else 
                {                   
                    result.Add((new int[]{nums[low], nums[high]}).ToList());
                    low++;
                    high--;
                }
            }

            return result; 
        }
    }
}

英雄宝刀未老的下一句是什么

 英雄宝刀未老没有固定的下句。英雄宝刀未老,烈士壮心不已。出自:烈士暮年,壮心不已。曹操《步出夏门行·龟虽寿》。

英雄宝刀未老,美人朱颜已改。出自:雕栏玉砌应犹在,只是朱颜改。—李煜《虞美人》。英雄宝刀未老,佳人风韵犹存。出自:清·王韬《淞隐漫录》:“其母虽属徐娘,丰韵犹饶,老蚌固宜出此明珠。


英雄宝刀未老的含义

宝刀不老:比喻虽然年龄已大或脱离本行已久,但功夫技术并没减退。宝刀未老的近义词老当益壮,主要指老年人继续保持旺盛的斗志丈夫为志,穷当益坚,老当益壮。《后汉书·马援传》。

宝刀未老的反义词,年老体衰。成语语法,主谓式。作谓语、定语、补语。比喻老将不减当年勇。感情色彩:中性成语。成语结构:主谓式成语。


Six Tips on How to Avoid Squandering a Financial Windfall

Sharon Tirabassi | I won $10m on the lottery but lost it all in 10 years

Sunday, July 16, 2023

Mark Minervini: How Discipline And Sacrifice Made Him A U.S. Investing Champion

Here is the link.

Mark Minervini, has 37 years of trading experience, authored books on trading and also won a U.S. Investing Championship. His consistent outperformance of the market comes down to discipline and sacrifice. Following rules keeps him from costly errors, like overtrading. He demonstrates his points with examples like Moderna (MRNA), Tesla (TSLA), Penske Automotive (PAG), Tempur Sealy (TPX), Sierra Wireless (SWIR), and FedEx (FDX). For the video version, show notes and charts, visit investors.com/podcast. Investor’s Business Daily has been helping people invest smarter results by providing exclusive stock lists, investing data, stock market research, education and the latest financial and business news to help investors make more money in the stock market.