func maximumTime(time string) string {
var ans = strings.Builder{}
for i := 0; i < len(time); i++ {
if time[i]=='?'{
switch i {
case 0:
if time[1]!='?' && time[1] >'3'{
ans.WriteByte('1')
}else {
ans.WriteByte('2')
}
case 1:
if ans.String()[0]<'2'{
ans.WriteByte('9')
}else {
ans.WriteByte('3')
}
case 3:
ans.WriteByte('5')
case 4:
ans.WriteByte('9')
}
}else {
ans.WriteByte(time[i])
}
}
return ans.String()
}
第二题
‘a~'z’都模拟一遍 ,就这题。。wa了三次,太了。
func min(a, b int) int {
if a<b{
return a
}
return b
}
func minCharacters(a string, b string) int {
ab,bb := make([]int, 26),make([]int, 26)
for i := 0; i < len(a); i++ {
ab[a[i]-'a']++
}
for i := 0; i < len(b); i++ {
bb[b[i]-'a']++
}
ans := min(operator(ab,bb), operator(bb,ab))
max:=0
for i := 0; i < 26; i++ {
ab[i]+=bb[i]
}
for i := 0; i < 26; i++ {
if max < ab[i]{
max = ab[i]
}
}
// fmt.Println(max)
return min(ans, len(a)+len(b)-max)
}
func operator(a, b []int) int {
ans :=math.MaxInt32
for i := 1; i < 26; i++ {
temp :=0
for j := 0; j < i; j++ {
temp +=a[j]
}
for j := i; j < 26; j++ {
temp +=b[j]
}
if temp < ans{
ans = temp
}
}
return ans
}
func minimumBoxes(n int) int {
i := 1
total := 1
for total+(i+1)*(i+2)/2 < n {
i++
total += (i + 1) * (i) / 2
}
n = n-total
temp :=int(math.Sqrt(float64(n*2)))
if temp*(temp+1)/2 >= n{
return (i+1)*i/2+temp
}else {
return (i+1)*i/2+temp+1
}
}
func LessThanMinCharacters(a string, b string) int {
aCharNums := [26]int{}
bCharNums := [26]int{}
for _, ch := range a {
aCharNums[ch - 'a']++
}
for _, ch := range b {
bCharNums[ch - 'a']++
}
min := math.MaxInt32
for i := 0; i < 26; i++ {
tmpt := 0
if i == 0 {
continue
}
for j := i; j < 26; j++ {
tmpt += aCharNums[j]
}
for j := 0; j < i; j++ {
tmpt += bCharNums[j]
}
if tmpt < min {
min = tmpt
}
}
return min
}
func SameCharacters(a string, b string) int {
charNums := [26]int{}
for _, ch := range a {
charNums[ch-'a']++
}
for _, ch := range b {
charNums[ch-'a']++
}
max := 0
for i := 0; i < 26; i++ {
if max < charNums[i] {
max = charNums[i]
}
}
return len(a) + len(b) - max
}
func minCharacters(a string, b string) int {
min1, min2, min3 := LessThanMinCharacters(a, b), LessThanMinCharacters(b, a), SameCharacters(a, b)
min := min1
if min > min2 {
min = min2
}
if min > min3 {
min = min3
}
return min
}
func kthLargestValue(matrix [][]int, k int) int {
if len(matrix) == 0 {
return 0
}
m, n := len(matrix), len(matrix[0])
xorMatrix := make([]int, m * n)
for i := 0; i < m; i++ {
for j := 0; j < n; j++ {
if i > 0 {
xorMatrix[i * n + j] ^= xorMatrix[(i-1) * n + j]
}
if j > 0 {
xorMatrix[i * n + j] ^= xorMatrix[i * n + j-1]
}
if i > 0 && j > 0 {
xorMatrix[i * n + j] ^= xorMatrix[(i-1) * n + j-1]
}
xorMatrix[i * n + j] ^= matrix[i][j]
}
}
sort.Ints(xorMatrix)
return xorMatrix[m*n-k]
}