【问题标题】:check if one vector<int> can be nested in another检查一个 vector<int> 是否可以嵌套在另一个中
【发布时间】:2019-06-18 17:42:58
【问题描述】:

我正在 Edabit 上尝试挑战,当前的挑战是关于嵌套向量。说明是: 如果第一个数组可以嵌套在第二个数组中,则创建一个返回 true 的函数。

如果满足以下条件,数组 1 可以嵌套在数组 2 中:

  1. 阵列 1 的最小值 > 阵列 2 的最小值
  2. 阵列 1 的最大值

例子:

canNest([1, 2, 3, 4], [0, 6]) ➞ true

canNest([3, 1], [4, 0]) ➞ true

canNest([9, 9, 8], [8, 9]) ➞ false

canNest([1, 2, 3, 4], [2, 3]) ➞ false

我编写的未通过所有测试的代码如下:

bool canNest(std::vector<int> arr1, std::vector<int> arr2) {
    return (std::min_element(arr1.begin(), arr1.end()) > 
                 std::min_element(arr2.begin(), arr2.end()) && 
                 std::max_element(arr1.begin(), arr1.end()) < 
                 std::max_element(arr2.begin(), arr2.end()));
}

此代码使 test3、test4 和 test5 通过,但不通过 test1 和 test2。

    It(test1){Assert::That(canNest({1, 2, 3, 4}, {0, 6}), Equals(true));}
    It(test2){Assert::That(canNest({3, 1}, {4, 0}), Equals(true));}
    It(test3){Assert::That(canNest({9, 9, 8}, {8, 9, 10}), Equals(false));}
    It(test4){Assert::That(canNest({9, 9, 8}, {8, 9}), Equals(false));}
    It(test5){Assert::That(canNest({1, 2, 3, 4}, {2, 3}), Equals(false));}

编辑:挑战可以在here 找到,用于测试解决方案!

【问题讨论】:

  • 我记得,你必须用*取消引用 min_element 和 max_element:cplusplus.com/reference/algorithm/min_element
  • 来到这样的论坛,是在寻求其他人帮助您解决问题,却缺少挑战的精神吗?
  • min/max_element 返回一个迭代器,尊重它们
  • std::min_elementstd::max_element 的文档中所述,它们返回迭代器,而not 元素。
  • 请注意,使用std::minmax_element 您可以做得更好。

标签: c++ arrays vector containers c++17


【解决方案1】:

如 cmets 中所述,您使用的算法返回需要取消引用的迭代器。对于这个任务,你可能应该看看他们的伙伴std::minmax_element,它返回迭代器到 min 和 max 元素,而不必多次遍历列表:

bool canNest(const std::vector<int>& arr1, const std::vector<int>& arr2) {
    auto [min1, max1] = std::minmax_element(arr1.begin(), arr1.end());
    auto [min2, max2] = std::minmax_element(arr2.begin(), arr2.end());

    return *min1 > *min2 && *max1 < *max2;
}

另一种方法是仅获取封闭向量的 minmax 并使用一种算法,如果整体条件不能,则该算法将提前返回 false得到满足。它可能更有效,尤其是在大数据集上。

bool canNestImproved(const std::vector<int>& arr1, const std::vector<int>& arr2) {
    auto [min2it, max2it] = std::minmax_element(arr2.begin(), arr2.end());

    // all elements in arr1 must fall within the boundaries and std::all_of
    // will stop iterating over arr1 as soon as the lambda returns false.
    return std::all_of(
        arr1.begin(), arr1.end(),
        [min2 = *min2it, max2 = *max2it](int x) { return x > min2 && x < max2; });
}

【讨论】:

    【解决方案2】:

    正如 Dyukha 在 cmets min/max_element 中所说,返回一个迭代器,您需要取消对它们的引用。

    bool canNest(std::vector<int> arr1, std::vector<int> arr2) {
        return (*std::min_element(arr1.begin(), arr1.end()) > 
                     *std::min_element(arr2.begin(), arr2.end()) && 
                     *std::max_element(arr1.begin(), arr1.end()) < 
                     *std::max_element(arr2.begin(), arr2.end()));
    }
    

    【讨论】:

      猜你喜欢
      • 2015-06-02
      • 1970-01-01
      • 2021-07-17
      • 2015-09-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-15
      • 2021-05-15
      相关资源
      最近更新 更多