Advertisement
Guest User

Untitled

a guest
Sep 10th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.55 KB | None | 0 0
  1. It is not necessary to pass the whole "N", "O" array - we only need to know how many of each remains (I've substituted the array param with `nRemainingCount` and `oRemainingCount`).
  2.  
  3. The bruteforce solution would be to generate all permutations, then filter out entries which violate the "never more "O"s than "N"s preceding it" rule afterwards. I believe this would be O(n^2) - n being the length of the input array.
  4.  
  5.    private static IEnumerable<string> Permutations(string aggregate,
  6.              int nAggregateCount, int oAggregateCount, int nRemainingCount, int oRemainingCount)
  7.    {
  8.        var nAppended = Enumerable.Empty<string>();
  9.        var oAppended = Enumerable.Empty<string>();
  10.  
  11.        if (nRemainingCount != 0)
  12.        {
  13.            nAppended = Permutations(aggregate + "N", nAggregateCount + 1, oAggregateCount, nRemainingCount - 1, oRemainingCount);
  14.        }
  15.  
  16.        if (oRemainingCount != 0 && nAggregateCount != oAggregateCount)
  17.        {
  18.            oAppended = Permutations(aggregate + "O", nAggregateCount, oAggregateCount + 1, nRemainingCount, oRemainingCount - 1);
  19.        }
  20.  
  21.        return nAppended.Concat(oAppended);
  22.    }
  23.  
  24. We could improve that by adding the filter early at the start of the function, resulting in:
  25.  
  26.    private static IEnumerable<string> Permutations(string aggregate,
  27.              int nAggregateCount, int oAggregateCount, int nRemainingCount, int oRemainingCount)
  28.    {
  29.        var areBothEmpty = nRemainingCount == 0 && oRemainingCount == 0;
  30.        var isBlockedByNrule = oRemainingCount == 0 && nAggregateCount == oAggregateCount;
  31.        if (areBothEmpty || isBlockedByNrule) return new[] { aggregate };
  32.  
  33.        var nAppended = Enumerable.Empty<string>();
  34.        var oAppended = Enumerable.Empty<string>();
  35.  
  36.        if (nRemainingCount != 0)
  37.        {
  38.            nAppended = Permutations(aggregate + "N", nAggregateCount + 1, oAggregateCount, nRemainingCount - 1, oRemainingCount);
  39.        }
  40.  
  41.        if (oRemainingCount != 0 && nAggregateCount != oAggregateCount)
  42.        {
  43.            oAppended = Permutations(aggregate + "O", nAggregateCount, oAggregateCount + 1, nRemainingCount, oRemainingCount - 1);
  44.        }
  45.  
  46.        return nAppended.Concat(oAppended);
  47.    }
  48.  
  49. I don't dare to guess of which algorithmic complexity this is (I think less than O(n^2) though).
  50.  
  51. We could further improve by introducing a lookup table - as there are repeating sequences of { nAggregateCount == oAggregateCount, nRemainingCount, oRemainingCount } occuring in the "search tree" - with the same tail generated.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement