Created
May 6, 2015 11:00
-
-
Save EBojilova/5930d8041b7324643ecd to your computer and use it in GitHub Desktop.
05. Longest Increasing Sequence
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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