mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-07 01:44:56 +08:00
Add solution 1573、1684、1685、1688、1689
This commit is contained in:
@ -0,0 +1,38 @@
|
||||
package leetcode
|
||||
|
||||
func numWays(s string) int {
|
||||
ones := 0
|
||||
for _, c := range s {
|
||||
if c == '1' {
|
||||
ones++
|
||||
}
|
||||
}
|
||||
if ones%3 != 0 {
|
||||
return 0
|
||||
}
|
||||
if ones == 0 {
|
||||
return (len(s) - 1) * (len(s) - 2) / 2 % 1000000007
|
||||
}
|
||||
N, a, b, c, d, count := ones/3, 0, 0, 0, 0, 0
|
||||
for i, letter := range s {
|
||||
if letter == '0' {
|
||||
continue
|
||||
}
|
||||
if letter == '1' {
|
||||
count++
|
||||
}
|
||||
if count == N {
|
||||
a = i
|
||||
}
|
||||
if count == N+1 {
|
||||
b = i
|
||||
}
|
||||
if count == 2*N {
|
||||
c = i
|
||||
}
|
||||
if count == 2*N+1 {
|
||||
d = i
|
||||
}
|
||||
}
|
||||
return (b - a) * (d - c) % 1000000007
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1573 struct {
|
||||
para1573
|
||||
ans1573
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1573 struct {
|
||||
s string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1573 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1573(t *testing.T) {
|
||||
|
||||
qs := []question1573{
|
||||
|
||||
{
|
||||
para1573{"10101"},
|
||||
ans1573{4},
|
||||
},
|
||||
|
||||
{
|
||||
para1573{"1001"},
|
||||
ans1573{0},
|
||||
},
|
||||
|
||||
{
|
||||
para1573{"0000"},
|
||||
ans1573{3},
|
||||
},
|
||||
|
||||
{
|
||||
para1573{"100100010100110"},
|
||||
ans1573{12},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1573------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1573, q.para1573
|
||||
fmt.Printf("【input】:%v 【output】:%v \n", p, numWays(p.s))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
108
leetcode/1573.Number-of-Ways-to-Split-a-String/README.md
Normal file
108
leetcode/1573.Number-of-Ways-to-Split-a-String/README.md
Normal file
@ -0,0 +1,108 @@
|
||||
# [1573. Number of Ways to Split a String](https://leetcode.com/problems/number-of-ways-to-split-a-string/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a binary string `s` (a string consisting only of '0's and '1's), we can split `s` into 3 **non-empty** strings s1, s2, s3 (s1+ s2+ s3 = s).
|
||||
|
||||
Return the number of ways `s` can be split such that the number of characters '1' is the same in s1, s2, and s3.
|
||||
|
||||
Since the answer may be too large, return it modulo 10^9 + 7.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: s = "10101"
|
||||
Output: 4
|
||||
Explanation: There are four ways to split s in 3 parts where each part contain the same number of letters '1'.
|
||||
"1|010|1"
|
||||
"1|01|01"
|
||||
"10|10|1"
|
||||
"10|1|01"
|
||||
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: s = "1001"
|
||||
Output: 0
|
||||
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: s = "0000"
|
||||
Output: 3
|
||||
Explanation: There are three ways to split s in 3 parts.
|
||||
"0|0|00"
|
||||
"0|00|0"
|
||||
"00|0|0"
|
||||
|
||||
```
|
||||
|
||||
**Example 4:**
|
||||
|
||||
```
|
||||
Input: s = "100100010100110"
|
||||
Output: 12
|
||||
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `3 <= s.length <= 10^5`
|
||||
- `s[i]` is `'0'` or `'1'`.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个二进制串 s (一个只包含 0 和 1 的字符串),我们可以将 s 分割成 3 个 非空 字符串 s1, s2, s3 (s1 + s2 + s3 = s)。请你返回分割 s 的方案数,满足 s1,s2 和 s3 中字符 '1' 的数目相同。由于答案可能很大,请将它对 10^9 + 7 取余后返回。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 这一题是考察的排列组合的知识。根据题意,如果 1 的个数不是 3 的倍数,直接返回 -1。如果字符串里面没有 1,那么切分的方案就是组合,在 n-1 个字母里面选出 2 个位置。利用组合的计算方法,组合数是 (n-1) * (n-2) / 2 。
|
||||
- 剩下的是 3 的倍数的情况。在字符串中选 2 个位置隔成 3 段。从第一段最后一个 1 到第二段第一个 1 之间的 0 的个数为 m1,从第二段最后一个 1 到第三段第一个 1 之间的 0 的个数为 m2。利用乘法原理,方案数为 m1 * m2。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func numWays(s string) int {
|
||||
ones := 0
|
||||
for _, c := range s {
|
||||
if c == '1' {
|
||||
ones++
|
||||
}
|
||||
}
|
||||
if ones%3 != 0 {
|
||||
return 0
|
||||
}
|
||||
if ones == 0 {
|
||||
return (len(s) - 1) * (len(s) - 2) / 2 % 1000000007
|
||||
}
|
||||
N, a, b, c, d, count := ones/3, 0, 0, 0, 0, 0
|
||||
for i, letter := range s {
|
||||
if letter == '0' {
|
||||
continue
|
||||
}
|
||||
if letter == '1' {
|
||||
count++
|
||||
}
|
||||
if count == N {
|
||||
a = i
|
||||
}
|
||||
if count == N+1 {
|
||||
b = i
|
||||
}
|
||||
if count == 2*N {
|
||||
c = i
|
||||
}
|
||||
if count == 2*N+1 {
|
||||
d = i
|
||||
}
|
||||
}
|
||||
return (b - a) * (d - c) % 1000000007
|
||||
}
|
||||
```
|
@ -0,0 +1,21 @@
|
||||
package leetcode
|
||||
|
||||
func countConsistentStrings(allowed string, words []string) int {
|
||||
allowedMap, res, flag := map[rune]int{}, 0, true
|
||||
for _, str := range allowed {
|
||||
allowedMap[str]++
|
||||
}
|
||||
for i := 0; i < len(words); i++ {
|
||||
flag = true
|
||||
for j := 0; j < len(words[i]); j++ {
|
||||
if _, ok := allowedMap[rune(words[i][j])]; !ok {
|
||||
flag = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if flag {
|
||||
res++
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
@ -0,0 +1,53 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1684 struct {
|
||||
para1684
|
||||
ans1684
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1684 struct {
|
||||
allowed string
|
||||
words []string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1684 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1684(t *testing.T) {
|
||||
|
||||
qs := []question1684{
|
||||
|
||||
{
|
||||
para1684{"ab", []string{"ad", "bd", "aaab", "baa", "badab"}},
|
||||
ans1684{2},
|
||||
},
|
||||
|
||||
{
|
||||
para1684{"abc", []string{"a", "b", "c", "ab", "ac", "bc", "abc"}},
|
||||
ans1684{7},
|
||||
},
|
||||
|
||||
{
|
||||
para1684{"cad", []string{"cc", "acd", "b", "ba", "bac", "bad", "ac", "d"}},
|
||||
ans1684{4},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1684------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1684, q.para1684
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, countConsistentStrings(p.allowed, p.words))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,79 @@
|
||||
# [1684. Count the Number of Consistent Strings](https://leetcode.com/problems/count-the-number-of-consistent-strings/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given a string `allowed` consisting of **distinct** characters and an array of strings `words`. A string is **consistent** if all characters in the string appear in the string `allowed`.
|
||||
|
||||
Return *the number of **consistent** strings in the array* `words`.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"]
|
||||
Output: 2
|
||||
Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'.
|
||||
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"]
|
||||
Output: 7
|
||||
Explanation: All strings are consistent.
|
||||
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"]
|
||||
Output: 4
|
||||
Explanation: Strings "cc", "acd", "ac", and "d" are consistent.
|
||||
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= words.length <= 104`
|
||||
- `1 <= allowed.length <= 26`
|
||||
- `1 <= words[i].length <= 10`
|
||||
- The characters in `allowed` are **distinct**.
|
||||
- `words[i]` and `allowed` contain only lowercase English letters.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个由不同字符组成的字符串 `allowed` 和一个字符串数组 `words` 。如果一个字符串的每一个字符都在 `allowed` 中,就称这个字符串是 一致字符串 。
|
||||
|
||||
请你返回 `words` 数组中 一致字符串 的数目。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。先将 `allowed` 转化成 map。将 `words` 数组中每个单词的字符都在 map 中查找一遍,如果都存在就累加 res。如果有不存在的字母,不累加。最终输出 res 即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func countConsistentStrings(allowed string, words []string) int {
|
||||
allowedMap, res, flag := map[rune]int{}, 0, true
|
||||
for _, str := range allowed {
|
||||
allowedMap[str]++
|
||||
}
|
||||
for i := 0; i < len(words); i++ {
|
||||
flag = true
|
||||
for j := 0; j < len(words[i]); j++ {
|
||||
if _, ok := allowedMap[rune(words[i][j])]; !ok {
|
||||
flag = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if flag {
|
||||
res++
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
@ -0,0 +1,42 @@
|
||||
package leetcode
|
||||
|
||||
//解法一 优化版 prefixSum + sufixSum
|
||||
func getSumAbsoluteDifferences(nums []int) []int {
|
||||
size := len(nums)
|
||||
sufixSum := make([]int, size)
|
||||
sufixSum[size-1] = nums[size-1]
|
||||
for i := size - 2; i >= 0; i-- {
|
||||
sufixSum[i] = sufixSum[i+1] + nums[i]
|
||||
}
|
||||
ans, preSum := make([]int, size), 0
|
||||
for i := 0; i < size; i++ {
|
||||
// 后面可以加到的值
|
||||
res, sum := 0, sufixSum[i]-nums[i]
|
||||
res += (sum - (size-i-1)*nums[i])
|
||||
// 前面可以加到的值
|
||||
res += (i*nums[i] - preSum)
|
||||
ans[i] = res
|
||||
preSum += nums[i]
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
||||
// 解法二 prefixSum
|
||||
func getSumAbsoluteDifferences1(nums []int) []int {
|
||||
preSum, res, sum := []int{}, []int{}, nums[0]
|
||||
preSum = append(preSum, nums[0])
|
||||
for i := 1; i < len(nums); i++ {
|
||||
sum += nums[i]
|
||||
preSum = append(preSum, sum)
|
||||
}
|
||||
for i := 0; i < len(nums); i++ {
|
||||
if i == 0 {
|
||||
res = append(res, preSum[len(nums)-1]-preSum[0]-nums[i]*(len(nums)-1))
|
||||
} else if i > 0 && i < len(nums)-1 {
|
||||
res = append(res, preSum[len(nums)-1]-preSum[i]-preSum[i-1]+nums[i]*i-nums[i]*(len(nums)-1-i))
|
||||
} else {
|
||||
res = append(res, nums[i]*len(nums)-preSum[len(nums)-1])
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1685 struct {
|
||||
para1685
|
||||
ans1685
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1685 struct {
|
||||
nums []int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1685 struct {
|
||||
one []int
|
||||
}
|
||||
|
||||
func Test_Problem1685(t *testing.T) {
|
||||
|
||||
qs := []question1685{
|
||||
|
||||
{
|
||||
para1685{[]int{2, 3, 5}},
|
||||
ans1685{[]int{4, 3, 5}},
|
||||
},
|
||||
|
||||
{
|
||||
para1685{[]int{1, 4, 6, 8, 10}},
|
||||
ans1685{[]int{24, 15, 13, 15, 21}},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1685------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1685, q.para1685
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, getSumAbsoluteDifferences(p.nums))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,88 @@
|
||||
# [1685. Sum of Absolute Differences in a Sorted Array](https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given an integer array `nums` sorted in **non-decreasing** order.
|
||||
|
||||
Build and return *an integer array* `result` *with the same length as* `nums` *such that* `result[i]` *is equal to the **summation of absolute differences** between* `nums[i]` *and all the other elements in the array.*
|
||||
|
||||
In other words, `result[i]` is equal to `sum(|nums[i]-nums[j]|)` where `0 <= j < nums.length` and `j != i` (**0-indexed**).
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: nums = [2,3,5]
|
||||
Output: [4,3,5]
|
||||
Explanation: Assuming the arrays are 0-indexed, then
|
||||
result[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4,
|
||||
result[1] = |3-2| + |3-3| + |3-5| = 1 + 0 + 2 = 3,
|
||||
result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: nums = [1,4,6,8,10]
|
||||
Output: [24,15,13,15,21]
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `2 <= nums.length <= 105`
|
||||
- `1 <= nums[i] <= nums[i + 1] <= 104`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个 非递减 有序整数数组 `nums` 。请你建立并返回一个整数数组 `result`,它跟 `nums` 长度相同,且`result[i]` 等于 `nums[i]` 与数组中所有其他元素差的绝对值之和。换句话说, `result[i]` 等于 `sum(|nums[i]-nums[j]|)` ,其中 `0 <= j < nums.length` 且 `j != i` (下标从 0 开始)。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 利用前缀和思路解题。题目中说明了是有序数组,所以在计算绝对值的时候可以拆开绝对值符号。假设要计算当前 `result[i]`,以 `i` 为界,把原数组 `nums` 分成了 3 段。`nums[0 ~ i-1]` 和 `nums[i+1 ~ n]`,前面一段 `nums[0 ~ i-1]` 中的每个元素都比 `nums[i]` 小,拆掉绝对值以后,`sum(|nums[i]-nums[j]|) = nums[i] * i - prefixSum[0 ~ i-1]`,后面一段 `nums[i+1 ~ n]` 中的每个元素都比 `nums[i]` 大,拆掉绝对值以后,`sum(|nums[i]-nums[j]|) = prefixSum[i+1 ~ n] - nums[i] * (n - 1 - i)`。特殊的情况,`i = 0` 和 `i = n` 的情况特殊处理一下就行。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
//解法一 优化版 prefixSum + sufixSum
|
||||
func getSumAbsoluteDifferences(nums []int) []int {
|
||||
size := len(nums)
|
||||
sufixSum := make([]int, size)
|
||||
sufixSum[size-1] = nums[size-1]
|
||||
for i := size - 2; i >= 0; i-- {
|
||||
sufixSum[i] = sufixSum[i+1] + nums[i]
|
||||
}
|
||||
ans, preSum := make([]int, size), 0
|
||||
for i := 0; i < size; i++ {
|
||||
// 后面可以加到的值
|
||||
res, sum := 0, sufixSum[i]-nums[i]
|
||||
res += (sum - (size-i-1)*nums[i])
|
||||
// 前面可以加到的值
|
||||
res += (i*nums[i] - preSum)
|
||||
ans[i] = res
|
||||
preSum += nums[i]
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
||||
// 解法二 prefixSum
|
||||
func getSumAbsoluteDifferences1(nums []int) []int {
|
||||
preSum, res, sum := []int{}, []int{}, nums[0]
|
||||
preSum = append(preSum, nums[0])
|
||||
for i := 1; i < len(nums); i++ {
|
||||
sum += nums[i]
|
||||
preSum = append(preSum, sum)
|
||||
}
|
||||
for i := 0; i < len(nums); i++ {
|
||||
if i == 0 {
|
||||
res = append(res, preSum[len(nums)-1]-preSum[0]-nums[i]*(len(nums)-1))
|
||||
} else if i > 0 && i < len(nums)-1 {
|
||||
res = append(res, preSum[len(nums)-1]-preSum[i]-preSum[i-1]+nums[i]*i-nums[i]*(len(nums)-1-i))
|
||||
} else {
|
||||
res = append(res, nums[i]*len(nums)-preSum[len(nums)-1])
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
@ -0,0 +1,21 @@
|
||||
package leetcode
|
||||
|
||||
// 解法一
|
||||
func numberOfMatches(n int) int {
|
||||
return n - 1
|
||||
}
|
||||
|
||||
// 解法二 模拟
|
||||
func numberOfMatches1(n int) int {
|
||||
sum := 0
|
||||
for n != 1 {
|
||||
if n&1 == 0 {
|
||||
sum += n / 2
|
||||
n = n / 2
|
||||
} else {
|
||||
sum += (n - 1) / 2
|
||||
n = (n-1)/2 + 1
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1688 struct {
|
||||
para1688
|
||||
ans1688
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1688 struct {
|
||||
n int
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1688 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1688(t *testing.T) {
|
||||
|
||||
qs := []question1688{
|
||||
|
||||
{
|
||||
para1688{7},
|
||||
ans1688{6},
|
||||
},
|
||||
|
||||
{
|
||||
para1688{14},
|
||||
ans1688{13},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1688------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1688, q.para1688
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, numberOfMatches(p.n))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
80
leetcode/1688.Count-of-Matches-in-Tournament/README.md
Normal file
80
leetcode/1688.Count-of-Matches-in-Tournament/README.md
Normal file
@ -0,0 +1,80 @@
|
||||
# [1688. Count of Matches in Tournament](https://leetcode.com/problems/count-of-matches-in-tournament/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given an integer `n`, the number of teams in a tournament that has strange rules:
|
||||
|
||||
- If the current number of teams is **even**, each team gets paired with another team. A total of `n / 2` matches are played, and `n / 2` teams advance to the next round.
|
||||
- If the current number of teams is **odd**, one team randomly advances in the tournament, and the rest gets paired. A total of `(n - 1) / 2` matches are played, and `(n - 1) / 2 + 1` teams advance to the next round.
|
||||
|
||||
Return *the number of matches played in the tournament until a winner is decided.*
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: n = 7
|
||||
Output: 6
|
||||
Explanation: Details of the tournament:
|
||||
- 1st Round: Teams = 7, Matches = 3, and 4 teams advance.
|
||||
- 2nd Round: Teams = 4, Matches = 2, and 2 teams advance.
|
||||
- 3rd Round: Teams = 2, Matches = 1, and 1 team is declared the winner.
|
||||
Total number of matches = 3 + 2 + 1 = 6.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: n = 14
|
||||
Output: 13
|
||||
Explanation: Details of the tournament:
|
||||
- 1st Round: Teams = 14, Matches = 7, and 7 teams advance.
|
||||
- 2nd Round: Teams = 7, Matches = 3, and 4 teams advance.
|
||||
- 3rd Round: Teams = 4, Matches = 2, and 2 teams advance.
|
||||
- 4th Round: Teams = 2, Matches = 1, and 1 team is declared the winner.
|
||||
Total number of matches = 7 + 3 + 2 + 1 = 13.
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= n <= 200`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个整数 n ,表示比赛中的队伍数。比赛遵循一种独特的赛制:
|
||||
|
||||
- 如果当前队伍数是 偶数 ,那么每支队伍都会与另一支队伍配对。总共进行 n / 2 场比赛,且产生 n / 2 支队伍进入下一轮。
|
||||
- 如果当前队伍数为 奇数 ,那么将会随机轮空并晋级一支队伍,其余的队伍配对。总共进行 (n - 1) / 2 场比赛,且产生 (n - 1) / 2 + 1 支队伍进入下一轮。
|
||||
|
||||
返回在比赛中进行的配对次数,直到决出获胜队伍为止。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题,按照题目的规则模拟。
|
||||
- 这一题还有更加简洁的代码,见解法一。n 个队伍,一个冠军,需要淘汰 n-1 个队伍。每一场比赛淘汰一个队伍,因此进行了 n-1 场比赛。所以共有 n-1 个配对。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
// 解法一
|
||||
func numberOfMatches(n int) int {
|
||||
return n - 1
|
||||
}
|
||||
|
||||
// 解法二 模拟
|
||||
func numberOfMatches1(n int) int {
|
||||
sum := 0
|
||||
for n != 1 {
|
||||
if n&1 == 0 {
|
||||
sum += n / 2
|
||||
n = n / 2
|
||||
} else {
|
||||
sum += (n - 1) / 2
|
||||
n = (n-1)/2 + 1
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
```
|
@ -0,0 +1,11 @@
|
||||
package leetcode
|
||||
|
||||
func minPartitions(n string) int {
|
||||
res := 0
|
||||
for i := 0; i < len(n); i++ {
|
||||
if int(n[i]-'0') > res {
|
||||
res = int(n[i] - '0')
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
package leetcode
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
)
|
||||
|
||||
type question1689 struct {
|
||||
para1689
|
||||
ans1689
|
||||
}
|
||||
|
||||
// para 是参数
|
||||
// one 代表第一个参数
|
||||
type para1689 struct {
|
||||
n string
|
||||
}
|
||||
|
||||
// ans 是答案
|
||||
// one 代表第一个答案
|
||||
type ans1689 struct {
|
||||
one int
|
||||
}
|
||||
|
||||
func Test_Problem1689(t *testing.T) {
|
||||
|
||||
qs := []question1689{
|
||||
|
||||
{
|
||||
para1689{"32"},
|
||||
ans1689{3},
|
||||
},
|
||||
|
||||
{
|
||||
para1689{"82734"},
|
||||
ans1689{8},
|
||||
},
|
||||
}
|
||||
|
||||
fmt.Printf("------------------------Leetcode Problem 1689------------------------\n")
|
||||
|
||||
for _, q := range qs {
|
||||
_, p := q.ans1689, q.para1689
|
||||
fmt.Printf("【input】:%v 【output】:%v\n", p, minPartitions(p.n))
|
||||
}
|
||||
fmt.Printf("\n\n\n")
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
# [1689. Partitioning Into Minimum Number Of Deci-Binary Numbers](https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/)
|
||||
|
||||
## 题目
|
||||
|
||||
A decimal number is called **deci-binary** if each of its digits is either `0` or `1` without any leading zeros. For example, `101` and `1100` are **deci-binary**, while `112` and `3001` are not.
|
||||
|
||||
Given a string `n` that represents a positive decimal integer, return *the **minimum** number of positive **deci-binary** numbers needed so that they sum up to* `n`*.*
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: n = "32"
|
||||
Output: 3
|
||||
Explanation: 10 + 11 + 11 = 32
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: n = "82734"
|
||||
Output: 8
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: n = "27346209830709182346"
|
||||
Output: 9
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= n.length <= 105`
|
||||
- `n` consists of only digits.
|
||||
- `n` does not contain any leading zeros and represents a positive integer.
|
||||
|
||||
## 题目大意
|
||||
|
||||
如果一个十进制数字不含任何前导零,且每一位上的数字不是 0 就是 1 ,那么该数字就是一个 十-二进制数 。例如,101 和 1100 都是 十-二进制数,而 112 和 3001 不是。给你一个表示十进制整数的字符串 n ,返回和为 n 的 十-二进制数 的最少数目。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 这一题也算是简单题,相通了以后,代码就 3 行。
|
||||
- 要想由 01 组成的十进制数组成 n,只需要在 n 这个数的各个数位上依次排上 0 和 1 即可。例如 n = 23423723,这是一个 8 位数。最大数字是 7,所以至少需要 7 个数累加能得到这个 n。这 7 个数的百位都为 1,其他数位按需求取 0 和 1 即可。例如万位是 2,那么这 7 个数中任找 2 个数的万位是 1 ,其他 5 个数的万位是 0 即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func minPartitions(n string) int {
|
||||
res := 0
|
||||
for i := 0; i < len(n); i++ {
|
||||
if int(n[i]-'0') > res {
|
||||
res = int(n[i] - '0')
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
@ -0,0 +1,108 @@
|
||||
# [1573. Number of Ways to Split a String](https://leetcode.com/problems/number-of-ways-to-split-a-string/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
Given a binary string `s` (a string consisting only of '0's and '1's), we can split `s` into 3 **non-empty** strings s1, s2, s3 (s1+ s2+ s3 = s).
|
||||
|
||||
Return the number of ways `s` can be split such that the number of characters '1' is the same in s1, s2, and s3.
|
||||
|
||||
Since the answer may be too large, return it modulo 10^9 + 7.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: s = "10101"
|
||||
Output: 4
|
||||
Explanation: There are four ways to split s in 3 parts where each part contain the same number of letters '1'.
|
||||
"1|010|1"
|
||||
"1|01|01"
|
||||
"10|10|1"
|
||||
"10|1|01"
|
||||
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: s = "1001"
|
||||
Output: 0
|
||||
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: s = "0000"
|
||||
Output: 3
|
||||
Explanation: There are three ways to split s in 3 parts.
|
||||
"0|0|00"
|
||||
"0|00|0"
|
||||
"00|0|0"
|
||||
|
||||
```
|
||||
|
||||
**Example 4:**
|
||||
|
||||
```
|
||||
Input: s = "100100010100110"
|
||||
Output: 12
|
||||
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `3 <= s.length <= 10^5`
|
||||
- `s[i]` is `'0'` or `'1'`.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个二进制串 s (一个只包含 0 和 1 的字符串),我们可以将 s 分割成 3 个 非空 字符串 s1, s2, s3 (s1 + s2 + s3 = s)。请你返回分割 s 的方案数,满足 s1,s2 和 s3 中字符 '1' 的数目相同。由于答案可能很大,请将它对 10^9 + 7 取余后返回。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 这一题是考察的排列组合的知识。根据题意,如果 1 的个数不是 3 的倍数,直接返回 -1。如果字符串里面没有 1,那么切分的方案就是组合,在 n-1 个字母里面选出 2 个位置。利用组合的计算方法,组合数是 (n-1) * (n-2) / 2 。
|
||||
- 剩下的是 3 的倍数的情况。在字符串中选 2 个位置隔成 3 段。从第一段最后一个 1 到第二段第一个 1 之间的 0 的个数为 m1,从第二段最后一个 1 到第三段第一个 1 之间的 0 的个数为 m2。利用乘法原理,方案数为 m1 * m2。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func numWays(s string) int {
|
||||
ones := 0
|
||||
for _, c := range s {
|
||||
if c == '1' {
|
||||
ones++
|
||||
}
|
||||
}
|
||||
if ones%3 != 0 {
|
||||
return 0
|
||||
}
|
||||
if ones == 0 {
|
||||
return (len(s) - 1) * (len(s) - 2) / 2 % 1000000007
|
||||
}
|
||||
N, a, b, c, d, count := ones/3, 0, 0, 0, 0, 0
|
||||
for i, letter := range s {
|
||||
if letter == '0' {
|
||||
continue
|
||||
}
|
||||
if letter == '1' {
|
||||
count++
|
||||
}
|
||||
if count == N {
|
||||
a = i
|
||||
}
|
||||
if count == N+1 {
|
||||
b = i
|
||||
}
|
||||
if count == 2*N {
|
||||
c = i
|
||||
}
|
||||
if count == 2*N+1 {
|
||||
d = i
|
||||
}
|
||||
}
|
||||
return (b - a) * (d - c) % 1000000007
|
||||
}
|
||||
```
|
@ -0,0 +1,79 @@
|
||||
# [1684. Count the Number of Consistent Strings](https://leetcode.com/problems/count-the-number-of-consistent-strings/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given a string `allowed` consisting of **distinct** characters and an array of strings `words`. A string is **consistent** if all characters in the string appear in the string `allowed`.
|
||||
|
||||
Return *the number of **consistent** strings in the array* `words`.
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: allowed = "ab", words = ["ad","bd","aaab","baa","badab"]
|
||||
Output: 2
|
||||
Explanation: Strings "aaab" and "baa" are consistent since they only contain characters 'a' and 'b'.
|
||||
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: allowed = "abc", words = ["a","b","c","ab","ac","bc","abc"]
|
||||
Output: 7
|
||||
Explanation: All strings are consistent.
|
||||
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: allowed = "cad", words = ["cc","acd","b","ba","bac","bad","ac","d"]
|
||||
Output: 4
|
||||
Explanation: Strings "cc", "acd", "ac", and "d" are consistent.
|
||||
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= words.length <= 104`
|
||||
- `1 <= allowed.length <= 26`
|
||||
- `1 <= words[i].length <= 10`
|
||||
- The characters in `allowed` are **distinct**.
|
||||
- `words[i]` and `allowed` contain only lowercase English letters.
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个由不同字符组成的字符串 `allowed` 和一个字符串数组 `words` 。如果一个字符串的每一个字符都在 `allowed` 中,就称这个字符串是 一致字符串 。
|
||||
|
||||
请你返回 `words` 数组中 一致字符串 的数目。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题。先将 `allowed` 转化成 map。将 `words` 数组中每个单词的字符都在 map 中查找一遍,如果都存在就累加 res。如果有不存在的字母,不累加。最终输出 res 即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func countConsistentStrings(allowed string, words []string) int {
|
||||
allowedMap, res, flag := map[rune]int{}, 0, true
|
||||
for _, str := range allowed {
|
||||
allowedMap[str]++
|
||||
}
|
||||
for i := 0; i < len(words); i++ {
|
||||
flag = true
|
||||
for j := 0; j < len(words[i]); j++ {
|
||||
if _, ok := allowedMap[rune(words[i][j])]; !ok {
|
||||
flag = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if flag {
|
||||
res++
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
@ -0,0 +1,88 @@
|
||||
# [1685. Sum of Absolute Differences in a Sorted Array](https://leetcode.com/problems/sum-of-absolute-differences-in-a-sorted-array/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given an integer array `nums` sorted in **non-decreasing** order.
|
||||
|
||||
Build and return *an integer array* `result` *with the same length as* `nums` *such that* `result[i]` *is equal to the **summation of absolute differences** between* `nums[i]` *and all the other elements in the array.*
|
||||
|
||||
In other words, `result[i]` is equal to `sum(|nums[i]-nums[j]|)` where `0 <= j < nums.length` and `j != i` (**0-indexed**).
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: nums = [2,3,5]
|
||||
Output: [4,3,5]
|
||||
Explanation: Assuming the arrays are 0-indexed, then
|
||||
result[0] = |2-2| + |2-3| + |2-5| = 0 + 1 + 3 = 4,
|
||||
result[1] = |3-2| + |3-3| + |3-5| = 1 + 0 + 2 = 3,
|
||||
result[2] = |5-2| + |5-3| + |5-5| = 3 + 2 + 0 = 5.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: nums = [1,4,6,8,10]
|
||||
Output: [24,15,13,15,21]
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `2 <= nums.length <= 105`
|
||||
- `1 <= nums[i] <= nums[i + 1] <= 104`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个 非递减 有序整数数组 `nums` 。请你建立并返回一个整数数组 `result`,它跟 `nums` 长度相同,且`result[i]` 等于 `nums[i]` 与数组中所有其他元素差的绝对值之和。换句话说, `result[i]` 等于 `sum(|nums[i]-nums[j]|)` ,其中 `0 <= j < nums.length` 且 `j != i` (下标从 0 开始)。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 利用前缀和思路解题。题目中说明了是有序数组,所以在计算绝对值的时候可以拆开绝对值符号。假设要计算当前 `result[i]`,以 `i` 为界,把原数组 `nums` 分成了 3 段。`nums[0 ~ i-1]` 和 `nums[i+1 ~ n]`,前面一段 `nums[0 ~ i-1]` 中的每个元素都比 `nums[i]` 小,拆掉绝对值以后,`sum(|nums[i]-nums[j]|) = nums[i] * i - prefixSum[0 ~ i-1]`,后面一段 `nums[i+1 ~ n]` 中的每个元素都比 `nums[i]` 大,拆掉绝对值以后,`sum(|nums[i]-nums[j]|) = prefixSum[i+1 ~ n] - nums[i] * (n - 1 - i)`。特殊的情况,`i = 0` 和 `i = n` 的情况特殊处理一下就行。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
//解法一 优化版 prefixSum + sufixSum
|
||||
func getSumAbsoluteDifferences(nums []int) []int {
|
||||
size := len(nums)
|
||||
sufixSum := make([]int, size)
|
||||
sufixSum[size-1] = nums[size-1]
|
||||
for i := size - 2; i >= 0; i-- {
|
||||
sufixSum[i] = sufixSum[i+1] + nums[i]
|
||||
}
|
||||
ans, preSum := make([]int, size), 0
|
||||
for i := 0; i < size; i++ {
|
||||
// 后面可以加到的值
|
||||
res, sum := 0, sufixSum[i]-nums[i]
|
||||
res += (sum - (size-i-1)*nums[i])
|
||||
// 前面可以加到的值
|
||||
res += (i*nums[i] - preSum)
|
||||
ans[i] = res
|
||||
preSum += nums[i]
|
||||
}
|
||||
return ans
|
||||
}
|
||||
|
||||
// 解法二 prefixSum
|
||||
func getSumAbsoluteDifferences1(nums []int) []int {
|
||||
preSum, res, sum := []int{}, []int{}, nums[0]
|
||||
preSum = append(preSum, nums[0])
|
||||
for i := 1; i < len(nums); i++ {
|
||||
sum += nums[i]
|
||||
preSum = append(preSum, sum)
|
||||
}
|
||||
for i := 0; i < len(nums); i++ {
|
||||
if i == 0 {
|
||||
res = append(res, preSum[len(nums)-1]-preSum[0]-nums[i]*(len(nums)-1))
|
||||
} else if i > 0 && i < len(nums)-1 {
|
||||
res = append(res, preSum[len(nums)-1]-preSum[i]-preSum[i-1]+nums[i]*i-nums[i]*(len(nums)-1-i))
|
||||
} else {
|
||||
res = append(res, nums[i]*len(nums)-preSum[len(nums)-1])
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
@ -0,0 +1,80 @@
|
||||
# [1688. Count of Matches in Tournament](https://leetcode.com/problems/count-of-matches-in-tournament/)
|
||||
|
||||
|
||||
## 题目
|
||||
|
||||
You are given an integer `n`, the number of teams in a tournament that has strange rules:
|
||||
|
||||
- If the current number of teams is **even**, each team gets paired with another team. A total of `n / 2` matches are played, and `n / 2` teams advance to the next round.
|
||||
- If the current number of teams is **odd**, one team randomly advances in the tournament, and the rest gets paired. A total of `(n - 1) / 2` matches are played, and `(n - 1) / 2 + 1` teams advance to the next round.
|
||||
|
||||
Return *the number of matches played in the tournament until a winner is decided.*
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: n = 7
|
||||
Output: 6
|
||||
Explanation: Details of the tournament:
|
||||
- 1st Round: Teams = 7, Matches = 3, and 4 teams advance.
|
||||
- 2nd Round: Teams = 4, Matches = 2, and 2 teams advance.
|
||||
- 3rd Round: Teams = 2, Matches = 1, and 1 team is declared the winner.
|
||||
Total number of matches = 3 + 2 + 1 = 6.
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: n = 14
|
||||
Output: 13
|
||||
Explanation: Details of the tournament:
|
||||
- 1st Round: Teams = 14, Matches = 7, and 7 teams advance.
|
||||
- 2nd Round: Teams = 7, Matches = 3, and 4 teams advance.
|
||||
- 3rd Round: Teams = 4, Matches = 2, and 2 teams advance.
|
||||
- 4th Round: Teams = 2, Matches = 1, and 1 team is declared the winner.
|
||||
Total number of matches = 7 + 3 + 2 + 1 = 13.
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= n <= 200`
|
||||
|
||||
## 题目大意
|
||||
|
||||
给你一个整数 n ,表示比赛中的队伍数。比赛遵循一种独特的赛制:
|
||||
|
||||
- 如果当前队伍数是 偶数 ,那么每支队伍都会与另一支队伍配对。总共进行 n / 2 场比赛,且产生 n / 2 支队伍进入下一轮。
|
||||
- 如果当前队伍数为 奇数 ,那么将会随机轮空并晋级一支队伍,其余的队伍配对。总共进行 (n - 1) / 2 场比赛,且产生 (n - 1) / 2 + 1 支队伍进入下一轮。
|
||||
|
||||
返回在比赛中进行的配对次数,直到决出获胜队伍为止。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 简单题,按照题目的规则模拟。
|
||||
- 这一题还有更加简洁的代码,见解法一。n 个队伍,一个冠军,需要淘汰 n-1 个队伍。每一场比赛淘汰一个队伍,因此进行了 n-1 场比赛。所以共有 n-1 个配对。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
// 解法一
|
||||
func numberOfMatches(n int) int {
|
||||
return n - 1
|
||||
}
|
||||
|
||||
// 解法二 模拟
|
||||
func numberOfMatches1(n int) int {
|
||||
sum := 0
|
||||
for n != 1 {
|
||||
if n&1 == 0 {
|
||||
sum += n / 2
|
||||
n = n / 2
|
||||
} else {
|
||||
sum += (n - 1) / 2
|
||||
n = (n-1)/2 + 1
|
||||
}
|
||||
}
|
||||
return sum
|
||||
}
|
||||
```
|
@ -0,0 +1,60 @@
|
||||
# [1689. Partitioning Into Minimum Number Of Deci-Binary Numbers](https://leetcode.com/problems/partitioning-into-minimum-number-of-deci-binary-numbers/)
|
||||
|
||||
## 题目
|
||||
|
||||
A decimal number is called **deci-binary** if each of its digits is either `0` or `1` without any leading zeros. For example, `101` and `1100` are **deci-binary**, while `112` and `3001` are not.
|
||||
|
||||
Given a string `n` that represents a positive decimal integer, return *the **minimum** number of positive **deci-binary** numbers needed so that they sum up to* `n`*.*
|
||||
|
||||
**Example 1:**
|
||||
|
||||
```
|
||||
Input: n = "32"
|
||||
Output: 3
|
||||
Explanation: 10 + 11 + 11 = 32
|
||||
```
|
||||
|
||||
**Example 2:**
|
||||
|
||||
```
|
||||
Input: n = "82734"
|
||||
Output: 8
|
||||
```
|
||||
|
||||
**Example 3:**
|
||||
|
||||
```
|
||||
Input: n = "27346209830709182346"
|
||||
Output: 9
|
||||
```
|
||||
|
||||
**Constraints:**
|
||||
|
||||
- `1 <= n.length <= 105`
|
||||
- `n` consists of only digits.
|
||||
- `n` does not contain any leading zeros and represents a positive integer.
|
||||
|
||||
## 题目大意
|
||||
|
||||
如果一个十进制数字不含任何前导零,且每一位上的数字不是 0 就是 1 ,那么该数字就是一个 十-二进制数 。例如,101 和 1100 都是 十-二进制数,而 112 和 3001 不是。给你一个表示十进制整数的字符串 n ,返回和为 n 的 十-二进制数 的最少数目。
|
||||
|
||||
## 解题思路
|
||||
|
||||
- 这一题也算是简单题,相通了以后,代码就 3 行。
|
||||
- 要想由 01 组成的十进制数组成 n,只需要在 n 这个数的各个数位上依次排上 0 和 1 即可。例如 n = 23423723,这是一个 8 位数。最大数字是 7,所以至少需要 7 个数累加能得到这个 n。这 7 个数的百位都为 1,其他数位按需求取 0 和 1 即可。例如万位是 2,那么这 7 个数中任找 2 个数的万位是 1 ,其他 5 个数的万位是 0 即可。
|
||||
|
||||
## 代码
|
||||
|
||||
```go
|
||||
package leetcode
|
||||
|
||||
func minPartitions(n string) int {
|
||||
res := 0
|
||||
for i := 0; i < len(n); i++ {
|
||||
if int(n[i]-'0') > res {
|
||||
res = int(n[i] - '0')
|
||||
}
|
||||
}
|
||||
return res
|
||||
}
|
||||
```
|
@ -548,6 +548,7 @@ headless: true
|
||||
- [1470.Shuffle-the-Array]({{< relref "/ChapterFour/1470.Shuffle-the-Array.md" >}})
|
||||
- [1480.Running-Sum-of-1d-Array]({{< relref "/ChapterFour/1480.Running-Sum-of-1d-Array.md" >}})
|
||||
- [1512.Number-of-Good-Pairs]({{< relref "/ChapterFour/1512.Number-of-Good-Pairs.md" >}})
|
||||
- [1573.Number-of-Ways-to-Split-a-String]({{< relref "/ChapterFour/1573.Number-of-Ways-to-Split-a-String.md" >}})
|
||||
- [1646.Get-Maximum-in-Generated-Array]({{< relref "/ChapterFour/1646.Get-Maximum-in-Generated-Array.md" >}})
|
||||
- [1647.Minimum-Deletions-to-Make-Character-Frequencies-Unique]({{< relref "/ChapterFour/1647.Minimum-Deletions-to-Make-Character-Frequencies-Unique.md" >}})
|
||||
- [1648.Sell-Diminishing-Valued-Colored-Balls]({{< relref "/ChapterFour/1648.Sell-Diminishing-Valued-Colored-Balls.md" >}})
|
||||
@ -568,4 +569,8 @@ headless: true
|
||||
- [1679.Max-Number-of-K-Sum-Pairs]({{< relref "/ChapterFour/1679.Max-Number-of-K-Sum-Pairs.md" >}})
|
||||
- [1680.Concatenation-of-Consecutive-Binary-Numbers]({{< relref "/ChapterFour/1680.Concatenation-of-Consecutive-Binary-Numbers.md" >}})
|
||||
- [1681.Minimum-Incompatibility]({{< relref "/ChapterFour/1681.Minimum-Incompatibility.md" >}})
|
||||
- [1684.Count-the-Number-of-Consistent-Strings]({{< relref "/ChapterFour/1684.Count-the-Number-of-Consistent-Strings.md" >}})
|
||||
- [1685.Sum-of-Absolute-Differences-in-a-Sorted-Array]({{< relref "/ChapterFour/1685.Sum-of-Absolute-Differences-in-a-Sorted-Array.md" >}})
|
||||
- [1688.Count-of-Matches-in-Tournament]({{< relref "/ChapterFour/1688.Count-of-Matches-in-Tournament.md" >}})
|
||||
- [1689.Partitioning-Into-Minimum-Number-Of-Deci-Binary-Numbers]({{< relref "/ChapterFour/1689.Partitioning-Into-Minimum-Number-Of-Deci-Binary-Numbers.md" >}})
|
||||
<br />
|
||||
|
Reference in New Issue
Block a user