From 8c963d3aa2d3c9a272be33e3023196d50295cff2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=9F=D1=8B=D1=82=D0=BA=D0=BE=D0=B2=20=D0=A0=D0=BE=D0=BC?= =?UTF-8?q?=D0=B0=D0=BD?= Date: Wed, 25 Mar 2026 20:28:57 +0300 Subject: [PATCH] 31. Next Permutation --- .../31. Next Permutation.csproj | 11 +++++++ 31. Next Permutation/Program.cs | 30 +++++++++++++++++++ 31. Next Permutation/Solution.cs | 17 +++++++++++ Leetcode.sln | 14 +++++++++ 4 files changed, 72 insertions(+) create mode 100644 31. Next Permutation/31. Next Permutation.csproj create mode 100644 31. Next Permutation/Program.cs create mode 100644 31. Next Permutation/Solution.cs diff --git a/31. Next Permutation/31. Next Permutation.csproj b/31. Next Permutation/31. Next Permutation.csproj new file mode 100644 index 0000000..dfe3535 --- /dev/null +++ b/31. Next Permutation/31. Next Permutation.csproj @@ -0,0 +1,11 @@ + + + + Exe + net10.0 + _31._Next_Permutation + enable + enable + + + diff --git a/31. Next Permutation/Program.cs b/31. Next Permutation/Program.cs new file mode 100644 index 0000000..69128ca --- /dev/null +++ b/31. Next Permutation/Program.cs @@ -0,0 +1,30 @@ +var sol = new Solution(); + +var cases = new (int[] input, int[] expected)[] +{ + (new[] { 1, 2, 3 }, new[] { 1, 3, 2 }), + (new[] { 3, 2, 1 }, new[] { 1, 2, 3 }), + (new[] { 1, 1, 5 }, new[] { 1, 5, 1 }) +}; + +foreach (var (input, expected) in cases) +{ + var nums = (int[])input.Clone(); + sol.NextPermutation(nums); + var status = AreEqual(nums, expected) ? "ok" : "fail"; + Console.WriteLine($"{Format(input)} -> {Format(nums)} (expected: {Format(expected)}) {status}"); +} + +static string Format(int[] nums) => $"[{string.Join(",", nums)}]"; + +static bool AreEqual(int[] left, int[] right) +{ + if (left.Length != right.Length) + return false; + for (var i = 0; i < left.Length; i++) + { + if (left[i] != right[i]) + return false; + } + return true; +} diff --git a/31. Next Permutation/Solution.cs b/31. Next Permutation/Solution.cs new file mode 100644 index 0000000..1fbb09b --- /dev/null +++ b/31. Next Permutation/Solution.cs @@ -0,0 +1,17 @@ +public class Solution +{ + public void NextPermutation(int[] nums) + { + var i = nums.Length - 2; + while(i >= 0 && nums[i] >= nums[i+1]) + i--; + if(i >= 0) + { + var j = nums.Length - 1; + while(nums[i] >= nums[j]) + j--; + (nums[i], nums[j]) = (nums[j], nums[i]); + } + Array.Reverse(nums, i+1, nums.Length-i-1); + } +} \ No newline at end of file diff --git a/Leetcode.sln b/Leetcode.sln index 3193445..ea36914 100644 --- a/Leetcode.sln +++ b/Leetcode.sln @@ -189,6 +189,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "23. Merge k Sorted Lists", EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "26. Remove Duplicates from Sorted Array", "26. Remove Duplicates from Sorted Array\26. Remove Duplicates from Sorted Array.csproj", "{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "31. Next Permutation", "31. Next Permutation\31. Next Permutation.csproj", "{DC76DEDE-AF9D-429B-B53B-204E89E90EA0}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -1315,6 +1317,18 @@ Global {5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Release|x64.Build.0 = Release|Any CPU {5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Release|x86.ActiveCfg = Release|Any CPU {5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Release|x86.Build.0 = Release|Any CPU + {DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Debug|Any CPU.Build.0 = Debug|Any CPU + {DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Debug|x64.ActiveCfg = Debug|Any CPU + {DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Debug|x64.Build.0 = Debug|Any CPU + {DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Debug|x86.ActiveCfg = Debug|Any CPU + {DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Debug|x86.Build.0 = Debug|Any CPU + {DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|Any CPU.ActiveCfg = Release|Any CPU + {DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|Any CPU.Build.0 = Release|Any CPU + {DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x64.ActiveCfg = Release|Any CPU + {DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x64.Build.0 = Release|Any CPU + {DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x86.ActiveCfg = Release|Any CPU + {DC76DEDE-AF9D-429B-B53B-204E89E90EA0}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE