private NodeWithAncestor <char> AdjustNodePosition(NodeWithAncestor <char> descendent, int depthDifference)
 {
     for (int i = 0; i < depthDifference; i++)
     {
         descendent = descendent.Ancestor;
     }
     return(descendent);
 }
 private NodeWithAncestor <char> BacktrackUntillGetCommonAncestor(NodeWithAncestor <char> descendentOne, NodeWithAncestor <char> descendentTwo)
 {
     while (descendentOne != descendentTwo)
     {
         descendentOne = descendentOne.Ancestor;
         descendentTwo = descendentTwo.Ancestor;
     }
     return(descendentOne);
 }
        private int GetDepth(NodeWithAncestor <char> root, NodeWithAncestor <char> descendent)
        {
            int depth = 0;

            while (descendent != root)
            {
                depth++;
                descendent = descendent.Ancestor;
            }
            return(depth);
        }
        //O(d) time (deapth of the deapest descendent) | O(1)
        public NodeWithAncestor <char> GetYoungestCommonAncestor(NodeWithAncestor <char> root, NodeWithAncestor <char> descendentOne, NodeWithAncestor <char> descendentTwo)
        {
            int depthOne        = GetDepth(root, descendentOne);
            int depthTwo        = GetDepth(root, descendentTwo);
            int depthDifference = Math.Abs(depthOne - depthTwo);

            if (depthOne > depthTwo)
            {
                descendentOne = AdjustNodePosition(descendentOne, depthDifference);
            }
            else
            {
                descendentTwo = AdjustNodePosition(descendentTwo, depthDifference);
            }
            return(BacktrackUntillGetCommonAncestor(descendentOne, descendentTwo));
        }
        public void Main()
        {
            var sufixTrie = new SufixTrie();

            sufixTrie.PopulateSufixTrieFrom("babc");
            sufixTrie.Contains("bcx");
            sufixTrie.Contains("babc");
            var anagrams = GroupAnagrams(new List <string>()
            {
                "yo", "act", "flop", "tac", "cat", "oy", "olfp"
            });
            var longest = LongestPalindromeSubstring("abaxyzzyxf");
            var isValid = IsValidBracket("({}[])");

            isValid = IsValidBracket("({}[]))");
            isValid = IsValidBracket("({}[]))");
            var minMaxStack = new MinMaxStack();

            minMaxStack.Push(5);
            var value = minMaxStack.GetMin();

            value = minMaxStack.GetMax();
            minMaxStack.Push(7);
            value = minMaxStack.GetMin();
            value = minMaxStack.GetMax();
            minMaxStack.Push(2);
            value = minMaxStack.GetMin();
            value = minMaxStack.GetMax();
            minMaxStack.Pop();
            value = minMaxStack.GetMin();
            value = minMaxStack.GetMax();

            var s = SearchSortedMatrix(new int[3][] {
                new int[4] {
                    1, 2, 3, 4
                },
                new int[4] {
                    5, 6, 7, 8
                },
                new int[4] {
                    9, 10, 11, 12
                }
            }, 10);
            var ps = PowerSetCombination(new int[3] {
                1, 2, 3
            });
            var permutation = PermutationsArray(new int[] { 1, 2, 3 });
            var l1          = new SinglyLinkedListNode(1);
            var l2          = new SinglyLinkedListNode(2);
            var l3          = new SinglyLinkedListNode(3);
            var l4          = new SinglyLinkedListNode(4);
            var l5          = new SinglyLinkedListNode(5);

            l1.next = l2;
            l2.next = l3;
            l3.next = l4;
            l4.next = l5;
            RemoveNthLinkedList(l1, 3);
            var A = new NodeWithAncestor <char>('A');
            var B = new NodeWithAncestor <char>('B');
            var G = new NodeWithAncestor <char>('G');
            var H = new NodeWithAncestor <char>('H');
            var I = new NodeWithAncestor <char>('I');
            var P = new NodeWithAncestor <char>('P');
            var T = new NodeWithAncestor <char>('T');
            var U = new NodeWithAncestor <char>('U');

            T.Ancestor = P;
            U.Ancestor = P;
            P.Ancestor = H;
            H.Ancestor = B;
            G.Ancestor = B;
            I.Ancestor = B;
            B.Ancestor = A;
            var cYa    = GetYoungestCommonAncestor(A, I, T);
            var matrix = new int[5][] {
                new int[5] {
                    1, 0, 0, 1, 0
                },
                new int[5] {
                    1, 0, 1, 0, 0
                },
                new int[5] {
                    0, 0, 1, 0, 1
                },
                new int[5] {
                    1, 0, 1, 0, 1
                },
                new int[5] {
                    1, 0, 1, 1, 0
                },
            };

            RiverSizes(matrix);
            var node1 = new Node(1);
            var node2 = new Node(2);
            var node3 = new Node(3);
            var node4 = new Node(4);
            var node5 = new Node(5);

            node2.children = new List <Node>()
            {
                node3
            };
            node1.children = new List <Node>()
            {
                node2, node4, node5
            };
            Bfs(node1);
            Dfs(node1);
            HasSingleCycleArray(new int[] { 2, 3, 1, -4, -4, 2 });
            GetPositionsOfMaxSumSubArrayKadanesAlgorithm(new int[] { 3, 5, -9, 1, 3, -2, 3, 4, 7, 2, -9, 6, 3, 1, -5, 4 });
            GetMaxSumSubArrayKadanesAlgorithm(new int[] { 3, 5, -9, 1, 3, -2, 3, 4, 7, 2, -9, 6, 3, 1, -5, 4 });
            LevenshteinDistanceNumberOfChangesToSameString("abc", "yabd");
            NumberOfWaysToMakeChange(new int[] { 1, 5, 10, 25 }, 10);
            var results    = MaxSubsetSumNoAdjacent(new int[] { 7, 10, 12, 7, 9, 14 });
            var resultTwos = MaxSubsetSumNoAdjacentOptimized(new int[] { 7, 10, 12, 7, 9, 14 });

            //var node1 = new TreeNode(1);
            //var node10 = new TreeNode(10);
            //var node5 = new TreeNode(5);
            //var node55 = new TreeNode(5);
            //var node2 = new TreeNode(2);
            //var node15 = new TreeNode(15);
            //var node13 = new TreeNode(13);
            //var node22 = new TreeNode(22);
            //var node14 = new TreeNode(14);
            //node10.left = node5;
            //node10.right = node15;
            //node5.left = node2;
            //node5.right = node55;
            //node2.left = node1;
            //node15.left = node13;
            //node15.right = node22;
            //node13.right = node14;
            //var result = InvertBinaryTree(node10);
            //var resultIterative = InvertBinaryTreeIterative(node10);

            LongestPeak(new int[] { 1, 2, 3, 3, 4, 0, 10, 6, 5, -1, -3, 2, 3 });
            SpiralTraverseMatrix(new int[4][] {
                new[] { 1, 2, 3, 4 },
                new[] { 12, 13, 14, 5 },
                new[] { 11, 16, 15, 6 },
                new[] { 10, 9, 8, 7 }
            });
        }