Day3. Start Algorithm

This commit is contained in:
Electrominch
2022-10-05 00:44:15 +03:00
parent c9916dabad
commit 062e03bdbf
32 changed files with 446 additions and 57 deletions

View File

@@ -0,0 +1,11 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>_278._First_Bad_Version</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>
</Project>

View File

@@ -0,0 +1,10 @@
namespace _278._First_Bad_Version
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine(new Solution().FirstBadVersion(2147483647, 2147483647));
}
}
}

View File

@@ -0,0 +1,33 @@
namespace _278._First_Bad_Version;
public class VersionControl
{
protected int _badVersion = 0;
protected bool IsBadVersion(int version) => version >= _badVersion;
}
public class Solution : VersionControl
{
public int FirstBadVersion(int n)
{
long l = -1;
long r = n;
while (l != r - 1)
{
long i = (r + l) / 2;
bool res = IsBadVersion((int)i);
if (res)
r = i;
else
l = i;
}
return (int)l + 1;
}
public int FirstBadVersion(int n, int bad)
{
_badVersion = bad;
return FirstBadVersion(n);
}
}