Skip to content

Instantly share code, notes, and snippets.

@EBojilova
Created May 6, 2015 11:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save EBojilova/5930d8041b7324643ecd to your computer and use it in GitHub Desktop.
Save EBojilova/5930d8041b7324643ecd to your computer and use it in GitHub Desktop.
05. Longest Increasing Sequence
using System;
using System.Collections.Generic;
using System.Linq;
class LongestIncreasingSequence
{
static void Main(string[] args)
{
int[] nums = Array.ConvertAll(Console.ReadLine().Split(' '), s => int.Parse(s));
Dictionary<int, List<int>> sorted = new Dictionary<int, List<int>>();
sorted[0] = new List<int>();
sorted[0].Add(nums[0]);
for (int i = 1, index = 0; i < nums.Length; i++)
{
if (nums[i] < nums[i - 1] || nums[i] == nums[i - 1])
{
index++;
}
if (!sorted.Keys.Contains(index))
{
sorted[index] = new List<int>();
}
sorted[index].Add(nums[i]);
}
foreach (var key in sorted.Keys)
{
Console.WriteLine(string.Join(" ", sorted[key]));
}
var longest = sorted.OrderByDescending(a => a.Value.Count).First().Value;
Console.WriteLine("Longest: {0}",string.Join(" ", longest));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment