Coding Friends

Sort by

recency

|

8 Discussions

|

  • + 0 comments

    JavaScript

    function minNum(samDaily, kellyDaily, difference) {
         if (kellyDaily <= samDaily) {
            return -1;
        }
        
        let days = 0
        while (difference >= 0){
            difference-= (kellyDaily - samDaily)
            days++
        }
        return days
    }
    
  • + 0 comments

    java 2 liner

    the delta for kelly has to be > 0, or she will never surpass

    the difference / delta are the required days

    adding 1 to difference to account for a day 0 case


    public static int minNum(int samDaily, int kellyDaily, int difference) {
        int kellyDelta = kellyDaily - samDaily;
        return kellyDelta <= 0 ? -1 : (int) Math.ceil((double) (difference + 1) / kellyDelta);
    }
    
  • + 0 comments

    **Binary Search approach ** bool canSurpass(int samDaily, int kellyDaily, int difference, int days) { int samSolved = difference + days * samDaily; int kellySolved = days * kellyDaily; return kellySolved > samSolved; }

    int minNum(int samDaily, int kellyDaily, int difference) { if (samDaily >= kellyDaily) { return -1; }

    int low = 0;
    int high = difference / (kellyDaily - samDaily) + 1;
    
    while (low <= high) {
        int mid = low + (high - low) / 2;
        if (canSurpass(samDaily, kellyDaily, difference, mid)) {
            high = mid - 1;
        } else {
            low = mid + 1;
        }
    }
    
    if (canSurpass(samDaily, kellyDaily, difference, low)) {
        return low;
    } else {
        return -1;
    }
    

    }****

  • + 0 comments
    def minNum(samDaily, kellyDaily, difference):
        # Write your code here
        if kellyDaily <= samDaily:
            return -1
        min_days = (difference+1) / (kellyDaily - samDaily)
        if min_days != int(min_days):
            min_days = int(min_days) + 1
        return int(min_days)
    
  • + 0 comments

    JAVA Solution:

    public static int minNum(int samDaily, int kellyDaily, int difference) {
        // Write your code here
        
        int samsolved = difference + samDaily;
        int kellysolved = kellyDaily;
        int count=1;
        
        if(kellyDaily==samDaily) return -1;
          
        while(samsolved-kellysolved>=0){
            samsolved=samsolved+samDaily;
            kellysolved=kellysolved+kellyDaily;
            count++;
        }
        return count;
        }