Turn Off the Lights – HackerRank Solution Java , Python 3, Python 2 , C , C++, Best and Optimal Solutions , All you need.
Solutions of Algorithms Data Structures Hard HackerRank:
Here are all the Solutions of Hard , Advanced , Expert Algorithms of Data Structure of Hacker Rank , Leave a comment for similar posts
C++ Turn Off the Lights HackerRank Solution
/*
*/
#pragma comment(linker, "/STACK:16777216")
#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <string>
#include <complex>
#include <math.h>
#include <set>
#include <vector>
#include <map>
#include <queue>
#include <stdio.h>
#include <stack>
#include <algorithm>
#include <list>
#include <ctime>
#include <memory.h>
#include <assert.h>
#define y0 sdkfaslhagaklsldk
#define y1 aasdfasdfasdf
#define yn askfhwqriuperikldjk
#define j1 assdgsdgasghsf
#define tm sdfjahlfasfh
#define lr asgasgash
#define norm asdfasdgasdgsd
#define eps 1e-9
#define M_PI 3.141592653589793
#define bs 1000000007
#define bsize 350
using namespace std;
const int INF = 1e9;
const int N = 200031;
int n, k, ar[N];
long long solve(int start)
{
long long res = 0;
for (int i = start; i <= n; i += 2 * k + 1)
{
res += ar[i];
if (i + k + 1 <= n&&i + k * 2 + 1 > n)
return 1e18;
}
return res;
}
bool good_mask(int mask)
{
int ar[25];
for (int i = 0; i < n; i++)
{
ar[i] = 1;
}
for (int i = 0; i < n; i++)
{
if (mask&(1 << i))
{
for (int j = i - k; j <= i + k; j++)
{
if (j >= 0 && j < n)
ar[j] ^= 1;
}
}
}
for (int i = 0; i < n; i++)
{
if (ar[i])
return 0;
}
return 1;
}
int ans_mask;
int main(){
//freopen("fabro.in","r",stdin);
//freopen("fabro.out","w",stdout);
//freopen("F:/in.txt", "r", stdin);
//freopen("F:/output.txt", "w", stdout);
ios_base::sync_with_stdio(0);
//cin.tie(0);
while (true)
{
cin >> n >> k;
for (int i = 1; i <= n; i++)
{
cin >> ar[i];
//ar[i] = rand() % 10;
}
long long ans = 1e18;
/*for (int mask = 0; mask < (1 << n); mask++)
{
if (good_mask(mask))
{
int here = 0;
for (int i = 0; i < n; i++)
{
if (mask&(1 << i))
here += ar[i + 1];
}
if (here < ans)
ans = min(ans, 0ll + here),
ans_mask = mask;
}
}*/
long long ans2 = 1e18;
for (int F = 1; F <= k + 1&&F<=n; F++)
{
ans2 = min(ans2, solve(F));
}
/*if (ans != ans2)
{
cout << "!" << endl;
for (int i = 1; i <= n; i++)
{
cout << ar[i] << " ";
}
cout << endl;
cout << ans << " " << ans2 << " " << ans_mask << endl;
while (true);
}
else
cout << "OK" << endl;*/
cout << ans2 << endl;
return 0;
}
cin.get(); cin.get();
return 0;
}
Java Turn Off the Lights HackerRank Solution
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
sc.nextLine();
String[] split = sc.nextLine().split(" ");
int[] c = new int[n];
for (int i = 0; i < n; i++) {
c[i] = Integer.parseInt(split[i]);
}
long minPrice = Long.MAX_VALUE;
for (int ch = 0; ch <= k && ch < n; ch++) {
int rem = n - k - ch - 1;
rem = rem % (2 * k + 1);
if (rem > 0 && rem <= k) {
continue;
}
long price = 0;
for (int i = ch; i < n; i += 2 * k + 1) {
price += c[i];
}
if (price < minPrice) {
minPrice = price;
}
}
System.out.println(minPrice);
}
}
Python 3 Turn Off the Lights HackerRank Solution
Suggest in comments
Python 2 Turn Off the Lights HackerRank Solution
n, k = map(int, raw_input().split())
c = map(int, raw_input().split())
dp = [float('inf') for i in xrange(n+ 1)]
dp[0] = 0
for i in xrange(1, n+1):
dp[min(i+k, n)] = min(dp[min(i+k, n)], dp[max(i-k-1, 0)] + c[i-1])
#print dp
print dp[n]
C Turn Off the Lights HackerRank Solution
suggest in comments
Warmup
Implementation
Strings
Sorting
Search
Graph Theory
Greedy
Dynamic Programming
Constructive Algorithms
Bit Manipulation
Recursion
Game Theory
NP Complete
Debugging
Leave a comment below