This section discusses a "specific programming problem" from On-Topic
I am working on an interview question from Interview with Amazon SoftwareQuestion: "Given the triangle of integers, find the path of the largest amount without gaps."
My question is: how do you represent the triangle of integers?
I looked at the Triangle of Integers and saw that the triangle of integers looks something like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
What is the best way (data structure) to present something like this? My idea was something like
int[] r1 = {1}; int[] r2 = {2, 3}; int[] r3 = {4, 5, 6}; int[] r4 = {7, 8, 9, 10}; int[] r5 = {11, 12, 13, 14, 15};
? 2- , .
:
int triangular(int row){ return row * (row + 1) / 2 + 1; } int[] r = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; for(int i=0; i<n_rows; i++){ for(int j=0; j<=i; j++){ System.out.print(r[triangular(i)+j]+" "); }System.out.println(""); } row, column if row>column: index=triangular(row)+column
. .
, .
.
Java ; .
int[][] triangle = {{1}, {2, 3}, {4, 5, 6}, {7, 8, 9, 10}, {11, 12, 13, 14, 15}};
2D- . 0 . , . , .
int[][] array = { { 0, 0, 0, 0, 0, 0, 0 }, { 0, 1, 0, 0, 0, 0, 0 }, { 0, 2, 3, 0, 0, 0, 0 }, { 0, 4, 5, 6, 0, 0, 0 }, { 0, 7, 8, 9,10, 0, 0 }, { 0,11,12,13,14,15, 0 }, { 0, 0, 0, 0, 0, 0, 0 } };
! r ( 0) :
r
r * (r + 1) / 2 + 1