-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
faf814b
commit 6565dd2
Showing
6 changed files
with
939 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,76 @@ | ||
import java.util.Arrays; | ||
import java.util.Scanner; | ||
|
||
public class Aggressive_Cows { | ||
|
||
public static void main(String[] args) { | ||
|
||
/* in this problem we have to maximise the minimum distance between the | ||
* cows since minimum distance is being linearly checked we apply | ||
* binary search on minimum distance */ | ||
|
||
Scanner scn = new Scanner(System.in); | ||
int nos = scn.nextInt(); // input for no. of stalls | ||
int noc = scn.nextInt(); // input for no. of cows | ||
|
||
int[] arr = new int[nos]; // storing the position of stalls in an array | ||
|
||
for (int i = 0; i < arr.length; i++) { | ||
arr[i] = scn.nextInt(); | ||
} | ||
|
||
Arrays.sort(arr); // to sort the positions of stalls in ascending order | ||
|
||
int finalAns = 0; | ||
|
||
int lo = 0; | ||
int hi = arr[arr.length - 1] - arr[0]; | ||
|
||
while (lo <= hi) { | ||
|
||
int mid = (lo + hi) / 2; | ||
|
||
if (isItPossible(nos, noc, arr, mid)) { | ||
|
||
finalAns = mid; | ||
lo = mid + 1; | ||
|
||
}else{ | ||
hi = mid - 1; | ||
} | ||
} | ||
|
||
System.out.println(finalAns); | ||
|
||
} | ||
|
||
// function to check if a particular arrangement of cows is possible or not | ||
private static boolean isItPossible(int nos, int noc, int[] arr, int mid) { | ||
|
||
int cowsPlaced = 1; | ||
int lastCowPos = arr[0]; // position at which cow is placed | ||
|
||
for (int i = 1; i < arr.length; i++) { | ||
|
||
if (arr[i] - lastCowPos >= mid) { | ||
cowsPlaced++; | ||
lastCowPos = arr[i]; | ||
|
||
if (cowsPlaced == noc) { | ||
return true; | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
|
||
// SAMPLE INPUT: 5 3 | ||
// 1 | ||
// 2 | ||
// 8 | ||
// 4 | ||
// 9 | ||
// | ||
// Output: | ||
// 3 |
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,110 @@ | ||
// C# Program for Boyer Moore String Matching Algorithm | ||
|
||
using System; | ||
|
||
public class Algorithm | ||
{ | ||
static int CHARACTERS = 256; | ||
|
||
// Getting maximum of two integers | ||
static int max (int a, int b) { return (a > b)? a: b; } | ||
|
||
// Bad Character Pre-Processing Function | ||
static void badChar( char []str, int size,int []badCharacter) | ||
{ | ||
int i; | ||
|
||
// Initializing all occurences to -1 | ||
for (i = 0; i < CHARACTERS; i++) | ||
badCharacter[i] = -1; | ||
|
||
// Filling the Actual Value | ||
for (i = 0; i < size; i++) | ||
badCharacter[(int) str[i]] = i; | ||
} | ||
|
||
// Pattern Searching Function | ||
static void search( char []txt, char []pat) | ||
{ | ||
int m = pat.Length; | ||
int n = txt.Length; | ||
|
||
int []Character = new int[CHARACTERS]; | ||
badChar(pat, m, Character); | ||
|
||
/* | ||
s is used to keep track of | ||
pattern shifting with respect to text | ||
*/ | ||
|
||
int s = 0; | ||
|
||
while(s <= (n - m)) | ||
{ | ||
int j = m - 1; | ||
|
||
while(j >= 0 && pat[j] == txt[s+j]) | ||
j--; | ||
|
||
/* | ||
If the pattern is present at current | ||
shift, then index j will become -1 after | ||
the above loop | ||
*/ | ||
|
||
if (j < 0) | ||
{ | ||
Console.WriteLine("Pattern occurs at index: " + s); | ||
s += (s+m < n)? m-Character[txt[s+m]] : 1; | ||
} | ||
|
||
else | ||
s += max(1, j - Character[txt[s+j]]); | ||
|
||
} | ||
} | ||
|
||
public static void Main() | ||
{ | ||
Console.WriteLine("Enter The String Value: "); | ||
String valueEntered = Console.ReadLine(); | ||
Console.WriteLine("Enter The Pattern To Search: "); | ||
String pattern = Console.ReadLine(); | ||
Console.WriteLine(); | ||
|
||
char []txt = valueEntered.ToCharArray(); | ||
char []pat = pattern.ToCharArray(); | ||
search(txt, pat); | ||
} | ||
} | ||
|
||
/** | ||
Enter The String Value: | ||
ABAAABCD | ||
Enter The Pattern To Search: | ||
ABC | ||
Pattern occurs at index: 4 | ||
-------------------------------------------------- | ||
Enter The String Value: | ||
AABAACAADAABAABA | ||
Enter The Pattern To Search: | ||
AABA | ||
Pattern occurs at index: 0 | ||
Pattern occurs at index: 9 | ||
Pattern occurs at index: 12 | ||
-------------------------------------------------- | ||
Enter The String Value: | ||
THIS IS A TEST TEXT | ||
Enter The Pattern To Search: | ||
TEST | ||
Pattern occurs at index: 10 | ||
*/ |
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,180 @@ | ||
/* | ||
Johnson’s algorithm for All-pairs shortest paths | ||
Given a weighted Directed Graph where the weights may be negative, | ||
find the shortest path between every pair of vertices in the Graph using | ||
Johnson’s Algorithm. | ||
*/ | ||
|
||
int MAX_INT = 9223372036854775807; | ||
|
||
int minDistance(distance, visited) | ||
{ | ||
var minimum = MAX_INT; | ||
var minVertex = 0; | ||
|
||
var v = distance.length; | ||
|
||
for(var vertex = 0; vertex < v; vertex++) | ||
{ | ||
if( (minimum > distance[vertex]) && (visited[vertex] == false) ) | ||
{ | ||
minimum = distance[vertex]; | ||
minVertex = vertex; | ||
} | ||
} | ||
return minVertex; | ||
} | ||
|
||
void Dijkstra(graph, modified, src) | ||
{ | ||
var num_vertices = graph.length; | ||
var distance = new List(num_vertices); | ||
|
||
var visited = new List(num_vertices); | ||
|
||
for (var i = 0; i < num_vertices; i++) | ||
{ | ||
distance[i] = MAX_INT; | ||
visited[i] = false; | ||
} | ||
|
||
distance[src] = 0; | ||
|
||
for(var count = 0; count < num_vertices; count++) | ||
{ | ||
var curVertex = minDistance(distance, visited); | ||
visited[curVertex] = true; | ||
for(var vertex = 0; vertex < num_vertices; vertex++) | ||
{ | ||
if ((visited[vertex] == false) && (distance[vertex] > (distance[curVertex] + | ||
modified[curVertex][vertex])) && (graph[curVertex][vertex] != 0)) | ||
{ | ||
distance[vertex] = (distance[curVertex] + modified[curVertex][vertex]); | ||
} | ||
} | ||
} | ||
|
||
for(var vertex = 0; vertex < num_vertices; vertex++) | ||
{ | ||
print('Vertex ${vertex} : ${distance[vertex]}'); | ||
} | ||
} | ||
|
||
List BellmanFord(edges, graph, num_vertices) | ||
{ | ||
var distance = new List(num_vertices+1); | ||
|
||
for(var i = 0; i <= num_vertices; i++) | ||
{ | ||
distance[i]=MAX_INT; | ||
} | ||
|
||
distance[num_vertices] = 0; | ||
|
||
for(var i = 0; i < num_vertices; i++) | ||
{ | ||
edges.add([num_vertices, i, 0]); | ||
} | ||
|
||
for(var i = 0; i < num_vertices; i++) | ||
{ | ||
for(var j in edges) | ||
{ | ||
|
||
if((distance[j[0]] != MAX_INT) && (distance[j[0]] + j[2] < distance[j[1]]) ) | ||
{ | ||
distance[j[1]] = distance[j[0]] + j[2]; | ||
} | ||
} | ||
} | ||
|
||
return distance; | ||
} | ||
|
||
|
||
void JohnsonAlgorithm(graph) | ||
{ | ||
var edges = new List(); | ||
|
||
for(var i = 0; i < graph.length ; i++) | ||
{ | ||
for(var j = 0; j < graph[i].length; j++) | ||
{ | ||
if(graph[i][j] != 0) | ||
{ | ||
edges.add([i, j, graph[i][j]]); | ||
} | ||
} | ||
} | ||
|
||
var modifiedwei = BellmanFord(edges, graph, graph.length); | ||
|
||
var modified = [[0,0,0,0], | ||
[0,0,0,0], | ||
[0,0,0,0], | ||
[0,0,0,0]]; | ||
|
||
for(var i = 0; i < graph.length; i++) | ||
{ | ||
for(var j = 0; j < graph[i].length; j++) | ||
{ | ||
if(graph[i][j] != 0) | ||
{ | ||
modified[i][j] = (graph[i][j] + | ||
modifiedwei[i] - modifiedwei[j]); | ||
} | ||
} | ||
} | ||
|
||
print ('Modified Graph: ${modified}'); | ||
|
||
for(var src = 0; src < graph.length; src++) | ||
{ | ||
print ('\nShortest Distance with vertex ${src} as the source:\n'); | ||
Dijkstra(graph, modified, src); | ||
} | ||
} | ||
|
||
void main() { | ||
|
||
var graph = [[0, -8, 2, 4], | ||
[0, 0, 2, 6], | ||
[0, 0, 0, 2], | ||
[0, 0, 0, 0]]; | ||
|
||
JohnsonAlgorithm(graph); | ||
} | ||
|
||
/* | ||
Modified Graph: [[0, 0, 8, 8], [0, 0, 0, 2], [0, 0, 0, 0], [0, 0, 0, 0]] | ||
Shortest Distance with vertex 0 as the source: | ||
Vertex 0 : 0 | ||
Vertex 1 : 0 | ||
Vertex 2 : 0 | ||
Vertex 3 : 0 | ||
Shortest Distance with vertex 1 as the source: | ||
Vertex 0 : 9223372036854775807 | ||
Vertex 1 : 0 | ||
Vertex 2 : 0 | ||
Vertex 3 : 0 | ||
Shortest Distance with vertex 2 as the source: | ||
Vertex 0 : 9223372036854775807 | ||
Vertex 1 : 9223372036854775807 | ||
Vertex 2 : 0 | ||
Vertex 3 : 0 | ||
Shortest Distance with vertex 3 as the source: | ||
Vertex 0 : 9223372036854775807 | ||
Vertex 1 : 9223372036854775807 | ||
Vertex 2 : -9223372036854775801 | ||
Vertex 3 : 0 | ||
*/ |
Oops, something went wrong.