Goodland Electricity
Problem Statement :
Goodland is a country with a number of evenly spaced cities along a line. The distance between adjacent cities is unit. There is an energy infrastructure project planning meeting, and the government needs to know the fewest number of power plants needed to provide electricity to the entire list of cities. Determine that number. If it cannot be done, return -1. You are given a list of city data. Cities that may contain a power plant have been labeled . Others not suitable for building a plant are labeled . Given a distribution range of , find the lowest number of plants that must be built such that all cities are served. The distribution range limits supply to cities where distance is less than k. Function Description Complete the pylons function in the editor below. pylons has the following parameter(s): int k: the distribution range int arr[n]: each city's suitability as a building site Returns int: the minimum number of plants required or -1 Input Format The first line contains two space-separated integers and , the number of cities in Goodland and the plants' range constant. The second line contains space-separated binary integers where each integer indicates suitability for building a plant. Output Format Print a single integer denoting the minimum number of plants that must be built so that all of Goodland's cities have electricity. If this is not possible for the given value of k , print -1.
Solution :
Solution in C :
In C :
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
int main() {
int n,k,s,i,j,a,f,z=0;
scanf("%d %d",&n,&k);
int arr[n];
int brr[n];
for(i=0;i<n;i++){scanf("%d",&arr[i]);brr[i]=0;}
s=0;a=0;f=0;
while(a<n)
{
// printf("%d\n",a);
if(a+k-1>n-1&&f==0){a=n-k;f=1;}
for(i=a+k-1;i>=0;i--){
z++;
if(z>100000){printf("-1\n");return 0;}
if(arr[i]==2){printf("-1\n");return 0;}
if(arr[i]==1){
for(j=0;j<k;j++){
if(i+j<n)brr[i+j]=1;
if(i-j>=0)brr[i-j]=1;
}
arr[i]=2;a=i+k;break;}
}
}
for(i=0;i<n;i++){if(brr[i]!=1){printf("-1\n");return 0;}}
s=0;
for(i=0;i<n;i++){if(arr[i]==2){s++;}}
printf("%d\n",s);
/* Enter your code here. Read input from STDIN. Print output to STDOUT */
return 0;
}
Solution in C++ :
In C++ :
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if(x < y) x = y; }
int main() {
int n; int k;
while(~scanf("%d%d", &n, &k)) {
vector<int> a(n);
for(int i = 0; i < n; ++ i)
scanf("%d", &a[i]);
vi v;
rep(i, n) if(a[i] != 0)
v.push_back(i);
int j = 0;
int ans = 0;
for(int i = 0; i < n; ) {
for(; j + 1 < (int)v.size() && v[j + 1] <= i + k - 1; ++ j);
if(j == v.size() || i + k - 1 < v[j]) {
ans = INF;
break;
}
++ ans;
i = v[j] + k;
++ j;
}
printf("%d\n", ans == INF ? -1 : ans);
}
return 0;
}
Solution in Java :
In Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] towers = new int[n];
for (int i = 0; i < n; i++) {
towers[i] = sc.nextInt();
}
int i = 0;
int maxTower = k-1;
int ans = 0;
while (true) {
int nextTower = -1;
while (i <= maxTower && i < n) {
if (towers[i] == 1) {
nextTower = i;
}
i++;
}
if (nextTower == -1) {
System.out.println(-1);
return;
}
ans++;
i = nextTower + 1;
maxTower = nextTower+2*k-1;
if (nextTower+k>=n)
break;
}
System.out.println(ans);
}
}
Solution in Python :
In Python3 :
n, k = map(int, input().split())
tower = list(map(int, input().split())) # 0 - no tower; 1 - tower
def voisins(i):
return range(min(n-1, i+k-1), max(-1, i-k), -1)
count = 0
past_fourni = 0
while past_fourni < n:
for i in voisins(past_fourni):
if tower[i]:
past_fourni = i + k
count += 1
break
else:
count = -1
break
print(count)
View More Similar Problems
Find Merge Point of Two Lists
This challenge is part of a tutorial track by MyCodeSchool Given pointers to the head nodes of 2 linked lists that merge together at some point, find the node where the two lists merge. The merge point is where both lists point to the same node, i.e. they reference the same memory location. It is guaranteed that the two head nodes will be different, and neither will be NULL. If the lists share
View Solution →Inserting a Node Into a Sorted Doubly Linked List
Given a reference to the head of a doubly-linked list and an integer ,data , create a new DoublyLinkedListNode object having data value data and insert it at the proper location to maintain the sort. Example head refers to the list 1 <-> 2 <-> 4 - > NULL. data = 3 Return a reference to the new list: 1 <-> 2 <-> 4 - > NULL , Function Description Complete the sortedInsert function
View Solution →Reverse a doubly linked list
This challenge is part of a tutorial track by MyCodeSchool Given the pointer to the head node of a doubly linked list, reverse the order of the nodes in place. That is, change the next and prev pointers of the nodes so that the direction of the list is reversed. Return a reference to the head node of the reversed list. Note: The head node might be NULL to indicate that the list is empty.
View Solution →Tree: Preorder Traversal
Complete the preorder function in the editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's preorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the preOrder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the tree's
View Solution →Tree: Postorder Traversal
Complete the postorder function in the editor below. It received 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's postorder traversal as a single line of space-separated values. Input Format Our test code passes the root node of a binary tree to the postorder function. Constraints 1 <= Nodes in the tree <= 500 Output Format Print the
View Solution →Tree: Inorder Traversal
In this challenge, you are required to implement inorder traversal of a tree. Complete the inorder function in your editor below, which has 1 parameter: a pointer to the root of a binary tree. It must print the values in the tree's inorder traversal as a single line of space-separated values. Input Format Our hidden tester code passes the root node of a binary tree to your $inOrder* func
View Solution →