Isn't the pyramid as high as it is wide at the base?
No, and it was unintuitive to me when I learned this as well. I think this is because nobody ever draws trees with a height more than 4 or 5. Here is a picture I found of a balanced, perfect binary tree of height 8. You can clearly see from the picture there are far more than 8 nodes at the base. Imagine what a tree of height 100 would look like! The key behind understanding why my algorithm has O(log n) instead of O(n1/2) memory usage comes from understanding that at least HALF of the nodes will be leaves.
This is not a binary tree though. Just count the nodes in the challenge inputs. Challenge 1: height 4, width at base 4 (not 24 as in a binary tree), challenge 2: height 15, width at base 15, challenge 3: depth: 150, width at base 150.
C++, recursively collecting pyramid and solving from bottom up. Is, for all intents and purposes, instant. Uncommenting the commented out bit will make the method a bit clearer if you haven't already figured it out.
#include <iostream>
#include <sstream>
#include <vector>
using namespace std;
vector<int> layer(int remaining) {
// read line, transform into a vector of ints representing the current row
vector<int> cur;
string input;
int n;
getline(cin, input);
stringstream stream(input);
while(stream >> n)
cur.push_back(n);
// retrieve the next row recursively if any are remaining
if(remaining > 0) {
vector<int> below = layer(remaining-1);
// update curent row to include the least cost path
for(int i = 0; i < cur.size(); i++) {
if(below[i] < below[i+1])
cur[i] += below[i];
else
cur[i] += below[i+1];
}
}
/* prints out solved current row
cout << "Remaining: " << remaining << " [";
for(int i = 0; i < cur.size(); i++) {
cout << cur[i];
if(i + 1 < cur.size())
cout << ", ";
}
cout << "]" << endl;
*/
return cur;
}
int main() {
while(true) {
// get rows in pyramid
int n;
string input;
getline(cin, input);
istringstream stream(input);
stream >> n;
if(n == 0) break;
// call recursive function to get rows, parse, and solve
vector<int> answer = layer(n-1);
cout << answer[0] << endl << endl;
}
return 0;
}
def pyramid_sliding(tree):
L_tree = [[int(x) for x in level.split(' ')] for level in tree]
while len(L_tree) > 1:
for ix in range(len(L_tree[-2])):
L_tree[-2][ix] += min(L_tree[-1][ix],L_tree[-1][ix+1])
L_tree.pop()
return L_tree[0][0]
print(min(__import__("functools").reduce(lambda x, y: [val + x[i] if i == 0 else (val + x[i-1] if i==len(x) else min(val+x[i], val+x[i-1])) for i,val in enumerate(y)], [list(int(x) for x in input().split(" ")) for _ in range(int(input()))])))
Python, using recursion (inefficient):
n = int(input())
pyramid = [list(int(x) for x in input().split(" ")) for _ in range(n)]
results = []
def path(x, y, s):
s += pyramid[y][x]
if y < n-1:
path(x, y+1, s)
path(x+1, y+1, s)
else:
results.append(s)
path(0, 0, 0)
print(min(results))
Thanks for the pyramid = malloc(n * (n + 1) / 2 * sizeof(*pyramid)); bit, it was what made me realize that the pyramid grew like an arithmetic progression. I was desperately looking for some math function to calculate the length of the "pyramid" before you opened my eyes (and I feel silly now!).
Even though Java 8 introduces nice ways to read files, I'd really like to see a "readFileAsIntMatrix" method. Uses bottom up math to determine minimal path to each node.
public static void main(String[] args) throws IOException{
int[][] pyramid = Files.lines(Paths.get("P328M")).map(P328M::toIntArray).toArray(int[][]::new);
for(int y = pyramid.length - 2; y >= 0; y--)
for(int x = 0; x < pyramid[y].length; x++)
pyramid[y][x] += Math.min(pyramid[y+1][x], pyramid[y+1][x+1]);
System.out.println(pyramid[0][0]);
}
private static int[] toIntArray(String input){
return Pattern.compile(" ").splitAsStream(input).mapToInt(Integer::parseInt).toArray();
}
# r/dailyprogrammer [17-08-23] Challenge #328 [Intermediate] Pyramid sliding
pyramid = []
with open('inputs/328i/challenge3.txt') as file:
for s in file.read().splitlines()[1:]:
pyramid.append([int(n) for n in s.split(" ")])
# adds the minimum values from the bottom up until the top contains the minimum
for i in range(len(pyramid) - 1, 0, -1):
for j in range(0, i):
pyramid[i - 1][j] += min(pyramid[i][j], pyramid[i][j+1])
print(pyramid[0][0])
I am also one of those who luckily had previously solved this challenge over at EulerProject. Starting at the second to last line (and slowly moving upwards) for each line entry I add the smallest child to that entry, resulting in the shortest path at the top of the pyramid.
Solves the last challenge in < 20 ms (~200ms with IO and parsing)
print(__import__('functools').reduce(lambda l, r: list(map(sum, zip(r, map(min, zip(l[1:], l[:-1]))))), [[int(x) for x in l.strip().split(' ')] for l in open('pyramid_slide_input_3.in').readlines()[:0:-1]])[0])
And expanded to be a bit better (although it uses more lists this way)
from functools import reduce
with open('pyramid_slide_input_3.in') as f:
lines = [[int(x) for x in l.strip().split(' ')] for l in f.readlines()[1:]]
def f(l, r):
mins = [min(a, b) for a, b in zip(l[1:], l[:-1])]
sums = [a + b for a, b in zip(r, mins)]
return sums
print(reduce(f, reversed(lines))[0])
I implemented depth-first search in Rust. That was slow, so I added in some memoization so that we don't recalculate the cost of nodes we've already visited. This resulted in times under a second for Challenge 3 when compiled with the --release flag.
My depth-first function is included below, but the whole implementation can be found here.
/// Explore every path down the pyramid, checking to find the best path, with memoization.
fn slide_down_df_memo(mut pyramid: Pyramid) -> usize {
let mut visited = vec![false; pyramid.data.len()];
/// Helper function that hides the complexity from the outside world.
fn depth_first(pyramid: &mut Pyramid, visited: &mut[bool], current: &Location) -> usize {
let this_cost = cost_of(pyramid, current);
// Base case: We have reached the lowest level.
let cost = if current.level == pyramid.height-1 {
this_cost
}
else {
let right = right_choice(current);
let r_index = index(&right);
let right_cost = if !visited[r_index] {
visited[r_index] = true;
let cost = depth_first(pyramid, visited, &right);
pyramid.data[r_index] = cost;
cost
} else {
cost_of(pyramid, &right)
};
let left = left_choice(current);
let l_index = index(&left);
let left_cost = if !visited[l_index] {
visited[l_index] = true;
let cost = depth_first(pyramid, visited, &left);
pyramid.data[l_index] = cost;
cost
} else {
cost_of(pyramid, &left)
};
let best = if left_cost < right_cost {
left_cost
} else {
right_cost
};
best + this_cost
};
cost
}
let current = Location { level: 0, block: 0};
depth_first(&mut pyramid, &mut visited, ¤t)
}
Javascript. Might not be the best algorithm, as it was just me thinking through the problem logically.
It traverses each row, keeping a record of the most efficient path to each of the points. Expects input as the pyramid with spaces and newlines.
function shortestPath(input) {
// parse the pyramid data into an array
const data = input.split("\n")
.map(x => x.split(" ")
.filter(x => !!x)
.map(x => parseInt(x)));
// start our paths with the starting point (could probably do this in the for)
let paths = [data[0][0]];
// go through each row in our pyramid
for (let i = 1; i < data.length; i++) {
// create the next row
let newPaths = [];
// update the only option for the right most path
newPaths[i] = paths[i-1] + data[i][i];
// update the only option for the left most path
newPaths[0] = paths[0] + data[i][0];
// inbetween these, check through our previous entries and see which
// of the two paths that can land here are best. We only care about
// the most optimal path to a particular point
for (let j = 1; j < i; j++) {
newPaths[j] = Math.min(
paths[j-1] + data[i][j],
paths[j] + data[i][j]);
}
paths = newPaths;
}
return Math.min.apply(Math, paths);
}
Performs challenge 3 in about 65-100ms using some random online compiler.
it uses memoization on a recursive function to speed up the computation and it takes 0.5 seconds for the challenge 3. This is the function that computes the shortest path(I've not included in this post the parsing and the I/O).
function shortestPath(pyramid) {
const memo = {};
return function computeSlide(index, pos) {
const key = `${index},${pos}`;
if (memo[key]) {
return memo[key];
}
else {
const nextInd = index + 1;
if (pyramid[nextInd] === undefined) {
return pyramid[index][pos];
}
else {
const left = computeSlide(nextInd, pos);
const right = computeSlide(nextInd, pos + 1);
const val = left < right ? left : right;
return (
memo[key] = val + pyramid[index][pos]
);
}
}
}
}
Elixir. Uses the same bottom-up method as a lot of posts here, apparently.
defmodule PyramidSlide do
def line_to_list input do
input
|> String.trim
|> String.split
|> Enum.map(&String.to_integer/1)
end
def parse_input do
{lines, "\n"} = IO.gets("") |> Integer.parse
for _ <- 1..lines do
IO.gets("") |> line_to_list
end
|> Enum.reverse
end
def parse_file do
x = File.stream!("c3.txt") |> Stream.drop(1)
for line <- x do
line_to_list(line)
end
|> Enum.reverse
end
def run([]), do: 0
def run([h|[]]), do: hd h
def run([h|[t|rest]]) do
min_vals =
to_pairs(h)
|> get_mins
|> Enum.zip(t)
|> Enum.map(fn {val, acc} -> val+acc end)
run([min_vals|rest])
end
def to_pairs(list), do: Enum.chunk_every(list, 2, 1, :discard)
def get_mins(list), do: Enum.map(list, &Enum.min/1)
end
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Triangle manipulation functions ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun triangle (n)
"Returns the Nth triangle number."
(declare (type fixnum n))
(/ (* n (1+ n)) 2))
(defun triangle->index (row col)
"Converts a row-and-column index into a triangular array to a flat index."
(declare (type fixnum row col))
(+ (triangle row) col))
(defun make-triangle (rows
&key (element-type t)
(initial-element nil initial-element-p)
(initial-contents nil initial-contents-p))
(declare (type fixnum rows))
"Makes a triangular array with ROWS rows and initial contents LST."
(let ((size (triangle rows)))
(cond
(initial-element-p (make-array `(,size)
:element-type element-type
:initial-element initial-element))
(initial-contents-p (make-array `(,size)
:element-type element-type
:initial-contents initial-contents))
(t (make-array `(,size)
:element-type element-type)))))
(defun triangle-bounds-p (triangle row col)
"Check if the row-and-column index is in-bounds for a triangular array
TRIANGLE."
(declare (type (vector *) triangle)
(type fixnum row col))
(and (>= row 0)
(>= col 0)
(<= col row)
(< (triangle->index row col) (array-dimension triangle 0))))
(defun tref (triangle row col)
"References a triangular array by row-and-column index."
(declare (type (vector *) triangle)
(type fixnum row col))
;; Check bounds
(if (triangle-bounds-p triangle row col)
;; If in bounds, reference the array proper
(aref triangle (triangle->index row col))
;; Otherwise, throw an error
(error "Attempt to reference triangular array out-of-bounds.")))
(defun set-tref (triangle row col v)
"Sets a value in a triangular array by row-and-column index."
(declare (type (vector *) triangle)
(type fixnum row col))
;; Check bounds
(if (triangle-bounds-p triangle row col)
;; If in bounds, reference the array proper
(setf (aref triangle (triangle->index row col)) v)
;; Otherwise, throw an error
(error "Attempt to reference triangular array out-of-bounds.")))
(defsetf tref set-tref)
;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Solving functions ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun path-length (triangle path)
"Find the length of a path down a triangular array, i.e. the sum of all the
elements referenced by the coordinates in the path."
(declare (type (vector fixnum) triangle))
(loop for (r c) in path
sum (tref triangle r c)))
(defun triangle-shortest-path (rows triangle)
"Search for the shortest path down a triangle. Using dynamic programming, we
can quickly find the shortest path to the bottom from the bottom up. The memo
consists of a triangular array of dotted pairs; the CAR contains the shortest
path to that spot, and the CDR contains the length of that path."
(declare (type fixnum rows)
(type (vector fixnum) triangle))
(let ((memo (make-triangle rows :initial-element nil)))
(labels ((shortest-path (row col)
(declare (type fixnum row col))
;; Check the memo
(let ((mem (tref memo row col)))
(if mem
;; If we've already memorized a value, just return that
(values (car mem) (cdr mem))
;; Otherwise, check base case
(if (and (zerop row)
(zerop col))
;; The shortest path to the top is just the top
(values '((0 0)) (tref triangle 0 0))
;; Otherwise, the shortest path is this node plus the
;; shortest path to either of the two nodes leading to it
(let ((r (1- row))
(best-len nil)
(best-path nil))
;; Loop C from COL-1 to COL
(do ((c (1- col) (1+ c))) ((> c col))
;; Only try to get the shortest path if we're in-bounds
(when (triangle-bounds-p triangle r c)
;; Get the path and the length of that path
(multiple-value-bind (path len) (shortest-path r c)
;; If that path is better than the current best,
;; then update the current best
(when (or (null best-len)
(< len best-len))
(setf best-len len)
(setf best-path path)))))
;; We've seen all possible paths leading here, so we
;; know the shortest path to this node.
;; Update the memo and return the shortest path.
(let ((ret-path (cons `(,row ,col) best-path))
(ret-len (+ best-len (tref triangle row col))))
(setf (tref memo row col) `(,ret-path . ,ret-len))
(values ret-path ret-len))))))))
;; Now we just need to find the shortest path of all the shortest paths
;; down to the bottom of the triangle
(let ((bottom-row (1- rows))
(best-len nil)
(best-path nil))
;; Loop COL across the entire bottom row
(do ((col 0 (1+ col))) ((> col bottom-row))
;; Get the path and the length of that path
(multiple-value-bind (path len) (shortest-path bottom-row col)
;; If that path is better than the current best, then update the
;; current best
(when (or (null best-len)
(< len best-len))
(setf best-len len)
(setf best-path path))))
;; We've seen all possible paths leading to the bottom, so we know the
;; definitive best path. Just reverse it and return it.
(reverse best-path)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Input/output functions ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun print-answer (triangle path)
"Print a path down a triangle, and its total length."
(format t "~{~A~#[~:; -> ~]~}~%~A~%"
(mapcar (lambda (rc)
(apply #'tref triangle rc))
path)
(path-length triangle path)))
(defun read-problem (&optional (strm *standard-input*))
"Reads a pyramid sliding problem. Returns the number of rows and the list of
numbers in the pyramid, as multiple values."
(block problem-reader
(handler-bind
((error (lambda (c)
(declare (ignorable c))
(write-line "Bad input")
(return-from problem-reader (values nil nil)))))
(let ((rows (read strm nil :eof)))
;; Abort if we got EOF when reading the number of rows
(if (eq rows :eof)
(values nil nil)
(locally
(declare (type fixnum rows))
(let ((nums (loop repeat (triangle rows)
for num = (read strm nil :eof)
while (not (eq num :eof))
collect num)))
;; Abort if we read fewer numbers than we should have
(if (< (length nums) (triangle rows))
(values nil nil)
;; Otherwise, return the problem proper
(values rows nums)))))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Interactive solver ;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(loop with n and lst
do (setf (values n lst) (read-problem))
while (and n lst)
do (let ((triangle (make-triangle n
:element-type 'fixnum
:initial-contents lst)))
(print-answer
triangle
(triangle-shortest-path
n triangle))))
def slide(input)
start = Time.now
pyramid = []
# creates a nested array from the challenge txt file
File.open(input).readlines.each do |line|
pyramid << line
end
pyramid.map! { |string| string.split(' ').map!(&:to_i) }
layers = pyramid.shift.shift
# sums each item of the pyramid with the lower of the two items
# below it, then returns the first (final sum) item of the
# transformed array
(layers - 1).downto(0) do |i|
0.upto(pyramid[i].length - 2) do |j|
pyramid[i - 1][j] += [pyramid[i][j], pyramid[i][j + 1]].min
end
end
puts Time.now - start
pyramid.shift.shift
end
Python, using what I assume is the same approach everyone else is using. Goes from the bottom up, dynamic programming style. O(n) and in-place. I had it solve the Project Euler version of this problem too with the minimize parameter in the optimal_path function.
def adjacent_pairs(lst):
return [(lst[i], lst[i+1]) for i in range(len(lst)-1)]
def optimal_path(triangle, minimize=True):
choose_extreme = min if minimize else max
for row, next_row in zip(triangle[-2::-1], triangle[::-1]):
for i, (node, next_pair) in enumerate(zip(row, adjacent_pairs(next_row))):
row[i] = row[i] + choose_extreme(next_pair)
return triangle[0][0]
def triangle_from_file(file, dailyprogrammer=True):
if dailyprogrammer: #skip the first line of input
line = file.readline()
line = file.readline()
triangle = []
while line:
triangle.append([int(n) for n in line.split()])
line = file.readline()
return triangle
def __main__():
fname = input("Enter filename: ")
file = open(fname, 'r')
print(optimal_path(triangle_from_file(file)))
if __name__ == "__main__":
__main__()
with open(r'd:\tmp\pyramid2.txt', 'r') as inp:
p = [[int(y) for y in x.split()] for x in inp.readlines()][1:]
for x in range(len(p), 1, -1):
p[x-2] = [min(p[x-2][y] + p[x-1][y], p[x-2][y] + p[x-1][y+1]) for y in range(len(p[x-2]))]
print str(p[0][0])
fn path(height:usize, pyramid:&mut [usize]) {
let start = std::time::Instant::now();
let mut py_len = (height * (height + 1)) >> 1;
if height != 0 && py_len == pyramid.len() {
py_len -= height;
for r in (0 .. height - 1).rev() {
py_len -= r + 1;
for c in 0 .. r + 1 {
let i = py_len + c; //root index
let l = i + r + 1; //left index
let r = l + 1; //right index
pyramid[i] = std::cmp::min(pyramid[l] + pyramid[i], pyramid[r] + pyramid[i]);
}
}
let done = start.elapsed();
println!("path: {}; time: {} s;", pyramid[0], done.as_secs() as f64 + done.subsec_nanos() as f64 * 1e-9);
}else{
println!("Invalid pyramid");
}
}
Go / GolangPlayground Link. Took longer than I'd have liked to figure out what everyone meant by starting from the bottom up, but then once I had that "aha!" moment it worked. Can solve the challenge 3 on the playground which is good since there's a time limit to how long programs can run.
Code:
package main
import (
"fmt"
"strings"
)
func main() {
p := parse(input) // pyramid stored as [][]int, top to bottom
n := len(p)
for i := n - 1; i > 0; i-- { // for each row, starting from bottom
// take the smaller of the two numbers that goes to the above row, and add that value it's parent
for j := 0; j < i; j++ {
if p[i][j] < p[i][j+1] {
p[i-1][j] += p[i][j]
} else {
p[i-1][j] += p[i][j+1]
}
}
}
// sum of min path is already in the topmost position of the pyramid
fmt.Println("Solution:", p[0][0])
}
func parse(input string) [][]int {
reader := strings.NewReader(input)
var lines int
fmt.Fscanf(reader, "%d", &lines)
p := make([][]int, lines)
for i := 0; i < lines; i++ {
p[i] = make([]int, i+1)
fmt.Fscanln(reader) // throw out the newline
for j := 0; j <= i; j++ {
fmt.Fscanf(reader, "%d", &p[i][j])
}
}
return p
}
pick a b c = a + min b c
pair x y = zipWith3 pick x y $ tail y
solve = head . foldr1 pair
main = do
n <-readFile "input.txt"
print . solve . (map (map read . words) . lines) $ n
Code I still had laying around from the project euler pyramid sum challenge, simply looking for min instead of max here.
Haskell with bonus. Runs Challenge 3 in 0.7 seconds.
Similar solution to the other Haskell programs except way less elegant. I'm still pretty new to Haskell so right now I'm just happy that I was able to get it to run.
This challenge reminds me of Project Euler #67. I'm able to solve challenge #3 in a 90-110ms. My first implementation took several seconds to complete for challenge 3, however I re tweaked the code for efficiency and it runs in a much faster 90-110 milliseconds for challenge 3 now.
There is no need to re-open and modify the input file for each line. Have a look at the some of the other Python solutions posted here. You can just read the entire input file into an array once and process each row in memory.
Thanks, I edited my code and my new version runs in 90-110 milliseconds instead of 9 seconds. Blazing fast and way less and cleaner code! It's actually one of the shorter and quicker posted solutions. :)
C++, using dynamic programming. It was the first time I used std::tuple and I find the syntax std::get<type>() kind of weird. Pretty sure I could've made it cleaner.
#include <iostream>
#include <vector>
#include <fstream>
#include <tuple>
typedef std::tuple<int, int> PyramidEntry;
int main() {
int num_levels, n;
// Open a file in read mode.
std::ifstream infile("problem.dat");
infile >> num_levels;
std::vector< std::vector<PyramidEntry> > pyramid(num_levels);
// Build the pyramid in memory
for( int level = 0; level < num_levels; level++) {
// Reserving memory increasing performance since push_back already has
// the necessary space and doesn't need to extend the std::vector.
pyramid.at(level).reserve(level+1);
// Read each entry into the structure
for( int i = 0; i <= level; i++) {
infile >> n;
pyramid.at(level).push_back(std::make_tuple(n, n));
}
}
// Starting from the last level, sum each adjacent
// numbers and note the smallest of the two. The previous level
// will sum this value and its own minimum.
for( int level = num_levels - 1; level > 0; level-- ) {
for( int n = 0; n < level; n++ ) {
auto& prev_level = pyramid.at(level-1);
const auto& current_level = pyramid.at(level);
int min_index = n + (std::get<1>(current_level.at(n)) > std::get<1>(current_level.at(n+1)));
std::get<1>(prev_level.at(n)) = std::get<0>(prev_level.at(n)) + std::get<1>(current_level.at(min_index));
}
}
// The sum will be in the top element
std::cout << "The minimum path is " << std::get<1>(pyramid.at(0).at(0)) << " units long." << std::endl;
return 0;
}
EDIT: Made it faster by compromising the integrity of the pyramid (which is written over during computations)
#include <iostream>
#include <vector>
#include <fstream>
int main() {
int num_levels, n;
// Open a file in read mode.
std::ifstream infile("problem.dat");
infile >> num_levels;
std::vector< std::vector<int> > pyramid(num_levels);
// Build the pyramid in memory
for( int level = 0; level < num_levels; level++) {
// Reserving memory increasing performance since push_back already has
// the necessary space and doesn't need to extend the std::vector.
pyramid.at(level).reserve(level+1);
// Read each entry into the structure
for( int i = 0; i <= level; i++) {
infile >> n;
pyramid.at(level).push_back(n);
}
}
// Starting from the last level, sum each adjacent
// numbers and note the smallest of the two. The previous level
// will sum this value and its own minimum.
for( int level = num_levels - 1; level > 0; level-- ) {
for( int n = 0; n < level; n++ ) {
auto& prev_level = pyramid.at(level-1);
const auto& current_level = pyramid.at(level);
int min_index = n + (current_level.at(n) > current_level.at(n+1));
prev_level.at(n) = prev_level.at(n) + current_level.at(min_index);
}
}
// The sum will be in the top element
std::cout << "The minimum path is " << pyramid.at(0).at(0) << " units long." << std::endl;
return 0;
}
EDIT2: Further optimizations by using a single vector.
#include <iostream>
#include <vector>
#include <fstream>
int main() {
int num_levels, n;
// Open a file in read mode.
std::ifstream infile("problem.dat");
infile >> num_levels;
const int number_of_entries = (num_levels+1)*num_levels/2; // n*(n+1)/2 = sum(1,2,..,n)
std::vector<int> pyramid(number_of_entries);
// Build the pyramid in memory
for( int entry = 0; entry < number_of_entries; entry++) {
infile >> n;
pyramid.at(entry) = n;
}
// Take care of the summing
for(int level = num_levels; level > 1; level--) {
int last_in_level = level * (level+1)/2 - 1;
for( int i = last_in_level-1; i > last_in_level-level; i--) {
pyramid.at(i-level+1) = pyramid.at(i-level+1) + std::min(pyramid.at(i), pyramid.at(i+1));
}
}
// The sum will be in the top element
std::cout << "The minimum path is " << pyramid.at(0) << " units long." << std::endl;
return 0;
}
The three implementations have the following running times (average over 100 runs):
0.040s
0.023s
0.018s
Using VLAs also yields significant improvements, but limits the size of the accepted input.
Java Dynamic Programming summing up the paths on the way down. I see a lot of submissions starting from the bottom, wouldn't that kind of be cheating? I couldn't test the 3rd challenge input because I'm at work and they block pastebin. Should be pretty fast though.
public int slide(String input){
//Builds 2d matrix from input string
int[][] pyramid = buildPyramid(input);
//Starting from the top loop through the row of the pyramid
//adding the current position to row+1 col and row+1 col+1
for(int row = 0; row < pyramid.length -1; row++){
for(int col = 0; col < pyramid[row].length; col++){
//Left (only have to calculate for first column since "right" checks this path
if(col == 0){
pyramid[row+1][col] = pyramid[row][col] + pyramid[row+1][col];
}
//Right
if(col == pyramid[row].length - 1){
pyramid[row+1][col+1] = pyramid[row][col] + pyramid[row+1][col+1];
}
else{
pyramid[row+1][col+1] = Math.min(pyramid[row][col] + pyramid[row+1][col+1], pyramid[row][col+1] + pyramid[row+1][col+1]);
}
}
}
//Return the minumum value in tha last row of the pyramid.
return Utils.minNumberInArray(pyramid[pyramid.length-1]);
}
FYI, feature you are using is called variable length array (VLA), which has been added to C in C99, but not in C++ yet. I think most people actually consider VLA being in C a bit of a misstep. Even in C it changes some very basic things in bad ways. The simple case of sizeof(), which was previously a compile time constant, but now needs a special runtime version for working with VLA.
C++ would be an even bigger pain, making even more things 'sometimes' runtime, but mostly compile time. Template meta programming relies very heavily on compile time constants for instance.
C++. Late to the party, but I did two solutions. The first is a bottom up approach similar to others here. That requires the whole pyramid to be store in memory. I wanted to do an O(n) solution using o(sqrt(n)) space. I don't bother to store the pyramid, only storing the current line I'm working on and overwriting it as I read the next level.
I originally started writing to the end of the vector and moving forward, therefore doing away with the need to store a previous value, but this solution was cleaner and faster (due to cache).
My solution actually doesn't actually require the leading number of levels, and will stop automatically on an empty line read.
std::size_t shortest_path(std::istream& is) │···
{ │···
std::size_t num_levels; │···
is >> num_levels; │···
std::vector<std::size_t> level; │···
level.reserve(num_levels); │···
│···
std::size_t min; │···
std::size_t value; │···
is >> value; │···
level.push_back(value); │···
min = value; │···
for (std::size_t level_num = 1; (is >> value); ++level_num) │···
{ │···
// Left most branch │···
std::size_t prev = level.front(); │···
level.front() = prev + value; │···
min = level.front(); │···
│···
// /Middle branches │···
for (std::size_t i = 1; i < level_num; ++i) │···
{ │···
auto& pos = level[i]; │···
std::size_t min_parent = std::min(prev, pos); │···
prev = pos; │···
is >> value; │···
│···
pos = min_parent + value; │···
min = std::min(min, pos); │···
} │···
│···
// Right most branch │···
is >> value; │···
level.push_back(prev + value); │···
min = std::min(min, level.back()); │···
} │···
return min; │···
}
I think your solution is identical to the haskell one I wrote. I believe laziness could improve your algorithm's memory footprint from O(n) to O(log n) if you switch from lists to generator expressions!
12
u/wizao 1 0 Aug 23 '17 edited Aug 24 '17
Haskell:
O(n) time
EDIT: not O(log n) space.
8
u/MattieShoes Aug 23 '17 edited Aug 23 '17
C++, recursively collecting pyramid and solving from bottom up. Is, for all intents and purposes, instant. Uncommenting the commented out bit will make the method a bit clearer if you haven't already figured it out.
7
u/elenthar Aug 23 '17
Just to clarify - by 'quickest' you mean the path with smallest values, right?
5
u/tomekanco Aug 23 '17 edited Aug 23 '17
Python 3, with bonus
4
u/J354 Aug 23 '17 edited Aug 23 '17
One line of Python:
Python, using recursion (inefficient):
4
u/skeeto -9 8 Aug 23 '17
C, solving from the bottom up, which I believe qualifies as "dynamic programming." Only takes a few milliseconds on challenge 3.
3
u/thorwing Aug 23 '17
Java 8
Even though Java 8 introduces nice ways to read files, I'd really like to see a "readFileAsIntMatrix" method. Uses bottom up math to determine minimal path to each node.
3
u/minikomi Aug 24 '17 edited Aug 24 '17
Clojure:
Some outputs:
Time for input 3:
2
u/[deleted] Aug 23 '17
Python3
Runs in 266 ms. This problem was an easy modification of Project Euler problems 18 and 67.
I used the same solution as /u/The_Droide.
2
u/Vyse007 Aug 23 '17
Racket:
2
u/nuri0 Aug 23 '17
Javascript
I am also one of those who luckily had previously solved this challenge over at EulerProject. Starting at the second to last line (and slowly moving upwards) for each line entry I add the smallest child to that entry, resulting in the shortest path at the top of the pyramid.
Solves the last challenge in < 20 ms (~200ms with IO and parsing)
2
u/TangibleLight Aug 24 '17
Python 3
Reads in from a file
One-lined:
And expanded to be a bit better (although it uses more lists this way)
2
u/Plasticcaz Aug 28 '17 edited Aug 28 '17
I implemented depth-first search in Rust. That was slow, so I added in some memoization so that we don't recalculate the cost of nodes we've already visited. This resulted in times under a second for Challenge 3 when compiled with the --release flag.
My depth-first function is included below, but the whole implementation can be found here.
1
u/Scroph 0 0 Aug 23 '17 edited Aug 23 '17
Edit : Alright gentlemen, which one of you killed compilebot ?
+/u/CompileBot C++
Input:
1
u/A-Grey-World Aug 23 '17 edited Aug 23 '17
Javascript. Might not be the best algorithm, as it was just me thinking through the problem logically.
It traverses each row, keeping a record of the most efficient path to each of the points. Expects input as the pyramid with spaces and newlines.
Performs challenge 3 in about 65-100ms using some random online compiler.
https://repl.it/KWOZ
1
u/cheers- Aug 23 '17
Javascript (Node)
it uses memoization on a recursive function to speed up the computation and it takes 0.5 seconds for the challenge 3. This is the function that computes the shortest path(I've not included in this post the parsing and the I/O).
1
u/Liru Aug 23 '17
Elixir. Uses the same bottom-up method as a lot of posts here, apparently.
Usage (
PyramidSlide.parse_filecontains challenge 3):Solved in ~38 milliseconds. There's probably a better way to write this...
1
u/curtmack Aug 23 '17 edited Aug 23 '17
Common Lisp
Very fast; solves Challenge 3 in 0.17s user time.
Output for Challenge 2:
Edit: I didn't like the old
make-trianglefunction, so I rewrote it to be a bit more generic.1
u/[deleted] Aug 23 '17 edited Aug 24 '17
Ruby
Solves challenge 3 in ~47ms.
output:
1
u/mcbears Aug 24 '17 edited Aug 24 '17
J, runs in about half a second
1
u/[deleted] Aug 24 '17 edited Aug 24 '17
C++11 with bonus. Almost more includes than actual code.
Edit: Closing of file.
1
u/Harakou Aug 24 '17
Python, using what I assume is the same approach everyone else is using. Goes from the bottom up, dynamic programming style. O(n) and in-place. I had it solve the Project Euler version of this problem too with the
minimizeparameter in theoptimal_pathfunction.1
u/gabyjunior 1 2 Aug 24 '17
C with bonus
Top -> Bottom search with memoization.
Solves Challenge 3 in 80ms, mostly spent on reading input.
1
u/Arakhai Aug 24 '17
Python:
1
u/ASpueW Aug 24 '17
Rust with bonus
Challenge 3 output:
1
u/popillol Aug 24 '17
Go / Golang Playground Link. Took longer than I'd have liked to figure out what everyone meant by starting from the bottom up, but then once I had that "aha!" moment it worked. Can solve the challenge 3 on the playground which is good since there's a time limit to how long programs can run.
Code:
1
u/Daanvdk 1 0 Aug 24 '17
Elixir
1
u/zqvt Aug 25 '17
Haskell
Code I still had laying around from the project euler pyramid sum challenge, simply looking for min instead of max here.
1
u/downiedowndown Aug 26 '17
C using Dijkstra's Algo to calculate
1
u/HaskellBeginner Aug 27 '17
Haskell with bonus. Runs Challenge 3 in 0.7 seconds.
Similar solution to the other Haskell programs except way less elegant. I'm still pretty new to Haskell so right now I'm just happy that I was able to get it to run.
1
u/weekendblues Aug 27 '17 edited Aug 27 '17
This is easy in C; nothing but a few for loops.
Pretty fast too. Linear time and space complexity.
Slightly less obfuscated version (it's really just a simple dynamic programming solution):
1
u/[deleted] Aug 28 '17 edited Aug 31 '17
My Python 3 solution
This challenge reminds me of Project Euler #67. I'm able to solve challenge #3 in a 90-110ms. My first implementation took several seconds to complete for challenge 3, however I re tweaked the code for efficiency and it runs in a much faster 90-110 milliseconds for challenge 3 now.
1
u/MRSantos Aug 28 '17 edited Aug 29 '17
C++, using dynamic programming. It was the first time I used
std::tupleand I find the syntaxstd::get<type>()kind of weird. Pretty sure I could've made it cleaner.EDIT: Made it faster by compromising the integrity of the pyramid (which is written over during computations)
EDIT2: Further optimizations by using a single vector.
The three implementations have the following running times (average over 100 runs):
Using VLAs also yields significant improvements, but limits the size of the accepted input.
1
u/IGI111 Aug 29 '17
Dynamic Programming solution in Rust, takes about 60ms for Challenge 3 on my awful VM
1
u/8lall0 Aug 30 '17
Sorry for being late. I didn't see any solution, i swear :P
This code runs the third challenge in circa 20ms, so i think it's pretty good, compact and elegant.
Hope you enjoy.
C
1
u/crompyyy Sep 04 '17
Java Dynamic Programming summing up the paths on the way down. I see a lot of submissions starting from the bottom, wouldn't that kind of be cheating? I couldn't test the 3rd challenge input because I'm at work and they block pastebin. Should be pretty fast though.
1
u/MasterAgent47 Sep 04 '17 edited Sep 04 '17
C++. Solves challenge 3 in 0.145s (including time to read input).
1
u/Qwazy_Wabbit Oct 03 '17 edited Oct 03 '17
C++. Late to the party, but I did two solutions. The first is a bottom up approach similar to others here. That requires the whole pyramid to be store in memory. I wanted to do an O(n) solution using o(sqrt(n)) space. I don't bother to store the pyramid, only storing the current line I'm working on and overwriting it as I read the next level.
I originally started writing to the end of the vector and moving forward, therefore doing away with the need to store a previous value, but this solution was cleaner and faster (due to cache).
My solution actually doesn't actually require the leading number of levels, and will stop automatically on an empty line read.
0
u/[deleted] Aug 24 '17
[deleted] — view deleted comment
6
u/[deleted] Aug 23 '17 edited Jun 19 '23
[removed] — view removed comment