PHP Exercises: Compute the maximum value of the sum of the passing integers
PHP: Exercise-75 with Solution
Arrange integers (0 to 99) as narrow hilltop, as illustrated in Figure 1. Reading such data representing huge, when starting from the top and proceeding according to the next rule to the bottom. Write a PHP program that compute the maximum value of the sum of the passing integers.
Pictorial Presentation:
Input: A series of integers separated by commas are given in diamonds. No spaces are included in each line. The input example corresponds to Figure 1. The number of lines of data is less than 100 lines.
Sample Output:
The maximum value of the sum of integers passing according to the rule on one line.
Sample Solution: -
PHP Code:
<?php
class Node {
public $left_part;
public $right_part;
public $value;
public $largest;
public function __construct($value) {
$this->value = $value;
}
public function max_value() {
if ($this->largest !== null) {
return $this->largest;
}
if (is_null($this->left_part) && is_null($this->right_part)) {
return $this->value;
}
$left_part = is_null($this->left_part) ? -1 : $this->left_part->max_value();
$right_part = is_null($this->right_part) ? -1 : $this->right_part->max_value();
return $this->largest = $this->value + max($left_part, $right_part);
}
}
$diamond = array();
while ($line = trim(fgets(STDIN))) {
$diamond[] = explode(',', $line);
}
$nodes = array();
for ($i = 0; $i < count($diamond); $i++) {
for ($j = 0; $j < count($diamond[$i]); $j++) {
$nodes[$i][$j] = new Node($diamond[$i][$j]);
}
}
for ($i = 0; $i < count($nodes); $i++) {
for ($j = 0; $j < count($nodes[$i]); $j++) {
$n = $nodes[$i][$j];
if ($i < count($diamond)/2 - 1) {
$n->left_part = isset($nodes[$i + 1][$j]) ? $nodes[$i + 1][$j] : null;
$n->right_part = isset($nodes[$i + 1][$j + 1]) ? $nodes[$i + 1][$j + 1] : null;
} else {
$n->left_part = isset($nodes[$i + 1][$j - 1]) ? $nodes[$i + 1][$j - 1] : null;
$n->right_part = isset($nodes[$i + 1][$j]) ? $nodes[$i + 1][$j] : null;
}
}
}
$top = $nodes[0][0];
echo $top->max_value() . "\n";
?>
Sample Input:
8
4, 9
9, 2, 1
3, 8, 5, 5
5, 6, 3, 7, 6
3, 8, 5, 5
9, 2, 1
4, 9
8
Sample Output:
64
Flowchart:
Flowchart:
PHP Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a PHP program to cut out words of 3 to 6 characters length from a given sentence not more than 1024 characters.
Next: Write a PHP program to print the number of combinations.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
PHP: Tips of the Day
How to Sort Multi-dimensional Array by Value?
Try a usort, If you are still on PHP 5.2 or earlier, you'll have to define a sorting function first:
Example:
function sortByOrder($a, $b) { return $a['order'] - $b['order']; } usort($myArray, 'sortByOrder');
Starting in PHP 5.3, you can use an anonymous function:
usort($myArray, function($a, $b) { return $a['order'] - $b['order']; });
And finally with PHP 7 you can use the spaceship operator:
usort($myArray, function($a, $b) { return $a['order'] <=> $b['order']; });
To extend this to multi-dimensional sorting, reference the second/third sorting elements if the first is zero - best explained below. You can also use this for sorting on sub-elements.
usort($myArray, function($a, $b) { $retval = $a['order'] <=> $b['order']; if ($retval == 0) { $retval = $a['suborder'] <=> $b['suborder']; if ($retval == 0) { $retval = $a['details']['subsuborder'] <=> $b['details']['subsuborder']; } } return $retval; });
If you need to retain key associations, use uasort() - see comparison of array sorting functions in the manual
Ref : https://bit.ly/3i77vCC
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework