-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a94b9e0
commit 047ce0a
Showing
4 changed files
with
112 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,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; | ||
} | ||
} |
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,2 @@ | ||
Time complexity - O(n * logk) | ||
Space complexity - O(1) |
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,2 @@ | ||
Time complexity - O(n * 2^n) | ||
Space complexity - O(n * 2^n) |
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,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; | ||
} | ||
} |