Created
February 21, 2021 10:06
-
-
Save HELLoWorlD01100/a300341ff091578a2a183223469d012a to your computer and use it in GitHub Desktop.
Гармоническая подпоследовательность
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.Collections.Generic; | |
using System.Linq; | |
namespace ConsoleApp1 | |
{ | |
public static class EnumerableExtensions | |
{ | |
public static IDictionary<T, int> GetFrequency<T>(this IEnumerable<T> enumerable) | |
{ | |
return enumerable | |
.GroupBy(x => x) | |
.ToDictionary(x => x.Key, | |
x => x.Count()); | |
} | |
} | |
} |
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.Collections.Generic; | |
using ConsoleApp1; | |
using FluentAssertions; | |
using NUnit.Framework; | |
namespace ConsoleApp1Tests | |
{ | |
public class EnumerableExtensionsTests | |
{ | |
[Test] | |
public void GetFrequency_ShouldReturnEmptyDictionary_WhenEmptyEnumerable() | |
{ | |
var arr = new int[] { }; | |
var expected = new Dictionary<int, int>(); | |
var actual = arr.GetFrequency(); | |
actual.Should().BeEquivalentTo(expected); | |
} | |
[Test] | |
public void GetFrequency_ShouldReturnCorrectDictionary_WhenNotEmptyEnumerable() | |
{ | |
var arr = new [] {1, 1, 1, 2}; | |
var expected = new Dictionary<int, int>() | |
{ | |
{1, 3}, | |
{2, 1} | |
}; | |
var actual = arr.GetFrequency(); | |
actual.Should().BeEquivalentTo(expected); | |
} | |
} | |
} |
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
namespace ConsoleApp1 | |
{ | |
public class Solution | |
{ | |
public static int GetLongestHarmonicSubsequence(int[] numbers) | |
{ | |
var frequency = numbers.GetFrequency(); // Находим таблицу [number] = countOfNumber для нашей последовательности. | |
var maxSubsequenceCount = 0; | |
foreach (var (number, countOfNumber) in frequency) | |
{ | |
var containsNumberGreaterByOne = frequency.TryGetValue(number + 1, out var countOfNumberGreaterByOne); | |
var containsNumberLessByOne = frequency.TryGetValue(number - 1, out var countOfNumberLessByOne); | |
int maxCountOfAdjacentNumber; | |
switch (containsNumberGreaterByOne) | |
{ | |
// Кейс - есть оба числа в последовательности | |
case true when containsNumberLessByOne is true: | |
maxCountOfAdjacentNumber = countOfNumberGreaterByOne > countOfNumberLessByOne | |
? countOfNumberGreaterByOne | |
: countOfNumberLessByOne; | |
break; | |
// Кейс - есть __только__ первое число | |
case true when containsNumberLessByOne is false: | |
maxCountOfAdjacentNumber = countOfNumberGreaterByOne; | |
break; | |
// Кейс есть __только__ второе число | |
case false when containsNumberLessByOne: | |
maxCountOfAdjacentNumber = countOfNumberLessByOne; | |
break; | |
// Кейс нет ни одного числа => пропускаем это число | |
default: | |
continue; | |
} | |
var subsequenceCount = maxCountOfAdjacentNumber + countOfNumber; | |
if (subsequenceCount > maxSubsequenceCount) | |
maxSubsequenceCount = subsequenceCount; | |
} | |
return maxSubsequenceCount; | |
} | |
} | |
} |
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 ConsoleApp1; | |
using FluentAssertions; | |
using NUnit.Framework; | |
namespace ConsoleApp1Tests | |
{ | |
public class SolutionTests | |
{ | |
[Test] | |
public void GetLongestHarmonicSubsequence_ShouldReturnZero_WhenSameNumber() | |
{ | |
var arr = new[] {1, 1, 1, 1}; | |
var actual = Solution.GetLongestHarmonicSubsequence(arr); | |
actual.Should().Be(0); | |
} | |
[Test] | |
public void GetLongestHarmonicSubsequence_ShouldReturnZero_WhenNotContainsSubsequence() | |
{ | |
var arr = new[] {1, 3, 5, 7}; | |
var actual = Solution.GetLongestHarmonicSubsequence(arr); | |
actual.Should().Be(0); | |
} | |
[Test] | |
public void GetLongestHarmonicSubsequence_ShouldReturnCountOfSubsequence_WhenContainsSubsequence() | |
{ | |
var arr = new[] {1, 2, 2}; | |
var actual = Solution.GetLongestHarmonicSubsequence(arr); | |
actual.Should().Be(3); | |
} | |
[Test] | |
public void GetLongestHarmonicSubsequence_ShouldReturnMaxCountOfSubsequence_WhenContainsTwoSubsequences() | |
{ | |
var arr = new[] {1, 2, 3, 3, 3, 3}; | |
var actual = Solution.GetLongestHarmonicSubsequence(arr); | |
actual.Should().Be(5); | |
} | |
[Test] | |
public void GetLongestHarmonicSubsequence_ShouldReturnZero_WhenEmptySequence() | |
{ | |
var arr = new int[] { }; | |
var actual = Solution.GetLongestHarmonicSubsequence(arr); | |
actual.Should().Be(0); | |
} | |
[Test] | |
public void GetLongestHarmonicSubsequence_ShouldPassFirstExample() | |
{ | |
var arr = new[] {2, 4, 3, 3, 6, 3, 4, 8}; | |
var actual = Solution.GetLongestHarmonicSubsequence(arr); | |
actual.Should().Be(5); | |
} | |
[Test] | |
public void GetLongestHarmonicSubsequence_ShouldPassSecondExample() | |
{ | |
var arr = new[] {1, 2, 3, 4}; | |
var actual = Solution.GetLongestHarmonicSubsequence(arr); | |
actual.Should().Be(2); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment