Giter Club home page Giter Club logo

datastructureassignment's Introduction

DataStructureAssignment

Refer From here https://www.geeksforgeeks.org/job-sequencing-problem-set-3-using-treeset-in-java/

import java.io.; import java.util.;

public class Solution {

// Job class 
public static class Job { 
	char id; 
	int deadline; 
	int profit; 

	// Constructor 
	Job(char id, int deadline, int profit) 
	{ 
		this.id = id; 
		this.deadline = deadline; 
		this.profit = profit; 
	} 
} 

public static class Sorted implements Comparator { 
	
	// Function to implement comparator 
	public int compare(Object o1, Object o2) 
	{ 
		Job j1 = (Job)o1; 
		Job j2 = (Job)o2; 

		if (j1.profit != j2.profit) 
			return j2.profit - j1.profit; 
		else
			return j2.deadline - j1.deadline; 
	} 
} 

// Function to print job scheduling 
public static void printJobScheduling(Job jobs[], int n) 
{ 
	// Creating object of Sorted class 
	Sorted sorter = new Sorted(); 
	
	Arrays.sort(jobs, sorter); 

	// Creating TreeSet Object 
	TreeSet<Integer> ts = new TreeSet<>(); 

	for (int i = 0; i < n; i++) 
		ts.add(i); 

	for (int i = 0; i < n; i++) { 
		Integer x = ts.floor(jobs[i].deadline - 1); 

		if (x != null) { 
			System.out.print(jobs[i].id + " "); 
			ts.remove(x); 
		} 
	} 
} 

// Driver Code 
public static void main(String[] args) 
{ 
	int n = 5; 
	Job[] jobs = new Job[n]; 

	jobs[0] = new Job('a', 2, 100); 
	jobs[1] = new Job('b', 1, 19); 
	jobs[2] = new Job('c', 2, 27); 
	jobs[3] = new Job('d', 1, 25); 
	jobs[4] = new Job('e', 3, 15); 

	printJobScheduling(jobs, n); 
} 
// Contributed by Dipesh Jain (dipesh_jain) 

}

datastructureassignment's People

Contributors

andy0926 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.