-
Notifications
You must be signed in to change notification settings - Fork 0
/
greedy.java
200 lines (190 loc) · 5.94 KB
/
greedy.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
public class greedy {
public static void Activity(int s[],int e[]){
int tdarr[][]=new int [s.length][3];
for(int i = 0;i<s.length;i++){
tdarr[i][0]=i;
tdarr[i][1]=s[i];
tdarr[i][2]=e[i];
}
//Lambda is java comparator for sorting arrayin sort time period
//why we are using this
// we are using this to sort our 2darray base on ending time so that we can perform our algorithem to find best solution
//-----------------------------------
Arrays.sort(tdarr,Comparator.comparingDouble(o->o[2]));
//-----------------------------------
ArrayList<Integer>arr=new ArrayList<>();
// when end array is not sorted
// arr.add(tdarr[0][0]);
// int maxcount = 1;
// int prevend = tdarr[0][2];
// for(int i = 1;i<e.length;i++){
// if(tdarr[i][1]>=prevend){
// maxcount++;
// arr.add(tdarr[i][0]);
// prevend=tdarr[i][2];
// }
// }
// when end array is sorted
arr.add(0);
int maxcount = 1;
int prevend = e[0];
for(int i = 1;i<e.length;i++){
if(s[i]>=prevend){
maxcount++;
arr.add(i);
prevend=e[i];
}
}
System.out.println("COunt"+maxcount+" ");
for(int i=0;i<arr.size();i++){
System.out.print("A"+arr.get(i)+" ");
}
}
public static void knapsack(int value[], int weight[] ,int n){
double arr[][]=new double [value.length][2];
for(int i=0;i<value.length;i++){
arr[i][0]=i;
arr[i][1]=(double)(value[i]/weight[i]);
}
int capacity = n;
int ans = 0;
Arrays.sort(arr,Comparator.comparingDouble(o->o[1]));
for(int i =arr.length-1;i>=0;i--){
int index = (int)arr[i][0];
if(capacity>=weight[index]){
capacity=capacity-weight[index];
ans += value[index];
}
else{
ans+=(int)arr[i][1]*capacity;
}
}
System.out.println("total profit is "+ans);
}
public static void absoluteDiffrence(int arr1[],int arr2[]){
Arrays.sort(arr1);Arrays.sort(arr2);
int ans=0;
for(int i = 0; i<arr1.length;i++){
ans +=Math.abs(arr1[i] - arr2[i]);
}
System.out.println("absolute diffrence is "+ans);
}
public static void rope(int arr[][]){
Arrays.sort(arr,Comparator.comparingDouble(o->o[1]));
int lastend = arr[0][1];
int ans = 1;
for(int i = 1;i<arr.length;i++){
if(arr[i][0]>lastend){
ans++;
lastend = arr[i][1];
}
}
System.out.println(ans);
}
public static void coins(int target,Integer arr[]){
Arrays.sort(arr, Comparator.reverseOrder());
int count = 0;
ArrayList<Integer> arrl = new ArrayList<Integer>();
for(int i = 0;i<arr.length;i++){
if(arr[i]<=target){
while(arr[i]<=target){
target -=arr[i];
count++;
arrl.add(arr[i]);
}
}
}
System.out.println("no.of coins "+count);
for(int i = 0;i<arrl.size();i++){
System.out.print(arrl.get(i)+" ");
}
}
static class job{
int idx;
int deadline;
int profit;
public job(int i , int d , int p){
this.idx = i;
this.deadline = d;
this.profit = p;
}
}
public static void jobSequence(int arr[][]){
int time = 0;
ArrayList<job>jobs =new ArrayList<>();
for(int i = 0;i<arr.length;i++){
jobs.add(new job(i,arr[i][0],arr[i][1]));
}
Collections.sort(jobs,(obj1,obj2)->(obj2.profit-obj1.profit));//dec
ArrayList<Integer> ans = new ArrayList<>();
for(int i = 0;i<jobs.size();i++){
job curr = jobs.get(i);
if(curr.deadline>time){
time++;
ans.add(curr.idx);
}
}
System.out.println("time : "+ time);
for(int i = 0;i<ans.size();i++){
System.out.print(ans.get(i)+" ");
}
}
public static void chocolatebar(Integer virtical[],Integer horizontal[]){
Arrays.sort(virtical,Comparator.reverseOrder()); Arrays.sort(horizontal,Comparator.reverseOrder());
int h = 0; int v = 0;
int hp=1;int vp = 1;
int cost=0;
while(h<horizontal.length&&v<virtical.length){
if(virtical[v]<=horizontal[h]){
cost += horizontal[h]*vp;
h++;hp++;
}
else{
cost += virtical[v]*hp;
v++;vp++;
}
}
while(h<horizontal.length){
cost += horizontal[h]*vp;
h++;hp++;
}
while(v<virtical.length){
cost += virtical[v]*hp;
v++;vp++;
}
System.out.println("the cost of the pices "+cost);
}
public static int kthodd(int arr[],int k){
if(k<=0){
return 0;
}
int l = arr[0];int r = arr[1];
if((r&1)>0){
int count = (int)Math.ceil((r-l+1)/2);
if(k>count){
return 0;
}
else{
return (r-2*k+2);
}
}else{
int count = ((r-l+1)/2);
if(k>count){
return 0;
}
else{
return (r-2*k+1);
}
}
}
public static void main(String arg[]){
int arr[]={-10,10};
int k = 8;
System.out.println(kthodd(arr, k));
}
}