-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #40 from Sherlock-dev/master
added some more problems
- Loading branch information
Showing
4 changed files
with
248 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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;i<n;i++) | ||
{ | ||
a[i]=sc.nextInt(); | ||
} | ||
int m=sc.nextInt(); | ||
Solution ob = new Solution(); | ||
System.out.println(ob.findPages(a,n,m)); | ||
} | ||
} | ||
}// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
class Solution | ||
{ | ||
//Function to find minimum number of pages. | ||
public static int findPages(int[]a,int n,int m) | ||
{ if(n<m) | ||
return -1; | ||
int sum = 0; | ||
for(int i = 0;i<n;i++) | ||
sum += a[i]; | ||
int s = 0,e = sum; | ||
int res = 0; | ||
while(s<=e) | ||
{ | ||
int mid = s+(e-s)/2; | ||
if(possible(a,n,m,mid)) | ||
{ | ||
res = mid; | ||
e = mid-1; | ||
} | ||
else | ||
s = mid+1; | ||
} | ||
return res; | ||
} | ||
public static boolean possible(int a[],int n,int m,int mid) | ||
{ int s = 1; | ||
int sum = 0; | ||
for(int i = 0;i<n;i++) | ||
{ | ||
if(a[i]>mid) | ||
return false; | ||
if(sum+a[i]>mid) | ||
{ s++; | ||
sum = a[i]; | ||
if(s>m) | ||
return false; | ||
} | ||
else | ||
sum+= a[i]; | ||
} | ||
|
||
return true; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<v;k++) | ||
for(int i = 0;i<v;i++) | ||
for(int j = 0;j<v;j++) | ||
{if(matrix[i][k]==-1 || matrix[k][j]==-1)continue; | ||
else if (matrix[i][j]==-1) | ||
matrix[i][j] = matrix[i][k]+matrix[k][j]; | ||
else if(matrix[i][k]+matrix[k][j]<matrix[i][j]) | ||
matrix[i][j] = matrix[i][k]+matrix[k][j]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
//Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class matrix_chain | ||
{ | ||
public static void main(String args[])throws IOException | ||
{ | ||
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(in.readLine()); | ||
while(t-- > 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<j;k++) | ||
{ | ||
int x = solve(a,i,k)+solve(a,k+1,j)+(a[i-1]*a[k]*a[j]); | ||
memo[i][j]=Math.min(memo[i][j],x); | ||
|
||
} | ||
return memo[i][j]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
//Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class palindrome_partitioning{ | ||
public static void main(String args[])throws IOException | ||
{ | ||
BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); | ||
int t = Integer.parseInt(in.readLine()); | ||
while(t-- > 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;k<j;k++) | ||
{ int l ,r; | ||
if(dp[i][k]!=-1) | ||
l = dp[i][k]; | ||
else | ||
l = solve(s,i,k); | ||
|
||
if(dp[k+1][j]!=-1) | ||
r = dp[k+1][j]; | ||
else | ||
r = solve(s,k+1,j); | ||
|
||
int x = 1+l+r; | ||
dp[i][j] = Math.min(x,dp[i][j]); | ||
} | ||
return dp[i][j]; | ||
} | ||
public static boolean palindrome(String s,int i,int j) | ||
{ | ||
if(i>j) | ||
return false; | ||
if(i==j) | ||
return true; | ||
while(i<j) | ||
{ | ||
if(s.charAt(i)!=s.charAt(j)) | ||
return false; | ||
i++; | ||
j--; | ||
|
||
} | ||
return true; | ||
} | ||
} |