319.bulb-switcher.go 319 B

123456789101112131415161718
  1. func bulbSwitchSlow(n int) (res int) { // Brute search, TLE
  2. bulbs := make([]bool, n)
  3. for i := 1; i <= n; i++ {
  4. for j := i-1; j < n; j += i {
  5. bulbs[j] = !bulbs[j]
  6. }
  7. }
  8. for i := 0; i < n; i++ {
  9. if bulbs[i] {
  10. res++
  11. }
  12. }
  13. return
  14. }
  15. func bulbSwitch(n int) int { // Math
  16. return int(math.Sqrt(float64(n)))
  17. }