funcgroupThePeople(groupSizes []int) [][]int { ans := make([][]int, 0) group := make(map[int][]int) for i, v := range groupSizes { group[v] = append(group[v], i) iflen(group[v]) == v { ans = append(ans, group[v]) group[v] = []int{} } } return ans }
funcmaxScore(s string)int { left := 0 right := 0 len := len(s) max := -1 for i := 0; i < len; i++ { if s[i] == '0' { left++ } else { left-- right++ } if left > max && i != (len-1) { max = left } } return right + max }
func(this *OrderedStream)Insert(idKey int, value string) []string { ans := make([]string, 0) this.elements[idKey] = value i := 0 for i = this.ptr; i < len(this.elements); i++ { iflen(this.elements[i]) == 0 { break } } ans = append(ans, this.elements[this.ptr:i]...) this.ptr = i return ans }
review
daily-1925.统计平方和三元组的数目(algorithms Easy)
分析
纯水题。
代码块
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
funccountTriples(n int)int { ans := 0 for a := 1; a <= n; a++ { for b := a + 1; b <= n; b++ { c := int(math.Sqrt(float64(a*a + b*b))) if c > n { continue } if c*c == a*a+b*b { ans++ } } } return ans * 2 }
funcmaxEqualFreq(nums []int)int { var check func()bool hashmap := make(map[int]int) check = func()bool { max := -1 maxtimes := -1 min := -1 mintimes := -1 for _, v := range hashmap { if v == 0 { continue } if max == -1 { max = v maxtimes = 1 } elseif max == v { maxtimes++ } elseif min == -1 { min = v mintimes = 1 } elseif min == v { mintimes++ } else { returnfalse } } temp := 0 if max < min { temp = max max = min min = temp temp = maxtimes maxtimes = mintimes mintimes = temp } fmt.Println(max, maxtimes, min, mintimes) if min == -1 { if max == 1 { returntrue } elseif maxtimes == 1 { returntrue } else { returnfalse } } if maxtimes == 1 && max-min == 1 { returntrue } if mintimes == 1 && min == 1 { returntrue } returnfalse } for _, v := range nums { hashmap[v]++ } for i := len(nums) - 1; i >= 0; i-- { if check() { return i + 1 } hashmap[nums[i]]-- } return1 }
review
实际上复杂度处理不难,适当剪枝很轻松。主要是情况种类比较多,容易遗漏。(3,4情况都没有考虑到)
220819
daily-1450.在既定时间做作业的学生人数(algorithms Easy)
分析
水题。
代码块
1 2 3 4 5 6 7 8 9
funcbusyStudent(startTime []int, endTime []int, queryTime int)int { ans := 0 for i, _ := range startTime { if startTime[i] <= queryTime && queryTime <= endTime[i] { ans++ } } return ans }