两个最大的连续子阵列

时间:2017-01-01 02:45:05

标签: c++ arrays algorithm dynamic-programming kadanes-algorithm

我目前正在解决与最大连续子阵列问题相似的问题。但是,我找到最多两个不重叠的连续子阵列,而不是只找到一个连续的子阵列。

例如,对于下面的测试用例,答案是20,因为我们可以采取除-20之外的所有内容。

long long n, nums[500500], dp[500500][2][3];

long long best(int numsLeft, int beenTaking, int arrLeft) {
    if (arrLeft < 0 || numsLeft < 0) return 0;

    if (dp[numsLeft][beenTaking][arrLeft] != -1)
        return dp[numsLeft][beenTaking][arrLeft];

    if (beenTaking) {
        // continue Taking
        long long c1 = best(numsLeft - 1, beenTaking, arrLeft) + nums[numsLeft];
        // stop Taking
        long long c2 = best(numsLeft - 1, 0, arrLeft);

        return dp[numsLeft][beenTaking][arrLeft] = max(c1, c2);
    } else {
        // continue not Taking
        long long c1 = best(numsLeft - 1, beenTaking, arrLeft);
        // start Taking
        long long c2 = best(numsLeft - 1, 1, arrLeft - 1) + nums[numsLeft];

        return dp[numsLeft][beenTaking][arrLeft] = max(c1,c2);
    }
}

为此,我实现了以下代码:

cout << best(n - 1, 0, 2) << endl;

这是函数调用:

(function () {
    angular.module("searchCountryApp", []).controller("searchCountryController", searchCountryControllerFunction);
    searchCountryControllerFunction.$inject = ['$scope', '$filter'];

    function searchCountryControllerFunction($scope, $filter) {
        $scope.input = "";
        $scope.getCountries = function () {
            $scope.allPhpCountries = phpCountries.filter(function (data) {
                return $filter("uppercase")(data).includes($filter("uppercase")($scope.input)); //here is the change
            });
        }
    }
    var phpCountries = ["Afghanistan", "Albania", "Algeria", "American Samoa", "Andorra", "Angola", "Anguilla", "Antarctica", "Antigua and Barbuda", "Argentina", "Armenia", "Aruba", "Australia", "Austria", "Azerbaijan", "Bahamas", "Bahrain", "Bangladesh", "Barbados", "Belarus", "Belgium", "Belize", "Benin", "Bermuda", "Bhutan", "Bolivia", "Bosnia and Herzegowina", "Botswana", "Bouvet Island", "Brazil", "British Indian Ocean Territory", "Brunei Darussalam", "Bulgaria", "Burkina Faso", "Burundi", "Cambodia", "Cameroon", "Canada", "Cape Verde", "Cayman Islands", "Central African Republic", "Chad", "Chile", "China", "Christmas Island", "Cocos (Keeling) Islands", "Colombia", "Comoros", "Congo", "Congo, the Democratic Republic of the", "Cook Islands", "Costa Rica", "Cote d'Ivoire", "Croatia (Hrvatska)", "Cuba", "Cyprus", "Czech Republic", "Denmark", "Djibouti", "Dominica", "Dominican Republic", "East Timor", "Ecuador", "Egypt", "El Salvador", "Equatorial Guinea", "Eritrea", "Estonia", "Ethiopia", "Falkland Islands (Malvinas)", "Faroe Islands", "Fiji", "Finland", "France", "France Metropolitan", "French Guiana", "French Polynesia", "French Southern Territories", "Gabon", "Gambia", "Georgia", "Germany", "Ghana", "Gibraltar", "Greece", "Greenland", "Grenada", "Guadeloupe", "Guam", "Guatemala", "Guinea", "Guinea-Bissau", "Guyana", "Haiti", "Heard and Mc Donald Islands", "Holy See (Vatican City State)", "Honduras", "Hong Kong", "Hungary", "Iceland", "India", "Indonesia", "Iran (Islamic Republic of)", "Iraq", "Ireland", "Israel", "Italy", "Jamaica", "Japan", "Jordan", "Kazakhstan", "Kenya", "Kiribati", "Korea, Democratic People's Republic of", "Korea, Republic of", "Kuwait", "Kyrgyzstan", "Lao, People's Democratic Republic", "Latvia", "Lebanon", "Lesotho", "Liberia", "Libyan Arab Jamahiriya", "Liechtenstein", "Lithuania", "Luxembourg", "Macau", "Macedonia, The Former Yugoslav Republic of", "Madagascar", "Malawi", "Malaysia", "Maldives", "Mali", "Malta", "Marshall Islands", "Martinique", "Mauritania", "Mauritius", "Mayotte", "Mexico", "Micronesia, Federated States of", "Moldova, Republic of", "Monaco", "Mongolia", "Montserrat", "Morocco", "Mozambique", "Myanmar", "Namibia", "Nauru", "Nepal", "Netherlands", "Netherlands Antilles", "New Caledonia", "New Zealand", "Nicaragua", "Niger", "Nigeria", "Niue", "Norfolk Island", "Northern Mariana Islands", "Norway", "Oman", "Pakistan", "Palau", "Panama", "Papua New Guinea", "Paraguay", "Peru", "Philippines", "Pitcairn", "Poland", "Portugal", "Puerto Rico", "Qatar", "Reunion", "Romania", "Russian Federation", "Rwanda", "Saint Kitts and Nevis", "Saint Lucia", "Saint Vincent and the Grenadines", "Samoa", "San Marino", "Sao Tome and Principe", "Saudi Arabia", "Senegal", "Seychelles", "Sierra Leone", "Singapore", "Slovakia (Slovak Republic)", "Slovenia", "Solomon Islands", "Somalia", "South Africa", "South Georgia and the South Sandwich Islands", "Spain", "Sri Lanka", "St. Helena", "St. Pierre and Miquelon", "Sudan", "Suriname", "Svalbard and Jan Mayen Islands", "Swaziland", "Sweden", "Switzerland", "Syrian Arab Republic", "Taiwan, Province of China", "Tajikistan", "Tanzania, United Republic of", "Thailand", "Togo", "Tokelau", "Tonga", "Trinidad and Tobago", "Tunisia", "Turkey", "Turkmenistan", "Turks and Caicos Islands", "Tuvalu", "Uganda", "Ukraine", "United Arab Emirates", "United Kingdom", "United States", "United States Minor Outlying Islands", "Uruguay", "Uzbekistan", "Vanuatu", "Venezuela", "Vietnam", "Virgin Islands (British)", "Virgin Islands (U.S.)", "Wallis and Futuna Islands", "Western Sahara", "Yemen", "Yugoslavia", "Zambia", "Zimbabwe"];
})();

在函数调用之前,dp数组已经填充了-1。 nums数组包含n个元素,并且是零索引的。

Ideone.com链接是:http://ideone.com/P5PB7h

虽然我的代码确实适用于上面显示的示例测试用例,但是对于其他一些测试用例(我无法使用),它失败了。是否有任何边缘情况没有被我的代码捕获?我哪里错了?谢谢你的帮助。

我尝试了一些这样的边缘案例,但我无法这样做。

1 个答案:

答案 0 :(得分:0)

问题似乎在以下几行:

if (beenTaking) {
    // continue Taking
    long long c1 = best(numsLeft - 1, beenTaking, arrLeft) + nums[numsLeft];
    ...
} else {
    ...
}

添加best(numsLeft - 1, 1, arrLeft)而不递减arrLeft意味着&#34;最佳&#34; numsLeft - 1中的第一个nums[]值的结果发生在nums []的末尾(在索引numsLeft - 1处)。这可能不是真的。

当有超过2个正值范围被负值分隔时,代码可能会失败。

此外,dp数组应初始化为明显超出范围的内容,例如LLONG_MIN,而不是-1,这可能是合法的总和。