funcmaskPII(s string)string { var ans string if strings.Contains(s, "@") { s = strings.ToLower(s) ans = s[0:1] + "*****" for i := 1; i < len(s); i++ { if s[i] == '@' { ans += s[i-1:] break } } } else { var tmp string for i := 0; i < len(s); i++ { if unicode.IsDigit(rune(s[i])) { tmp += string(s[i]) } }
ans = tmp[len(tmp)-4:] switchlen(tmp)-10 { case0: ans = "***-***-" + ans case1: ans = "+*-***-***-" + ans case2: ans = "+**-***-***-" + ans case3: ans = "+***-***-***-" + ans default: } }
funcminScoreTriangulation(values []int)int { m := len(values) dp := make([][]int, m) for i := 0; i < m; i++ { dp[i] = make([]int, m) }
// 宽度 for g := 2; g < m; g++ { // i for i := 0; i < m-g; i++ { // j = i + g j := g + i dp[i][j] = math.MaxInt for k := i + 1; k < j; k++ { dp[i][j] = min(dp[i][j], dp[i][k]+values[i]*values[k]*values[j]+dp[k][j]) } } } return dp[0][m-1] }
funcmin(i int, i2 int)int { if i < i2 { return i } return i2 }
funcmergeStones(stones []int, k int)int { m := len(stones)
if (m-1)%(k-1) != 0 { return-1 }
sum := make([][]int, m) for i := 0; i < m; i++ { sum[i] = make([]int, m) sum[i][i] = stones[i] for j := i + 1; j < m; j++ { sum[i][j] = sum[i][j-1] + stones[j] } }
dp := make([][][]int, m) for i := m - 1; i >= 0; i-- { dp[i] = make([][]int, m) dp[i][i] = make([]int, k+1) dp[i][i][1] = 0 for j := i + 1; j < m; j++ { dp[i][j] = make([]int, k+1) for p := 2; p < k+1; p++ { dp[i][j][p] = math.MaxInt for l := i; l < j; l += k - 1 { dp[i][j][p] = min(dp[i][j][p], dp[i][l][1]+dp[l+1][j][p-1]) } } dp[i][j][1] = dp[i][j][k] + sum[i][j] } }
return dp[0][m-1][1] }
funcmin(i int, i2 int)int { if i < i2 { return i } return i2 }
2023年04月05日
2427. 公因子的数目
解法1:按照题目要求,从1便利到a或者b其中较小的数,求余即可
1 2 3 4 5 6 7 8 9 10 11
funccommonFactors(a int, b int)int { var ans int
for i := 1; i <= a && i <= b ; i++ { if a %i == 0 && b %i == 0 { ans ++ } }
funcbaseNeg2(n int)string { if n == 0 { return"0" } var ans []string k := -1 for n != 0 { if n%2 == 0 { ans = append(ans, "0") } else { ans = append(ans, "1") n += k } k *= -1 n >>= 1 } for i := 0; i < len(ans)/2; i++ { ans[i], ans[len(ans)-1-i] = ans[len(ans)-1-i], ans[i] }
funcnumMovesStonesII(stones []int) []int { sort.Ints(stones) m := len(stones)
s1 := stones[m-1] - stones[0] + 1 - m s2 := min(stones[m-1]-stones[m-2]-1, stones[1]-stones[0]-1) max := s1 - s2
mi := max for i, j := 0, 0; i < m-1; i++ { for j+1 < m && stones[j+1]-stones[i]+1 <= m { j++ } cost := m - (j - i + 1) if j-i+1 == m-1 && stones[j]-stones[i]+1 == m-1 { cost = 2 } mi = min(mi, cost) }
return []int{mi, s1 - s2} }
funcmin(i, i2 int)int { if i < i2 { return i } return i2 }
funcsmallestSufficientTeam(reqSkills []string, people [][]string) (ans []int) { // 将人员的能力压缩成二进制 peopleSkill := make([]int, len(people)) for i, p := range people { var tmp int for _, skill := range p { for k, reqSkill := range reqSkills { if reqSkill == skill { tmp += 1 << k break } } } peopleSkill[i] = tmp } //fmt.Println(peopleSkill)
funccamelMatch(queries []string, pattern string) []bool { ans := make([]bool, len(queries))
for i, query := range queries { var m, n int for m < len(query) && n < len(pattern) { if query[m] == pattern[n] { m++ n++ } else { if unicode.IsLower(rune(query[m])) { m++ } else { break } } } for m < len(query) && unicode.IsLower(rune(query[m])) { m++ } if n == len(pattern) && m == len(query) { ans[i] = true } }
type MajorityChecker struct { arr []int loc map[int][]int }
funcConstructor(arr []int) MajorityChecker { rand.Seed(time.Now().UnixNano()) loc := map[int][]int{} for i, x := range arr { loc[x] = append(loc[x], i) } return MajorityChecker{arr, loc} }
func(mc *MajorityChecker) Query(left, right, threshold int) int { length := right - left + 1 for i := 0; i < 20; i++ { x := mc.arr[rand.Intn(right-left+1)+left] pos := mc.loc[x] occ := sort.SearchInts(pos, right+1) - sort.SearchInts(pos, left) if occ >= threshold { return x } if occ*2 >= length { break } } return-1 }
funcstoreWater(bucket []int, vat []int)int { n := len(bucket) maxk := 0 for _, v := range vat { if v > maxk { maxk = v } } if maxk == 0 { return0 } res := math.MaxInt32 for k := 1; k <= maxk && k < res; k++ { t := 0 for i := 0; i < n; i++ { t += max(0, (vat[i] + k - 1) / k - bucket[i]) } res = min(res, t+k) } return res }
funcmax(x, y int)int { if x > y { return x } return y }
funcmin(x, y int)int { if x < y { return x } return y }
funcfrogPosition(n int, edges [][]int, t int, target int)float64 { G := make([][]int, n+1) for i := 0; i <= n; i++ { G[i] = make([]int, 0) } for _, e := range edges { G[e[0]] = append(G[e[0]], e[1]) G[e[1]] = append(G[e[1]], e[0]) } seen := make([]bool, n+1) return dfs(G, seen, 1, t, target) }
funcdfs(G [][]int, seen []bool, i int, t int, target int)float64 { nxt := len(G[i]) if i > 1 { nxt -= 1 } if t == 0 || nxt == 0 { if i == target { return1.0 } else { return0.0 } } seen[i] = true ans := 0.0 for _, j := range G[i] { if !seen[j] { ans += dfs(G, seen, j, t - 1, target) } } return ans / float64(nxt) }
2023年04月25日
2418. 按身高排序
解法1: sort排序即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funcsortPeople(names []string, heights []int) []string { n := len(names) indices := make([]int, n) for i := 0; i < n; i++ { indices[i] = i } sort.Slice(indices, func(i, j int)bool { return heights[indices[j]] < heights[indices[i]] }) res := make([]string, n) for i := 0; i < n; i++ { res[i] = names[indices[i]] } return res }
2023年04月26日
1031. 两个非重叠子数组的最大和
解法1: 计算左边字段和右边字段相加的最大和,通过中间的线往右移动,进行比较
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
funcmaxSumTwoNoOverlap(nums []int, firstLen, secondLen int) (ans int) { n := len(nums) s := make([]int, n+1) for i, x := range nums { s[i+1] = s[i] + x // 计算 nums 的前缀和 } maxSumA, maxSumB := 0, 0 for i := firstLen + secondLen; i <= n; i++ { maxSumA = max(maxSumA, s[i-secondLen]-s[i-secondLen-firstLen]) maxSumB = max(maxSumB, s[i-firstLen]-s[i-firstLen-secondLen]) ans = max(ans, max(maxSumA+s[i]-s[i-secondLen], // 左 a 右 b maxSumB+s[i]-s[i-firstLen])) // 左 b 右 a } return }
funcmax(a, b int)int { if b > a { return b }; return a }
funclongestStrChain(words []string)int { cnt := map[string]int{} sort.Slice(words, func(i, j int)bool { returnlen(words[i]) < len(words[j]) }) res := 0 for _, word := range words { cnt[word] = 1 for i := range word { prev := word[:i] + word[i + 1:] if j := cnt[prev] + 1; j > cnt[word] { cnt[word] = j } } if cnt[word] > res { res = cnt[word] } } return res }
funcequalFrequency(word string)bool { charCount := [26]int{} for _, c := range word { charCount[c - 'a']++ } for i := 0; i < 26; i++ { if charCount[i] == 0 { continue } charCount[i]-- frequency := make(map[int]bool) for _, f := range charCount { if f > 0 { frequency[f] = true } } iflen(frequency) == 1 { returntrue } charCount[i]++ } returnfalse }