Re: [闲聊] 每日LeetCode

楼主: oin1104 (是oin的说)   2023-12-27 01:00:48
※ 引述 《Rushia (みけねこ的鼻屎)》 之铭言:
:  
: https://leetcode.com/problems/number-of-dice-rolls-with-target-sum/description
: 1155. Number of Dice Rolls With Target Sum
: 给你三个数字 n、k、target,n 表示丢几次骰子,k 表示骰子有几面(1~k点),target
: 表示目标点数,求出丢 n 次骰子后共有几种可能点数和为 target,因为数字很大所以
: 要取模运算。
:  
: 思路:
: 1.如果骰子全骰到最大的和小于target,不用算可以直接返回 0。
: 2.定义 f(n, m) 表示使用 n 颗骰子和为 m 的方法数,很明显的 f(1, 1:k) 都是 1
: ,而当我们投一颗新骰子时每个点数会产生 k 个新的和,举例来说当骰子数为2时
: 可能是 1+1, 1+2, 1+3, ..., 1+k,所以我们遍历所有之前不为0(可以骰出来的数字)
: 的点数加上当前的点数做 n 轮之后检查 f(n, target) 即可。
我的想法是直接像画图一样
弄一个阵列出来
假设有k=6 n=3
然后target 是9
我的阵列会长这样
目前总和 0 1 2 3 4 5 6 7 8 9
零个骰子 1 0 0 0 0 0 0 0 0 0
一个骰子 0 1 1 1 1 1 1 0 0 0
两个骰子 0 0 1 2 3 4 5 6 5 4
三个骰子
就是算当前的骰子是第几个
假设是3
他要到哪里不重要 假设是6
有什么数字
假设现在看的是1这面
那他到6的方法数就 加上左上格子的算法
假设看的是2这面
那他到6的方法数就 加上左左上格的算法
假设3这面
加左左左上的算法
把所有骰子跟他们面数弄一次
就可以了捏
主要是这样

今天这题好有趣喔
/**
* Return an array of arrays of size *returnSize.
* The sizes of the arrays are returned as *returnColumnSizes array.
* Note: Both returned array and *columnSizes array must be malloced, assume cal
ler calls free().
*/
int** construct2DArray(int* original, int originalSize, int m, int n, int* retur
nSize, int** returnColumnSizes)
{
int max = 0;
max = m*n;
int* no = malloc(sizeof(int) * 0);
if(originalSize != max)
{
*returnSize = 0;
*returnColumnSizes = malloc(sizeof(int) * 0);
return no;
}
*returnSize = m;
*returnColumnSizes = malloc(sizeof(int) * m);
for(int i = 0 ; i < m ; i ++)
{
(*returnColumnSizes)[i] = n;
}
int** map = malloc(sizeof(int*) * m);
for(int i = 0 ; i < m ; i ++)
{
map[i] = malloc(sizeof(int) * n);
}
int c = 0;
printf("hi");
for(int i = 0 ; i < m; i ++)
{
for(int j = 0 ; j < n ; j ++)
{
map[i][j] = original[c] ;
c ++;
}
}
return map;
}
作者: pandix (面包屌)   2023-12-27 01:12:00
大师

Links booklink

Contact Us: admin [ a t ] ucptt.com