Skip to content

Commit a83ccce

Browse files
committed
add solution for replace elements and valid mountain tasks
1 parent e61fb2e commit a83ccce

File tree

3 files changed

+82
-1
lines changed

3 files changed

+82
-1
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCodeResolves.Arrays.ReplaceElements
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
var nums = new int[] { 0, 3, 2, 1 };
12+
var result = ReplaceElements(nums);
13+
14+
Console.ReadLine();
15+
}
16+
17+
static int[] ReplaceElements(int[] arr)
18+
{
19+
if (arr.Length == 1) return new int[] { -1 };
20+
for(var i = 0; i < arr.Length; i++)
21+
{
22+
if (i == arr.Length - 1)
23+
{
24+
arr[i] = -1;
25+
}
26+
else
27+
{
28+
var maxValue = FindMax(arr, i + 1);
29+
arr[i] = maxValue;
30+
}
31+
}
32+
33+
return arr;
34+
}
35+
36+
static int FindMax(int[] arr, int startPosition)
37+
{
38+
var maxValue = 0;
39+
for(var i = startPosition; i < arr.Length; i++)
40+
{
41+
maxValue = Math.Max(arr[i], maxValue);
42+
}
43+
44+
return maxValue;
45+
}
46+
}
47+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace LeetCodeResolves.Arrays.ValidMountainArray
6+
{
7+
class Program
8+
{
9+
static void Main(string[] args)
10+
{
11+
var nums = new int[] { 0, 3, 2, 1 };
12+
var result = ValidMountainArray(nums);
13+
14+
Console.ReadLine();
15+
}
16+
17+
static bool ValidMountainArray(int[] arr)
18+
{
19+
var n = arr.Length;
20+
var i = 0;
21+
22+
while (i + 1 < n && arr[i] < arr[i + 1])
23+
i++;
24+
25+
if (i == 0 || i == n - 1)
26+
return false;
27+
28+
while (i + 1 < n && arr[i] > arr[i + 1])
29+
i++;
30+
31+
return i == n - 1;
32+
}
33+
}
34+
}

C#/LeetCodeResolves/LeetCodeResolves/LeetCodeResolves.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp3.1</TargetFramework>
6-
<StartupObject>LeetCodeResolves.Arrays.CheckIfDoubleExist.Program</StartupObject>
6+
<StartupObject>LeetCodeResolves.Arrays.ValidMountainArray.Program</StartupObject>
77
</PropertyGroup>
88

99
</Project>

0 commit comments

Comments
 (0)