fork download
  1. import java.util.* ;
  2. import java.io.*;
  3. class Path
  4. {
  5. int x; int y; int z;
  6. //z is the minimum distance required to reach grid element at index(x,y)
  7. Path(int x, int y,int z)
  8. {
  9. this.x=x;
  10. this.y=y;
  11. this.z=z;
  12. }
  13. }
  14. public class Solution {
  15. public static int minimumPathSum(int[][] triangle, int n) {
  16. // Write your code here.
  17. int min=Integer.MAX_VALUE;
  18. for(int i=0;i<n;i++)
  19. {
  20. min=Math.min(minPathSum(triangle,n-1,i,n),min);
  21. }
  22. return min;
  23. }
  24. public static int minPathSum(int[][] grid, int row1, int col1,int n) {
  25. // int row=grid.length;
  26. // int col=grid[0].length;
  27. boolean[][] visited=new boolean[n][n];
  28. int []dx={1,1};
  29. int []dy={0,1};
  30. PriorityQueue <Path> queue=new PriorityQueue<Path>((e,f)->(e.z-f.z));
  31. queue.offer(new Path(0,0,grid[0][0]));
  32. while(!queue.isEmpty())
  33. {
  34. Path temp=queue.poll();
  35.  
  36. if(temp.x==row1&&temp.y==col1)
  37. return temp.z;
  38. for(int i=0;i<2;i++)
  39. {
  40. int a=temp.x+dx[i];
  41. int b=temp.y+dy[i];
  42. if(a<0||a>=n||b<0||b>=n||visited[a][b]==true)
  43. continue;
  44. visited[a][b]=true;
  45. queue.offer(new Path(a,b,temp.z+grid[a][b]));
  46. }
  47. }
  48. return -1;
  49. }
  50. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:14: error: class Solution is public, should be declared in a file named Solution.java
public class Solution {
       ^
1 error
stdout
Standard output is empty