Advertisement
Guest User

13. Longest Increasing Subsequence

a guest
Oct 10th, 2016
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _13.LongestIncreasSubseq
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             var nums = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  14.             var longestIncrease = new int[nums.Length];
  15.             AddOnes(longestIncrease); //Променя стойността на всеки елемент на масива на "1"
  16.             List<int> len = new List<int>();
  17.  
  18.             for (int i = 0; i < nums.Length-1; i++)
  19.             {
  20.                 for (int j = i+1; j < nums.Length; j++)
  21.                 {
  22.                     if (nums[i]<nums[j])
  23.                     {
  24.                         longestIncrease[j] = longestIncrease[i] + 1;
  25.                     }
  26.                 }
  27.             }
  28.             for (int i = 0; i < longestIncrease.Length-1; i++)
  29.             {
  30.                 if (longestIncrease[i]<longestIncrease[i+1])
  31.                 {
  32.                     len.Add(nums[i]);
  33.                 }
  34.             }
  35.  
  36.             Console.WriteLine(string.Join(" ",len));
  37.         }
  38.  
  39.         private static void AddOnes(int[] arr1)
  40.         {
  41.             for (int i = 0; i < arr1.Length; i++)
  42.             {
  43.                 arr1[i] = 1;
  44.             }
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement