Skip to content

Commit 24e21fc

Browse files
author
Bogdan Hladiuc
committed
Q189 Rotate Array, k times
1 parent f34f85c commit 24e21fc

File tree

8 files changed

+179
-2
lines changed

8 files changed

+179
-2
lines changed

LeetcodeNew.sln

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Q253_Meeting_Room_II", "Q25
7171
EndProject
7272
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Q211_Design_Add_Search_Words_Data_Structure", "Q211_Design_Add_Search_Words_Data_Structure\Q211_Design_Add_Search_Words_Data_Structure.csproj", "{E17C0532-8798-4245-8644-D4F02C3A6E13}"
7373
EndProject
74+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Q189_Rotate_Array", "Q189_Rotate_Array\Q189_Rotate_Array.csproj", "{321FC947-2B8B-45E0-B89E-3AB9F2A95F44}"
75+
EndProject
7476
Global
7577
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7678
Debug|Any CPU = Debug|Any CPU
@@ -492,5 +494,17 @@ Global
492494
{E17C0532-8798-4245-8644-D4F02C3A6E13}.Release|x64.Build.0 = Release|Any CPU
493495
{E17C0532-8798-4245-8644-D4F02C3A6E13}.Release|x86.ActiveCfg = Release|Any CPU
494496
{E17C0532-8798-4245-8644-D4F02C3A6E13}.Release|x86.Build.0 = Release|Any CPU
497+
{321FC947-2B8B-45E0-B89E-3AB9F2A95F44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
498+
{321FC947-2B8B-45E0-B89E-3AB9F2A95F44}.Debug|Any CPU.Build.0 = Debug|Any CPU
499+
{321FC947-2B8B-45E0-B89E-3AB9F2A95F44}.Debug|x64.ActiveCfg = Debug|Any CPU
500+
{321FC947-2B8B-45E0-B89E-3AB9F2A95F44}.Debug|x64.Build.0 = Debug|Any CPU
501+
{321FC947-2B8B-45E0-B89E-3AB9F2A95F44}.Debug|x86.ActiveCfg = Debug|Any CPU
502+
{321FC947-2B8B-45E0-B89E-3AB9F2A95F44}.Debug|x86.Build.0 = Debug|Any CPU
503+
{321FC947-2B8B-45E0-B89E-3AB9F2A95F44}.Release|Any CPU.ActiveCfg = Release|Any CPU
504+
{321FC947-2B8B-45E0-B89E-3AB9F2A95F44}.Release|Any CPU.Build.0 = Release|Any CPU
505+
{321FC947-2B8B-45E0-B89E-3AB9F2A95F44}.Release|x64.ActiveCfg = Release|Any CPU
506+
{321FC947-2B8B-45E0-B89E-3AB9F2A95F44}.Release|x64.Build.0 = Release|Any CPU
507+
{321FC947-2B8B-45E0-B89E-3AB9F2A95F44}.Release|x86.ActiveCfg = Release|Any CPU
508+
{321FC947-2B8B-45E0-B89E-3AB9F2A95F44}.Release|x86.Build.0 = Release|Any CPU
495509
EndGlobalSection
496510
EndGlobal
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
#nullable disable warnings
2+
using System.Diagnostics;
3+
4+
namespace Q189_Rotate_Array {
5+
public class Q189_Rotate_Array {
6+
public static void Run() {
7+
var tests = new List<Test> {
8+
new Test {
9+
Array = new int[] { 1, 2, 3, 4, 5 },
10+
Expected = new int[] { 5, 1, 2, 3, 4 },
11+
K = 1,
12+
Id = 1
13+
},
14+
new Test {
15+
Array = new int[] { 1, 2, 3, 4, 5 },
16+
Expected = new int[] { 1, 2, 3, 4, 5 },
17+
K = 0,
18+
Id = 2
19+
},
20+
new Test {
21+
Array = new int[] { 1, 2, 3, 4, 5 },
22+
Expected = new int[] { 3, 4, 5, 1, 2 },
23+
K = 3,
24+
Id = 3
25+
},
26+
new Test {
27+
Array = new int[] { 1, 2, 3, 4, 5 },
28+
Expected = new int[] { 1, 2, 3, 4, 5 },
29+
K = 5,
30+
Id = 4
31+
},
32+
new Test {
33+
Array = new int[] { 1, 2, 3, 4, 5 },
34+
Expected = new int[] { 5, 1, 2, 3, 4 },
35+
K = 6,
36+
Id = 5
37+
}
38+
};
39+
40+
foreach (var test in tests) {
41+
Rotate(test.Array, test.K);
42+
test.Verify();
43+
}
44+
}
45+
46+
private static void Rotate(int[] a, int k) {
47+
k %= a.Length;
48+
Rotate(a, 0, a.Length - 1);
49+
Rotate(a, 0, k - 1);
50+
Rotate(a, k, a.Length - 1);
51+
}
52+
53+
private static void Rotate(int[] a, int l, int r) {
54+
while (l < r) {
55+
Swap(a, l, r);
56+
l++;
57+
r--;
58+
}
59+
}
60+
61+
private static void Swap(int[] a, int l, int r) {
62+
var tmp = a[l];
63+
a[l] = a[r];
64+
a[r] = tmp;
65+
}
66+
}
67+
68+
internal class Test {
69+
public int[] Array { get; set; }
70+
public int K { get; set; }
71+
public int[] Expected { get; set; }
72+
public int Id { get; set; }
73+
74+
public void Verify() {
75+
Debug.Assert(Array.SequenceEqual(Expected), $"Test {Id} failed");
76+
}
77+
}
78+
}
Lines changed: 9 additions & 0 deletions
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>

Run/Program.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,9 @@ static void Main(string[] args) {
99
// Q71_Simplify_Path.Q71_Simplify_Path.Run();
1010
// Q152_Maximum_Product_Subarray.Q152_Maximum_Product_Subarray.Run();
1111
// Q173_Binary_Search_Tree_Iterator.Q173_Binary_Search_Tree_Iterator.Run();
12+
Q189_Rotate_Array.Q189_Rotate_Array.Run();
1213
// Q199_Binary_Tree_Right_Side_View.Q199_Binary_Tree_Right_Side_View.Run();
13-
Q211_Design_Add_Search_Words_Data_Structure.Q211_Design_Add_Search_Words_Data_Structure.Run();
14+
// Q211_Design_Add_Search_Words_Data_Structure.Q211_Design_Add_Search_Words_Data_Structure.Run();
1415
// Q215_Kth_Largest_Element.Q215_Kth_Largest_Element.Run();
1516
// Q236_Lowest_Common_Ancestor_of_a_Binary_Tree.Q236_Lowest_Common_Ancestor_of_a_Binary_Tree.Run();
1617
// Q253_Meeting_Room_II.Q253_Meeting_Room_II.Run();

Run/Run.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
<ProjectReference Include="..\Q71_Simplify_Path\Q71_Simplify_Path.csproj" />
3636
<ProjectReference Include="..\Q253_Meeting_Room_II\Q253_Meeting_Room_II.csproj" />
3737
<ProjectReference Include="..\Q211_Design_Add_Search_Words_Data_Structure\Q211_Design_Add_Search_Words_Data_Structure.csproj" />
38+
<ProjectReference Include="..\Q189_Rotate_Array\Q189_Rotate_Array.csproj" />
3839
</ItemGroup>
3940

4041
<PropertyGroup>

Run/obj/Run.csproj.nuget.dgspec.json

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,58 @@
492492
}
493493
}
494494
},
495+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q189_Rotate_Array/Q189_Rotate_Array.csproj": {
496+
"version": "1.0.0",
497+
"restore": {
498+
"projectUniqueName": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q189_Rotate_Array/Q189_Rotate_Array.csproj",
499+
"projectName": "Q189_Rotate_Array",
500+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q189_Rotate_Array/Q189_Rotate_Array.csproj",
501+
"packagesPath": "/Users/Bogdan/.nuget/packages/",
502+
"outputPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q189_Rotate_Array/obj/",
503+
"projectStyle": "PackageReference",
504+
"configFilePaths": [
505+
"/Users/Bogdan/.nuget/NuGet/NuGet.Config"
506+
],
507+
"originalTargetFrameworks": [
508+
"net6.0"
509+
],
510+
"sources": {
511+
"https://api.nuget.org/v3/index.json": {}
512+
},
513+
"frameworks": {
514+
"net6.0": {
515+
"targetAlias": "net6.0",
516+
"projectReferences": {}
517+
}
518+
},
519+
"warningProperties": {
520+
"warnAsError": [
521+
"NU1605"
522+
]
523+
}
524+
},
525+
"frameworks": {
526+
"net6.0": {
527+
"targetAlias": "net6.0",
528+
"imports": [
529+
"net461",
530+
"net462",
531+
"net47",
532+
"net471",
533+
"net472",
534+
"net48"
535+
],
536+
"assetTargetFallback": true,
537+
"warn": true,
538+
"frameworkReferences": {
539+
"Microsoft.NETCore.App": {
540+
"privateAssets": "all"
541+
}
542+
},
543+
"runtimeIdentifierGraphPath": "/usr/local/share/dotnet/sdk/6.0.202/RuntimeIdentifierGraph.json"
544+
}
545+
}
546+
},
495547
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q199_Binary_Tree_Right_Side_View/Q199_Binary_Tree_Right_Side_View.csproj": {
496548
"version": "1.0.0",
497549
"restore": {
@@ -1909,6 +1961,9 @@
19091961
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q1792_Buildings_With_Ocean_View/Q1792_Buildings_With_Ocean_View.csproj": {
19101962
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q1792_Buildings_With_Ocean_View/Q1792_Buildings_With_Ocean_View.csproj"
19111963
},
1964+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q189_Rotate_Array/Q189_Rotate_Array.csproj": {
1965+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q189_Rotate_Array/Q189_Rotate_Array.csproj"
1966+
},
19121967
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q199_Binary_Tree_Right_Side_View/Q199_Binary_Tree_Right_Side_View.csproj": {
19131968
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q199_Binary_Tree_Right_Side_View/Q199_Binary_Tree_Right_Side_View.csproj"
19141969
},

Run/obj/project.assets.json

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,16 @@
107107
"bin/placeholder/Q1792_Buildings_With_Ocean_View.dll": {}
108108
}
109109
},
110+
"Q189_Rotate_Array/1.0.0": {
111+
"type": "project",
112+
"framework": ".NETCoreApp,Version=v6.0",
113+
"compile": {
114+
"bin/placeholder/Q189_Rotate_Array.dll": {}
115+
},
116+
"runtime": {
117+
"bin/placeholder/Q189_Rotate_Array.dll": {}
118+
}
119+
},
110120
"Q199_Binary_Tree_Right_Side_View/1.0.0": {
111121
"type": "project",
112122
"framework": ".NETCoreApp,Version=v6.0",
@@ -456,6 +466,11 @@
456466
"path": "../Q1792_Buildings_With_Ocean_View/Q1792_Buildings_With_Ocean_View.csproj",
457467
"msbuildProject": "../Q1792_Buildings_With_Ocean_View/Q1792_Buildings_With_Ocean_View.csproj"
458468
},
469+
"Q189_Rotate_Array/1.0.0": {
470+
"type": "project",
471+
"path": "../Q189_Rotate_Array/Q189_Rotate_Array.csproj",
472+
"msbuildProject": "../Q189_Rotate_Array/Q189_Rotate_Array.csproj"
473+
},
459474
"Q199_Binary_Tree_Right_Side_View/1.0.0": {
460475
"type": "project",
461476
"path": "../Q199_Binary_Tree_Right_Side_View/Q199_Binary_Tree_Right_Side_View.csproj",
@@ -593,6 +608,7 @@
593608
"Q1650_Lowest_Common_Ancestor_of_a_Binary_Tree_III >= 1.0.0",
594609
"Q173_Binary_Search_Tree_Iterator >= 1.0.0",
595610
"Q1792_Buildings_With_Ocean_View >= 1.0.0",
611+
"Q189_Rotate_Array >= 1.0.0",
596612
"Q199_Binary_Tree_Right_Side_View >= 1.0.0",
597613
"Q211_Design_Add_Search_Words_Data_Structure >= 1.0.0",
598614
"Q215_Kth_Largest_Element >= 1.0.0",
@@ -672,6 +688,9 @@
672688
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q1792_Buildings_With_Ocean_View/Q1792_Buildings_With_Ocean_View.csproj": {
673689
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q1792_Buildings_With_Ocean_View/Q1792_Buildings_With_Ocean_View.csproj"
674690
},
691+
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q189_Rotate_Array/Q189_Rotate_Array.csproj": {
692+
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q189_Rotate_Array/Q189_Rotate_Array.csproj"
693+
},
675694
"/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q199_Binary_Tree_Right_Side_View/Q199_Binary_Tree_Right_Side_View.csproj": {
676695
"projectPath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Q199_Binary_Tree_Right_Side_View/Q199_Binary_Tree_Right_Side_View.csproj"
677696
},

Run/obj/project.nuget.cache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"version": 2,
3-
"dgSpecHash": "xN5Ax3GsX9TGkF16BoarHqxzHxis3zC8GFQpmvaQ1KmPY8QLtli5abKeGWX0wp5po6Mi4HQYe02seugBweac2g==",
3+
"dgSpecHash": "7y4ZZg5ahKQ0flSNDEk77NHVvJHNkeq9V4oxzois0izVJC6h2QbAoD8pWsyaqez/P2IoqBtIMi1I7xaVUW2I3g==",
44
"success": true,
55
"projectFilePath": "/Users/Bogdan/Documents/Work/CtCI/VS_Code/LeetcodeNew/Run/Run.csproj",
66
"expectedPackageFiles": [],

0 commit comments

Comments
 (0)