Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

activity selection java solution

public static void printMaxActivities(int s[], int f[], int n)
    {
    int i, j;
      
    System.out.print("Following activities are selected : n");
    i = 0;
    System.out.print(i+" ");
    for (j = 1; j < n; j++)
    {
         if (s[j] >= f[i])
         {
              System.out.print(j+" ");
              i = j;
          }
     }
    }
Comment

Activity Selection Solution in Java

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

class Activity
{
	int start;
	int finish;

	Activity(int s, int f)
	{
		start = s;
		finish = f;
	}
}

class MyCmp implements Comparator<Activity>
{
	public int compare(Activity a1, Activity a2)
	{
		return a1.finish - a2.finish;
	}
}

class GFG 
{ 

    static int maxActivity(Activity arr[], int n)
    {	
	    Arrays.sort(arr, new MyCmp());

	    int res = 1;
	    int prev = 0;

	    for(int curr = 1; curr < n; curr++)
	    {
	       
	    	if(arr[curr].start >= arr[prev].finish)
	    	{
	    		res++;

	    		prev = curr;
	    	}
	    }

	    return res;
    }

    public static void main(String args[]) 
    {
    	Activity arr[] = {new Activity(12, 25), 
    					  new Activity(10, 20), 
    					  new Activity(20, 30)};
    	int n = 3;

        System.out.println(maxActivity(arr, n));

    } 

}
Comment

PREVIOUS NEXT
Code Example
Java :: variable for java 
Java :: change the default port in spring boot codegrepper 
Java :: java memory cleaner 
Java :: java random.nextint 
Java :: expandablelistview android 
Java :: hasAuthority method not working with thymeleaf 
Java :: linked list introduction 
Java :: java windowbuilder launch on second monitor 
Java :: bluetooth chat example android server 
Java :: Which package contains the Math classin java? 
Java :: java data table source tymeLeaf 
Java :: ordenar numeros java 
Java :: string stack in java 
Java :: how to disable the auto-configuration? 
Java :: break statement in Java switch...case 
Java :: sethomebutton listener android 
Java :: Spring AOP 
Java :: Pattern using recursion in Java 
Java :: Fast Search in java 
Java :: Java double Keyword 
Java :: origin model color 
Java :: correct lcd initialize 
Java :: crud repository count number of items in a list 
Java :: java structure 
Java :: what is method overriding in java 
Java :: how to replace a character with another character in a string in java 
Java :: how to delete last array in java 
Java :: java deserialize json object 
Java :: import class from package java 
Java :: java swing tablecellrenderer 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =