Skip to content

Commit 8e3c3bf

Browse files
committed
Implement Day018
1 parent 858f4f9 commit 8e3c3bf

11 files changed

+193
-1
lines changed

DailyCodingSolutions.sln

+16
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day017", "Day017\Day017.csp
105105
EndProject
106106
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day017.Test", "Day017.Test\Day017.Test.csproj", "{1B34914A-05D8-4107-A48A-54092F6788CC}"
107107
EndProject
108+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Day018", "Day018", "{E45179C1-4656-439A-A77F-9019B38A09E1}"
109+
EndProject
110+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day018", "Day018\Day018.csproj", "{4CEC2A44-690C-4271-8ECD-AF268FD611FC}"
111+
EndProject
112+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Day018.Test", "Day018.Test\Day018.Test.csproj", "{5B7BD630-F10B-45A1-BB83-00D5C3C84AAD}"
113+
EndProject
108114
Global
109115
GlobalSection(SolutionConfigurationPlatforms) = preSolution
110116
Debug|Any CPU = Debug|Any CPU
@@ -148,6 +154,8 @@ Global
148154
{AD4EABC8-21ED-4EB2-BA80-84B821DEC22D} = {906AF173-B3C4-41F6-89D0-15BEA3A5B628}
149155
{6F837E67-D8AB-410F-A50E-F77A4619D4FC} = {0446A598-9610-4DE6-B83D-490056506F0E}
150156
{1B34914A-05D8-4107-A48A-54092F6788CC} = {0446A598-9610-4DE6-B83D-490056506F0E}
157+
{4CEC2A44-690C-4271-8ECD-AF268FD611FC} = {E45179C1-4656-439A-A77F-9019B38A09E1}
158+
{5B7BD630-F10B-45A1-BB83-00D5C3C84AAD} = {E45179C1-4656-439A-A77F-9019B38A09E1}
151159
EndGlobalSection
152160
GlobalSection(ProjectConfigurationPlatforms) = postSolution
153161
{A8D06C50-49F9-4BC0-A128-D6A85B6DBB21}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
@@ -286,5 +294,13 @@ Global
286294
{1B34914A-05D8-4107-A48A-54092F6788CC}.Debug|Any CPU.Build.0 = Debug|Any CPU
287295
{1B34914A-05D8-4107-A48A-54092F6788CC}.Release|Any CPU.ActiveCfg = Release|Any CPU
288296
{1B34914A-05D8-4107-A48A-54092F6788CC}.Release|Any CPU.Build.0 = Release|Any CPU
297+
{4CEC2A44-690C-4271-8ECD-AF268FD611FC}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
298+
{4CEC2A44-690C-4271-8ECD-AF268FD611FC}.Debug|Any CPU.Build.0 = Debug|Any CPU
299+
{4CEC2A44-690C-4271-8ECD-AF268FD611FC}.Release|Any CPU.ActiveCfg = Release|Any CPU
300+
{4CEC2A44-690C-4271-8ECD-AF268FD611FC}.Release|Any CPU.Build.0 = Release|Any CPU
301+
{5B7BD630-F10B-45A1-BB83-00D5C3C84AAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
302+
{5B7BD630-F10B-45A1-BB83-00D5C3C84AAD}.Debug|Any CPU.Build.0 = Debug|Any CPU
303+
{5B7BD630-F10B-45A1-BB83-00D5C3C84AAD}.Release|Any CPU.ActiveCfg = Release|Any CPU
304+
{5B7BD630-F10B-45A1-BB83-00D5C3C84AAD}.Release|Any CPU.Build.0 = Release|Any CPU
289305
EndGlobalSection
290306
EndGlobal

DailyCodingSolutions.sln.DotSettings.user

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
22
<s:String x:Key="/Default/Environment/Highlighting/HighlightingSourceSnapshotLocation/@EntryValue">/Users/davidedepretto/Library/Caches/JetBrains/Rider2021.3/resharper-host/temp/Rider/vAny/CoverageData/_DailyCodingSolutions.1584796420/Snapshot/snapshot.utdcvr</s:String>
3-
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=426eb0ab_002Db449_002D43b0_002Dbd5f_002D231eb60b0710/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="Session" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
3+
<s:String x:Key="/Default/Environment/UnitTesting/UnitTestSessionStore/Sessions/=f79e0671_002Ddf0b_002D41e3_002D9f2f_002D764725835cc0/@EntryIndexedValue">&lt;SessionState ContinuousTestingMode="0" IsActive="True" Name="All tests from &amp;lt;Day018&amp;gt;" xmlns="urn:schemas-jetbrains-com:jetbrains-ut-session"&gt;
44
&lt;Or&gt;
55
&lt;Project Location="/Users/davidedepretto/GitHub/daily-coding-solutions" Presentation="&amp;lt;Day001&amp;gt;" /&gt;
66
&lt;Project Location="/Users/davidedepretto/GitHub/daily-coding-solutions" Presentation="&amp;lt;Day002&amp;gt;" /&gt;
@@ -19,6 +19,7 @@
1919
&lt;Project Location="/Users/davidedepretto/GitHub/daily-coding-solutions" Presentation="&amp;lt;Day015&amp;gt;" /&gt;
2020
&lt;Project Location="/Users/davidedepretto/GitHub/daily-coding-solutions" Presentation="&amp;lt;Day016&amp;gt;" /&gt;
2121
&lt;Project Location="/Users/davidedepretto/GitHub/daily-coding-solutions" Presentation="&amp;lt;Day017&amp;gt;" /&gt;
22+
&lt;Project Location="/Users/davidedepretto/GitHub/daily-coding-solutions" Presentation="&amp;lt;Day018&amp;gt;" /&gt;
2223
&lt;/Or&gt;
2324
&lt;/SessionState&gt;</s:String>
2425

@@ -40,6 +41,7 @@
4041

4142

4243

44+
4345
<s:Boolean x:Key="/Default/UserDictionary/Words/=aaaaa/@EntryIndexedValue">True</s:Boolean>
4446

4547
<s:Boolean x:Key="/Default/UserDictionary/Words/=abcba/@EntryIndexedValue">True</s:Boolean>

Day018.Test/Day018.Test.csproj

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
7+
<IsPackable>false</IsPackable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
12+
<PackageReference Include="xunit" Version="2.4.1" />
13+
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
14+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
15+
<PrivateAssets>all</PrivateAssets>
16+
</PackageReference>
17+
<PackageReference Include="coverlet.collector" Version="3.1.0">
18+
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
19+
<PrivateAssets>all</PrivateAssets>
20+
</PackageReference>
21+
</ItemGroup>
22+
23+
<ItemGroup>
24+
<ProjectReference Include="..\Day018\Day018.csproj" />
25+
</ItemGroup>
26+
27+
</Project>

Day018.Test/Strategy1Test.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Day018.Test;
2+
3+
public class Strategy1Test : StrategyTestBase
4+
{
5+
public Strategy1Test() : base(new Strategy1())
6+
{
7+
}
8+
}

Day018.Test/Strategy2Test.cs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Day018.Test;
2+
3+
public class Strategy2Test : StrategyTestBase
4+
{
5+
public Strategy2Test() : base(new Strategy2())
6+
{
7+
}
8+
}

Day018.Test/StrategyTestBase.cs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Collections.Generic;
2+
using Xunit;
3+
4+
namespace Day018.Test;
5+
6+
public abstract class StrategyTestBase
7+
{
8+
private readonly IStrategy _strategy;
9+
10+
protected StrategyTestBase(IStrategy strategy)
11+
{
12+
_strategy = strategy;
13+
}
14+
15+
[Theory]
16+
[InlineData(new[] {10, 5, 2, 7, 8, 7}, 1, new[] {10, 5, 2, 7, 8, 7})]
17+
[InlineData(new[] {10, 5, 2, 7, 8, 7}, 2, new[] {10, 5, 7, 8, 8})]
18+
[InlineData(new[] {10, 5, 2, 7, 8, 7}, 3, new[] {10, 7, 8, 8})]
19+
[InlineData(new[] {10, 5, 2, 7, 8, 7}, 4, new[] {10, 8, 8})]
20+
[InlineData(new[] {10, 5, 2, 7, 8, 7}, 6, new[] {10})]
21+
[InlineData(new[] {1, 1, 1, 1, 1, 1}, 1, new[] {1, 1, 1, 1, 1, 1})]
22+
[InlineData(new[] {1, 1, 1, 1, 1, 1}, 2, new[] {1, 1, 1, 1, 1})]
23+
[InlineData(new[] {1, 1, 1, 1, 1, 1}, 3, new[] {1, 1, 1, 1})]
24+
[InlineData(new[] {1, 1, 1, 1, 1, 1}, 6, new[] {1})]
25+
[InlineData(new[] {1, 2, 3, 10, 2, 5, -4, -7, -2, 10}, 3,
26+
new[] {3, 10, 10, 10, 5, 5, -2, 10})]
27+
public void Execute_GivenIntArrayAndSubsetLength_ReturnsMaxOfRollingSubsets(
28+
int[] inputList, int subsetLength, int[] expected)
29+
{
30+
var actual = new List<int>();
31+
32+
_strategy.Execute(inputList, subsetLength, actual);
33+
34+
Assert.Equal(expected, actual);
35+
}
36+
}

Day018/Day018.csproj

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<ImplicitUsings>enable</ImplicitUsings>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

Day018/IStrategy.cs

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace Day018;
2+
3+
public interface IStrategy
4+
{
5+
void Execute(
6+
IReadOnlyList<int> inputList,
7+
int subsetLength,
8+
IList<int> output);
9+
}

Day018/README.md

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Day 18
2+
3+
Given an array of integers and a number k, where 1 <= k <= length of the array,
4+
compute the maximum values of each subarray of length k.
5+
6+
For example, given array = `[10, 5, 2, 7, 8, 7]` and k = 3, we should
7+
get: `[10, 7, 8, 8]`, since:
8+
9+
- 10 = max(10, 5, 2)
10+
- 7 = max(5, 2, 7)
11+
- 8 = max(2, 7, 8)
12+
- 8 = max(7, 8, 7)
13+
14+
Do this in O(n) time and O(k) space. You can modify the input array in-place and
15+
you do not need to store the results. You can simply print them out as you
16+
compute them.

Day018/Strategy1.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
namespace Day018;
2+
3+
/**
4+
* This is not in O(n) time and O(k) space, but it's just a first approximation
5+
* to validate unit tests and understand the general problem
6+
*/
7+
public class Strategy1 : IStrategy
8+
{
9+
public void Execute(
10+
IReadOnlyList<int> inputList,
11+
int subsetLength,
12+
IList<int> output)
13+
{
14+
var inputArray = inputList as int[] ?? inputList.ToArray();
15+
16+
for (var i = 0; i <= inputArray.Length - subsetLength; i++)
17+
output.Add(inputArray[i..(i + subsetLength)].Max());
18+
}
19+
}

Day018/Strategy2.cs

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
namespace Day018;
2+
3+
/**
4+
* This is shoudl be in O(n) time and O(k) space, but I'm not sure TBH because
5+
* of those nested while loops.. 🧐
6+
*/
7+
public class Strategy2 : IStrategy
8+
{
9+
public void Execute(
10+
IReadOnlyList<int> inputList,
11+
int subsetLength,
12+
IList<int> output)
13+
{
14+
var indexes = new LinkedList<int>();
15+
16+
for (var i = 0; i < subsetLength; ++i)
17+
{
18+
while (indexes.Count > 0 &&
19+
inputList[i] >= inputList[indexes.Last!.Value])
20+
indexes.RemoveLast();
21+
22+
indexes.AddLast(i);
23+
}
24+
25+
output.Add(inputList[indexes.First!.Value]);
26+
27+
for (var i = subsetLength; i < inputList.Count; ++i)
28+
{
29+
while (indexes.Count > 0 &&
30+
indexes.First.Value <= i - subsetLength)
31+
indexes.RemoveFirst();
32+
33+
while (indexes.Count > 0 &&
34+
inputList[i] >= inputList[indexes.Last!.Value])
35+
indexes.RemoveLast();
36+
37+
indexes.AddLast(i);
38+
39+
output.Add(inputList[indexes.First.Value]);
40+
}
41+
}
42+
}

0 commit comments

Comments
 (0)