最优算法

时间:2012-04-07 19:38:52

标签: algorithm

我得到一个输入,“N”,我必须找到长度为N的列表的编号,从1开始,这样下一个要添加的数字最多比现在添加的最大数量多1个。例如,

N = 3,可能的列表=> (111,112,121,122,123),[113或131是不可能的,因为在列表中添加'3'时,列表中存在的最大数字将为'1',因此我们只能添加1或2]。

N = 4,列表1213是可能的,因为加上3,列表中的最大数字是'2',因此可以添加3。

问题是计算给定输入“N”可能的列表数量。

我的代码是: -

public static void Main(string[] args)
        {
            var noOfTestCases = Convert.ToInt32(Console.ReadLine());
            var listOfOutput = new List<long>();
            for (int i = 0; i < noOfTestCases; i++)
            {
                var requiredSize = Convert.ToInt64(Console.ReadLine());
                long result;
                const long listCount = 1;
                const long listMaxTillNow = 1;
                if (requiredSize < 3)
                    result = requiredSize;
                else
                {
                    SeqCount.Add(requiredSize, 0);
                    AddElementToList(requiredSize, listCount, listMaxTillNow);
                    result = SeqCount[requiredSize];
                }
                listOfOutput.Add(result);
            }
            foreach (var i in listOfOutput)
            {
                Console.WriteLine(i);
            }
        }

        private static Dictionary<long, long> SeqCount = new Dictionary<long, long>();

        private static void AddElementToList(long requiredSize, long listCount, long listMaxTillNow)
        {
            if (listCount == requiredSize)
            {
                SeqCount[requiredSize] = SeqCount[requiredSize] + 1;
                return;
            }
            var listMaxTillNowNew = listMaxTillNow + 1;
            for(var i = listMaxTillNowNew; i > 0; i--)
            {
                AddElementToList(requiredSize, listCount + 1,
                    i == listMaxTillNowNew ? listMaxTillNowNew : listMaxTillNow);
            }
            return;
        }

这是蛮力方法。我想知道什么是最好的问题算法? PS:我只想知道这些列表的数量,所以我确信不需要创建所有列表。 (我在代码中的方式) 我的算法并不好,所以请原谅长期的问题。

2 个答案:

答案 0 :(得分:3)

此问题是动态编程问题的典型示例:

如果将函数dp(k,m)定义为最大数为m的长度为k的列表数,则表示您具有递归关系:

dp(1, 1) = 1
dp(1, m) = 0, for m > 1
dp(k, m) = dp(k-1, m) * m + dp(k-1, m-1)

实际上,只有一个长度为1的列表,其最大元素为1。 当您使用max element m构建长度为k的列表时,可以使用max = m的任何(k-1)列表并附加1或2或....或m。或者您可以使用最大元素m-1的(k-1)-list并附加m。如果你得到一个(k-1)-list,其中max元素小于m-1那么根据你的规则,你只能通过附加一个元素来获得最大m。

您可以使用O(N^2)中的动态编程计算所有k = 1,...,N和m = 1,...,N + 1的dp(k,m),然后回答您的问题问题是

dp(N,1) + dp(N,2) + ... + dp(N,N+1)

因此算法为O(N^2)


请参阅下文,了解在C#中执行dp计算:

        int[] arr = new int[N + 2];
        for (int m = 1; m < N + 2; m++)
            arr[m] = 0;
        arr[1] = 1;

        int[] newArr = new int[N + 2];
        int[] tmp;
        for (int k = 1; k < N; k++)
        {
            for (int m = 1; m < N + 2; m++)
                newArr[m] = arr[m] * m + arr[m - 1];
            tmp = arr;
            arr = newArr;
            newArr = tmp;
        }

        int answer = 0;strong text
        for (int m = 1; m < N + 2; m++)
            answer += arr[m];

        Console.WriteLine("The answer for " + N + " is " + answer);

答案 1 :(得分:0)

好吧,今天下午我被一场大火打断了(真的!)但是FWIW,这是我的贡献:

    /*
     * Counts the number of possible integer list on langth N, with the
     * property that no integer in a list(starting with one) may be more
     * than one greater than the greatest integer preceeding it in the list.
     * 
     * I am calling this "Semi-Factorial" since it  is somewhat similar to
     * the factorial function and its constituent integer combinations.
     */
    public int SemiFactorial(int N)
    {
        int sumCounts = 0;

        // get a list of the counts of all valid lists of length N,
        //whose maximum integer is listCounts[maxInt].
        List<int> listCounts = SemiFactorialCounts(N);

        for (int maxInt = 1; maxInt <= N; maxInt++)
        {
            // Get the number of lists, of length N-1 whose maximum integer
            //is (maxInt):
            int maxIntCnt = listCounts[maxInt];

            // just sum them up
            sumCounts += maxIntCnt;
        }

        return sumCounts;
    }

    // Returns a list of the counts of all valid lists of length N, and
    //whose maximum integer is [i], where [i] is also its index in this
    //returned list. (0 is not used).
    public List<int> SemiFactorialCounts(int N)
    {
        List<int> cnts;
        if (N == 0)
        {
            // no valid lists, 
            cnts = new List<int>();
            // (zero isn't used)
            cnts.Add(0);
        }
        else if (N == 1)
            {
                // the only valid list is {1}, 
                cnts = new List<int>();
                // (zero isn't used)
                cnts.Add(0);
                //so that's one list of length 1
                cnts.Add(1);
            }
            else
            {
            // start with the maxInt counts of lists whose length is N-1:
            cnts = SemiFactorialCounts(N - 1);

            // add an entry for (N)
            cnts.Add(0);

            // (reverse order because we overwrite the list using values
            // from the next lower index.)
            for (int K = N; K > 0; K--) 
            {
                // The number of lists of length N and maxInt K { SF(N,K) }
                // Equals K times # of lists one shorter, but same maxInt,
                // Plus, the number of lists one shorter with maxInt-1.
                cnts[K] = K * cnts[K] + cnts[K - 1];
            }
        }

        return cnts;
    }

与其他人非常相似。虽然我不会将这种“经典动态编程”称为“经典递归”。