| | | 1 | | // -------------------------------------------------------------------------------- |
| | | 2 | | // Copyright (C) 2026 Eugene Eremeev (also known as Yevhenii Yeriemeieiv). |
| | | 3 | | // All Rights Reserved. |
| | | 4 | | // -------------------------------------------------------------------------------- |
| | | 5 | | // This software is the confidential and proprietary information of Eugene Eremeev |
| | | 6 | | // (also known as Yevhenii Yeriemeieiv) ("Confidential Information"). You shall not |
| | | 7 | | // disclose such Confidential Information and shall use it only in accordance with |
| | | 8 | | // the terms of the license agreement you entered into with Eugene Eremeev (also |
| | | 9 | | // known as Yevhenii Yeriemeieiv). |
| | | 10 | | // -------------------------------------------------------------------------------- |
| | | 11 | | |
| | | 12 | | namespace LeetCode.Algorithms.New21Game; |
| | | 13 | | |
| | | 14 | | /// <inheritdoc /> |
| | | 15 | | public sealed class New21GameDictionaryPriorityQueue : INew21Game |
| | | 16 | | { |
| | | 17 | | /// <summary> |
| | | 18 | | /// Time complexity - O(k * maxPts * log k) |
| | | 19 | | /// Space complexity - O(k) |
| | | 20 | | /// </summary> |
| | | 21 | | /// <param name="n"></param> |
| | | 22 | | /// <param name="k"></param> |
| | | 23 | | /// <param name="maxPts"></param> |
| | | 24 | | /// <returns></returns> |
| | | 25 | | public double New21Game(int n, int k, int maxPts) |
| | 3 | 26 | | { |
| | 3 | 27 | | if (k == 0 || n >= k - 1 + maxPts) |
| | 1 | 28 | | { |
| | 1 | 29 | | return 1; |
| | | 30 | | } |
| | | 31 | | |
| | 2 | 32 | | var result = 0.0; |
| | | 33 | | |
| | 2 | 34 | | var probabilityByScoreDictionary = new Dictionary<int, double>(); |
| | | 35 | | |
| | 44 | 36 | | for (var point = 1; point <= maxPts; point++) |
| | 20 | 37 | | { |
| | 20 | 38 | | probabilityByScoreDictionary.Add(point, 1.0 / maxPts); |
| | 20 | 39 | | } |
| | | 40 | | |
| | 2 | 41 | | var scoresPriorityQueue = new PriorityQueue<int, int>(); |
| | | 42 | | |
| | 46 | 43 | | foreach (var (score, probability) in probabilityByScoreDictionary) |
| | 20 | 44 | | { |
| | 20 | 45 | | if (score < k) |
| | 10 | 46 | | { |
| | 10 | 47 | | scoresPriorityQueue.Enqueue(score, score); |
| | 10 | 48 | | } |
| | 10 | 49 | | else if (score <= n) |
| | 6 | 50 | | { |
| | 6 | 51 | | result += probability; |
| | 6 | 52 | | } |
| | 20 | 53 | | } |
| | | 54 | | |
| | 117 | 55 | | while (scoresPriorityQueue.Count > 0) |
| | 115 | 56 | | { |
| | 115 | 57 | | var currentScore = scoresPriorityQueue.Dequeue(); |
| | | 58 | | |
| | 115 | 59 | | var currentProbability = probabilityByScoreDictionary[currentScore]; |
| | | 60 | | |
| | 115 | 61 | | if (currentProbability == 0.0) |
| | 99 | 62 | | { |
| | 99 | 63 | | continue; |
| | | 64 | | } |
| | | 65 | | |
| | 16 | 66 | | probabilityByScoreDictionary[currentScore] = 0.0; |
| | | 67 | | |
| | 16 | 68 | | var probabilityPerDraw = currentProbability / maxPts; |
| | | 69 | | |
| | 352 | 70 | | for (var drawValue = 1; drawValue <= maxPts; drawValue++) |
| | 160 | 71 | | { |
| | 160 | 72 | | var nextScore = currentScore + drawValue; |
| | | 73 | | |
| | 160 | 74 | | if (nextScore < k) |
| | 105 | 75 | | { |
| | 105 | 76 | | probabilityByScoreDictionary.TryGetValue(nextScore, out var oldProbability); |
| | 105 | 77 | | probabilityByScoreDictionary[nextScore] = oldProbability + probabilityPerDraw; |
| | | 78 | | |
| | 105 | 79 | | scoresPriorityQueue.Enqueue(nextScore, nextScore); |
| | 105 | 80 | | } |
| | 55 | 81 | | else if (nextScore <= n) |
| | 40 | 82 | | { |
| | 40 | 83 | | result += probabilityPerDraw; |
| | 40 | 84 | | } |
| | 160 | 85 | | } |
| | 16 | 86 | | } |
| | | 87 | | |
| | 2 | 88 | | return result; |
| | 3 | 89 | | } |
| | | 90 | | } |