Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Friday's quiz - MIN PATH

Alex RJul 4 2025

Hello, everybody!

Yet another programming challenge which imho can be solved nicely and efficiently in pure SQL.

The task is to find a way from top right to bottom left cell with min path sum.

For the matrix below the answer is 2427.

with
matrix(id, str) as
(
   select 1, '131, 673, 234, 103,  18' from dual
   union all select 2, '201,  96, 342, 965, 150' from dual
   union all select 3, '630, 803, 746, 422, 111' from dual
   union all select 4, '537, 699, 497, 121, 956' from dual
   union all select 5, '805, 732, 524,  37, 331' from dual
),
t as
(
    select id i, j, n
    from matrix, xmltable(str columns j for ordinality, n int path '.')
)
select * from t;

Write a query to get the answer for 100*100 matrix.

create table matrix as
with t(rn, x) as
(
   -- reproducible "random" across different RDBMS
   select rownum, abs(trunc(sin(rownum)*1e4))
   from dual
   connect by level <= 1e4
)
select mod(rn-1, 1e2)+1 id, listagg(x, ',') within group (order by rn) str
from t
group by mod(rn-1, 1e2);

I will post my solutions later today.

This post has been answered by James Su on Jul 4 2025
Jump to Answer
Comments
Post Details
Added on Jul 4 2025
30 comments
376 views