Skip to content

Commit

Permalink
Added codes for 28 March
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Mar 28, 2024
1 parent cb9529d commit 87ce6e7
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 0 deletions.
84 changes: 84 additions & 0 deletions GeeksForGeeks/March/28-3-24/GFG.java
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/March/28-3-24/README.md
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)
2 changes: 2 additions & 0 deletions LeetCode/March/28-3-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
20 changes: 20 additions & 0 deletions LeetCode/March/28-3-24/Solution.java
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;
}
}

0 comments on commit 87ce6e7

Please sign in to comment.