代码之家  ›  专栏  ›  技术社区  ›  Evanss

HackerRank挑战的最大玩具任务数?

  •  -1
  • Evanss  · 技术社区  · 7 年前

    我正在努力解决这个问题: http://www.hackerrank.com/challenges/mark-and-toys

    马克和简生了第一个孩子后非常高兴。他们的儿子 喜欢玩具,所以马克想买一些。有许多不同的 他用这些钱买东西。

    给出一份价格清单和消费金额,最高限额是多少 马克能买多少玩具?例如,如果和马克不得不花钱, 第一组项目。

    我的解决方案正常工作,但显然是不正确的,当一个非常大的数字集抛出它。

    // Complete the maximumToys function below.
    function maximumToys(prices, k) {
        const pricesSorted = prices.sort();
        // console.log('pricesSorted ', pricesSorted);
    
        let budget = k;
        let noToys = 0;
    
        pricesSorted.forEach(toyPrice=>{
            if (toyPrice <= budget) {
                noToys++;
                budget = budget - toyPrice;
            }
        });
    
        // console.log('noToys ', noToys);
        return noToys;
    }
    
    3 回复  |  直到 7 年前
        1
  •  0
  •   nice_dev    7 年前

    如果你不把类似的函数传递给 sort() ,它将值假定为字符串,并根据Unicode代码点对它们进行排序。资料来源:- Sort

    因此,传递一个comaparable函数,JS将根据该函数的返回值对对象进行排序。其余代码是正确的。

    const pricesSorted = prices.sort(function(a,b){return a-b;});
    

    更新:

    自从 分类到位, pricesSorted 是多余的。

        2
  •  1
  •   akshay    7 年前

    把玩具按价格分类,只要你还有钱就继续拿。

    // Complete the maximumToys function below.
    function maximumToys(prices, k) {
        var bought = 0
        var pricings = prices.sort((a, b) => a - b)
        var amtLeft = k;
        for (var i = 0; i < pricings.length; i++){
            if (amtLeft < pricings[i]) {
                break;
            } else {
                amtLeft = amtLeft - pricings[i];
                bought++;
            }
        }
        return bought
    }
    
        3
  •  0
  •   Surendiran S    6 年前
    function maximumToys(prices, k) {
    let cost=0,count=0;
    var x = prices.sort((a,b)=>a-b);
    for(let i=0; i<prices.length; i++)
        {
            if(cost<k)
            {
                cost+=x[i];
                count++;
            }
        }
        return count-1;
    }
    
        4
  •  0
  •   Aamir Afridi    5 年前

    function maximumToys(prices, b) {
        var t = 0;
        var i = 0;
        var arr = prices.sort((a,b) => a-b);
        for (let j=0; j < arr.length; j++) {
            t += arr[j];
            if (t < b) {i++}
            else {break;};
        }
        return i;
    }
    
    console.log(maximumToys([1, 12, 5, 111, 200, 1000, 10], 50));
        5
  •  -2
  •   Shivan    7 年前
    #include <bits/stdc++.h>
    
    using namespace std;
    
    vector<string> split_string(string);
    
    // Complete the maximumToys function below.
    int maximumToys(vector<int> prices, int k) {
    
    
    }
    
    int main()
    {
        ofstream fout(getenv("OUTPUT_PATH"));
    
        string nk_temp;
        getline(cin, nk_temp);
    
        vector<string> nk = split_string(nk_temp);
    
        int n = stoi(nk[0]);
    
        int k = stoi(nk[1]);
    
        string prices_temp_temp;
        getline(cin, prices_temp_temp);
    
        vector<string> prices_temp = split_string(prices_temp_temp);
    
        vector<int> prices(n);
    
        for (int i = 0; i < n; i++) {
            int prices_item = stoi(prices_temp[i]);
    
            prices[i] = prices_item;
        }
    
        int result = maximumToys(prices, k);
    
        fout << result << "\n";
    
        fout.close();
    
        return 0;
    }
    
    vector<string> split_string(string input_string) {
        string::iterator new_end = unique(input_string.begin(), input_string.end(), [] (const char &x, const char &y) {
            return x == y and x == ' ';
        });
    
        input_string.erase(new_end, input_string.end());
    
        while (input_string[input_string.length() - 1] == ' ') {
            input_string.pop_back();
        }
    
        vector<string> splits;
        char delimiter = ' ';
    
        size_t i = 0;
        size_t pos = input_string.find(delimiter);
    
        while (pos != string::npos) {
            splits.push_back(input_string.substr(i, pos - i));
    
            i = pos + 1;
            pos = input_string.find(delimiter, i);
        }
    
        splits.push_back(input_string.substr(i, min(pos, input_string.length()) - i + 1));
    
        return splits;
    }