On the sequence of logical values (true or false) we can build up an OR-Pascal-triangle structure. Instead of summing the values, as in a standard Pascal-triangle, we will combine them using the OR function. That means that the lowest row is simply the input sequence, and every entry in each subsequent row is the OR of the two elements below it. For example, the OR-Pascal-triangle built on the array [true, false, false, true, false] is as follows:
Your job is to count the number of nodes in the OR-Pascal-triangle that contain the value true (this number is 11 for the animation above).
Write a function:
class Solution { public int solution(boolean[] P); }
that, given an array P of N Booleans, returns the number of fields in the OR-Pascal-triangle built on P that contain the value true. If the result is greater than 1,000,000,000, your function should return 1,000,000,000.
Given P = [true, false, false, true, false], the function should return 11, as explained above.
Given P = [true, false, false, true], the function should return 7, as can be seen in the animation below.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000].
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(boolean[] P) {
List<Boolean> out = new ArrayList<>();
for (boolean s: P) {
out.add(s);
}
int count = 0;
while (!out.isEmpty()){
if (out.get(0))
count++;
ArrayList<Boolean> newOut = new ArrayList<>();
for (int i = 1; i < out.size() - 1; i++) {
if (out.get(i))
count++;
if (out.get(i) || out.get(i + - 1)){
newOut.add(true);
}else{
newOut.add(false;
}
}
out = newOut;
}
return count;
}
}
Solution.java:25: error: ')' expected newOut.add(false; ^ 1 error
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public int solution(boolean[] P) {
List<Boolean> out = new ArrayList<>();
for (boolean s: P) {
out.add(s);
}
int count = 0;
while (!out.isEmpty()){
if (out.get(0))
count++;
ArrayList<Boolean> newOut = new ArrayList<>();
for (int i = 1; i < out.size() - 1; i++) {
if (out.get(i))
count++;
if (out.get(i) || out.get(i + - 1)){
newOut.add(true);
}else{
newOut.add(false);
}
}
out = newOut;
}
return count;
}
}
Solution.java:9: error: cannot find symbol List<Boolean> out = new ArrayList<>(); ^ symbol: class List location: class Solution Solution.java:9: error: cannot find symbol List<Boolean> out = new ArrayList<>(); ^ symbol: class ArrayList location: class Solution Solution.java:18: error: cannot find symbol ArrayList<Boolean> newOut = new ArrayList<>(); ^ symbol: class ArrayList location: class Solution Solution.java:18: error: cannot find symbol ArrayList<Boolean> newOut = new ArrayList<>(); ^ symbol: class ArrayList location: class Solution 4 errors
import java.util.ArrayList;
import java.util.List;
class Solution {
public int solution(boolean[] P) {
List<Boolean> out = new ArrayList<>();
for (boolean s: P) {
out.add(s);
}
int count = 0;
while (!out.isEmpty()){
if (out.get(0))
count++;
ArrayList<Boolean> newOut = new ArrayList<>();
for (int i = 1; i < out.size() - 1; i++) {
if (out.get(i))
count++;
if (out.get(i) || out.get(i + - 1)){
newOut.add(true);
}else{
newOut.add(false);
}
}
out = newOut;
}
return count;
}
}
import java.util.ArrayList;
import java.util.List;
class Solution {
public int solution(boolean[] P) {
List<Boolean> out = new ArrayList<>();
for (boolean s: P) {
out.add(s);
}
int count = 0;
while (!out.isEmpty()){
if (out.get(0))
count++;
ArrayList<Boolean> newOut = new ArrayList<>();
for (int i = 1; i < out.size(); i++) {
if (out.get(i))
count++;
if (out.get(i) || out.get(i + - 1)){
newOut.add(true);
}else{
newOut.add(false);
}
}
out = newOut;
}
return count;
}
}
import java.util.ArrayList;
import java.util.List;
class Solution {
public int solution(boolean[] P) {
List<Boolean> out = new ArrayList<>();
for (boolean s: P) {
out.add(s);
}
int count = 0;
while (!out.isEmpty()){
if (out.get(0))
count++;
ArrayList<Boolean> newOut = new ArrayList<>();
for (int i = 1; i < out.size(); i++) {
if (out.get(i))
count++;
if (out.get(i) || out.get(i + - 1)){
newOut.add(true);
}else{
newOut.add(false);
}
}
out = newOut;
}
return count;
}
}
import java.util.ArrayList;
import java.util.List;
class Solution {
public int solution(boolean[] P) {
List<Boolean> out = new ArrayList<>();
for (boolean s: P) {
out.add(s);
}
int count = 0;
while (!out.isEmpty()){
if (out.get(0))
count++;
ArrayList<Boolean> newOut = new ArrayList<>();
for (int i = 1; i < out.size(); i++) {
if (out.get(i))
count++;
if (out.get(i) || out.get(i + - 1)){
newOut.add(true);
}else{
newOut.add(false);
}
}
out = newOut;
}
return count;
}
}
The following issues have been detected: timeout errors.
Big tests, only True values.
running time: >6.00 sec., time limit: 0.34 sec.
Big tests, no two neighbors False.
running time: >6.00 sec., time limit: 0.34 sec.
Big random tests.
running time: >6.00 sec., time limit: 0.26 sec.