add new solutions

This commit is contained in:
Yiyang Kang 2026-03-19 15:29:54 +09:00
parent f297e11859
commit 4720cbefc4
8 changed files with 290 additions and 0 deletions

View file

@ -0,0 +1,22 @@
// Package q829 implements a solution for https://leetcode.com/problems/consecutive-numbers-sum/
package q829
import "math"
func consecutiveNumbersSum(n int) int {
maxN := int(math.Sqrt(float64(n) * 2))
for maxN*(maxN+1)/2 <= n {
maxN++
}
cnt := 1
for nNums := maxN - 1; nNums > 1; nNums-- {
start := (n*2/nNums + 1 - nNums) / 2
if (start+start+nNums-1)*nNums/2 == n {
cnt++
}
}
return cnt
}
var _ = consecutiveNumbersSum