-
Notifications
You must be signed in to change notification settings - Fork 7
/
Program.cs
37 lines (36 loc) · 985 Bytes
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System;
namespace PreviousPermutationWithOneSwap
{
class Program
{
static void Main(string[] args)
{
int[] A = { 3, 1, 1, 3 };
Console.WriteLine(PrevPermOpt1(A));
}
static int[] PrevPermOpt1(int[] A)
{
for (int i = A.Length - 2; i >= 0; i--)
{
if (A[i] > A[i + 1])
{
int swapPos = i + 1;
int max = 0;
for (int j = i + 1; j < A.Length; j++)
{
if(A[j] > max && A[j] < A[i])
{
swapPos = j;
max = A[j];
}
}
int tem = A[swapPos];
A[swapPos] = A[i];
A[i] = tem;
return A;
}
}
return A;
}
}
}