Ayush-Tibrewal / LEETCODE---Java-Solutions-

0 stars 0 forks source link

Greedy ALGO #2

Open Ayush-Tibrewal opened 2 weeks ago

Ayush-Tibrewal commented 2 weeks ago

It operates on the principle of “taking the best option now” without considering the long-term consequences. In most of the greedy solution sorting has been done

Ayush-Tibrewal commented 2 weeks ago

You are given an integer array nums. You are initially positioned at the array's first index, and each element in the array represents your maximum jump length at that position. Return true if you can reach the last index, or false otherwise. (JUMP 1)

Arrya = [2 3 1 1 4] ye batana haii ki hum jump kar payenge kisi value se to reach the last index if(i+ arr[i] == last index ) then we can say yes

int maxIndex = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            if (i + nums[i] > maxIndex) {
                maxIndex = i + nums[i];
            }
            if (maxIndex >= nums.length - 1) {
                return true;
            }
            // what if first number is 0 hence if maxindex is equal to 1 we 
            //  have to return false
            if (i == maxIndex) {
                return false;