From d166db7f09dfda6d82c89283ff9f92c2602ae8ba Mon Sep 17 00:00:00 2001 From: Vishal Singh Rajput <201951171@iiitvadodara.ac.in> Date: Sun, 3 Oct 2021 00:30:16 +0530 Subject: [PATCH] added some more problems --- allocate_minimum_number_of_pages.java | 71 ++++++++++++++++++++++++++ floyd_warshall.java | 51 +++++++++++++++++++ matrix_chain_multiplication.java | 54 ++++++++++++++++++++ palindrome_partitioning.java | 72 +++++++++++++++++++++++++++ 4 files changed, 248 insertions(+) create mode 100644 allocate_minimum_number_of_pages.java create mode 100644 floyd_warshall.java create mode 100644 matrix_chain_multiplication.java create mode 100644 palindrome_partitioning.java diff --git a/allocate_minimum_number_of_pages.java b/allocate_minimum_number_of_pages.java new file mode 100644 index 0000000..d143799 --- /dev/null +++ b/allocate_minimum_number_of_pages.java @@ -0,0 +1,71 @@ +import java.io.*; +import java.util.*; +class pages { + public static void main (String[] args) { + Scanner sc=new Scanner(System.in); + + int t=sc.nextInt(); + + while(t-->0) + { + int n=sc.nextInt(); + int a[]=new int[n]; + + for(int i=0;imid) + return false; + if(sum+a[i]>mid) + { s++; + sum = a[i]; + if(s>m) + return false; + } + else + sum+= a[i]; + } + + return true; + } +}; \ No newline at end of file diff --git a/floyd_warshall.java b/floyd_warshall.java new file mode 100644 index 0000000..6203069 --- /dev/null +++ b/floyd_warshall.java @@ -0,0 +1,51 @@ +//Initial template for JAVA + +import java.util.*; +import java.lang.*; +import java.io.*; +class floyd_warshall +{ + public static void main(String[] args) throws IOException + { + BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + int T = Integer.parseInt(br.readLine().trim()); + while(T-->0) + { + int n = Integer.parseInt(br.readLine().trim()); + int[][] matrix = new int[n][n]; + for(int i = 0; i < n; i++){ + String[] s = br.readLine().trim().split(" "); + for(int j = 0; j < n; j++) + matrix[i][j] =Integer.parseInt(s[j]); + } + Solution obj = new Solution(); + obj.shortest_distance(matrix); + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++){ + System.out.print(matrix[i][j] + " "); + } + System.out.println(); + } + } + } +} +// } Driver Code Ends + + +//User function template for JAVA + +class Solution +{ + public void shortest_distance(int[][] matrix) + { int v = matrix.length; + for(int k = 0;k 0){ + int N = Integer.parseInt(in.readLine()); + String input_line[] = in.readLine().trim().split("\\s+"); + int arr[]= new int[N]; + for(int i = 0; i < N; i++) + arr[i] = Integer.parseInt(input_line[i]); + + Solution ob = new Solution(); + System.out.println(ob.matrixMultiplication(N, arr)); + } + } +} +// } Driver Code Ends + + +//User function Template for Java + +class Solution{ + static int[][] memo = new int[100][100];; + static int matrixMultiplication(int n, int arr[]) + { + + for(int[] row:memo) + Arrays.fill(row,-1); + return solve(arr,1,n-1); + + } + public static int solve(int [] a,int i,int j) + { + if(i>=j) + return 0; + if(memo[i][j]!=-1) + return memo[i][j]; + memo[i][j] = Integer.MAX_VALUE; + for(int k = i;k 0){ + String str = in.readLine(); + Solution ob = new Solution(); + System.out.println(ob.palindromicPartition(str)); + } + } +}// } Driver Code Ends + + +//User function Template for Java + +class Solution{ + static int[][] dp = new int[501][501]; + static int palindromicPartition(String s) + { + for(int [] rows:dp) + Arrays.fill(rows,-1); + return solve(s,0,s.length()-1); + } + public static int solve(String s,int i ,int j) + { + if(i>=j) + return 0; + if(palindrome(s,i,j)) + return 0; + if(dp[i][j]!=-1) + return dp[i][j]; + dp[i][j] = Integer.MAX_VALUE; + for(int k = i;kj) + return false; + if(i==j) + return true; + while(i