Leetcode Note: Bulb Switcher

Source: Internet
Author: User
Tags rounds

Leetcode Note: Bulb Switcher

I. Description

There are n bulbs that are initially off. you first turn on all the bulbs. then, you turn off every second bulb. on the third round, you toggle every third bulb (turning on if it's off or turning off if it's on ). for the nth round, you only toggle the last bulb. find how many bulbs are on after n rounds.

Example:

Given n = 3.

At first, the three bulbs are [off, off, off].After first round, the three bulbs are [on, on, on].After second round, the three bulbs are [on, off, on].After third round, the three bulbs are [on, off, off]. 

So you shoshould return 1, because there is only one bulb is on.

Ii. Question Analysis

N bulbs are in the closed state. First open all the bulbs (first round ). Then, extinguish all the bulbs with multiples of 2. In the third round, switch all the bulbs whose serial numbers are multiples of 3 (turn them off when they are on, and turn them on when they are closed ). In the nth round, you only switch the last light bulb. Several bulbs are still on after n rounds of computing.

Take a look at the following rules:

1st bulbs: a multiple of 1, will change the status once:off -> on

2nd bulbs: a multiple of 1, a multiple of 2, will change the status twice:off -> on -> off

3rd bulbs: a multiple of 1, a multiple of 3, will change the status twice:off -> on -> off

4th bulbs: multiples of 1, multiples of 2, and multiples of 4 change the status three times:off -> on -> off -> on

5th bulbs: multiples of 1 and multiples of 5 change the status twice:off -> on -> off

6th bulbs: multiples of 1, multiples of 2, multiples of 3, multiples of 6, will change 4 times:off -> on -> off -> on -> off

7th bulbs: multiples of 1 and multiples of 7 change the status twice:off -> on -> off

8th bulbs: multiples of 1, multiples of 2, multiples of 4, multiples of 8, will change 4 times:off -> on -> off -> on -> off

9th bulbs: multiples of 1, multiples of 2, and multiples of 4 change the status three times:off -> on -> off -> on

......

The above description shows thatn = 2,3,5,6,7,8To find an integer multiple of a number, always find a symmetric integer multiple, such1 * 2, There will certainly be one2 * 1. Therefore, the bulb with these numbers will change to an even number, and the final result must beoff.

Only whenn = 1,4,9This type of complete bulb changes an odd number and the final bulb willon.

As long as you can draw the above conclusions, you can solve the problem with a code sentence.

Iii. Sample Code

class Solution {public:    int bulbSwitch(int n) {        return sqrt(n);    }};

Iv. Summary

For some combination of algorithm questions, finding a rule is more important than directly implementing it.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.