Compare commits

...

10 Commits

30 changed files with 1069 additions and 0 deletions

View File

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

View File

@@ -0,0 +1,67 @@
var sol = new Solution();
var cases = new (int?[] input, int?[] expected)[]
{
(new int?[] { 1, 2, 5, 3, 4, null, 6 }, new int?[] { 1, null, 2, null, 3, null, 4, null, 5, null, 6 }),
// (Array.Empty<int?>(), Array.Empty<int?>()),
// (new int?[] { 0 }, new int?[] { 0 })
};
foreach (var (input, expected) in cases)
{
var root = BuildTreeFromLevelOrder(input);
if (root != null)
sol.Flatten(root);
var actual = FlattenToOutputList(root);
Console.WriteLine($"[{string.Join(", ", input.Select(FormatNullable))}] -> [{string.Join(", ", actual.Select(FormatNullable))}] (expected: [{string.Join(", ", expected.Select(FormatNullable))}])");
}
static TreeNode? BuildTreeFromLevelOrder(int?[] data)
{
if (data.Length == 0 || data[0] == null)
return null;
var root = new TreeNode(data[0]!.Value);
var queue = new Queue<TreeNode>();
queue.Enqueue(root);
var i = 1;
while (queue.Count > 0 && i < data.Length)
{
var node = queue.Dequeue();
if (i < data.Length && data[i] != null)
{
node.left = new TreeNode(data[i]!.Value);
queue.Enqueue(node.left);
}
i++;
if (i < data.Length && data[i] != null)
{
node.right = new TreeNode(data[i]!.Value);
queue.Enqueue(node.right);
}
i++;
}
return root;
}
static List<int?> FlattenToOutputList(TreeNode? root)
{
var result = new List<int?>();
var node = root;
while (node != null)
{
result.Add(node.val);
if (node.right != null)
result.Add(null);
node = node.right;
}
return result;
}
static string FormatNullable(int? value) => value?.ToString() ?? "null";

View File

@@ -0,0 +1,27 @@
public class Solution
{
public void Flatten(TreeNode? root)
{
if (root == null)
return;
var head = root;
var stack = new Stack<TreeNode>();
if (root.right != null)
stack.Push(root.right);
if (root.left != null)
stack.Push(root.left);
while (stack.Count > 0)
{
var node = stack.Pop();
head.left = null;
head.right = node;
head = node;
if (node.right != null)
stack.Push(node.right);
if (node.left != null)
stack.Push(node.left);
}
}
}

View File

@@ -0,0 +1,12 @@
public class TreeNode
{
public int val;
public TreeNode? left;
public TreeNode? right;
public TreeNode(int val = 0, TreeNode? left = null, TreeNode? right = null)
{
this.val = val;
this.left = left;
this.right = right;
}
}

11
18. 4Sum/18. 4Sum.csproj Normal file
View File

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

40
18. 4Sum/Program.cs Normal file
View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
var sol = new Solution();
var cases = new (int[] nums, int target, int[][] expected)[]
{
// (new[] { 1, 0, -1, 0, -2, 2 }, 0, new[]
// {
// new[] { -2, -1, 1, 2 },
// new[] { -2, 0, 0, 2 },
// new[] { -1, 0, 0, 1 }
// }),
// (new[] { 2, 2, 2, 2, 2 }, 8, new[]
// {
// new[] { 2, 2, 2, 2 }
// }),
// (new[] { -3, -1, 0, 2, 4, 5 }, 2, new[]
// {
// new[] { -3, -1, 2, 4 }
// }),
(new[] { 1000000000, 1000000000, 1000000000, 1000000000 }, -294967296, Array.Empty<int[]>())
};
foreach (var (nums, target, expected) in cases)
{
var actual = sol.FourSum(nums, target);
Console.WriteLine($"nums={FormatArray(nums)}, target={target} -> {FormatResults(actual)} (expected: {FormatResults(expected)})");
}
static string FormatArray(int[] nums)
{
return $"[{string.Join(",", nums)}]";
}
static string FormatResults(IList<IList<int>> results)
{
return "[" + string.Join(",", results.Select(r => $"[{string.Join(",", r)}]")) + "]";
}

143
18. 4Sum/Solution.cs Normal file
View File

@@ -0,0 +1,143 @@
public class Solution
{
record struct TwoSumSource(int A, int B)
{
public override string ToString()
{
return $"({A}+{B}={A + B})";
}
}
record struct AnswerRow(int? N0 = null, int? N1 = null, int? N2 = null, int? N3 = null)
{
public void PushNum(int num)
{
if (N0 == null)
{
N0 = num;
}
else if (N1 == null)
{
N1 = num;
if (N0 > N1)
{
(N0, N1) = (N1, N0);
}
}
else if (N2 == null)
{
N2 = num;
if (N1 > N2)
{
(N1, N2) = (N2, N1);
}
if (N0 > N1)
{
(N0, N1) = (N1, N0);
}
}
else if (N3 == null)
{
N3 = num;
if (N2 > N3)
{
(N2, N3) = (N3, N2);
}
if (N1 > N2)
{
(N1, N2) = (N2, N1);
}
if (N0 > N1)
{
(N0, N1) = (N1, N0);
}
}
else if (num < N3)
{
N3 = num;
if (N2 > N3)
{
(N2, N3) = (N3, N2);
}
if (N1 > N2)
{
(N1, N2) = (N2, N1);
}
if (N0 > N1)
{
(N0, N1) = (N1, N0);
}
}
}
}
public IList<IList<int>> FourSum(int[] nums, int target)
{
var dict = new Dictionary<int, int>();
// суммы из двух слагаемых, все возможные
var twoSums = new Dictionary<long, HashSet<TwoSumSource>>();
for (var i = 0; i < nums.Length; i++)
{
var num1 = nums[i];
dict.TryAdd(num1, 0);
dict[num1]++;
for (var j = i + 1; j < nums.Length; j++)
{
var num2 = nums[j];
var sum = num1 + num2;
twoSums.TryAdd(sum, new HashSet<TwoSumSource>());
twoSums[sum].Add(new TwoSumSource() { A = num1, B = num2 });
}
}
var rows = new HashSet<AnswerRow>();
// словарь для требуемоего количества чисел
var needDict = new Dictionary<int, int>();
foreach (var kp1 in dict)
{
var needThreeSum = target - kp1.Key;
needDict.TryAdd(kp1.Key, 0);
needDict[kp1.Key]++;
foreach (var kp2 in dict)
{
needDict.TryAdd(kp2.Key, 0);
needDict[kp2.Key]++;
var needTwoSum = (long)needThreeSum - kp2.Key;
if (twoSums.TryGetValue(needTwoSum, out var curTwoSums))
{
foreach (var curTwoSum in curTwoSums)
{
needDict.TryAdd(curTwoSum.A, 0);
needDict[curTwoSum.A]++;
needDict.TryAdd(curTwoSum.B, 0);
needDict[curTwoSum.B]++;
if (needDict.All(kp => dict[kp.Key] >= kp.Value))
{
var row = new AnswerRow();
row.PushNum(kp1.Key);
row.PushNum(kp2.Key);
row.PushNum(curTwoSum.A);
row.PushNum(curTwoSum.B);
rows.Add(row);
}
needDict[curTwoSum.A]--;
needDict[curTwoSum.B]--;
}
}
needDict[kp2.Key]--;
}
needDict.Clear();
}
var answer = new List<IList<int>>(rows.Count);
foreach (var row in rows)
answer.Add(new List<int> { row.N0!.Value, row.N1!.Value, row.N2!.Value, row.N3!.Value });
return answer;
}
}

View File

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

View File

@@ -0,0 +1,11 @@
public class ListNode
{
public int val;
public ListNode? next;
public ListNode(int val = 0, ListNode? next = null)
{
this.val = val;
this.next = next;
}
}

View File

@@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

View File

@@ -0,0 +1,29 @@
public class Solution
{
public ListNode? MergeKLists(ListNode[] lists)
{
ListNode? head = null;
ListNode? tail = null;
var queue = new PriorityQueue<ListNode, int>();
foreach (var l in lists)
if (l != null)
queue.Enqueue(l, l.val);
while (queue.Count > 0)
{
var l = queue.Dequeue();
if (head == null || tail == null)
{
head = l;
tail = l;
}
else
{
tail.next = l;
tail = l;
}
if (l.next != null)
queue.Enqueue(l.next, l.next.val);
}
return head;
}
}

View File

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

View File

@@ -0,0 +1,2 @@
// See https://aka.ms/new-console-template for more information
Console.WriteLine("Hello, World!");

View File

@@ -0,0 +1,16 @@
public class Solution {
public int RemoveDuplicates(int[] nums) {
var k = nums.Length;
var putIndex = 0;
for(var i = 0; i < nums.Length; i++)
{
while(i < nums.Length - 1 && nums[i+1] == nums[i])
{
i++;
k--;
}
nums[putIndex++] = nums[i];
}
return k;
}
}

View File

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

View File

@@ -0,0 +1,18 @@
var sol = new Solution();
var cases = new (string s, string[] words, int[] expected)[]
{
// ("barfoothefoobarman", new[] { "foo", "bar" }, new[] { 0, 9 }),
// ("wordgoodgoodgoodbestword", new[] { "word", "good", "best", "word" }, Array.Empty<int>()),
// ("barfoofoobarthefoobarman", new[] { "bar", "foo", "the" }, new[] { 6, 9, 12 }),
("wordgoodgoodgoodbestword", new[] { "word", "good", "best", "good" }, new[] { 8 })
};
string Format(IEnumerable<int> values) => $"[{string.Join(",", values)}]";
string FormatWords(IEnumerable<string> values) => $"[{string.Join(",", values)}]";
foreach (var (s, words, expected) in cases)
{
var actual = sol.FindSubstring(s, words);
Console.WriteLine($"{s} | {FormatWords(words)} -> {Format(actual)} (expected: {Format(expected)})");
}

View File

@@ -0,0 +1,35 @@
public class Solution
{
public IList<int> FindSubstring(string s, string[] words)
{
var len = words[0].Length;
var allLen = len * words.Length;
var dict = new Dictionary<string, int>();
foreach (var word in words)
{
dict.TryAdd(word, 0);
dict[word]++;
}
var answer = new List<int>();
var windowDict = new Dictionary<string, int>();
for (var offset = 0; offset < len; offset++)
{
for (var i = offset; i <= s.Length-len; i += len)
{
var word = s[i..(i + len)];
var prevInx = i - allLen;
if (prevInx >= 0)
{
var leftWord = s[prevInx..(prevInx + len)];
windowDict[leftWord]--;
}
windowDict.TryAdd(word, 0);
windowDict[word]++;
if (dict.All(kvp => windowDict.TryGetValue(kvp.Key, out var count) && count == kvp.Value))
answer.Add(prevInx + len);
}
windowDict.Clear();
}
return answer;
}
}

View File

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

View File

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

View File

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

View File

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

View File

@@ -0,0 +1,5 @@
var sol = new Solution();
// System.Console.WriteLine(sol.LongestValidParentheses(")()())"));
// System.Console.WriteLine(sol.LongestValidParentheses("()(()"));
System.Console.WriteLine(sol.LongestValidParentheses("()(())"));

View File

@@ -0,0 +1,43 @@
public class Solution
{
struct Item
{
public char Sym;
public int Count;
public char? PrevSym;
}
public int LongestValidParentheses(string s)
{
var stack = new Stack<Item>();
var answer = 0;
foreach (var ch in s)
{
if (stack.Count > 0 && (stack.Peek().Sym == '(' || stack.Peek().PrevSym == '(') && ch == ')')
{
var prev = stack.Pop();
var count = 2;
if (prev.Sym == '#')
{
stack.Pop();
count += prev.Count;
}
while (stack.Count > 0 && stack.Peek().Sym == '#')
count += stack.Pop().Count;
char? prevSym = null;
if (stack.Count > 0)
prevSym = stack.Peek().Sym;
stack.Push(new Item() { Sym = '#', Count = count, PrevSym = prevSym });
answer = Math.Max(answer, count);
}
else
{
stack.Push(new Item() { Sym = ch, Count = 0 });
}
}
return answer;
}
}

View File

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

View File

@@ -0,0 +1,29 @@
var sol = new Solution();
var cases = new (int[][] grid, bool expected, string name)[]
{
(
new[]
{
new[] { 1, 4 },
new[] { 2, 3 }
},
true,
"Example 1"
),
(
new[]
{
new[] { 1, 3 },
new[] { 2, 4 }
},
false,
"Example 2"
)
};
foreach (var (grid, expected, name) in cases)
{
var actual = sol.CanPartitionGrid(grid);
Console.WriteLine($"{name}: {actual} (expected: {expected})");
}

View File

@@ -0,0 +1,49 @@
public class Solution
{
struct SumInfo
{
public long Left;
public long Right;
public long Value;
}
public bool CanPartitionGrid(int[][] grid)
{
var height = grid.Length;
var width = grid[0].Length;
var colSums = new SumInfo[width];
var rowSums = new SumInfo[height];
for (var i = 0; i < height; i++)
{
for (var j = 0; j < grid[i].Length; j++)
{
colSums[j].Value += grid[i][j];
rowSums[i].Value += grid[i][j];
}
rowSums[i].Left = rowSums[i].Value;
if (i > 0)
rowSums[i].Left += rowSums[i - 1].Left;
}
for (var i = height - 1; i >= 0; i--)
{
rowSums[i].Right = rowSums[i].Value;
if (i < height - 1)
rowSums[i].Right += rowSums[i + 1].Right;
}
for (var j = 0; j < width; j++)
{
colSums[j].Left = colSums[j].Value;
if (j > 0)
colSums[j].Left += colSums[j - 1].Left;
}
for (var j = width - 1; j >= 0; j--)
{
colSums[j].Right = colSums[j].Value;
if (j < width - 1)
colSums[j].Right += colSums[j + 1].Right;
}
return rowSums.Any(row => Math.Abs(row.Left - row.Right) == row.Value) || colSums.Any(col => Math.Abs(col.Left - col.Right) == col.Value);
}
}

View File

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

View File

@@ -0,0 +1,139 @@
var sol = new Solution();
var cases = new (int[][] grid, bool expected, string name)[]
{
(
new[]
{
new[] { 1, 4 },
new[] { 2, 3 }
},
true,
"Example 1"
),
(
new[]
{
new[] { 1, 2 },
new[] { 3, 4 }
},
true,
"Example 2"
),
(
new[]
{
new[] { 1, 2, 4 },
new[] { 2, 3, 5 }
},
false,
"Example 3"
),
(
new[]
{
new[] { 4, 1, 8 },
new[] { 3, 2, 6 }
},
false,
"Example 4"
),
(
new[]
{
new[] { 5, 5, 6, 2, 2, 2 }
},
true,
"Example 5"
),
(
new[]
{
new[] { 100000 },
new[] { 86218 },
new[] { 100000 }
},
true,
"Example 6"
),
(
new[]
{
new[] { 1, 2, 4 },
new[] { 1, 6, 6 },
new[] { 5, 6, 7 }
},
true,
"Example 7"
),
(
new[]
{
new[] { 1, 2, 1, 1, 1 },
new[] { 1, 1, 1, 2, 1 }
},
true,
"Example 8"
),
(
new[]
{
new[] { 1, 1 },
new[] { 2, 1 },
new[] { 1, 1 },
new[] { 1, 2 },
new[] { 1, 1 }
},
true,
"Example 9"
),
(
new[]
{
new[] { 10, 5, 4, 5 }
},
false,
"Example 10"
),
(
new[]
{
new[] { 1, 1 },
new[] { 2, 1 },
new[] { 4, 3 }
},
false,
"Example 11"
),
(
new[]
{
new[] { 4 },
new[] { 3 },
new[] { 4 },
new[] { 4 },
new[] { 4 }
},
false,
"Example 12"
),
(
new[]
{
new[] { 100000 },
new[] { 100000 },
new[] { 100000 },
new[] { 100000 },
new[] { 1 }
},
true,
"Example 13"
),
};
foreach (var (grid, expected, name) in cases)
{
var actual = sol.CanPartitionGrid(grid);
Console.WriteLine($"{name}: {actual} (expected: {expected})");
}

View File

@@ -0,0 +1,130 @@
public class Solution
{
struct SumInfo
{
public long Left;
public long Right;
public long Value;
// ключ - число, значение - индексы, где оно встречается
public Dictionary<long, SortedSet<int>> Nums;
}
public bool CanPartitionGrid(int[][] grid)
{
var height = grid.Length;
var width = grid[0].Length;
var colSums = new SumInfo[width];
var rowSums = new SumInfo[height];
for (var i = 0; i < height; i++)
{
for (var j = 0; j < width; j++)
{
var num = grid[i][j];
colSums[j].Value += num;
rowSums[i].Value += num;
rowSums[i].Nums ??= new Dictionary<long, SortedSet<int>>();
colSums[j].Nums ??= new Dictionary<long, SortedSet<int>>();
rowSums[i].Nums.TryAdd(num, new SortedSet<int>());
rowSums[i].Nums[num].Add(j);
colSums[j].Nums.TryAdd(num, new SortedSet<int>());
colSums[j].Nums[num].Add(i);
}
rowSums[i].Left = rowSums[i].Value;
if (i > 0)
rowSums[i].Left += rowSums[i - 1].Left;
}
for (var i = height - 1; i >= 0; i--)
{
rowSums[i].Right = rowSums[i].Value;
if (i < height - 1)
rowSums[i].Right += rowSums[i + 1].Right;
}
for (var j = 0; j < width; j++)
{
colSums[j].Left = colSums[j].Value;
if (j > 0)
colSums[j].Left += colSums[j - 1].Left;
}
for (var j = width - 1; j >= 0; j--)
{
colSums[j].Right = colSums[j].Value;
if (j < width - 1)
colSums[j].Right += colSums[j + 1].Right;
}
var answer = false;
for (var i = 0; i < rowSums.Length - 1 && !answer; i++)
{
var row = rowSums[i];
var nextRow = rowSums[i + 1];
var diff = Math.Abs(row.Left - nextRow.Right);
var edgesOnly = colSums.Length == 1;
var colLen = height;
if (diff == 0 ||
(row.Left < nextRow.Right && CanDelete(diff, colSums, i, true, i + 1 == rowSums.Length - 1, edgesOnly, colLen)) ||
(row.Left > nextRow.Right && CanDelete(diff, colSums, i, false, i == 0, edgesOnly, colLen)))
answer |= true;
}
for (var j = 0; j < colSums.Length - 1 && !answer; j++)
{
var col = colSums[j];
var nextCol = colSums[j + 1];
var diff = Math.Abs(col.Left - nextCol.Right);
// тут проход по колонкам, значит поиск элемента для удаления надо выполнять по строкам
// если j == 0 и отрезаем в левой части, то надо смотреть только первую и последнюю строку
// если j+1 == colSums.Len-1 и отрезаем в правой части, то надо смотреть только первую и последнюю строку
var edgesOnly = rowSums.Length == 1;
var rowLen = width;
if (diff == 0 ||
(col.Left < nextCol.Right && CanDelete(diff, rowSums, j, true, j + 1 == colSums.Length - 1, edgesOnly, rowLen)) ||
(col.Left > nextCol.Right && CanDelete(diff, rowSums, j, false, j == 0, edgesOnly, rowLen)))
answer |= true;
}
return answer;
}
/// <summary>
///
/// </summary>
/// <param name="target">Целевое число для поиска</param>
/// <param name="infos">Что перебирать</param>
/// <param name="idx">Индекс отсечения (для удаления справа строго больше, для удаления слева включительно)</param>
/// <param name="deleteAfterIdx">Удалять ли спарва от индекса</param>
/// <param name="firstAndLastOnly">Посмотреть только первый и последний SumInfo</param>
/// <param name="edgesOnly">В sumInfo брать только крайние элементы</param>
/// <param name="len">Количество чисел в SumInfo</param>
/// <returns></returns>
private static bool CanDelete(long target, SumInfo[] infos, int idx, bool deleteAfterIdx, bool firstAndLastOnly, bool edgesOnly, int len)
{
for (var i = 0; i < infos.Length; i++)
{
var nums = infos[i].Nums;
if (nums.TryGetValue(target, out var indexes))
{
var before = false;
var after = false;
if (edgesOnly)
{
before = indexes.Contains(0) || indexes.Contains(idx);
after = indexes.Contains(len - 1) || indexes.Contains(idx+1);
}
else
{
before = indexes.GetViewBetween(int.MinValue, idx).Count > 0;
after = indexes.GetViewBetween(idx + 1, int.MaxValue).Count > 0;
}
if ((before && !deleteAfterIdx) || (after && deleteAfterIdx))
return true;
}
// если только границы, перейти сразу к последнему элементу
if (firstAndLastOnly && i == 0)
i = Math.Max(i, infos.Length - 2);
}
return false;
}
}

View File

@@ -181,6 +181,24 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "10. Regular Expression Matc
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "2906. Construct Product Matrix", "2906. Construct Product Matrix\2906. Construct Product Matrix.csproj", "{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "2906. Construct Product Matrix", "2906. Construct Product Matrix\2906. Construct Product Matrix.csproj", "{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3546. Equal Sum Grid Partition I", "3546. Equal Sum Grid Partition I\3546. Equal Sum Grid Partition I.csproj", "{8533B947-4274-46C9-8EEA-1439778FEE32}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "18. 4Sum", "18. 4Sum\18. 4Sum.csproj", "{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "23. Merge k Sorted Lists", "23. Merge k Sorted Lists\23. Merge k Sorted Lists.csproj", "{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}"
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
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "32. Longest Valid Parentheses", "32. Longest Valid Parentheses\32. Longest Valid Parentheses.csproj", "{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "114. Flatten Binary Tree to Linked List", "114. Flatten Binary Tree to Linked List\114. Flatten Binary Tree to Linked List.csproj", "{D86CCC06-597D-4DF8-8DCC-E7804E773530}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "30. Substring with Concatenation of All Words", "30. Substring with Concatenation of All Words\30. Substring with Concatenation of All Words.csproj", "{DC410457-B769-4597-857D-5AAD2BDBAAC3}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "3548. Equal Sum Grid Partition II", "3548. Equal Sum Grid Partition II\3548. Equal Sum Grid Partition II.csproj", "{D4C2B514-1A13-495A-8627-8B40AD8B2770}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@@ -1259,6 +1277,114 @@ Global
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x64.Build.0 = Release|Any CPU {D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x64.Build.0 = Release|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x86.ActiveCfg = Release|Any CPU {D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x86.ActiveCfg = Release|Any CPU
{D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x86.Build.0 = Release|Any CPU {D4E00D31-9A2A-402F-AF77-E4B7DB8182C9}.Release|x86.Build.0 = Release|Any CPU
{8533B947-4274-46C9-8EEA-1439778FEE32}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8533B947-4274-46C9-8EEA-1439778FEE32}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8533B947-4274-46C9-8EEA-1439778FEE32}.Debug|x64.ActiveCfg = Debug|Any CPU
{8533B947-4274-46C9-8EEA-1439778FEE32}.Debug|x64.Build.0 = Debug|Any CPU
{8533B947-4274-46C9-8EEA-1439778FEE32}.Debug|x86.ActiveCfg = Debug|Any CPU
{8533B947-4274-46C9-8EEA-1439778FEE32}.Debug|x86.Build.0 = Debug|Any CPU
{8533B947-4274-46C9-8EEA-1439778FEE32}.Release|Any CPU.ActiveCfg = Release|Any CPU
{8533B947-4274-46C9-8EEA-1439778FEE32}.Release|Any CPU.Build.0 = Release|Any CPU
{8533B947-4274-46C9-8EEA-1439778FEE32}.Release|x64.ActiveCfg = Release|Any CPU
{8533B947-4274-46C9-8EEA-1439778FEE32}.Release|x64.Build.0 = Release|Any CPU
{8533B947-4274-46C9-8EEA-1439778FEE32}.Release|x86.ActiveCfg = Release|Any CPU
{8533B947-4274-46C9-8EEA-1439778FEE32}.Release|x86.Build.0 = Release|Any CPU
{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Debug|x64.ActiveCfg = Debug|Any CPU
{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Debug|x64.Build.0 = Debug|Any CPU
{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Debug|x86.ActiveCfg = Debug|Any CPU
{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Debug|x86.Build.0 = Debug|Any CPU
{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Release|Any CPU.Build.0 = Release|Any CPU
{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Release|x64.ActiveCfg = Release|Any CPU
{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Release|x64.Build.0 = Release|Any CPU
{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Release|x86.ActiveCfg = Release|Any CPU
{0AF9E85C-7CCC-4E82-80B6-39931EA17D52}.Release|x86.Build.0 = Release|Any CPU
{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Debug|x64.ActiveCfg = Debug|Any CPU
{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Debug|x64.Build.0 = Debug|Any CPU
{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Debug|x86.ActiveCfg = Debug|Any CPU
{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Debug|x86.Build.0 = Debug|Any CPU
{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Release|Any CPU.Build.0 = Release|Any CPU
{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Release|x64.ActiveCfg = Release|Any CPU
{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Release|x64.Build.0 = Release|Any CPU
{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Release|x86.ActiveCfg = Release|Any CPU
{B586286C-CBA7-4AF0-A2B3-E488639B2E5C}.Release|x86.Build.0 = Release|Any CPU
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Debug|x64.ActiveCfg = Debug|Any CPU
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Debug|x64.Build.0 = Debug|Any CPU
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Debug|x86.ActiveCfg = Debug|Any CPU
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Debug|x86.Build.0 = Debug|Any CPU
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Release|Any CPU.Build.0 = Release|Any CPU
{5CE01D6E-7C12-45DC-8C26-DFE67F16AD3F}.Release|x64.ActiveCfg = Release|Any CPU
{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
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Debug|x64.ActiveCfg = Debug|Any CPU
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Debug|x64.Build.0 = Debug|Any CPU
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Debug|x86.ActiveCfg = Debug|Any CPU
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Debug|x86.Build.0 = Debug|Any CPU
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|Any CPU.Build.0 = Release|Any CPU
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x64.ActiveCfg = Release|Any CPU
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x64.Build.0 = Release|Any CPU
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x86.ActiveCfg = Release|Any CPU
{9BD247FC-E91E-4FBD-9E97-6B917E4606D2}.Release|x86.Build.0 = Release|Any CPU
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Debug|x64.ActiveCfg = Debug|Any CPU
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Debug|x64.Build.0 = Debug|Any CPU
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Debug|x86.ActiveCfg = Debug|Any CPU
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Debug|x86.Build.0 = Debug|Any CPU
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|Any CPU.Build.0 = Release|Any CPU
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|x64.ActiveCfg = Release|Any CPU
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|x64.Build.0 = Release|Any CPU
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|x86.ActiveCfg = Release|Any CPU
{D86CCC06-597D-4DF8-8DCC-E7804E773530}.Release|x86.Build.0 = Release|Any CPU
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Debug|x64.ActiveCfg = Debug|Any CPU
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Debug|x64.Build.0 = Debug|Any CPU
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Debug|x86.ActiveCfg = Debug|Any CPU
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Debug|x86.Build.0 = Debug|Any CPU
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Release|Any CPU.Build.0 = Release|Any CPU
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Release|x64.ActiveCfg = Release|Any CPU
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Release|x64.Build.0 = Release|Any CPU
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Release|x86.ActiveCfg = Release|Any CPU
{DC410457-B769-4597-857D-5AAD2BDBAAC3}.Release|x86.Build.0 = Release|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Debug|x64.ActiveCfg = Debug|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Debug|x64.Build.0 = Debug|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Debug|x86.ActiveCfg = Debug|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Debug|x86.Build.0 = Debug|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Release|Any CPU.Build.0 = Release|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Release|x64.ActiveCfg = Release|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Release|x64.Build.0 = Release|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Release|x86.ActiveCfg = Release|Any CPU
{D4C2B514-1A13-495A-8627-8B40AD8B2770}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE