-
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
cb9529d
commit 87ce6e7
Showing
4 changed files
with
108 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,84 @@ | ||
//{ Driver Code Starts | ||
// Initial Template for Java | ||
|
||
import java.util.*; | ||
import java.io.*; | ||
import java.lang.*; | ||
|
||
class Main { | ||
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 m = sc.nextInt(); | ||
int[][] adj = new int[m][3]; | ||
|
||
for (int i = 0; i < m; i++) { | ||
|
||
for (int j = 0; j < 3; j++) { | ||
adj[i][j] = sc.nextInt(); | ||
} | ||
} | ||
|
||
int dist = sc.nextInt(); | ||
Solution obj = new Solution(); | ||
int ans = obj.findCity(n, m, adj, dist); | ||
System.out.println(ans); | ||
} | ||
} | ||
} | ||
|
||
// } Driver Code Ends | ||
|
||
|
||
// User function Template for Java | ||
|
||
class Solution { | ||
int findCity(int n, int m, int[][] edges,int distanceThreshold) | ||
{ | ||
//code here | ||
int min=101; | ||
int ans=-1; | ||
int[][] mat=new int[n][n]; | ||
|
||
for(int[] mm: mat) | ||
Arrays.fill(mm,10001); | ||
|
||
for(int i=0;i<n;i++) | ||
mat[i][i]=0; | ||
|
||
for(int[] edge: edges) | ||
mat[edge[0]][edge[1]]=mat[edge[1]][edge[0]]=edge[2]; | ||
|
||
for(int k=0;k<n;k++) | ||
{ | ||
for(int i=0;i<n;i++) | ||
{ | ||
for(int j=0;j<n;j++) | ||
{ | ||
mat[i][j] = Math.min(mat[i][j], mat[i][k]+mat[k][j]); | ||
} | ||
} | ||
} | ||
|
||
for(int i=0;i<n;i++) | ||
{ | ||
int cnt=0; | ||
for(int j=0;j<n;j++) | ||
{ | ||
if(mat[i][j]<=distanceThreshold) | ||
cnt++; | ||
} | ||
|
||
min=Math.min(cnt,min); | ||
|
||
if(min==cnt) | ||
ans=i; | ||
} | ||
|
||
return ans; | ||
} | ||
} |
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*logn) | ||
Space complexity - O(n^2) |
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) | ||
Space complexity - O(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,20 @@ | ||
class Solution | ||
{ | ||
public int maxSubarrayLength(int[] nums, int k) | ||
{ | ||
int ans = 0; | ||
Map<Integer, Integer> count = new HashMap<>(); | ||
|
||
for (int l = 0, r = 0; r < nums.length; ++r) | ||
{ | ||
count.merge(nums[r], 1, Integer::sum); | ||
|
||
while (count.get(nums[r]) == k + 1) | ||
count.merge(nums[l++], -1, Integer::sum); | ||
|
||
ans = Math.max(ans, r - l + 1); | ||
} | ||
|
||
return ans; | ||
} | ||
} |