Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified .DS_Store
Binary file not shown.
40 changes: 40 additions & 0 deletions Greedy/activitySelection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package Greedy;
import java.util.*;
public class activitySelection {
public static void main(String[] args) { //o(nlogn)
int[] start = {1, 3, 0, 5, 8, 5};
int[] end = {2, 4, 6, 7, 9, 9}; //already sorted based on end time

//IF SORTING BASED ON END
//using comparators
int[][] sortingEnding = new int[start.length][3];
for(int i=0; i<start.length; i++)
{
sortingEnding[i][0] = i;
sortingEnding[i][1]=start[i];
sortingEnding[i][2]=end[i];
}
Arrays.sort(sortingEnding, Comparator.comparingDouble(o -> o[2]));
//

//intitializing the variabes
int maxAct = 0;
ArrayList<Integer> ar =new ArrayList<>();

//add activity 1 anyways
maxAct =1;
ar.add(sortingEnding[0][0]);
int lastEnd = sortingEnding[0][2];
for(int i =1; i<end.length; i++)
{
if(lastEnd<=sortingEnding[i][1])
{
ar.add(sortingEnding[i][0]); //add the index of the element that was added
maxAct++; //increment the maxAct
lastEnd=sortingEnding[i][2]; //update the end[]i
}
}
System.out.println(maxAct);
System.out.println(ar);
}
}
51 changes: 51 additions & 0 deletions Greedy/chocola.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package Greedy;
import java.util.*;
public class chocola {
public static void breakDown(int n, int m, Integer costVer[], Integer costHori[])
{
//sort them in descending order
Arrays.sort(costVer, Collections.reverseOrder());
Arrays.sort(costHori, Collections.reverseOrder());

int h = 0, v=0; //vertical and horizontal pointers that will work on costVer and costHori
int hp = 1, vp = 1; //vertical and horizontal pieces checker
int cost=0;
//compare horizonatal and vertical until they reach the length till they can be compared
while(h<costHori.length && v<costVer.length)
{
if(costVer[v]>costHori[h]) //vertical cut
{
cost += costVer[v] * hp;//number of hori pieces * cost of that vert peice
vp++;//number of vertpieces increases
v++;// incremeneting the vert pointer to point to the next cost
}
else//horizontalcut
{
cost += costHori[h] * vp; //number of vertical pieces * cost of that hori peice
hp++; //number of horipieces increases
h++; // incremeneting the hori pointer to point to the next cost
}
}
//remaining vertical cost adding
while(v<costVer.length)
{
cost += costVer[v] * hp;
vp++;
v++;
}
//remaining horizontal cost adding
while(h<costHori.length)
{
cost += costHori[h] * vp;
hp++;
h++;
}
System.out.println("The minimum cost is "+ cost);
}
public static void main(String[] args) {
int n=4; int m=6;
Integer costVer[] = {2, 1, 3, 1, 4};
Integer costHori[] = {4, 1, 2};
breakDown(n, m, costVer, costHori);
}
}
40 changes: 40 additions & 0 deletions Greedy/fractionalKnapsack.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package Greedy;

import java.util.*;


public class fractionalKnapsack {
public static int knapsack(int[] value, int[] weight, int target)
{
int maxVal = 0;
double[][] ratio = new double[value.length][2]; //storing index and ratio of each value/weight
for(int i=0; i<value.length; i++)
{
ratio[i][0] = i;
ratio[i][1] = value[i]/(double)weight[i];
}
Arrays.sort(ratio, Comparator.comparingDouble(o -> o[1])); //sort based on ratios - Higher the ratio better value it will get for its weight
for(int i=ratio.length-1; i>=0; i--) //we want in decreaseing order 6,5,4 etc, coz higher ration values go in first as they will yield the max value for its weight
{
if(weight[(int)ratio[i][0]]<=target) //if the weight at that index is <=target
{
maxVal += value[(int)ratio[i][0]]; //increement value
target -= weight[(int)ratio[i][0]]; //reduce the weight added
}
else
{
maxVal += ratio[i][1]*target; //muliply ratio * weight to get value
//if target is 0 then nothing will be added to max val after multiplication
break; //break coz the target is met
}
}
return maxVal;
}
public static void main(String[] args) {
int[] value = {60, 100, 120};
int[] weight = {10, 20, 30};
int target = 50;
int max = knapsack(value, weight, target);
System.out.println(max);
}
}
27 changes: 27 additions & 0 deletions Greedy/indianCoins.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package Greedy;
import java.util.*;
public class indianCoins {
public static int coinChange(Integer[] coins, int target)
{
int minChange = 0;
Arrays.sort(coins, Comparator.reverseOrder());
for(int i=0; i<coins.length; i++)
{
if(coins[i]<=target)
{
while(coins[i]<=target)
{
minChange++;
target -= coins[i];
}
}
}
return minChange;
}
public static void main(String[] args) {
Integer[] coins = {1,2,5,10,20,50,100,500,2000};
int target = 590; //500+50+20+20
int target1 = 121; //100+20+1
System.out.println("Minimum number of change is "+coinChange(coins, target));
}
}
44 changes: 44 additions & 0 deletions Greedy/jobSequence.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package Greedy;
import java.util.*;
public class jobSequence {
public static class jobs
{
int timePeriod;
int profit;
int id; //job id which will be shown later

public jobs(int i, int p, int id)
{
this.timePeriod=i;
this.profit=p;
this.id = id;
}
}
public static void main(String[] args) {
int[][] jobsInfo = {{1,40}, {1,30}, {4,20}, {1,10}};
ArrayList<jobs> arr = new ArrayList<>();

for(int i=0; i<jobsInfo.length; i++)
{
arr.add(new jobs(jobsInfo[i][0], jobsInfo[i][1], i));
}
//Collections.sort(arr, (obj1, obj2) -> obj1.profit-obj2.profit); //ascending order
Collections.sort(arr, (obj1, obj2) -> obj2.profit-obj1.profit); //descending order
ArrayList<Integer> ans = new ArrayList<>();
int time = 0;
for(int i=0; i<arr.size(); i++)
{
if(arr.get(i).timePeriod>time)
{
ans.add(arr.get(i).id);
time++;
}
}
System.out.println("size is"+ ans.size());
System.out.println("Job sequence is: ");
for(int i=0; i<ans.size(); i++)
{
System.out.println(ans.get(i));
}
}
}