-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPancakes.java
More file actions
76 lines (66 loc) · 1.87 KB
/
Pancakes.java
File metadata and controls
76 lines (66 loc) · 1.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package CodeJam2017.Pancakes;
import java.io.*;
import java.util.Scanner;
/**
* Author - archit.s
* Date - 08/10/18
* Time - 11:30 AM
*/
public class Pancakes {
void flipValues(char[] t, int s, int e){
for(int i=s;i<e;i++) {
if (t[i] == '+') {
t[i] = '-';
} else {
t[i] = '+';
}
}
}
boolean isValid(char[] t){
for(int i=0;i<t.length;i++){
if(t[i] == '-'){
return false;
}
}
return true;
}
String determineFlips(char[] t, int K){
int count = 0;
for(int i=0;i<t.length-K+1;i++){
if(t[i] == '-'){
flipValues(t, i, i+K);
count++;
}
}
if(isValid(t)){
return String.valueOf(count);
}
return "IMPOSSIBLE";
}
private void flips() throws FileNotFoundException {
File file = new File("./Programming/CodeJam2017/Pancakes/A-large-practice.in");
Scanner in = new Scanner(file);
int t;
t = Integer.valueOf(in.nextLine());
int i=1;
while(i<=t) {
String input = in.nextLine();
String value = input.split(" ")[0];
int K = Integer.valueOf(input.split(" ")[1]);
try(BufferedWriter bw = new BufferedWriter(new FileWriter("./Programming/CodeJam2017/Pancakes/LargeOutput.txt", true))){
String temp = "Case #" + i + ": " + (determineFlips(value.toCharArray(), K));
bw.write(temp);
if(i!=t){
bw.write("\n");
}
}
catch (Exception e){
e.printStackTrace();
}
i++;
}
}
public static void main(String[] args) throws FileNotFoundException {
new Pancakes().flips();
}
}