Skip to content

Instantly share code, notes, and snippets.

@unilecs
Created September 29, 2024 23:57
Show Gist options
  • Save unilecs/c884acbca7f93304974f8ddb6e6d811e to your computer and use it in GitHub Desktop.
Save unilecs/c884acbca7f93304974f8ddb6e6d811e to your computer and use it in GitHub Desktop.
Задача: Максимальная глубина вложенности скобок
using System;
public class Program
{
public static int MaxDepth(string s) {
int depth = 0;
int curDepth = 0;
for (int i = 0; i < s.Length; i++)
{
curDepth = s[i] == '(' ? curDepth + 1 : s[i] == ')' ? curDepth - 1 : curDepth;
depth = Math.Max(depth, curDepth);
}
return depth;
}
public static void Main()
{
Console.WriteLine("UniLecs");
// tests
Console.WriteLine(MaxDepth("(4)+((5))+(((6)))")); // 3
Console.WriteLine(MaxDepth("(1+(2*3)+((8)/4))+1")); // 3
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment