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].
package com.PJ;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
/**
*
*/
public class Challenge_2018_03_Nickel2018 {
private static int MAX_RESULT = 1_000_000_000;
private static int MAX_NODES_FOR_MAX_RESULT = 44720;
@Test
void test() {
// regular cases
assertEquals(7, solution(new boolean[]{true, false, false, true}));
assertEquals(11, solution(new boolean[]{true, false, false, true, false}));
assertEquals(8, solution(new boolean[]{true, false, true, false}));
// corner cases
assertEquals(0, solution(new boolean[]{false, false, false, false}));
assertEquals(10, solution(new boolean[]{true, true, true, true}));
}
@Test
void test_huge_corner_cases() {
boolean[] bigArray = new boolean[4_000];
Arrays.fill(bigArray, true);
assertEquals(8_002_000, solution(bigArray));
boolean[] largeArray = new boolean[10_000];
Arrays.fill(largeArray, true);
assertEquals(50_005_000, solution(largeArray));
boolean[] hugeArray = new boolean[50_000];
Arrays.fill(hugeArray, true);
assertEquals(MAX_RESULT, solution(hugeArray));
boolean[] hugeArrayFalse = new boolean[50_000];
Arrays.fill(hugeArrayFalse, false);
assertEquals(0, solution(hugeArrayFalse));
}
private int solution(boolean[] P) {
int result = 0;
for (int i = 0; i < P.length; i++) {
if (P[i]) {
result++;
}
}
result += calculateRow(P);
return (result >= 0 && result < MAX_RESULT) ? result : MAX_RESULT;
}
private int calculateRow(boolean[] row) {
int result = 0;
boolean[] upperRow = new boolean[row.length - 1];
for (int i = 0; i < row.length - 1; i++) {
boolean product = row[i] || row[i + 1];
upperRow[i] = product;
if (product) {
result++;
}
}
if (result == 0) {
return result;
} else if (result == row.length - 1) {
if (result > MAX_NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return result + countAllNodesAbove(result);
}
} else {
result += calculateRow(upperRow);
if (result > MAX_RESULT) {
return MAX_RESULT;
}
}
return result;
}
private int countAllNodesAbove(int count) {
if (count > MAX_NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return (count)*(count - 1)/2;
}
}
}
package com.PJ;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
/**
*
*/
public class Solution {
private static int MAX_RESULT = 1_000_000_000;
private static int MAX_NODES_FOR_MAX_RESULT = 44720;
@Test
void test() {
// regular cases
assertEquals(7, solution(new boolean[]{true, false, false, true}));
assertEquals(11, solution(new boolean[]{true, false, false, true, false}));
assertEquals(8, solution(new boolean[]{true, false, true, false}));
// corner cases
assertEquals(0, solution(new boolean[]{false, false, false, false}));
assertEquals(10, solution(new boolean[]{true, true, true, true}));
}
@Test
void test_huge_corner_cases() {
boolean[] bigArray = new boolean[4_000];
Arrays.fill(bigArray, true);
assertEquals(8_002_000, solution(bigArray));
boolean[] largeArray = new boolean[10_000];
Arrays.fill(largeArray, true);
assertEquals(50_005_000, solution(largeArray));
boolean[] hugeArray = new boolean[50_000];
Arrays.fill(hugeArray, true);
assertEquals(MAX_RESULT, solution(hugeArray));
boolean[] hugeArrayFalse = new boolean[50_000];
Arrays.fill(hugeArrayFalse, false);
assertEquals(0, solution(hugeArrayFalse));
}
private int solution(boolean[] P) {
int result = 0;
for (int i = 0; i < P.length; i++) {
if (P[i]) {
result++;
}
}
result += calculateRow(P);
return (result >= 0 && result < MAX_RESULT) ? result : MAX_RESULT;
}
private int calculateRow(boolean[] row) {
int result = 0;
boolean[] upperRow = new boolean[row.length - 1];
for (int i = 0; i < row.length - 1; i++) {
boolean product = row[i] || row[i + 1];
upperRow[i] = product;
if (product) {
result++;
}
}
if (result == 0) {
return result;
} else if (result == row.length - 1) {
if (result > MAX_NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return result + countAllNodesAbove(result);
}
} else {
result += calculateRow(upperRow);
if (result > MAX_RESULT) {
return MAX_RESULT;
}
}
return result;
}
private int countAllNodesAbove(int count) {
if (count > MAX_NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return (count)*(count - 1)/2;
}
}
}
package com.PJ;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import static org.junit.Assert.assertEquals;
/**
*
*/
public class Solution {
private static int MAX_RESULT = 1_000_000_000;
private static int MAX_NODES_FOR_MAX_RESULT = 44720;
public int solution(boolean[] P) {
int result = 0;
for (int i = 0; i < P.length; i++) {
if (P[i]) {
result++;
}
}
result += calculateRow(P);
return (result >= 0 && result < MAX_RESULT) ? result : MAX_RESULT;
}
private int calculateRow(boolean[] row) {
int result = 0;
boolean[] upperRow = new boolean[row.length - 1];
for (int i = 0; i < row.length - 1; i++) {
boolean product = row[i] || row[i + 1];
upperRow[i] = product;
if (product) {
result++;
}
}
if (result == 0) {
return result;
} else if (result == row.length - 1) {
if (result > MAX_NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return result + countAllNodesAbove(result);
}
} else {
result += calculateRow(upperRow);
if (result > MAX_RESULT) {
return MAX_RESULT;
}
}
return result;
}
private int countAllNodesAbove(int count) {
if (count > MAX_NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return (count)*(count - 1)/2;
}
}
}
public class Solution {
private static int MAX_RESULT = 1_000_000_000;
private static int MAX_NODES_FOR_MAX_RESULT = 44720;
public int solution(boolean[] P) {
int result = 0;
for (int i = 0; i < P.length; i++) {
if (P[i]) {
result++;
}
}
result += calculateRow(P);
return (result >= 0 && result < MAX_RESULT) ? result : MAX_RESULT;
}
private int calculateRow(boolean[] row) {
int result = 0;
boolean[] upperRow = new boolean[row.length - 1];
for (int i = 0; i < row.length - 1; i++) {
boolean product = row[i] || row[i + 1];
upperRow[i] = product;
if (product) {
result++;
}
}
if (result == 0) {
return result;
} else if (result == row.length - 1) {
if (result > MAX_NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return result + countAllNodesAbove(result);
}
} else {
result += calculateRow(upperRow);
if (result > MAX_RESULT) {
return MAX_RESULT;
}
}
return result;
}
private int countAllNodesAbove(int count) {
if (count > MAX_NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return (count)*(count - 1)/2;
}
}
}
public class Solution {
private static int MAX_RESULT = 1_000_000_000;
private static int MNODES_FOR_MAX_RESULT = 44720;
public int solution(boolean[] P) {
int result = 0;
for (int i = 0; i < P.length; i++) {
if (P[i]) {
result++;
}
}
result += calculateRow(P);
return (result >= 0 && result < MAX_RESULT) ? result : MAX_RESULT;
}
private int calculateRow(boolean[] row) {
int result = 0;
boolean[] upperRow = new boolean[row.length - 1];
for (int i = 0; i < row.length - 1; i++) {
boolean product = row[i] || row[i + 1];
upperRow[i] = product;
if (product) {
result++;
}
}
if (result == 0) {
return result;
} else if (result == row.length - 1) {
if (result > MAX_NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return result + countAllNodesAbove(result);
}
} else {
result += calculateRow(upperRow);
if (result > MAX_RESULT) {
return MAX_RESULT;
}
}
return result;
}
private int countAllNodesAbove(int count) {
if (count > MAX_NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return (count)*(count - 1)/2;
}
}
}
public class Solution {
private static int MAX_RESULT = 1_000_000_000;
private static int NODES_FOR_MAX_RESULT = 44720;
public int solution(boolean[] P) {
int result = 0;
for (int i = 0; i < P.length; i++) {
if (P[i]) {
result++;
}
}
result += calculateRow(P);
return (result >= 0 && result < MAX_RESULT) ? result : MAX_RESULT;
}
private int calculateRow(boolean[] row) {
int result = 0;
boolean[] upperRow = new boolean[row.length - 1];
for (int i = 0; i < row.length - 1; i++) {
boolean product = row[i] || row[i + 1];
upperRow[i] = product;
if (product) {
result++;
}
}
if (result == 0) {
return result;
} else if (result == row.length - 1) {
if (result > NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return result + countAllNodesAbove(result);
}
} else {
result += calculateRow(upperRow);
if (result > MAX_RESULT) {
return MAX_RESULT;
}
}
return result;
}
private int countAllNodesAbove(int count) {
if (count > NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return (count)*(count - 1)/2;
}
}
}
public class Solution {
private static int MAX_RESULT = 1_000_000_000;
private static int NODES_FOR_MAX_RESULT = 44720;
public int solution(boolean[] P) {
int result = 0;
for (int i = 0; i < P.length; i++) {
if (P[i]) {
result++;
}
}
result += calculateRow(P);
return (result >= 0 && result < MAX_RESULT) ? result : MAX_RESULT;
}
private int calculateRow(boolean[] row) {
int result = 0;
boolean[] upperRow = new boolean[row.length - 1];
for (int i = 0; i < row.length - 1; i++) {
boolean product = row[i] || row[i + 1];
upperRow[i] = product;
if (product) {
result++;
}
}
if (result == 0) {
return result;
} else if (result == row.length - 1) {
if (result > NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return result + countAllNodesAbove(result);
}
} else {
result += calculateRow(upperRow);
if (result > MAX_RESULT) {
return MAX_RESULT;
}
}
return result;
}
private int countAllNodesAbove(int count) {
if (count > NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return (count)*(count - 1)/2;
}
}
}
public class Solution {
private static int MAX_RESULT = 1_000_000_000;
private static int NODES_FOR_MAX_RESULT = 44720;
public int solution(boolean[] P) {
int result = 0;
for (int i = 0; i < P.length; i++) {
if (P[i]) {
result++;
}
}
result += calculateRow(P);
return (result >= 0 && result < MAX_RESULT) ? result : MAX_RESULT;
}
private int calculateRow(boolean[] row) {
int result = 0;
boolean[] upperRow = new boolean[row.length - 1];
for (int i = 0; i < row.length - 1; i++) {
boolean product = row[i] || row[i + 1];
upperRow[i] = product;
if (product) {
result++;
}
}
if (result == 0) {
return result;
} else if (result == row.length - 1) {
if (result > NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return result + countAllNodesAbove(result);
}
} else {
result += calculateRow(upperRow);
if (result > MAX_RESULT) {
return MAX_RESULT;
}
}
return result;
}
private int countAllNodesAbove(int count) {
if (count > NODES_FOR_MAX_RESULT) {
return MAX_RESULT;
} else {
return (count)*(count - 1)/2;
}
}
}
The following issues have been detected: timeout errors.