add new solutions
This commit is contained in:
parent
ca24d0a56a
commit
0c73608ce5
36 changed files with 791 additions and 0 deletions
31
solutions/0/q85/solution.go
Normal file
31
solutions/0/q85/solution.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package q85
|
||||
|
||||
func maximalRectangle(matrix [][]byte) int {
|
||||
var maxArea int
|
||||
|
||||
for row := range matrix {
|
||||
for col := range matrix[0] {
|
||||
matrix[row][col] -= '0'
|
||||
if matrix[row][col] == 0 {
|
||||
continue
|
||||
}
|
||||
|
||||
if col > 0 {
|
||||
matrix[row][col] += matrix[row][col-1]
|
||||
}
|
||||
|
||||
maxW := matrix[row][col]
|
||||
for i := 0; row-i >= 0; i++ {
|
||||
if matrix[row-i][col] == 0 {
|
||||
break
|
||||
}
|
||||
maxW = min(maxW, matrix[row-i][col])
|
||||
maxArea = max(maxArea, int(maxW)*(i+1))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return maxArea
|
||||
}
|
||||
|
||||
var _ = maximalRectangle
|
||||
15
solutions/10/q1018/solution.go
Normal file
15
solutions/10/q1018/solution.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package q1018
|
||||
|
||||
func prefixesDivBy5(nums []int) []bool {
|
||||
ret := make([]bool, len(nums))
|
||||
|
||||
num := 0
|
||||
for i, n := range nums {
|
||||
num = num<<1 + n
|
||||
ret[i] = num%5 == 0
|
||||
num %= 5
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = prefixesDivBy5
|
||||
20
solutions/12/q1226/solution.go
Normal file
20
solutions/12/q1226/solution.go
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
package q1226
|
||||
|
||||
func abs(i int) int {
|
||||
if i < 0 {
|
||||
return -i
|
||||
}
|
||||
return i
|
||||
}
|
||||
|
||||
func minTimeToVisitAllPoints(points [][]int) int {
|
||||
time := 0
|
||||
for i := 0; i < len(points)-1; i++ {
|
||||
cur, next := points[i], points[i+1]
|
||||
dx, dy := abs(cur[0]-next[0]), abs(cur[1]-next[1])
|
||||
time += max(dx, dy)
|
||||
}
|
||||
return time
|
||||
}
|
||||
|
||||
var _ = minTimeToVisitAllPoints
|
||||
12
solutions/13/q1304/solution.go
Normal file
12
solutions/13/q1304/solution.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package q1304
|
||||
|
||||
func sumZero(n int) []int {
|
||||
ret := make([]int, n)
|
||||
for i := range n / 2 {
|
||||
ret[i*2] = i + 1
|
||||
ret[i*2+1] = -i - 1
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = sumZero
|
||||
25
solutions/13/q1317/solution.go
Normal file
25
solutions/13/q1317/solution.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package q1317
|
||||
|
||||
func noZero(n int) bool {
|
||||
for ; n > 0; n /= 10 {
|
||||
if n%10 == 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func getNoZeroIntegers(n int) []int {
|
||||
for i := 1; i <= n/2; i++ {
|
||||
if !noZero(i) {
|
||||
continue
|
||||
}
|
||||
j := n - i
|
||||
if noZero(j) {
|
||||
return []int{i, j}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var _ = getNoZeroIntegers
|
||||
19
solutions/14/q1437/solution.go
Normal file
19
solutions/14/q1437/solution.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package q1437
|
||||
|
||||
func kLengthApart(nums []int, k int) bool {
|
||||
dist := k
|
||||
|
||||
for _, n := range nums {
|
||||
if n == 0 {
|
||||
dist++
|
||||
} else {
|
||||
if dist < k {
|
||||
return false
|
||||
}
|
||||
dist = 0
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
var _ = kLengthApart
|
||||
19
solutions/15/q1518/solution.go
Normal file
19
solutions/15/q1518/solution.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package q1518
|
||||
|
||||
func numWaterBottles(numBottles int, numExchange int) int {
|
||||
drink := 0
|
||||
empty := 0
|
||||
for {
|
||||
drink += numBottles
|
||||
empty += numBottles
|
||||
if empty < numExchange {
|
||||
break
|
||||
}
|
||||
|
||||
numBottles = empty / numExchange
|
||||
empty = empty % numExchange
|
||||
}
|
||||
return drink
|
||||
}
|
||||
|
||||
var _ = numWaterBottles
|
||||
13
solutions/15/q1523/solution.go
Normal file
13
solutions/15/q1523/solution.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package q1523
|
||||
|
||||
func countOdds(low int, high int) int {
|
||||
if low%2 == 1 {
|
||||
low -= 1
|
||||
}
|
||||
if high%2 == 1 {
|
||||
return (high-low)/2 + 1
|
||||
}
|
||||
return (high - low) / 2
|
||||
}
|
||||
|
||||
var _ = countOdds
|
||||
16
solutions/17/q1716/solution.go
Normal file
16
solutions/17/q1716/solution.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package q1716
|
||||
|
||||
func totalMoney(n int) int {
|
||||
if n <= 7 {
|
||||
return (1 + n) * n / 2
|
||||
}
|
||||
base := 28
|
||||
|
||||
rem := n % 7
|
||||
fullWeeks := n / 7
|
||||
moneyFW := (base-7)*fullWeeks + 7*(1+fullWeeks)*fullWeeks/2
|
||||
moneyRem := (1+rem)*rem/2 + rem*fullWeeks
|
||||
return moneyFW + moneyRem
|
||||
}
|
||||
|
||||
var _ = totalMoney
|
||||
25
solutions/19/q1925/solution.go
Normal file
25
solutions/19/q1925/solution.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package q1925
|
||||
|
||||
import "math"
|
||||
|
||||
func countTriples(n int) int {
|
||||
cnt := 0
|
||||
for a := 1; a <= n; a++ {
|
||||
for b := a; b <= n; b++ {
|
||||
sqC := a*a + b*b
|
||||
c := int(math.Sqrt(float64(sqC)))
|
||||
if c > n {
|
||||
break
|
||||
}
|
||||
if c*c == sqC { // c is integer
|
||||
cnt += 2
|
||||
if a == b {
|
||||
cnt -= 1
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
return cnt
|
||||
}
|
||||
|
||||
var _ = countTriples
|
||||
25
solutions/19/q1935/solution.go
Normal file
25
solutions/19/q1935/solution.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package q1935
|
||||
|
||||
import "strings"
|
||||
|
||||
func canBeTypedWords(text string, brokenLetters string) int {
|
||||
var possible = 0
|
||||
broken := [26]bool{}
|
||||
for i := range brokenLetters {
|
||||
broken[brokenLetters[i]-'a'] = true
|
||||
}
|
||||
|
||||
Outer:
|
||||
for _, w := range strings.Fields(text) {
|
||||
for i := range w {
|
||||
if broken[w[i]-'a'] {
|
||||
continue Outer
|
||||
}
|
||||
}
|
||||
possible++
|
||||
}
|
||||
|
||||
return possible
|
||||
}
|
||||
|
||||
var _ = canBeTypedWords
|
||||
16
solutions/20/q2011/solution.go
Normal file
16
solutions/20/q2011/solution.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package q2011
|
||||
|
||||
func finalValueAfterOperations(operations []string) int {
|
||||
x := 0
|
||||
for i := range operations {
|
||||
switch operations[i] {
|
||||
case "X++", "++X":
|
||||
x++
|
||||
case "X--", "--X":
|
||||
x--
|
||||
}
|
||||
}
|
||||
return x
|
||||
}
|
||||
|
||||
var _ = finalValueAfterOperations
|
||||
16
solutions/21/q2154/solution.go
Normal file
16
solutions/21/q2154/solution.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package q2154
|
||||
|
||||
import "slices"
|
||||
|
||||
func findFinalValue(nums []int, original int) int {
|
||||
slices.Sort(nums)
|
||||
|
||||
for i := range nums {
|
||||
if nums[i] == original {
|
||||
original *= 2
|
||||
}
|
||||
}
|
||||
return original
|
||||
}
|
||||
|
||||
var _ = findFinalValue
|
||||
15
solutions/21/q2169/solution.go
Normal file
15
solutions/21/q2169/solution.go
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
package q2169
|
||||
|
||||
func countOperations(num1 int, num2 int) int {
|
||||
cnt := 0
|
||||
num1, num2 = max(num1, num2), min(num1, num2)
|
||||
|
||||
for num2 > 0 {
|
||||
t := num1 % num2
|
||||
cnt += num1 / num2
|
||||
num1, num2 = num2, t
|
||||
}
|
||||
return cnt
|
||||
}
|
||||
|
||||
var _ = countOperations
|
||||
37
solutions/22/q2273/solution.go
Normal file
37
solutions/22/q2273/solution.go
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
package q2273
|
||||
|
||||
var buf = [26]int{}
|
||||
var bufZ = [26]int{}
|
||||
|
||||
func isAnagram(a, b string) bool {
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
seen := buf[:]
|
||||
copy(seen, bufZ[:])
|
||||
for i := range len(a) {
|
||||
c := a[i] - 'a'
|
||||
seen[c]++
|
||||
}
|
||||
for i := range len(b) {
|
||||
c := b[i] - 'a'
|
||||
seen[c]--
|
||||
if seen[c] < 0 {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func removeAnagrams(words []string) []string {
|
||||
p := 0
|
||||
for i := 1; i < len(words); i++ {
|
||||
if !isAnagram(words[p], words[i]) {
|
||||
p++
|
||||
words[p] = words[i]
|
||||
}
|
||||
}
|
||||
return words[:p+1]
|
||||
}
|
||||
|
||||
var _ = removeAnagrams
|
||||
18
solutions/23/q2390/solution.go
Normal file
18
solutions/23/q2390/solution.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package q2390
|
||||
|
||||
func removeStars(s string) string {
|
||||
edit := []byte(s)
|
||||
p := 0
|
||||
|
||||
for _, c := range edit {
|
||||
if c == '*' {
|
||||
p--
|
||||
} else {
|
||||
edit[p] = c
|
||||
p++
|
||||
}
|
||||
}
|
||||
return string(edit[:p])
|
||||
}
|
||||
|
||||
var _ = removeStars
|
||||
18
solutions/24/q2405/solution.go
Normal file
18
solutions/24/q2405/solution.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package q2405
|
||||
|
||||
func partitionString(s string) int {
|
||||
var seen int32
|
||||
|
||||
partitions := 1
|
||||
for i := range len(s) {
|
||||
offset := s[i] - 'a'
|
||||
if seen|1<<offset == seen {
|
||||
partitions += 1
|
||||
seen = 0
|
||||
}
|
||||
seen |= 1 << offset
|
||||
}
|
||||
return partitions
|
||||
}
|
||||
|
||||
var _ = partitionString
|
||||
27
solutions/30/q3005/solution.go
Normal file
27
solutions/30/q3005/solution.go
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
package q3005
|
||||
|
||||
import "slices"
|
||||
|
||||
func maxFrequencyElements(nums []int) int {
|
||||
slices.Sort(nums)
|
||||
maxFreq := 0
|
||||
nMaxFreq := 0
|
||||
|
||||
freq := 1
|
||||
for i := 1; i <= len(nums); i++ {
|
||||
if i < len(nums) && nums[i] == nums[i-1] {
|
||||
freq++
|
||||
} else {
|
||||
if freq > maxFreq {
|
||||
maxFreq = freq
|
||||
nMaxFreq = 1
|
||||
} else if freq == maxFreq {
|
||||
nMaxFreq++
|
||||
}
|
||||
freq = 1
|
||||
}
|
||||
}
|
||||
return maxFreq * nMaxFreq
|
||||
}
|
||||
|
||||
var _ = maxFrequencyElements
|
||||
21
solutions/30/q3074/solution.go
Normal file
21
solutions/30/q3074/solution.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package q3074
|
||||
|
||||
import "slices"
|
||||
|
||||
func minimumBoxes(apple []int, capacity []int) int {
|
||||
apples := 0
|
||||
for _, a := range apple {
|
||||
apples += a
|
||||
}
|
||||
|
||||
slices.Sort(capacity)
|
||||
for i := len(capacity) - 1; i >= 0; i-- {
|
||||
apples -= capacity[i]
|
||||
if apples <= 0 {
|
||||
return len(capacity) - i
|
||||
}
|
||||
}
|
||||
return len(capacity)
|
||||
}
|
||||
|
||||
var _ = minimumBoxes
|
||||
13
solutions/31/q3190/solution.go
Normal file
13
solutions/31/q3190/solution.go
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
package q3190
|
||||
|
||||
func minimumOperations(nums []int) int {
|
||||
nOps := 0
|
||||
for _, num := range nums {
|
||||
if num%3 != 0 {
|
||||
nOps++
|
||||
}
|
||||
}
|
||||
return nOps
|
||||
}
|
||||
|
||||
var _ = minimumOperations
|
||||
19
solutions/32/q3289/solution.go
Normal file
19
solutions/32/q3289/solution.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package q3289
|
||||
|
||||
func getSneakyNumbers(nums []int) []int {
|
||||
ret := [2]int{}
|
||||
p := 0
|
||||
|
||||
for _, num := range nums {
|
||||
num %= 1000
|
||||
if nums[num] >= 1000 {
|
||||
ret[p] = num
|
||||
p++
|
||||
} else {
|
||||
nums[num] += 1000
|
||||
}
|
||||
}
|
||||
return ret[:]
|
||||
}
|
||||
|
||||
var _ = getSneakyNumbers
|
||||
49
solutions/33/q3318/solution.go
Normal file
49
solutions/33/q3318/solution.go
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
package q3318
|
||||
|
||||
import "slices"
|
||||
|
||||
type NumOccur struct {
|
||||
num, occur int
|
||||
}
|
||||
|
||||
func xSum(x int, occurances map[int]int, buf []NumOccur) int {
|
||||
buf = buf[:]
|
||||
for n, o := range occurances {
|
||||
buf = append(buf, NumOccur{n, o})
|
||||
}
|
||||
slices.SortFunc(buf, func(a, b NumOccur) int {
|
||||
if a.occur != b.occur {
|
||||
return b.occur - a.occur
|
||||
}
|
||||
return b.num - a.num
|
||||
})
|
||||
|
||||
sum := 0
|
||||
for i := 0; i < x && i < len(buf); i++ {
|
||||
sum += buf[i].num * buf[i].occur
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
func findXSum(nums []int, k int, x int) []int {
|
||||
ret := make([]int, len(nums)-k+1)
|
||||
|
||||
buf := make([]NumOccur, k)
|
||||
occur := make(map[int]int, k)
|
||||
for i := range nums {
|
||||
occur[nums[i]]++
|
||||
if i >= k {
|
||||
occur[nums[i-k]]--
|
||||
if occur[nums[i-k]] == 0 {
|
||||
delete(occur, nums[i-k])
|
||||
}
|
||||
}
|
||||
if i >= k-1 {
|
||||
ret[i-k+1] = xSum(x, occur, buf)
|
||||
}
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = findXSum
|
||||
25
solutions/33/q3349/solution.go
Normal file
25
solutions/33/q3349/solution.go
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
package q3349
|
||||
|
||||
func hasIncreasingSubarrays(nums []int, k int) bool {
|
||||
seqLen := 1
|
||||
prevSeqTail := -2
|
||||
for i := 1; i < len(nums); i++ {
|
||||
if nums[i-1] < nums[i] {
|
||||
seqLen++
|
||||
if seqLen == k && prevSeqTail == i-k {
|
||||
return true
|
||||
}
|
||||
if seqLen >= 2*k {
|
||||
return true
|
||||
}
|
||||
} else {
|
||||
if seqLen >= k {
|
||||
prevSeqTail = i - 1
|
||||
}
|
||||
seqLen = 1
|
||||
}
|
||||
}
|
||||
return k == 1
|
||||
}
|
||||
|
||||
var _ = hasIncreasingSubarrays
|
||||
28
solutions/33/q3354/solution.go
Normal file
28
solutions/33/q3354/solution.go
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
package q3354
|
||||
|
||||
func countValidSelections(nums []int) int {
|
||||
sumR := 0
|
||||
for i := range nums {
|
||||
sumR += nums[i]
|
||||
}
|
||||
|
||||
sumL := 0
|
||||
nValid := 0
|
||||
for i := range nums {
|
||||
sumR -= nums[i]
|
||||
if i > 0 {
|
||||
sumL += nums[i-1]
|
||||
}
|
||||
if nums[i] == 0 {
|
||||
diff := sumL - sumR
|
||||
if diff == 0 {
|
||||
nValid += 2
|
||||
} else if diff*diff == 1 {
|
||||
nValid++
|
||||
}
|
||||
}
|
||||
}
|
||||
return nValid
|
||||
}
|
||||
|
||||
var _ = countValidSelections
|
||||
12
solutions/33/q3370/solution.go
Normal file
12
solutions/33/q3370/solution.go
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package q3370
|
||||
|
||||
func smallestNumber(n int) int {
|
||||
i := 0
|
||||
for n > 0 {
|
||||
n >>= 1
|
||||
i++
|
||||
}
|
||||
return 1<<i - 1
|
||||
}
|
||||
|
||||
var _ = smallestNumber
|
||||
23
solutions/34/q3432/solution.go
Normal file
23
solutions/34/q3432/solution.go
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
package q3432
|
||||
|
||||
func countPartitions(nums []int) int {
|
||||
sumR := 0
|
||||
for i := range nums {
|
||||
sumR += nums[i]
|
||||
}
|
||||
|
||||
sumL := 0
|
||||
cnt := 0
|
||||
|
||||
for i := range len(nums) - 1 {
|
||||
sumL += nums[i]
|
||||
sumR -= nums[i]
|
||||
if (sumL-sumR)%2 == 0 {
|
||||
cnt++
|
||||
}
|
||||
}
|
||||
|
||||
return cnt
|
||||
}
|
||||
|
||||
var _ = countPartitions
|
||||
18
solutions/34/q3461/solution.go
Normal file
18
solutions/34/q3461/solution.go
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
package q3461
|
||||
|
||||
func hasSameDigits(s string) bool {
|
||||
buf := []byte(s)
|
||||
for i := range buf {
|
||||
buf[i] = buf[i] - '0'
|
||||
}
|
||||
|
||||
for len(buf) > 2 {
|
||||
for i := range len(buf) - 1 {
|
||||
buf[i] = (buf[i] + buf[i+1]) % 10
|
||||
}
|
||||
buf = buf[:len(buf)-1]
|
||||
}
|
||||
return buf[0] == buf[1]
|
||||
}
|
||||
|
||||
var _ = hasSameDigits
|
||||
11
solutions/35/q3512/solution.go
Normal file
11
solutions/35/q3512/solution.go
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
package q3512
|
||||
|
||||
func minOperations(nums []int, k int) int {
|
||||
sum := 0
|
||||
for i := range nums {
|
||||
sum += nums[i]
|
||||
}
|
||||
return sum % k
|
||||
}
|
||||
|
||||
var _ = minOperations
|
||||
21
solutions/35/q3516/solution.go
Normal file
21
solutions/35/q3516/solution.go
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package q3516
|
||||
|
||||
func abs(n int) int {
|
||||
if n < 0 {
|
||||
return -n
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
func findClosest(x int, y int, z int) int {
|
||||
d1, d2 := abs(x-z), abs(y-z)
|
||||
switch {
|
||||
case d1 == d2:
|
||||
return 0
|
||||
case d1 < d2:
|
||||
return 1
|
||||
}
|
||||
return 2
|
||||
}
|
||||
|
||||
var _ = findClosest
|
||||
22
solutions/35/q3541/solution.go
Normal file
22
solutions/35/q3541/solution.go
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
package q3541
|
||||
|
||||
func maxFreqSum(s string) int {
|
||||
freqs := [26]int{}
|
||||
for i := range len(s) {
|
||||
freqs[s[i]-'a']++
|
||||
}
|
||||
|
||||
max1, max2 := 0, 0
|
||||
for i, freq := range freqs {
|
||||
c := byte(i + 'a')
|
||||
switch c {
|
||||
case 'a', 'e', 'i', 'o', 'u':
|
||||
max1 = max(max1, freq)
|
||||
default:
|
||||
max2 = max(max2, freq)
|
||||
}
|
||||
}
|
||||
return max1 + max2
|
||||
}
|
||||
|
||||
var _ = maxFreqSum
|
||||
58
solutions/36/q3606/solution.go
Normal file
58
solutions/36/q3606/solution.go
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package q3606
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func isValidCode(code string) bool {
|
||||
for i := range len(code) {
|
||||
c := code[i]
|
||||
if (c < 'a' || c > 'z') &&
|
||||
(c < 'A' || c > 'Z') &&
|
||||
(c < '0' || c > '9') &&
|
||||
c != '_' {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return len(code) > 0
|
||||
}
|
||||
|
||||
func businessToI(b string) int8 {
|
||||
switch b {
|
||||
case "electronics":
|
||||
return 1
|
||||
case "grocery":
|
||||
return 2
|
||||
case "pharmacy":
|
||||
return 3
|
||||
case "restaurant":
|
||||
return 4
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
func validateCoupons(code []string, businessLine []string, isActive []bool) []string {
|
||||
validCodes := []int{}
|
||||
for i := range code {
|
||||
if isValidCode(code[i]) && businessToI(businessLine[i]) > 0 && isActive[i] {
|
||||
validCodes = append(validCodes, i)
|
||||
}
|
||||
}
|
||||
|
||||
slices.SortFunc(validCodes, func(a, b int) int {
|
||||
ba, bb := businessToI(businessLine[a]), businessToI(businessLine[b])
|
||||
if ba != bb {
|
||||
return int(ba - bb)
|
||||
}
|
||||
return strings.Compare(code[a], code[b])
|
||||
})
|
||||
|
||||
ret := make([]string, 0, len(validCodes))
|
||||
for _, i := range validCodes {
|
||||
ret = append(ret, code[i])
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
var _ = validateCoupons
|
||||
31
solutions/7/q712/solution.go
Normal file
31
solutions/7/q712/solution.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package q712
|
||||
|
||||
func minimumDeleteSum(s1 string, s2 string) int {
|
||||
mds := make([][]int, len(s1)+1)
|
||||
for i := range mds {
|
||||
mds[i] = make([]int, len(s2)+1)
|
||||
}
|
||||
|
||||
for i := range len(s1) {
|
||||
mds[i+1][0] = mds[i][0] + int(s1[i])
|
||||
}
|
||||
for i := range len(s2) {
|
||||
mds[0][i+1] = mds[0][i] + int(s2[i])
|
||||
}
|
||||
|
||||
for i1 := 1; i1 <= len(s1); i1++ {
|
||||
for i2 := 1; i2 <= len(s2); i2++ {
|
||||
if s1[i1-1] == s2[i2-1] {
|
||||
mds[i1][i2] = mds[i1-1][i2-1]
|
||||
} else {
|
||||
mds[i1][i2] = min(
|
||||
mds[i1-1][i2]+int(s1[i1-1]),
|
||||
mds[i1][i2-1]+int(s2[i2-1]),
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
return mds[len(s1)][len(s2)]
|
||||
}
|
||||
|
||||
var _ = minimumDeleteSum
|
||||
17
solutions/7/q717/solution.go
Normal file
17
solutions/7/q717/solution.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package q717
|
||||
|
||||
func isOneBitCharacter(bits []int) bool {
|
||||
if len(bits) == 1 {
|
||||
return true
|
||||
}
|
||||
if bits[len(bits)-2] == 0 {
|
||||
return true
|
||||
}
|
||||
cnt1 := 0
|
||||
for i := len(bits) - 2; i >= 0 && bits[i] == 1; i-- {
|
||||
cnt1++
|
||||
}
|
||||
return cnt1%2 == 0
|
||||
}
|
||||
|
||||
var _ = isOneBitCharacter
|
||||
24
solutions/8/q812/solution.go
Normal file
24
solutions/8/q812/solution.go
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
package q812
|
||||
|
||||
func area2(a, b []int) float64 {
|
||||
return float64((a[1]+b[1])*(b[0]-a[0])) / 2
|
||||
}
|
||||
|
||||
func largestTriangleArea(points [][]int) float64 {
|
||||
var maxArea float64
|
||||
|
||||
for a := 0; a < len(points)-2; a++ {
|
||||
for b := a + 1; b < len(points)-1; b++ {
|
||||
for c := b + 1; c < len(points); c++ {
|
||||
area := area2(points[a], points[b]) + area2(points[b], points[c]) + area2(points[c], points[a])
|
||||
if area < 0 {
|
||||
area = -area
|
||||
}
|
||||
maxArea = max(maxArea, area)
|
||||
}
|
||||
}
|
||||
}
|
||||
return maxArea
|
||||
}
|
||||
|
||||
var _ = largestTriangleArea
|
||||
16
solutions/9/q944/solution.go
Normal file
16
solutions/9/q944/solution.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package q944
|
||||
|
||||
func minDeletionSize(strs []string) int {
|
||||
nDel := 0
|
||||
for col := range len(strs[0]) {
|
||||
for row := 1; row < len(strs); row++ {
|
||||
if strs[row][col] < strs[row-1][col] {
|
||||
nDel++
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
return nDel
|
||||
}
|
||||
|
||||
var _ = minDeletionSize
|
||||
16
solutions/9/q976/solution.go
Normal file
16
solutions/9/q976/solution.go
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
package q976
|
||||
|
||||
import "slices"
|
||||
|
||||
func largestPerimeter(nums []int) int {
|
||||
slices.Sort(nums)
|
||||
|
||||
for i := len(nums) - 1; i > 1; i-- {
|
||||
if nums[i-1]+nums[i-2] > nums[i] {
|
||||
return nums[i] + nums[i-1] + nums[i-2]
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
var _ = largestPerimeter
|
||||
Loading…
Add table
Add a link
Reference in a new issue