This content originally appeared on DEV Community and was authored by shashi
import java.util.HashMap;
import java.util.Map;
public class GoldmanSachs {
    /******************************************************************************
     Given an integer array nums and an integer k, return the length of the shortest non-empty subarray of nums
     with a sum of at least k.
     Input : [2,7,3,-8,4,10], target = 12
     Output : 2
     *******************************************************************************/
        static int pathSum=Integer.MAX_VALUE;
        public static void main(String[] args) {
            int nums[] = {2,7,3,-8,4,10};
//      System.out.println(shortest(nums, 12));
            pathSum=dfs(new int[][]   {{ 1, 3 ,1},
                            {1, 5, 1},
                            {4, 2, 1}},
                    0,
                    0,
                    3,
                    3,
                    1,
                    Integer.MAX_VALUE,
                    new HashMap<>()
            );
            System.out.println(pathSum);
            /*. 2, 9, 12, 4, 8, 18*/
        }
        public static int shortest(int nums[], int target){
            int shortestSubArrLen=Integer.MAX_VALUE, n= nums.length ;
            for( int i=0;i<n; i++){
                int currSum=0;
                for( int j=i; j<n; j++){
                    currSum+=nums[j];
                    if( currSum >= target) {
                        shortestSubArrLen=Math.min(shortestSubArrLen, j-i+1);
                        break;
                    }
                }
            }
            return shortestSubArrLen;
        }
    /*
         [ 1, 3 ,1]
         [1, 5, 1]
        [4, 2, 1]
        1 , 1, ,4, 2, ,1. --> 1
                5,  1, 1
                5, 2, , 1
    */
        public static int dfs( int[][] grid, int i, int j, int r, int c,int currSum, int pathSum, Map<String,Integer> cache ){
            String key="";
            if( cache.containsKey(key) ) return cache.get(key);
            if( i>=r || j >=c  || i<0 || j<0) return 0;
            if( i == r-1 && j==c-1) {
                pathSum= Math.min(pathSum , currSum);
                return pathSum ;
            }
            currSum+=grid[i][j];
            //left
            int left= dfs(grid,i, j+1, r, c, currSum,pathSum,cache);
            //bottom
            int right= dfs(grid, i+1,j, r, c, currSum,pathSum,cache);
            return Math.min(left, right);
        }
    }
result : not selected
This content originally appeared on DEV Community and was authored by shashi
Print
				
				Share
		
				
				Comment
				
				
				Cite
		
		Upload
				
					Translate
				
				Updates
				
			
  
    
			
		
    
				
	
			
				
			
			
	
	 
			
				
			
		
    
    
        
				
							
    
        
There are no updates yet.
Click the Upload button above to add an update.
				
				
					
						
		
				
				
				
				
				
				
		
		
		
				
				
		
	
    
				
	APA
MLA
				
shashi | Sciencx (2024-07-24T18:24:52+00:00) Goldman Sachs SDE-1 Interview Experience (2024). Retrieved from https://www.scien.cx/2024/07/24/goldman-sachs-sde-1-interview-experience-2024/
" » Goldman Sachs SDE-1 Interview Experience (2024)." shashi | Sciencx - Wednesday July 24, 2024, https://www.scien.cx/2024/07/24/goldman-sachs-sde-1-interview-experience-2024/
HARVARDshashi | Sciencx Wednesday July 24, 2024  » Goldman Sachs SDE-1 Interview Experience (2024)., viewed ,<https://www.scien.cx/2024/07/24/goldman-sachs-sde-1-interview-experience-2024/>
VANCOUVERshashi | Sciencx -  » Goldman Sachs SDE-1 Interview Experience (2024). [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/07/24/goldman-sachs-sde-1-interview-experience-2024/
CHICAGO" » Goldman Sachs SDE-1 Interview Experience (2024)." shashi | Sciencx - Accessed . https://www.scien.cx/2024/07/24/goldman-sachs-sde-1-interview-experience-2024/
IEEE" » Goldman Sachs SDE-1 Interview Experience (2024)." shashi | Sciencx [Online]. Available: https://www.scien.cx/2024/07/24/goldman-sachs-sde-1-interview-experience-2024/. [Accessed: ]
rf:citation » Goldman Sachs SDE-1 Interview Experience (2024) | shashi | Sciencx | https://www.scien.cx/2024/07/24/goldman-sachs-sde-1-interview-experience-2024/ | 
				
	 
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.
		