diff --git a/.DS_Store b/.DS_Store index e8fab94..1ea8cf4 100644 Binary files a/.DS_Store and b/.DS_Store differ diff --git a/Greedy/activitySelection.java b/Greedy/activitySelection.java new file mode 100644 index 0000000..eb705ca --- /dev/null +++ b/Greedy/activitySelection.java @@ -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 o[2])); + // + + //intitializing the variabes + int maxAct = 0; + ArrayList ar =new ArrayList<>(); + + //add activity 1 anyways + maxAct =1; + ar.add(sortingEnding[0][0]); + int lastEnd = sortingEnding[0][2]; + for(int i =1; icostHori[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 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); + } +} diff --git a/Greedy/indianCoins.java b/Greedy/indianCoins.java new file mode 100644 index 0000000..8f07d9c --- /dev/null +++ b/Greedy/indianCoins.java @@ -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 arr = new ArrayList<>(); + + for(int i=0; i obj1.profit-obj2.profit); //ascending order + Collections.sort(arr, (obj1, obj2) -> obj2.profit-obj1.profit); //descending order + ArrayList ans = new ArrayList<>(); + int time = 0; + for(int i=0; itime) + { + 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