Skip to content

Commit

Permalink
Added codes for 22 May
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed May 22, 2024
1 parent a94b9e0 commit 047ce0a
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 0 deletions.
73 changes: 73 additions & 0 deletions GeeksForGeeks/May/22-5-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//{ Driver Code Starts
// Initial Template for Java

import java.io.*;
import java.lang.*;
import java.math.*;
import java.util.*;

class GFG {
public static void main(String[] args) throws IOException {
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 k = sc.nextInt();
Solution obj = new Solution();
double ans = obj.findSmallestMaxDist(a, k);
System.out.printf("%.2f", ans);
System.out.println();
}
}
}

// } Driver Code Ends


// User function Template for Java

class Solution
{
static double findSmallestMaxDist(int a[], int k)
{
// code here
int n=a.length;
double l=0, r=maxDist(a);
while(l+1e-6<r)
{
double mid = (r+l)/2;
if(possible(a,n,k,mid))
r=mid;
else
l=mid;
}

return r;
}

static boolean possible(int a[],int n,int k,double mid)
{
int req=0;
for(int i=1;i<n;i++)
{
double diff = a[i]-a[i-1];
req += Math.ceil(diff/mid)-1;
}
return req<=k;
}

static double maxDist(int a[])
{
int n = a.length;
double maxDist=0.0;

for(int i=1;i<n;i++)
maxDist = Math.max(maxDist,a[i]-a[i-1]);

return maxDist;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/May/22-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * logk)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/May/22-5-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n * 2^n)
Space complexity - O(n * 2^n)
35 changes: 35 additions & 0 deletions LeetCode/May/22-5-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
class Solution
{
public List<List<String>> partition(String s)
{
List<List<String>> ans = new ArrayList<>();
dfs(s, 0, new ArrayList<>(), ans);
return ans;
}

private void dfs(final String s, int start, List<String> path, List<List<String>> ans)
{
if (start == s.length())
{
ans.add(new ArrayList<>(path));
return;
}

for (int i = start; i < s.length(); ++i)
if (isPalindrome(s, start, i))
{
path.add(s.substring(start, i + 1));
dfs(s, i + 1, path, ans);
path.remove(path.size() - 1);
}
}

private boolean isPalindrome(final String s, int l, int r)
{
while (l < r)
if (s.charAt(l++) != s.charAt(r--))
return false;

return true;
}
}

0 comments on commit 047ce0a

Please sign in to comment.