From b167589350d9e5758a36c834b51a0a23b3127c97 Mon Sep 17 00:00:00 2001 From: Divyansh Singh Date: Thu, 29 Jun 2023 00:46:56 -0500 Subject: [PATCH 1/3] Activity Selection Greedy --- .DS_Store | Bin 10244 -> 10244 bytes Greedy/activitySelection.java | 40 ++++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+) create mode 100644 Greedy/activitySelection.java diff --git a/.DS_Store b/.DS_Store index e8fab9431267293315553dba5c560fab5de76612..1ea8cf4bef7332581ee963eb357dcafad268c3c9 100644 GIT binary patch delta 169 zcmZn(XbIS$FIdmU;LcFQkP3t;43# 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; i Date: Thu, 29 Jun 2023 21:42:52 -0500 Subject: [PATCH 2/3] More Problems on Greedy --- Greedy/fractionalKnapsack.java | 40 ++++++++++++++++++++++++++++++++++ Greedy/indianCoins.java | 27 +++++++++++++++++++++++ 2 files changed, 67 insertions(+) create mode 100644 Greedy/fractionalKnapsack.java create mode 100644 Greedy/indianCoins.java diff --git a/Greedy/fractionalKnapsack.java b/Greedy/fractionalKnapsack.java new file mode 100644 index 0000000..8d12169 --- /dev/null +++ b/Greedy/fractionalKnapsack.java @@ -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 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 Date: Sun, 2 Jul 2023 18:44:08 -0500 Subject: [PATCH 3/3] greedy theory done --- Greedy/chocola.java | 51 +++++++++++++++++++++++++++++++++++++++++ Greedy/jobSequence.java | 44 +++++++++++++++++++++++++++++++++++ 2 files changed, 95 insertions(+) create mode 100644 Greedy/chocola.java create mode 100644 Greedy/jobSequence.java diff --git a/Greedy/chocola.java b/Greedy/chocola.java new file mode 100644 index 0000000..c3d070f --- /dev/null +++ b/Greedy/chocola.java @@ -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(hcostHori[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 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