Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 27c079f

Browse files
authored
Merge pull request #1 from aQuaYi/master
Get new Code
2 parents f0b077d + a971469 commit 27c079f

File tree

7 files changed

+239
-58
lines changed

7 files changed

+239
-58
lines changed

Algorithms/1155.number-of-dice-rolls-with-target-sum/number-of-dice-rolls-with-target-sum.go

+9-9
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,22 @@ package problem1155
22

33
const mod = 1e9 + 7
44

5-
func numRollsToTarget(d int, f int, target int) int {
5+
func numRollsToTarget(dices, faces, target int) int {
66
dp := [31][1001]int{}
77
dp[0][0] = 1
88

9-
for i := 1; i <= d; i++ {
10-
maxJ := min(target, i*f)
11-
for j := i; j <= maxJ; j++ {
12-
maxK := min(f, j)
13-
for k := 1; k <= maxK; k++ {
14-
dp[i][j] += dp[i-1][j-k]
9+
for d := 1; d <= dices; d++ {
10+
maxT := min(target, d*faces)
11+
for t := d; t <= maxT; t++ {
12+
maxF := min(faces, t)
13+
for f := 1; f <= maxF; f++ {
14+
dp[d][t] += dp[d-1][t-f]
1515
}
16-
dp[i][j] %= mod
16+
dp[d][t] %= mod
1717
}
1818
}
1919

20-
return dp[d][target]
20+
return dp[dices][target]
2121
}
2222

2323
func min(a, b int) int {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# [1156. Swap For Longest Repeated Character Substring](https://leetcode.com/problems/swap-for-longest-repeated-character-substring/)
2+
3+
Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters.
4+
5+
Example 1:
6+
7+
```text
8+
Input: text = "ababa"
9+
Output: 3
10+
Explanation: We can swap the first 'b' with the last 'a', or the last 'b' with the first 'a'. Then, the longest repeated character substring is "aaa", which its length is 3.
11+
```
12+
13+
Example 2:
14+
15+
```text
16+
Input: text = "aaabaaa"
17+
Output: 6
18+
Explanation: Swap 'b' with the last 'a' (or the first 'a'), and we get longest repeated character substring "aaaaaa", which its length is 6.
19+
```
20+
21+
Example 3:
22+
23+
```text
24+
Input: text = "aaabbaaa"
25+
Output: 4
26+
```
27+
28+
Example 4:
29+
30+
```text
31+
Input: text = "aaaaa"
32+
Output: 5
33+
Explanation: No need to swap, longest repeated character substring is "aaaaa", length is 5.
34+
```
35+
36+
Example 5:
37+
38+
```text
39+
Input: text = "abcdef"
40+
Output: 1
41+
```
42+
43+
Constraints:
44+
45+
- `1 <= text.length <= 20000`
46+
- `text consist of lowercase English characters only.`
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package problem1156
2+
3+
import "strings"
4+
5+
func maxRepOpt1(text string) int {
6+
rec := [26][][3]int{}
7+
segments, index := split(text), 0
8+
for _, s := range segments {
9+
b, n := int(s[0]-'a'), len(s)
10+
rec[b] = append(rec[b], [3]int{index, index + n + 1, n})
11+
index += n
12+
}
13+
14+
res := 0
15+
for _, r := range rec {
16+
n := len(r)
17+
if n == 0 {
18+
continue
19+
}
20+
// extension a lonely segment
21+
ext := 0
22+
if n > 1 {
23+
ext = 1
24+
}
25+
// connect two neighbor segments
26+
con := 0
27+
if n > 2 {
28+
con = 1
29+
}
30+
prev := r[0]
31+
res = max(res, ext+prev[2])
32+
for i := 1; i < len(r); i++ {
33+
cur := r[i]
34+
if prev[1] == cur[0] {
35+
res = max(res, con+prev[2]+cur[2])
36+
} else {
37+
res = max(res, ext+cur[2])
38+
}
39+
prev = cur
40+
}
41+
}
42+
43+
return res
44+
}
45+
46+
func split(s string) []string {
47+
var sb strings.Builder
48+
p := s[0]
49+
sb.WriteByte(p)
50+
for i := 1; i < len(s); i++ {
51+
n := s[i]
52+
if p != n {
53+
sb.WriteByte('\n')
54+
}
55+
sb.WriteByte(n)
56+
p = n
57+
}
58+
return strings.Split(sb.String(), "\n")
59+
}
60+
61+
func max(a, b int) int {
62+
if a > b {
63+
return a
64+
}
65+
return b
66+
}
67+
68+
func min(a, b int) int {
69+
if a < b {
70+
return a
71+
}
72+
return b
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package problem1156
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
// tcs is testcase slice
10+
var tcs = []struct {
11+
text string
12+
ans int
13+
}{
14+
15+
{
16+
"ababa",
17+
3,
18+
},
19+
20+
{
21+
"aaabaaa",
22+
6,
23+
},
24+
25+
{
26+
"aaabbbbbaaa",
27+
5,
28+
},
29+
30+
{
31+
"aaabbaaa",
32+
4,
33+
},
34+
35+
{
36+
"aaaaa",
37+
5,
38+
},
39+
40+
{
41+
"abcdef",
42+
1,
43+
},
44+
45+
// 可以有多个 testcase
46+
}
47+
48+
func Test_maxRepOpt1(t *testing.T) {
49+
ast := assert.New(t)
50+
51+
for _, tc := range tcs {
52+
ast.Equal(tc.ans, maxRepOpt1(tc.text), "输入:%v", tc)
53+
}
54+
}
55+
56+
func Benchmark_maxRepOpt1(b *testing.B) {
57+
for i := 0; i < b.N; i++ {
58+
for _, tc := range tcs {
59+
maxRepOpt1(tc.text)
60+
}
61+
}
62+
}

Favorite.md

+7-7
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@
6666
|[0279](https://leetcode.com/problems/perfect-squares/)|[Perfect Squares](./Algorithms/0279.perfect-squares)|42%|Medium|[](https://leetcode.com/list/oussv5j)|
6767
|[0282](https://leetcode.com/problems/expression-add-operators/)|[Expression Add Operators](./Algorithms/0282.expression-add-operators)|33%|Hard|[](https://leetcode.com/list/oussv5j)|
6868
|[0287](https://leetcode.com/problems/find-the-duplicate-number/)|[Find the Duplicate Number](./Algorithms/0287.find-the-duplicate-number)|50%|Medium|[](https://leetcode.com/list/oussv5j)|
69-
|[0289](https://leetcode.com/problems/game-of-life/)|[Game of Life](./Algorithms/0289.game-of-life)|46%|Medium|[](https://leetcode.com/list/oussv5j)|
69+
|[0289](https://leetcode.com/problems/game-of-life/)|[Game of Life](./Algorithms/0289.game-of-life)|47%|Medium|[](https://leetcode.com/list/oussv5j)|
7070
|[0292](https://leetcode.com/problems/nim-game/)|[Nim Game](./Algorithms/0292.nim-game)|55%|Easy|[](https://leetcode.com/list/oussv5j)|
7171
|[0295](https://leetcode.com/problems/find-median-from-data-stream/)|[Find Median from Data Stream](./Algorithms/0295.find-median-from-data-stream)|37%|Hard|[](https://leetcode.com/list/oussv5j)|
7272
|[0300](https://leetcode.com/problems/longest-increasing-subsequence/)|[Longest Increasing Subsequence](./Algorithms/0300.longest-increasing-subsequence)|41%|Medium|[](https://leetcode.com/list/oussv5j)|
@@ -202,7 +202,7 @@
202202
|[0736](https://leetcode.com/problems/parse-lisp-expression/)|[Parse Lisp Expression](./Algorithms/0736.parse-lisp-expression)|44%|Hard|[](https://leetcode.com/list/oussv5j)|
203203
|[0739](https://leetcode.com/problems/daily-temperatures/)|[Daily Temperatures](./Algorithms/0739.daily-temperatures)|60%|Medium|[](https://leetcode.com/list/oussv5j)|
204204
|[0740](https://leetcode.com/problems/delete-and-earn/)|[Delete and Earn](./Algorithms/0740.delete-and-earn)|46%|Medium|[](https://leetcode.com/list/oussv5j)|
205-
|[0752](https://leetcode.com/problems/open-the-lock/)|[Open the Lock](./Algorithms/0752.open-the-lock)|46%|Medium|[](https://leetcode.com/list/oussv5j)|
205+
|[0752](https://leetcode.com/problems/open-the-lock/)|[Open the Lock](./Algorithms/0752.open-the-lock)|47%|Medium|[](https://leetcode.com/list/oussv5j)|
206206
|[0753](https://leetcode.com/problems/cracking-the-safe/)|[Cracking the Safe](./Algorithms/0753.cracking-the-safe)|47%|Hard|[](https://leetcode.com/list/oussv5j)|
207207
|[0754](https://leetcode.com/problems/reach-a-number/)|[Reach a Number](./Algorithms/0754.reach-a-number)|33%|Easy|[](https://leetcode.com/list/oussv5j)|
208208
|[0756](https://leetcode.com/problems/pyramid-transition-matrix/)|[Pyramid Transition Matrix](./Algorithms/0756.pyramid-transition-matrix)|52%|Medium|[](https://leetcode.com/list/oussv5j)|
@@ -240,9 +240,9 @@
240240
|[0849](https://leetcode.com/problems/maximize-distance-to-closest-person/)|[Maximize Distance to Closest Person](./Algorithms/0849.maximize-distance-to-closest-person)|41%|Easy|[](https://leetcode.com/list/oussv5j)|
241241
|[0850](https://leetcode.com/problems/rectangle-area-ii/)|[Rectangle Area II](./Algorithms/0850.rectangle-area-ii)|45%|Hard|[](https://leetcode.com/list/oussv5j)|
242242
|[0851](https://leetcode.com/problems/loud-and-rich/)|[Loud and Rich](./Algorithms/0851.loud-and-rich)|48%|Medium|[](https://leetcode.com/list/oussv5j)|
243-
|[0852](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Peak Index in a Mountain Array](./Algorithms/0852.peak-index-in-a-mountain-array)|70%|Easy|[](https://leetcode.com/list/oussv5j)|
243+
|[0852](https://leetcode.com/problems/peak-index-in-a-mountain-array/)|[Peak Index in a Mountain Array](./Algorithms/0852.peak-index-in-a-mountain-array)|69%|Easy|[](https://leetcode.com/list/oussv5j)|
244244
|[0854](https://leetcode.com/problems/k-similar-strings/)|[K-Similar Strings](./Algorithms/0854.k-similar-strings)|34%|Hard|[](https://leetcode.com/list/oussv5j)|
245-
|[0857](https://leetcode.com/problems/minimum-cost-to-hire-k-workers/)|[Minimum Cost to Hire K Workers](./Algorithms/0857.minimum-cost-to-hire-k-workers)|47%|Hard|[](https://leetcode.com/list/oussv5j)|
245+
|[0857](https://leetcode.com/problems/minimum-cost-to-hire-k-workers/)|[Minimum Cost to Hire K Workers](./Algorithms/0857.minimum-cost-to-hire-k-workers)|48%|Hard|[](https://leetcode.com/list/oussv5j)|
246246
|[0862](https://leetcode.com/problems/shortest-subarray-with-sum-at-least-k/)|[Shortest Subarray with Sum at Least K](./Algorithms/0862.shortest-subarray-with-sum-at-least-k)|22%|Hard|[](https://leetcode.com/list/oussv5j)|
247247
|[0863](https://leetcode.com/problems/all-nodes-distance-k-in-binary-tree/)|[All Nodes Distance K in Binary Tree](./Algorithms/0863.all-nodes-distance-k-in-binary-tree)|49%|Medium|[](https://leetcode.com/list/oussv5j)|
248248
|[0864](https://leetcode.com/problems/shortest-path-to-get-all-keys/)|[Shortest Path to Get All Keys](./Algorithms/0864.shortest-path-to-get-all-keys)|36%|Hard|[](https://leetcode.com/list/oussv5j)|
@@ -276,7 +276,7 @@
276276
|[0948](https://leetcode.com/problems/bag-of-tokens/)|[Bag of Tokens](./Algorithms/0948.bag-of-tokens)|39%|Medium|[](https://leetcode.com/list/oussv5j)|
277277
|[0949](https://leetcode.com/problems/largest-time-for-given-digits/)|[Largest Time for Given Digits](./Algorithms/0949.largest-time-for-given-digits)|34%|Easy|[](https://leetcode.com/list/oussv5j)|
278278
|[0952](https://leetcode.com/problems/largest-component-size-by-common-factor/)|[Largest Component Size by Common Factor](./Algorithms/0952.largest-component-size-by-common-factor)|26%|Hard|[](https://leetcode.com/list/oussv5j)|
279-
|[0955](https://leetcode.com/problems/delete-columns-to-make-sorted-ii/)|[Delete Columns to Make Sorted II](./Algorithms/0955.delete-columns-to-make-sorted-ii)|31%|Medium|[](https://leetcode.com/list/oussv5j)|
279+
|[0955](https://leetcode.com/problems/delete-columns-to-make-sorted-ii/)|[Delete Columns to Make Sorted II](./Algorithms/0955.delete-columns-to-make-sorted-ii)|32%|Medium|[](https://leetcode.com/list/oussv5j)|
280280
|[0956](https://leetcode.com/problems/tallest-billboard/)|[Tallest Billboard](./Algorithms/0956.tallest-billboard)|38%|Hard|[](https://leetcode.com/list/oussv5j)|
281281
|[0960](https://leetcode.com/problems/delete-columns-to-make-sorted-iii/)|[Delete Columns to Make Sorted III](./Algorithms/0960.delete-columns-to-make-sorted-iii)|53%|Hard|[](https://leetcode.com/list/oussv5j)|
282282
|[0962](https://leetcode.com/problems/maximum-width-ramp/)|[Maximum Width Ramp](./Algorithms/0962.maximum-width-ramp)|42%|Medium|[](https://leetcode.com/list/oussv5j)|
@@ -295,7 +295,7 @@
295295
|[0991](https://leetcode.com/problems/broken-calculator/)|[Broken Calculator](./Algorithms/0991.broken-calculator)|41%|Medium|[](https://leetcode.com/list/oussv5j)|
296296
|[0992](https://leetcode.com/problems/subarrays-with-k-different-integers/)|[Subarrays with K Different Integers](./Algorithms/0992.subarrays-with-k-different-integers)|44%|Hard|[](https://leetcode.com/list/oussv5j)|
297297
|[0996](https://leetcode.com/problems/number-of-squareful-arrays/)|[Number of Squareful Arrays](./Algorithms/0996.number-of-squareful-arrays)|47%|Hard|[](https://leetcode.com/list/oussv5j)|
298-
|[1000](https://leetcode.com/problems/minimum-cost-to-merge-stones/)|[Minimum Cost to Merge Stones](./Algorithms/1000.minimum-cost-to-merge-stones)|33%|Hard|[](https://leetcode.com/list/oussv5j)|
298+
|[1000](https://leetcode.com/problems/minimum-cost-to-merge-stones/)|[Minimum Cost to Merge Stones](./Algorithms/1000.minimum-cost-to-merge-stones)|34%|Hard|[](https://leetcode.com/list/oussv5j)|
299299
|[1001](https://leetcode.com/problems/grid-illumination/)|[Grid Illumination](./Algorithms/1001.grid-illumination)|34%|Hard|[](https://leetcode.com/list/oussv5j)|
300300
|[1004](https://leetcode.com/problems/max-consecutive-ones-iii/)|[Max Consecutive Ones III](./Algorithms/1004.max-consecutive-ones-iii)|54%|Medium|[](https://leetcode.com/list/oussv5j)|
301301
|[1009](https://leetcode.com/problems/complement-of-base-10-integer/)|[Complement of Base 10 Integer](./Algorithms/1009.complement-of-base-10-integer)|59%|Easy|[](https://leetcode.com/list/oussv5j)|
@@ -327,7 +327,7 @@
327327
|[1074](https://leetcode.com/problems/number-of-submatrices-that-sum-to-target/)|[Number of Submatrices That Sum to Target](./Algorithms/1074.number-of-submatrices-that-sum-to-target)|57%|Hard|[](https://leetcode.com/list/oussv5j)|
328328
|[1079](https://leetcode.com/problems/letter-tile-possibilities/)|[Letter Tile Possibilities](./Algorithms/1079.letter-tile-possibilities)|75%|Medium|[](https://leetcode.com/list/oussv5j)|
329329
|[1080](https://leetcode.com/problems/insufficient-nodes-in-root-to-leaf-paths/)|[Insufficient Nodes in Root to Leaf Paths](./Algorithms/1080.insufficient-nodes-in-root-to-leaf-paths)|42%|Medium|[](https://leetcode.com/list/oussv5j)|
330-
|[1089](https://leetcode.com/problems/duplicate-zeros/)|[Duplicate Zeros](./Algorithms/1089.duplicate-zeros)|58%|Easy|[](https://leetcode.com/list/oussv5j)|
330+
|[1089](https://leetcode.com/problems/duplicate-zeros/)|[Duplicate Zeros](./Algorithms/1089.duplicate-zeros)|59%|Easy|[](https://leetcode.com/list/oussv5j)|
331331
|[1092](https://leetcode.com/problems/shortest-common-supersequence/)|[Shortest Common Supersequence](./Algorithms/1092.shortest-common-supersequence)|48%|Hard|[](https://leetcode.com/list/oussv5j)|
332332
|[1105](https://leetcode.com/problems/filling-bookcase-shelves/)|[Filling Bookcase Shelves](./Algorithms/1105.filling-bookcase-shelves)|54%|Medium|[](https://leetcode.com/list/oussv5j)|
333333
|[1125](https://leetcode.com/problems/smallest-sufficient-team/)|[Smallest Sufficient Team](./Algorithms/1125.smallest-sufficient-team)|44%|Hard|[](https://leetcode.com/list/oussv5j)|

0 commit comments

Comments
 (0)