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

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _1._Two_Sum;
namespace _1._Two_Sum;
public class Solution
{

View File

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

View File

@@ -0,0 +1,10 @@
namespace _121._Best_Time_to_Buy_and_Sell_Stock
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine(new Solution().MaxProfit(new int[] { 7, 1, 5, 3, 6, 4 }));
}
}
}

View File

@@ -0,0 +1,27 @@
namespace _121._Best_Time_to_Buy_and_Sell_Stock;
public class Solution
{
public int MaxProfit(int[] prices)
{
int min = prices[0];
int max = prices[0];
bool hasMax = false;
int res = 0;
foreach (int price in prices)
{
if (price > max || hasMax == false)
{
max = price;
hasMax = true;
res = Math.Max(max - min, res);
}
if (price < min)
{
min = price;
hasMax = false;
}
}
return res > 0 ? res : 0;
}
}

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _1480._Running_Sum_of_1d_Array;
namespace _1480._Running_Sum_of_1d_Array;
public class Solution
{

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _205._Isomorphic_Strings;
namespace _205._Isomorphic_Strings;
public class Solution
{

View File

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

View File

@@ -0,0 +1,36 @@
namespace _206._Reverse_Linked_List;
//Definition for singly-linked list.
public class ListNode
{
public int val;
public ListNode? next;
public ListNode(int val = 0, ListNode? next = null)
{
this.val = val;
this.next = next;
}
public static ListNode Create(int[] nums)
{
ListNode l = new ListNode(nums[0]);
ListNode cur = l;
for (int i = 1; i < nums.Length; i++)
{
cur.next = new ListNode(nums[i]);
cur = cur.next;
}
return l;
}
public override string ToString()
{
List<int> list = new List<int>();
ListNode? cur = this;
while (cur != null)
{
list.Add(cur.val);
cur = cur.next;
}
return String.Join(" ", list);
}
}

View File

@@ -0,0 +1,11 @@
namespace _206._Reverse_Linked_List
{
internal class Program
{
static void Main(string[] args)
{
var l = ListNode.Create(new int[] { 1 });
Console.WriteLine(new Solution().ReverseList(l));
}
}
}

View File

@@ -0,0 +1,22 @@
namespace _206._Reverse_Linked_List;
public class Solution
{
public ListNode? ReverseList(ListNode head)
{
if (head == null || head.next == null)
return head;
ListNode cur = head;
ListNode newHead = cur.next!;
cur.next = null;
while (true)
{
ListNode? buf = newHead.next;
newHead.next = cur;
if (buf == null)
return newHead;
cur = newHead;
newHead = buf;
}
}
}

View File

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

View File

@@ -0,0 +1,37 @@
namespace _21._Merge_Two_Sorted_Lists;
//Definition for singly-linked list.
public class ListNode
{
public int val;
public ListNode? next;
public ListNode(int val = 0, ListNode? next = null)
{
this.val = val;
this.next = next;
}
public static ListNode Create(int[] nums)
{
ListNode l = new ListNode(nums[0]);
ListNode cur = l;
for (int i = 1; i < nums.Length; i++)
{
cur.next = new ListNode(nums[i]);
cur = cur.next;
}
return l;
}
public override string ToString()
{
List<int> list = new List<int>();
ListNode? cur = this;
while (cur != null)
{
list.Add(cur.val);
cur = cur.next;
}
return String.Join(" ", list);
}
}

View File

@@ -0,0 +1,12 @@
namespace _21._Merge_Two_Sorted_Lists
{
internal class Program
{
static void Main(string[] args)
{
var l1 = ListNode.Create(new int[] { 1, 2, 4 });
var l2 = ListNode.Create(new int[] { 1, 3, 4 });
Console.WriteLine(new Solution().MergeTwoLists(l1, l2));
}
}
}

View File

@@ -0,0 +1,44 @@
namespace _21._Merge_Two_Sorted_Lists;
public class Solution
{
public ListNode MergeTwoLists(ListNode? list1, ListNode? list2)
{
ListNode? start = null;
ListNode? current = null;
List<ListNode?> listNodes = new List<ListNode?>() { list1, list2 };
while (listNodes.Any(l => l != null))
{
int i = MinIndex(listNodes);
ListNode? curMin = listNodes[i];
listNodes[i] = listNodes[i]!.next;
if (start == null)
{
start = curMin;
current = start;
}
else if (current != null)
{
current.next = curMin;
current = current.next;
}
}
return start!;
}
private int MinIndex(List<ListNode?> list)
{
int minValue = int.MaxValue;
int index = -1;
for (int i = 0; i < list.Count; i++)
{
if (list[i]?.val < minValue)
{
minValue = list[i]!.val;
index = i;
}
}
return index;
}
}

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);
}
}

View File

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

View File

@@ -0,0 +1,10 @@
namespace _35._Search_Insert_Position
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
}

View File

@@ -0,0 +1,19 @@
namespace _35._Search_Insert_Position;
public class Solution
{
public int SearchInsert(int[] nums, int target)
{
int l = -1;
int r = nums.Length;
while (l != r - 1)
{
int i = (r + l) / 2;
if (nums[i] >= target)
r = i;
else
l = i;
}
return l + 1;
}
}

View File

@@ -4,6 +4,24 @@ public class Solution
{
public int[] Intersect(int[] nums1, int[] nums2)
{
List<int> result = new List<int>();
Dictionary<int, int> table = new Dictionary<int, int>();
foreach (int n in nums1)
{
if (table.ContainsKey(n))
table[n]++;
else
table.Add(n, 1);
}
foreach (int n in nums2)
{
if (table.ContainsKey(n) && table[n] > 0)
{
result.Add(n);
table[n]--;
}
}
return result.ToArray();
}
}

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _392._Is_Subsequence;
namespace _392._Is_Subsequence;
public class Solution
{

View File

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

View File

@@ -0,0 +1,10 @@
namespace _704._Binary_Search
{
internal class Program
{
static void Main(string[] args)
{
Console.WriteLine(new Solution().Search(new int[] { -1, 0, 3, 5, 9, 12 }, 13));
}
}
}

View File

@@ -0,0 +1,22 @@
namespace _704._Binary_Search;
public class Solution
{
public int Search(int[] nums, int target)
{
int l = -1;
int r = nums.Length;
while (l < r - 1)
{
int i = l + (r - l) / 2;
int cur = nums[i];
if (cur == target)
return i;
if (cur > target)
r = i;
else
l = i;
}
return -1;
}
}

View File

@@ -1,10 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _724._Find_Pivot_Index;
namespace _724._Find_Pivot_Index;
public class Solution
{

View File

@@ -1,8 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
namespace _88._Merge_Sorted_Array;
namespace _88._Merge_Sorted_Array;
public class Solution
{

View File

@@ -19,7 +19,19 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "205. Isomorphic Strings", "
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "392. Is Subsequence", "392. Is Subsequence\392. Is Subsequence.csproj", "{82D9F372-85D0-4293-932D-6F90D4AF9CBD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "350. Intersection of Two Arrays II", "350. Intersection of Two Arrays II\350. Intersection of Two Arrays II.csproj", "{3EDA43F9-22B1-4CFE-9912-031FFAA2200E}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "350. Intersection of Two Arrays II", "350. Intersection of Two Arrays II\350. Intersection of Two Arrays II.csproj", "{3EDA43F9-22B1-4CFE-9912-031FFAA2200E}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "121. Best Time to Buy and Sell Stock", "121. Best Time to Buy and Sell Stock\121. Best Time to Buy and Sell Stock.csproj", "{79A12189-DF47-4F0B-ADB1-A5FD930AE7EB}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "21. Merge Two Sorted Lists", "21. Merge Two Sorted Lists\21. Merge Two Sorted Lists.csproj", "{3063143F-AAF9-4B23-8D98-A7236E01AA27}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "206. Reverse Linked List", "206. Reverse Linked List\206. Reverse Linked List.csproj", "{1C1BEA62-325F-48F8-8CC0-D5828A62F14F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "704. Binary Search", "704. Binary Search\704. Binary Search.csproj", "{69081812-D523-4211-AE09-9DAF8E9B83D8}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "278. First Bad Version", "278. First Bad Version\278. First Bad Version.csproj", "{3BA31E13-3748-4C4A-BE68-24D2E3DF0DC1}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "35. Search Insert Position", "35. Search Insert Position\35. Search Insert Position.csproj", "{B595BB51-666D-4E39-86EE-F9A3B8D8DA58}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
@@ -63,6 +75,30 @@ Global
{3EDA43F9-22B1-4CFE-9912-031FFAA2200E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3EDA43F9-22B1-4CFE-9912-031FFAA2200E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3EDA43F9-22B1-4CFE-9912-031FFAA2200E}.Release|Any CPU.Build.0 = Release|Any CPU
{79A12189-DF47-4F0B-ADB1-A5FD930AE7EB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{79A12189-DF47-4F0B-ADB1-A5FD930AE7EB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{79A12189-DF47-4F0B-ADB1-A5FD930AE7EB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{79A12189-DF47-4F0B-ADB1-A5FD930AE7EB}.Release|Any CPU.Build.0 = Release|Any CPU
{3063143F-AAF9-4B23-8D98-A7236E01AA27}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3063143F-AAF9-4B23-8D98-A7236E01AA27}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3063143F-AAF9-4B23-8D98-A7236E01AA27}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3063143F-AAF9-4B23-8D98-A7236E01AA27}.Release|Any CPU.Build.0 = Release|Any CPU
{1C1BEA62-325F-48F8-8CC0-D5828A62F14F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{1C1BEA62-325F-48F8-8CC0-D5828A62F14F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1C1BEA62-325F-48F8-8CC0-D5828A62F14F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1C1BEA62-325F-48F8-8CC0-D5828A62F14F}.Release|Any CPU.Build.0 = Release|Any CPU
{69081812-D523-4211-AE09-9DAF8E9B83D8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{69081812-D523-4211-AE09-9DAF8E9B83D8}.Debug|Any CPU.Build.0 = Debug|Any CPU
{69081812-D523-4211-AE09-9DAF8E9B83D8}.Release|Any CPU.ActiveCfg = Release|Any CPU
{69081812-D523-4211-AE09-9DAF8E9B83D8}.Release|Any CPU.Build.0 = Release|Any CPU
{3BA31E13-3748-4C4A-BE68-24D2E3DF0DC1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3BA31E13-3748-4C4A-BE68-24D2E3DF0DC1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3BA31E13-3748-4C4A-BE68-24D2E3DF0DC1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3BA31E13-3748-4C4A-BE68-24D2E3DF0DC1}.Release|Any CPU.Build.0 = Release|Any CPU
{B595BB51-666D-4E39-86EE-F9A3B8D8DA58}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B595BB51-666D-4E39-86EE-F9A3B8D8DA58}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B595BB51-666D-4E39-86EE-F9A3B8D8DA58}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B595BB51-666D-4E39-86EE-F9A3B8D8DA58}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE