Skip to content

Commit 99837ec

Browse files
committed
#Modification 139
1 parent c0abf7c commit 99837ec

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+319
-36
lines changed

README.md

Lines changed: 28 additions & 1 deletion

greedy/Problem_02.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ public Problem_02(char id, int profit, int deadline){
1616
this.profit = profit;
1717
}
1818

19-
void printJobScheduling(ArrayList<Problem_02> arr, int t){
20-
int n = arr.size();
19+
void printJobScheduling(ArrayList<Problem_02> arr){
2120

22-
Collections.sort(arr, (a,b) -> b.profit - a.profit);
23-
boolean result[] = new boolean[t];
24-
char job[] = new char[t];
21+
arr.sort((a, b) -> b.profit - a.profit);
22+
boolean[] result = new boolean[3];
23+
char[] job = new char[3];
2524

26-
for(int i = 0; i < n; i++){
27-
for(int j = Math.min(t - 1, arr.get(i).deadline - 1); j >= 0; j--){
28-
if(result[j] == false){
25+
for (Problem_02 problem_02 : arr) {
26+
for (int j = Math.min(3 - 1, problem_02.deadline - 1); j >= 0; j--) {
27+
if (!result[j]) {
2928
result[j] = true;
30-
job[j] = arr.get(i).id;
29+
job[j] = problem_02.id;
3130
break;
3231
}
3332
}
@@ -38,8 +37,8 @@ void printJobScheduling(ArrayList<Problem_02> arr, int t){
3837
System.out.println();
3938
}
4039

41-
public static void main(String args[]) {
42-
ArrayList<Problem_02> arr = new ArrayList<Problem_02>();
40+
public static void main(String[] args) {
41+
ArrayList<Problem_02> arr = new ArrayList<>();
4342

4443
arr.add(new Problem_02('a', 2, 100));
4544
arr.add(new Problem_02('b', 1, 19));
@@ -53,6 +52,6 @@ public static void main(String args[]) {
5352
Problem_02 job = new Problem_02();
5453

5554
// Calling function
56-
job.printJobScheduling(arr, 3);
55+
job.printJobScheduling(arr);
5756
}
5857
}

greedy/Problem_04.java

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package greedy;
2+
3+
// Water Connection Problem
4+
// Question From GFG
5+
import java.util.*;
6+
7+
public class Problem_04 {
8+
9+
static class House{
10+
int in;
11+
int out;
12+
int dia;
13+
14+
House(int in,int out,int dia){
15+
this.in = in;
16+
this.out = out;
17+
this.dia = dia;
18+
}
19+
}
20+
21+
ArrayList<ArrayList<Integer>> solve(int n, int p, ArrayList<Integer> a ,ArrayList<Integer> b ,ArrayList<Integer> d) {
22+
HashMap<Integer,House> map = new HashMap<>();
23+
int[] in = new int[n + 1];
24+
int[] out = new int[n + 1];
25+
26+
for(int i = 0; i < p; i++){
27+
out[a.get(i)] = 1;
28+
in[b.get(i)] = 1;
29+
map.put(a.get(i), new House(a.get(i), b.get(i), d.get(i)));
30+
}
31+
32+
ArrayList<ArrayList<Integer>> ans = new ArrayList<>();
33+
for(int i = 1; i <= n; i++){
34+
if(in[i] == 0 && out[i] == 1){
35+
House curr = map.get(i);
36+
int e = curr.out;
37+
int dia = curr.dia;
38+
39+
while(map.containsKey(curr.out)) {
40+
curr = map.get(curr.out);
41+
e = curr.out;
42+
dia = Math.min(dia,curr.dia);
43+
}
44+
45+
ArrayList<Integer> arr = new ArrayList<>();
46+
arr.add(i);
47+
arr.add(e);
48+
arr.add(dia);
49+
ans.add(arr);
50+
}
51+
}
52+
return ans;
53+
}
54+
55+
public static void main(String[] args) {
56+
// System.out.println(solve());
57+
}
58+
}

greedy/Fractional_Knapsack.java renamed to greedy/Problem_05.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<---------------------------------------------------------------------------------------------------------------------->
1717
* */
1818

19-
class Fractional_Knapsack {
19+
class Problem_05 {
2020

2121
//Function to get the maximum total value in the knapsack.
2222
private static double getMaxValue(int[] wt, int[] val, int capacity) {
@@ -27,17 +27,11 @@ private static double getMaxValue(int[] wt, int[] val, int capacity) {
2727
iVal[i] = new ItemValue(wt[i], val[i], i);
2828

2929
// sorting items by value;
30-
Arrays.sort(iVal, new Comparator<ItemValue>() {
31-
@Override
32-
public int compare(ItemValue o1, ItemValue o2) {
33-
return o2.cost.compareTo(o1.cost);
34-
}
35-
});
30+
Arrays.sort(iVal, (o1, o2) -> o2.cost.compareTo(o1.cost));
3631

3732
double totalValue = 0d;
3833

3934
for (ItemValue i : iVal) {
40-
4135
int curWt = (int)i.wt;
4236
int curVal = (int)i.val;
4337

@@ -50,7 +44,6 @@ public int compare(ItemValue o1, ItemValue o2) {
5044
// item can't be picked whole
5145
double fraction = ((double)capacity / (double)curWt);
5246
totalValue += (curVal * fraction);
53-
capacity = (int)(capacity - (curWt * fraction));
5447
break;
5548
}
5649
}
@@ -64,18 +57,16 @@ static class ItemValue {
6457
double wt, val, ind;
6558

6659
// item value function
67-
public ItemValue(int wt, int val, int ind)
68-
{
60+
public ItemValue(int wt, int val, int ind) {
6961
this.wt = wt;
7062
this.val = val;
7163
this.ind = ind;
72-
cost = new Double((double)val / (double)wt);
64+
cost = (double) val / (double) wt;
7365
}
7466
}
7567

7668
// Driver code
77-
public static void main(String[] args)
78-
{
69+
public static void main(String[] args) {
7970
int[] wt = { 10, 40, 20, 30 };
8071
int[] val = { 60, 40, 100, 120 };
8172
int capacity = 50;
@@ -88,7 +79,7 @@ public static void main(String[] args)
8879
}
8980
/*
9081
<---------------------------------------------------------------------------------------------------------------------->
91-
| Time Complexity -> | O(n.logn)
82+
| Time Complexity -> | O(n.log n)
9283
| Auxiliary Space -> | O(1)
9384
<---------------------------------------------------------------------------------------------------------------------->
9485
*/

greedy/Problem_06.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package greedy;
2+
3+
// Min number of coins [Greedy + DP]
4+
5+
// Variant 1 - Min Elements(CodeStudio)
6+
// Variant 2 - Min no of coins to make a target
7+
// Variant 1 - Min no of coins to make a amount
8+
9+
// Amazon + Adobe
10+
11+
public class Problem_06 {
12+
public static int solve(int ind, int[] arr, int x, int[][] dp){
13+
if(ind == 0){
14+
if(x % arr[0]==0)
15+
return x/arr[0];
16+
else return 1000000000+7;
17+
}
18+
19+
if(dp[ind][x] != -1)
20+
return dp[ind][x];
21+
22+
int notTake = solve(ind - 1, arr, x, dp);
23+
int take = Integer.MAX_VALUE;
24+
25+
if(arr[ind] <= x){
26+
take = 1 + solve(ind, arr, x - arr[ind], dp);
27+
}
28+
29+
return dp[ind][x] = Math.min(take, notTake);
30+
}
31+
32+
// public static int minimumElements(int[] num, int x) {
33+
// //Write your code here.. //
34+
// int n = num.length;
35+
// int[][] dp = new int[n][x+1];
36+
//
37+
// for(int i = 0; i < n; i++)
38+
// for(int j = 0; j < x + 1; j++)
39+
// dp[i][j] = -1;
40+
//
41+
// int ans = solve(n - 1, num, x, dp);
42+
// if(ans >= 1000000000 + 7)
43+
// return -1;
44+
//
45+
// return ans;
46+
//
47+
// for(int T = 0; T <= x; T++){
48+
// if( T % num[0] == 0)
49+
// dp[0][T] = T / num[0];
50+
//
51+
// else dp[0][T] = 1000000000+7;
52+
// }
53+
//
54+
// for(int ind = 1; ind < n; ind++){
55+
// for(int T = 0; T <= x; T++){
56+
// int notTake = 0 + dp[ind-1][T];
57+
// int take = Integer.MAX_VALUE;
58+
// if(num[ind] <= T){
59+
// take = 1 + dp[ind][T-num[ind]];
60+
// }
61+
// dp[ind][T] = Math.min(take,notTake);
62+
// }
63+
// }
64+
//
65+
// if(ans >= 1000000000 + 7)
66+
// return -1;
67+
// return ans;
68+
// }
69+
//
70+
}

greedy/Problem_3.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static void printCode(HuffmanNode root, String s){
4242

4343
// recursive calls for left and
4444
// right sub-tree of the generated tree.
45+
assert root.left != null;
4546
printCode(root.left, s + "0");
4647
printCode(root.right, s + "1");
4748
}
@@ -63,7 +64,7 @@ public static void main(String[] args) {
6364

6465
// creating a priority queue q.
6566
// makes a min-priority queue(min-heap).
66-
PriorityQueue<HuffmanNode> q = new PriorityQueue<HuffmanNode>(n, (Comparator<? super HuffmanNode>) new MyComparator());
67+
PriorityQueue<HuffmanNode> q = new PriorityQueue<>(n, (Comparator<? super HuffmanNode>) new MyComparator());
6768

6869
for (int i = 0; i < n; i++) {
6970

@@ -100,6 +101,7 @@ public static void main(String[] args) {
100101
HuffmanNode f = new HuffmanNode();
101102

102103
// to the sum of the frequency of the two nodes assigning values to the f node.
104+
assert y != null;
103105
f.data = x.data + y.data;
104106
f.c = '-';
105107

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.

0 commit comments

Comments
 (0)