Skip to content

Commit

Permalink
added greedy problems
Browse files Browse the repository at this point in the history
  • Loading branch information
anirudherabelly committed Oct 16, 2018
1 parent 9db78f5 commit 7fd9150
Show file tree
Hide file tree
Showing 2 changed files with 143 additions and 0 deletions.
82 changes: 82 additions & 0 deletions Greedy/ActivitySelection/ActivitySelection.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Given N activities with their start and finish times.
Select the maximum number of activities that can be performed by a single person,
assuming that a person can only work on a single activity at a time.
Note : The start time and end time of two activities may coincide.
Input:
The first line contains T denoting the number of testcases.
Then follows description of testcases. First line is N number of activities
then second line contains N numbers which are starting time of activies.
Third line contains N finishing time of activities.
Output:
For each test case, output a single number denoting maximum activites which can be performed in new line.
Constraints:
1<=T<=50
1<=N<=1000
1<=A[i]<=100
Example:
Input:
1
6
1 3 0 5 8 5
2 4 6 7 9 9
Output:
4
*/
import java.util.*;
import java.lang.*;
import java.io.*;
class Activity implements Comparable<Activity>{
int startTime;
int endTime;
Activity(int start,int end){
this.startTime=start;
this.endTime=end;
}
public int compareTo(Activity a){
return this.endTime-a.endTime;
}
}
class GFG
{
public static void main (String[] args)
{
//code
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
ArrayList<Activity> al=new ArrayList<Activity>();
int n=sc.nextInt(),temp;
int[] startAr=new int[n];
for(int j=0;j<n;j++){
startAr[j]=sc.nextInt();
}
for(int j=0;j<n;j++){
temp=sc.nextInt();
al.add(new Activity(startAr[j],temp));
}
Collections.sort(al);
ActivityProblem(al,n);
}
}
private static void ActivityProblem(ArrayList<Activity> al,int n){
Activity prev=al.get(0),curr;
int count=1;
for(int i=1;i<n;i++){
curr=al.get(i);
if(prev.endTime<=curr.startTime){
count++;
prev=curr;
}
}
System.out.println(count);
}
}
61 changes: 61 additions & 0 deletions Greedy/MinimumCoins/MinimumNumOfCoins.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Minimum number of Coins
----------------------------------------------------------------------------------
Given a value N, if we want to make change for N Rs,
and we have infinite supply of each of the denominations in Indian currency,
i.e., we have infinite supply of { 1, 2, 5, 10, 20, 50, 100, 500, 1000} valued coins/notes,
Find the minimum number of coins and/or notes needed to make the change.
Input:
The first line of input contains an integer T denoting the number of test cases.
Each test case consist of an Integer value N denoting the amount to get change for.
Output:
Print all the possible denominations needed to make the change in a separate line.
Constraints:
1 ≤ T ≤ 30
1 ≤ N ≤ 2000
Example:
Input
1
43
Output
20 20 2 1
*/

import java.util.*;
import java.lang.*;
import java.io.*;

class GFG {
public static void main (String[] args) {
//code
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
int[] array={1, 2, 5, 10, 20, 50, 100, 500, 1000};
for(int i=0;i<t;i++){
int n=sc.nextInt();
MinimumNumberOfCoins(array,n);
System.out.println();
}
}
private static void MinimumNumberOfCoins(int[] array,int n){
int length=array.length;
for(int i=length-1;i>=0;i--){
if(array[i]<=n){
int numOfCoins=n/array[i];
for(int j=0;j<numOfCoins;j++)System.out.print(array[i]+" ");
n%=array[i];
}
}
}
}

0 comments on commit 7fd9150

Please sign in to comment.