Advertisement
elena1234

LongestIncreasingSubsequence - algorithm

Oct 9th, 2020 (edited)
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3.  
  4. namespace LongestIncreasingSubsequence
  5. {
  6.     class MainClass
  7.     {
  8.         public static void Main(string[] args)
  9.         {
  10.             int[] sequence = Console.ReadLine().Split().Select(int.Parse).ToArray();
  11.             int[] len = new int[sequence.Length];
  12.             int maxLength = 0;
  13.  
  14.             for (int currentI = 0; currentI < sequence.Length; currentI++)
  15.             {
  16.                 len[currentI] = 1;
  17.  
  18.                 for (int prevI = 0; prevI <= currentI - 1; prevI++)
  19.                 {
  20.                     if (sequence[currentI] > sequence[prevI] && len[prevI] >= len[currentI])
  21.                     {
  22.  
  23.                         len[currentI] = len[prevI] + 1;
  24.                     }
  25.                 }
  26.  
  27.                 if (len[currentI] > maxLength)
  28.                 {
  29.                     maxLength = len[currentI];
  30.                 }
  31.             }
  32.             Console.WriteLine(maxLength);
  33.         }
  34.     }
  35. }
  36.  
  37.  
  38.  
  39.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement