diff options
author | Jack Sangdahl <[email protected]> | 2025-08-17 08:23:37 -0600 |
---|---|---|
committer | Jack Sangdahl <[email protected]> | 2025-08-17 08:23:37 -0600 |
commit | 5d449e06b0ccf99ad3d10e2ed2c37e6e902bab1c (patch) | |
tree | 9b7652508cd2aef2ffd2fe16afaf2acdc41eeedd | |
parent | f359b85a14d62e61cd108f8c1f5818f08cde5a20 (diff) | |
download | leetcode-5d449e06b0ccf99ad3d10e2ed2c37e6e902bab1c.tar.gz leetcode-5d449e06b0ccf99ad3d10e2ed2c37e6e902bab1c.zip |
837. new 21 game
-rw-r--r-- | new_21_game.c | 19 | ||||
-rw-r--r-- | new_21_game.py | 39 |
2 files changed, 48 insertions, 10 deletions
diff --git a/new_21_game.c b/new_21_game.c index 4076f33..a9a0f5a 100644 --- a/new_21_game.c +++ b/new_21_game.c @@ -1,37 +1,36 @@ // 837. New 21 Game -#include <stdio.h> -#include <stdlib.h> +#include "leetcode.h" /* * alice plays the following game, loosely based on the card game "21". alice * starts with 0 points and draws numbers while she has less than 'k' points. * during each draw, she gains an integer number of points randomly from range - * '[1, max_pts]', where 'max_pts' is an integer. each draw is independent and + * '[1, maxPts]', where 'maxPts' is an integer. each draw is independent and * the outcomes have equal posibilities. alice stops drawing numbers when she * gets 'k' or more points. return the probability that alice has 'n' or fewer * points. answers within 10^-5 of the actual answer are considered accepted */ -double new21Game(int n, int k, int max_pts) { - if (!k || n >= k + max_pts) +double new21Game(int n, int k, int maxPts) { + if (!k || n >= k + maxPts) return 1.0; - double *dp = malloc((n + 1) * sizeof(double)); + double *dp = (double *)malloc((n + 1) * sizeof(double)); dp[0] = 1.0; double sum = 1.0, ans = 0.0; for (int i = 1; i <= n; ++i) { - dp[i] = sum / max_pts; + dp[i] = sum / maxPts; if (i < k) sum += dp[i]; else ans += dp[i]; - if (i - max_pts >= 0) - sum -= dp[i - max_pts]; + if (i - maxPts >= 0) + sum -= dp[i - maxPts]; } + free(dp); return ans; } int main() { - // new21Game(21,17,10); printf("%f\n", new21Game(10, 1, 10)); // expect: 1.00000 printf("%f\n", new21Game(6, 1, 10)); // expect: 0.60000 printf("%f\n", new21Game(21, 17, 10)); // expect: 0.73278 diff --git a/new_21_game.py b/new_21_game.py new file mode 100644 index 0000000..cc634d0 --- /dev/null +++ b/new_21_game.py @@ -0,0 +1,39 @@ +# 837. New 21 Game + +""" +alice plays the following game, loosely based on the card game "21". alice +starts with 0 points and draws numbers while she has less than 'k' points. +during each draw, she gains an integer number of points randomly from range +'[1, maxPts]', where 'maxPts' is an integer. each draw is independent and +the outcomes have equal posibilities. alice stops drawing numbers when she +gets 'k' or more points. return the probability that alice has 'n' or fewer +points. answers within 10^-5 of the actual answer are considered accepted +""" + + +class Solution(object): + def new21Game(self, n, k, maxPts): + """ + :type n: int + :type k: int + :type maxPts: int + :rtype: float + """ + if k == 0 or n >= k + maxPts: + return 1 + dp = [1.0] + [0.0] * n + sum = 1.0 + for i in range(1, n + 1): + dp[i] = sum / maxPts + if i < k: + sum += dp[i] + if i - maxPts >= 0: + sum -= dp[i - maxPts] + return sum(dp[k:]) + + +if __name__ == "__main__": + obj = Solution() + print(obj.new21Game(n=10, k=1, maxPts=10)) + print(obj.new21Game(n=6, k=1, maxPts=10)) + print(obj.new21Game(n=21, k=17, maxPts=10)) |