Java exercise

Joined
Nov 5, 2024
Messages
1
Reaction score
0
Can anyone solve me this
We are given a street with N possible positions on which we can put a light. A single light will illuminate its own position and 2 more positions to the left and 2 more to the right of it. Our task is to illuminate the street with the minimum possible lights. Not all possible positions must contain light.
Input: The first number in the input is the number of possible positions to put a light bulb N and the length of the street M, then in the next line are the possible positions on which we can put a light.
Output: The minimum lights we need to illuminate the street.
For example:
Input
Result
n=5 m=5
for posisions 1 2 3 4 5
answer 1 lamp
n=3 m=10
for posisions 3 8 9
answer 2 lamp
 
Joined
Nov 22, 2023
Messages
16
Reaction score
0
Sorry, I don't know, I am also want to ask the code of Shoelace Program, se my thread

I think that exercis would use selection
 
Joined
Sep 21, 2022
Messages
186
Reaction score
26
My first thought was to test every combination of switches. Find the combination that lights the whole area and uses the least number of lit bulbs.

Which would work, but feels like overkill.

On the other hand, I think this would work.

Start in position 1.

Walk forward until your position is dark. Lets call that position x.

You need to switch on a light, at position x, x+1 or x+2.

If there is a bulb at x+2, its the best choice, because it doesn't waste light behind you, and reaches the furthest in front of you.

If there is no bulb at x+2, try x+1, if there is no bulb at x+1, you must use x.

Switch on the best bulb and continue walking, until you're in darkness again.

Repeat.

I could be wrong.
 
Joined
Sep 21, 2022
Messages
186
Reaction score
26
I tried a few tests, and I found a case where the light you need to switch on is behind you.

001100

So you need to switch on a light, at position x+2, x+1, x, x-1, or x-2. Whichever is greatest.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,039
Messages
2,570,376
Members
47,031
Latest member
AndreBucki

Latest Threads

Top