We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
- Goodland Electricity
- Discussions
Goodland Electricity
Goodland Electricity
Sort by
recency
|
52 Discussions
|
Please Login in order to post a comment
!/bin/python3
import math import os import random import re import sys import bisect #
Complete the 'pylons' function below.
#
The function is expected to return an INTEGER.
The function accepts following parameters:
1. INTEGER k
2. INTEGER_ARRAY arr
#
def pylons(k, arr): n=len(arr) List=[] for i in range(n): if arr[i]==1: List.append(i) end=-1 ans=0 istart=0 while end
if name == 'main': fptr = open(os.environ['OUTPUT_PATH'], 'w')
Java
C#:
My approach for this was essentially: given a city position, find the optimal point to place a power plant for that city position. Once I find a place for it, move to the furthest out city position not covered by the power plant I just placed, and do that again. Continue doing that until I reach the end of the array.
To find the optimal position for a given city, I use the k value to go as far out as possible (taking care to remain in-bounds of the array). I check if the position can contain a power plant. If it can, I use that position. If not, I reduce the value by one and continue. The lowest value is the previously placed power plant + 1 (or 0 if none has been placed yet).