public static int select(int s[] , int e[]) {
//sorted endlist given
int count = 0;
ArrayList<Integer> al = new ArrayList<>();
count = 1;
al.add(0);
int lastele = e[0];
for (int i = 1; i < e.length; i++) {
if (s[i] >= lastele) {
count++;
al.add(i);
lastele = e[i];
}
}
return count;
}
public static int noSorted(int s[] , int e[]) {
int act[][] = new int[s.length][3];
for (int i = 0; i < s.length; i++) {
act[i][0] = i;
act[i][1] = s[i];
act[i][2] = e[i];
}
Arrays.sort(act,Comparator.comparing(o -> o[2]));
int c = 0;
ArrayList<Integer> al = new ArrayList<>();
int lastele = act[0][2];
al.add(act[0][0]);
c = 1;
for (int i = 0; i < act.length; i++) {
if (act[i][1] >= lastele) {
c++;
al.add(act[i][0]);
lastele = act[i][2];
}
}
return c;
}
static int solve(int bt[] ) {
// code here
int n = bt.length;
Arrays.sort(bt);
int wt = 0;
int t = 0;
for(int i = 0; i < n ; i++){
wt += t;
t += bt[i];
}
return wt/n;
}
class Solution {
public boolean canJump(int[] nums) {
int max = 0;
for(int i = 0 ; i < nums.length ; i++){
if(i > max){
return false;
}
max = Math.max(max,nums[i]+i);
}
return true;
}
}
Approach → Yaha humne range liye ki ek jump mein konse element se konse element tak ja shakte hn (Coverage to lastjump) and destination banaya ki kha tak jaana hn minimum jumps mein for loop laga ke coverage mein max leliya jump ka or next jumps annalisye karte gaye agar lastjump or coverage same hote hn tho jumpcount ko inrease kardenge or coverage mein new max jump daaldenge in the last hum lastjump or destination ko match karenge agar same aata hn tho jumps ko return kardenge
class Solution {
public int jump(int[] nums) {
int destination = nums.length-1;
int jump = 0;
int coverage = 0;
int lastjump = 0;
if(nums.length == 1){
return 0;
}
for(int i = 0 ; i < nums.length-1 ; i++){
coverage = Math.max(coverage,i+nums[i]);
if(i == lastjump){
lastjump = coverage;
jump++;
}
if(lastjump >= destination){
return jump;
}
}
return jump;
}
}
Code for sorting Objects → Collection.sort(al,(a,b) -> b.profit - a.profit); sorting on the bases of profit in object if we sort(DESC) on the bases of profit and make day size array where we put in on the basis of deadline in the array and if deadline is filled with work we can go one day back to perform those job
class Solution
{
//Function to find the maximum profit and the number of jobs done.
int[] JobScheduling(Job arr[], int n)
{
// Your code here
Arrays.sort(arr , (a,b) -> b.profit - a.profit);
int maxdeadline = 0;
for(int i = 0 ; i < arr.length ; i++){
//maxdeadline mein humne yhe store kiya ki sabse jayada kis
//job ki deadline hn uske bases pe new array banayenge jo deadline
//ke bases pe task ko array mein daalega
maxdeadline = Math.max(maxdeadline , arr[i].deadline);
}
int deadline[] = new int[maxdeadline + 1];
Arrays.fill(deadline , -1);
int profit = 0;
int job = 0;
for(int i = 0 ; i < arr.length ; i++){
for(int j = arr[i].deadline ; j > 0 ; j-- ){
if(deadline[j] == -1){
deadline[j] = arr[i].profit;
profit += arr[i].profit;
job++;
break;
}
}
}
return new int[]{job,profit};
}
}
/*
class Job {
int id, profit, deadline;
Job(int x, int y, int z){
this.id = x;
this.deadline = y;
this.profit = z;
}
}
int[][] act = new int[n][3];
for (int i = 0; i < n; i++) {
act[i][0] = i;
act[i][1] = s[i];
act[i][2] = e[i];
}
// Sort meetings by their end time
Arrays.sort(act, Comparator.comparing(o -> o[2]));
// Initialize variables
int c = 1; // Count the first meeting
int lastele = act[0][2]; // End time of the first meeting
// Loop through the rest of the meetings
for (int i = 1; i < act.length; i++) {
if (act[i][1] > lastele) { // If the start time of the current meeting is after the last selected meeting's end time
c++;
lastele = act[i][2]; // Update lastele to the end time of the current meeting
}
}
return c;
class Solution {
public int eraseOverlapIntervals(int[][] in) {
int c = 1;
Arrays.sort(in,Comparator.comparing(o -> o[1]));
int lastele = in[0][1];
for(int i = 1 ; i < in.length ; i++){
if(in[i][0] >= lastele){
c++;
lastele = in[i][1];
}
}
return in.length - c;
}
}
class Solution {
public int[][] insert(int[][] intervals, int[] newInterval) {
ArrayList<int[]> al = new ArrayList<>();
int n = intervals.length;
int i = 0;
//before newIntervals
while(i < n && intervals[i][1] < newInterval[0]){
al.add(intervals[i]);
i++;
}
//At the time of overlapping
while(i < n && intervals[i][0] <= newInterval[1]){
newInterval[0] = Math.min(newInterval[0],intervals[i][0]);
newInterval[1] = Math.max(newInterval[1],intervals[i][1]);
i++;
}
al.add(newInterval);
//after overlapping part
while(i < n){
al.add(intervals[i]);
i++;
}
return al.toArray(new int [al.size()][]);
}
}
//Brute force on pass 8 tastcases
// add your code here
int mc = 0;
for(int i = 0 ; i < n ; i++){
int c = 1;
for(int j = i+1 ; j < n ; j++){
if((arr[i] >= arr[j] && arr[i] <= dep[j]) || (arr[j] >= arr[i] && arr[j] <= dep[i])){
c++;
}
}
mc = Math.max(mc,c);
}
return mc;