Sort by

recency

|

2402 Discussions

|

  • + 0 comments

    Here is my c++ solution, you can find the video here : https://youtu.be/yC-TXToDbD0

    int getMoneySpent(vector<int> keyboards, vector<int> drives, int b) {
        int ans = -1;
        for(int k : keyboards){
            for(int d : drives){
                if(k + d > ans && k + d <= b) ans = k + d;
            }
        }
        return ans;
    }
    
  • + 0 comments
    def getMoneySpent(keyboards, drives, b):
        
        arr = []
        max = -1
        
        for i in keyboards:
            for j in drives:
                
                sum = i+j
                if sum <= b and sum > max:
                    max = sum
                    
        return max
    
  • + 0 comments

    Kotlin:

    fun getMoneySpent(keyboards: Array<Int>, drives: Array<Int>, b: Int): Int {
        var max=0
        for(k in 0 until keyboards.size){
            for(d in 0 until drives.size){
                var sum=keyboards[k]+drives[d]
                if(sum>max && b>=sum){
                    max=sum
                }
            }
        }
        return if(max==0) -1 else max
    }
    
  • + 0 comments

    Perl:

    sub getMoneySpent {
        my ($keyboard, $drives, $b) = @_;
        
        my $most_expensive = 0;
        my $keyb = 0;
        my $device = 0;
        for (my $i = 0; $i < scalar(@$keyboard); $i++) {
            for (my $j = 0; $j < scalar(@$drives); $j++) {
                if ($keyboard->[$i] < $b || $drives->[$j] < $b) {
                    my $sum = $keyboard->[$i] + $drives->[$j];
                    if ($sum > $most_expensive && $sum <= $b) {
                        $most_expensive = $sum;
                        $keyb = $keyboard->[$i];
                        $device = $drives->[$j];
                    }                
                }            
            }
        }
        return (($keyb + $device) > 0) ? $keyb + $device : -1;
    }
    
  • + 0 comments

    Java

        int finalPrice=-1;
        for(int keyboard:keyboards){
            for(int drive:drives){
    
                if(finalPrice<keyboard+drive && keyboard+drive<=b){
                    finalPrice=keyboard+drive;
                }
            }
        }
        return finalPrice;