Exemple #1
0
        private bool Get(MyAVLCountTree <int, int> avl, int index1, int index2, out int amount)
        {
            avl.SearchLTE(index1, out var resultNode1);
            avl.SearchLTE(index2, out var resultNode2);

            if (resultNode1 == resultNode2)
            {
                amount = index2 - index1;
                return(true);
            }
            else
            {
                int amount1 = avl.AmountNodesGT(resultNode1);
                int amount2 = avl.AmountNodesGT(resultNode2);
                amount  = index2 - index1;
                amount -= amount1 - amount2;
                return(true);
            }
        }
Exemple #2
0
 private void SplitAvl(MyAVLCountTree <int, int> avl, int index)
 {
     avl.Insert(index + 1, 0);
 }
Exemple #3
0
        public string MinInteger(string num, int k)
        {
            var numDigits = num.Select(e => int.Parse(e.ToString())).ToArray();
            var indices   = new LinkedList <int> [10];

            for (int i = 0; i < indices.Length; i++)
            {
                indices[i] = new LinkedList <int>();
            }

            for (int i = 0; i < numDigits.Length; i++)
            {
                indices[numDigits[i]].AddLast(i);
            }

            MyAVLCountTree <int, int> avl = new MyAVLCountTree <int, int>();

            avl.Insert(0, int.MaxValue);

            LinkedList <int> result = new LinkedList <int>();

            bool[] processed = new bool[numDigits.Length];

            for (int i = 0; i < numDigits.Length; i++)
            {
                if (processed[i])
                {
                    continue;
                }

                if (k != 0)
                {
                    bool isSelected = false;

                    for (int j = 0; j < numDigits[i]; j++)
                    {
                        if (indices[j].Count == 0)
                        {
                            continue;
                        }
                        else
                        {
                            while (indices[j].Count > 0 && indices[j].First.Value < i)
                            {
                                indices[j].RemoveFirst();
                            }
                            if (indices[j].Count == 0)
                            {
                                continue;
                            }
                        }

                        Get(avl, i, indices[j].First.Value, out int amount);
                        if (k >= amount)
                        {
                            isSelected = true;
                            processed[indices[j].First.Value] = true;
                            SplitAvl(avl, indices[j].First.Value);
                            indices[j].RemoveFirst();
                            k -= amount;
                            result.AddLast(j);
                            i--;
                            break;
                        }
                    }
                    if (!isSelected)
                    {
                        result.AddLast(numDigits[i]);
                    }
                }
                else
                {
                    result.AddLast(numDigits[i]);
                }
            }

            return(new string(result.Select(e => e.ToString()[0]).ToArray()));
        }