Skip to content

Commit 6d23159

Browse files
committed
feat: add No.1155,525
1 parent 9df1761 commit 6d23159

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
# 1155. Number of Dice Rolls With Target Sum
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Dynamic Programming.
5+
- Similar Questions: Equal Sum Arrays With Minimum Number of Operations, Find Missing Observations.
6+
7+
## Problem
8+
9+
You have `n` dice, and each die has `k` faces numbered from `1` to `k`.
10+
11+
Given three integers `n`, `k`, and `target`, return **the number of possible ways (out of the **`kn`** total ways) ****to roll the dice, so the sum of the face-up numbers equals **`target`. Since the answer may be too large, return it **modulo** `109 + 7`.
12+
13+
 
14+
Example 1:
15+
16+
```
17+
Input: n = 1, k = 6, target = 3
18+
Output: 1
19+
Explanation: You throw one die with 6 faces.
20+
There is only one way to get a sum of 3.
21+
```
22+
23+
Example 2:
24+
25+
```
26+
Input: n = 2, k = 6, target = 7
27+
Output: 6
28+
Explanation: You throw two dice, each with 6 faces.
29+
There are 6 ways to get a sum of 7: 1+6, 2+5, 3+4, 4+3, 5+2, 6+1.
30+
```
31+
32+
Example 3:
33+
34+
```
35+
Input: n = 30, k = 30, target = 500
36+
Output: 222616187
37+
Explanation: The answer must be returned modulo 109 + 7.
38+
```
39+
40+
 
41+
**Constraints:**
42+
43+
44+
45+
- `1 <= n, k <= 30`
46+
47+
- `1 <= target <= 1000`
48+
49+
50+
51+
## Solution
52+
53+
```javascript
54+
/**
55+
* @param {number} n
56+
* @param {number} k
57+
* @param {number} target
58+
* @return {number}
59+
*/
60+
var numRollsToTarget = function(n, k, target) {
61+
var dp = Array(n + 1).fill(0).map(() => ({}));
62+
return helper(n, k, target, dp);
63+
};
64+
65+
var helper = function(n, k, target, dp) {
66+
if (dp[n][target] !== undefined) return dp[n][target];
67+
if (n === 0 && target === 0) return 1;
68+
if (n <= 0 || target <= 0) return 0;
69+
var res = 0;
70+
var mod = Math.pow(10, 9) + 7;
71+
for (var i = 1; i <= k; i++) {
72+
if (target < i) break;
73+
res += helper(n - 1, k, target - i, dp);
74+
res %= mod;
75+
}
76+
dp[n][target] = res;
77+
return res;
78+
};
79+
```
80+
81+
**Explain:**
82+
83+
nope.
84+
85+
**Complexity:**
86+
87+
* Time complexity : O(n * target).
88+
* Space complexity : O(n * target).

501-600/525. Contiguous Array.md

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# 525. Contiguous Array
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Array, Hash Table, Prefix Sum.
5+
- Similar Questions: Maximum Size Subarray Sum Equals k.
6+
7+
## Problem
8+
9+
Given a binary array `nums`, return **the maximum length of a contiguous subarray with an equal number of **`0`** and **`1`.
10+
11+
 
12+
Example 1:
13+
14+
```
15+
Input: nums = [0,1]
16+
Output: 2
17+
Explanation: [0, 1] is the longest contiguous subarray with an equal number of 0 and 1.
18+
```
19+
20+
Example 2:
21+
22+
```
23+
Input: nums = [0,1,0]
24+
Output: 2
25+
Explanation: [0, 1] (or [1, 0]) is a longest contiguous subarray with equal number of 0 and 1.
26+
```
27+
28+
 
29+
**Constraints:**
30+
31+
32+
33+
- `1 <= nums.length <= 105`
34+
35+
- `nums[i]` is either `0` or `1`.
36+
37+
38+
39+
## Solution
40+
41+
```javascript
42+
/**
43+
* @param {number[]} nums
44+
* @return {number}
45+
*/
46+
var findMaxLength = function(nums) {
47+
var map = {};
48+
var count = 0;
49+
var max = 0;
50+
map[0] = -1;
51+
for (var i = 0; i < nums.length; i++) {
52+
count += (nums[i] === 1 ? 1 : -1);
53+
if (map[count] !== undefined) {
54+
max = Math.max(max, i - map[count])
55+
} else {
56+
map[count] = i;
57+
}
58+
}
59+
return max;
60+
};
61+
```
62+
63+
**Explain:**
64+
65+
nope.
66+
67+
**Complexity:**
68+
69+
* Time complexity : O(n).
70+
* Space complexity : O(n).

0 commit comments

Comments
 (0)