Beispiel #1
0
        public int lis(List <int> A)
        {
            var index       = 0;
            var max         = 1;
            var checkedList = new bool[A.Count];

            while (index < A.Count)
            {
                if (checkedList[index])
                {
                    index++;
                    continue;
                }

                TreeMultiNode root = new TreeMultiNode(A[index]);
                checkedList[index] = true;

                for (int i = index + 1; i < A.Count; i++)
                {
                    if (A[i] < root.val)
                    {
                        continue;
                    }

                    checkedList[i] = true;
                    var tmp = insertNode(root, A[i]);
                    if (max < tmp)
                    {
                        max = tmp;
                    }
                }

                index++;
            }

            if (max == 1)
            {
                return(1);
            }

            return(max + 1);
        }
Beispiel #2
0
        private int insertNode(TreeMultiNode root, int value)
        {
            var node = new TreeMultiNode(value);

            if (!root.children.Any())
            {
                root.children.Add(node);
                return(1);
            }

            var isAdded = false;
            var max     = 0;

            foreach (var child in root.children)
            {
                if (child.val > value)
                {
                    continue;
                }

                isAdded = true;

                var tmp = insertNode(child, value) + 1;

                if (tmp > max)
                {
                    max = tmp;
                }
            }

            if (!isAdded)
            {
                root.children.Add(node);

                if (max < 2)
                {
                    max = 2;
                }
            }

            return(max);
        }