Problem solving/Algorithms

[LeetCode] 300. Longest Increasing Subsequence

Young_A 2025. 7. 12. 09:44

๋ชฉ์ฐจ

    LeetCode - Problems - Algorithms - 300. Longest Increasing Subsequence

    Problem Description

    Given an integer array nums, return the length of the longest strictly increasing subsequence.

     

    Example:

     

    Constraints:

    My Solution (C#)

    public int LengthOfLIS(int[] nums)
    {
        int numsLength = nums.Length;
        int[] maxSubsequenceLengths = new int[numsLength];
        Array.Fill(maxSubsequenceLengths, 1);   //maxSubsequenceLengths[i]๋Š” 0๋ถ€ํ„ฐ [i]๊นŒ์ง€๋ฅผ ๋น„๊ตํ–ˆ์„ ๋•Œ ์ตœ์žฅ ์ˆ˜์—ด ๊ธธ์ด.
    
        for(int i = 1; i < numsLength; i++)
        {
            for (int j = 0; j < i; j++)
            {
                if (nums[i] > nums[j])
                {
                    maxSubsequenceLengths[i] = Math.Max(maxSubsequenceLengths[i], maxSubsequenceLengths[j] + 1);
                }
            }
        }
        return maxSubsequenceLengths.Max();
    }

     

    ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ์œผ๋กœ ์ง„ํ–‰ํ•ด์•ผํ–ˆ๋‹ค.

    ๋” ๋น ๋ฅด๊ฒŒ ์ง„ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ํ’€์ด๊ฐ€ ์žˆ๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ ์ด ๋ถ€๋ถ„์€ ๋‚˜์ค‘์— ์ง„ํ–‰ํ•ด๋ด์•ผ๊ฒ ๋‹ค.

    ์œ ํŠœ๋ธŒ ํ•ด์„ค์„ ๋ด๋„ ์ดํ•ด๊ฐ€ ์• ๋งคํ•ด์„œ.. ์–ด์ฐŒ์ €์ฐŒ ํ’€๊ธด ํ–ˆ๋Š”๋ฐ, ์ด๊ฒŒ ๋™์ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์ž์ฒด์— ๋Œ€ํ•œ ์ดํ•ด๋„๋ฅผ ๋†’ํžˆ๋Š” ๊ฑด ์•„๋‹ˆ์—ˆ๋‹ค.

    ์กฐ๊ธˆ ๋” ์‰ฌ์šด ๋‚œ์ด๋„๋กœ ๋™์  ํ”„๋กœ๊ทธ๋ž˜๋ฐ์„ ์ดํ•ดํ•˜๊ณ  ์ ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ๋ฌธ์ œ๋ฅผ ๋ช‡๊ฐ€์ง€ ๋” ํ’€์–ด๋ด์•ผ๊ฒ ๋‹ค...