funcfindMinimumTime(tasks [][]int)int { slices.SortFunc(tasks, func(a, b []int)int { return a[1] - b[1] }) run := make([]bool, tasks[len(tasks)-1][1]+1) res := 0 for _, task := range tasks { start, end, d := task[0], task[1], task[2] for _, b := range run[start : end+1] { if b { d-- } } for i := end; d > 0; i-- { if !run[i] { run[i] = true d-- res++ } } } return res }
funcmaxProfitAssignment(difficulty []int, profit []int, worker []int)int { n := len(difficulty) jobs := make([][]int, n) for i, d := range difficulty { jobs[i] = []int{d, profit[i]} } sort.Slice(jobs, func(i, j int)bool { return jobs[i][0] < jobs[j][0] }) sort.Ints(worker) i, maxP, res := 0, 0, 0 for _, w := range worker { for i < n && jobs[i][0] <= w { // 找到难度小于等于当前工人能力的工作 maxP = max(maxP, jobs[i][1]) i++ // } res += maxP } return res }
funcmaxDivScore(nums []int, divisors []int)int { res := 0 maxCnt := -1 for _, d := range divisors { cnt := 0 for _, n := range nums { if n%d == 0 { cnt++ } } if cnt > maxCnt || cnt == maxCnt && d < res { maxCnt = cnt res = d } } return res }
funcmaxDivScore(nums []int, divisors []int)int { res := 0 maxCnt := -1 sort.Slice(nums, func(i, j int)bool { return nums[i] > nums[j] }) for _, d := range divisors { cnt := 0 for _, n := range nums { if n < d { break } if n%d == 0 { cnt++ } } if cnt > maxCnt || cnt == maxCnt && d < res { maxCnt = cnt res = d } } return res }
funclongestAwesome(s string)int { const D = 10 n := len(s) pos := [1 << D]int{} for i := range pos { pos[i] = n // 没有前缀和默认用最大值 } pos[0] = -1 pre := 0// 因为是遍历的同时计算,所以pre即pre[j] res := 0 for i, c := range s { pre ^= 1 << (c - '0') for d := 0; d < D; d++ { res = max(res, i-pos[pre^(1<<d)]) // 奇数pre[i]=pre[j]^(1<<d) } res = max(res, i-pos[pre]) // 偶数 pre[i]=pre[j] if pos[pre] == n { // 标记前缀和首次出现位置 pos[pre] = i } } return res }