📜  Expedia 编码轮经验 – 实习生 2021

📅  最后修改于: 2021-11-16 07:47:06             🧑  作者: Mango

Expedia Inten 2021 的编码问题: Expedia 2021 实习生回合的编码回合有 2 个编码问题和 6 个 MCQ。

问题 1:有多种方法可以将对象划分为组,从而使任何组的对象都少于先前形成的组?

例子:

objects=8, groups=4 Answer: 5
[1,1,1,5], [1,1,2,4], [1,1,3,3], [1,2,2,3], [2,2,2,2]
Input:
8 4 
Output:
5

解决方案:

简单的方法: 这个问题可以通过使用递归来解决。

C++
// C++ program to count the
// number of ways to divide objetcs in
// groups.
 
#include 
 
using namespace std;
 
// Function to count the number
// of ways to divide the number objects
// in groups.
int count(int pos, int prev, int objects, int groups)
{
 
    if (pos == groups) {
        if (objects == 0)
            return 1;
        else
            return 0;
    }
 
    // if objects is divides completely
    // into less than groups
    if (objects == 0)
        return 0;
 
    int solution = 0;
 
    // put all possible values
    // greater equal to prev
    for (int i = prev; i <= objects; i++) {
        solution += count(pos + 1, i, objects - i, groups);
    }
    return solution;
}
 
// Function to count the number of
// ways to divide into objects
int WaysToGo(int objects, int groups)
{
    return count(0, 1, objects, groups);
}
 
// Main Code
int main()
{
    int objects, groups;
    objects = 8;
    groups = 4;
    cout << WaysToGo(objects, groups);
    return 0;
}


C++
// C++ implementation to count the
// number of ways to divide objects in
// groups.
 
#include 
 
using namespace std;
// DP 3DArray
int dp[500][500][500];
 
// Function to count the number
// of ways to divide the objects
// in groups.
int count(int pos, int prev, int objects, int groups)
{
    // Base Case
    if (pos == groups) {
        if (left == 0)
            return 1;
        else
            return 0;
    }
    // if objects is divides completely
    // into groups
    if (objects == 0)
        return 0;
 
    // If the subproblem has been
    // solved, use the value
    if (dp[pos][prev][objects] != -1)
        return dp[pos][prev][objects];
 
    int solution = 0;
    // put all possible values
    // greater equal to prev
    for (int i = prev; i <= objects; i++) {
        solution += count(pos + 1, i, objects - i, groups);
    }
 
    return dp[pos][prev][objects] = solution;
}
 
// Function to count the number of
// ways to divide into groups
int WaystoDivide(int objects, int groups)
{
    // Initialize DP Table as -1
    memset(dp, -1, sizeof(dp));
 
    return count(0, 1, objects, groups);
}
 
// Main Code
int main()
{
    int objects, groups;
    objects = 8;
    groups = 4;
    cout << WaystoDivide(objects, groups);
    return 0;
}


C++
#include 
using namespace std;
 
// Function to find distintc id's
int distIds(int a[], int n, int m)
{
    unordered_map mi;
    vector > v;
    int count = 0;
 
    // Store the occurrence of ids
    for (int i = 0; i < n; i++)
        mi[a[i]]++;
 
    // Store into the vector second as first and vice-versa
    for (auto it = mi.begin(); it != mi.end(); it++)
        v.push_back(make_pair(it->second, it->first));
 
    // Sort the vector
    sort(v.begin(), v.end());
 
    int size = v.size();
 
    // Start removing elements from the beginning
    for (int i = 0; i < size; i++) {
 
        // Remove if current value is less than
        // or equal to m
        if (v[i].first <= m) {
            m -= v[i].first;
            count++;
        }
 
        // Return the remaining size
        else
            return size - count;
    }
    return size - count;
}
 
// Main code
int main()
{
    int arr[] = { 2, 3, 1, 2, 3, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int m = 3;
 
    cout << distIds(arr, n, m);
    return 0;
}


时间复杂度: O(对象

动态方法:上述方法会随着时间复杂度的超出而失效,因此我们将应用动态规划。

C++

// C++ implementation to count the
// number of ways to divide objects in
// groups.
 
#include 
 
using namespace std;
// DP 3DArray
int dp[500][500][500];
 
// Function to count the number
// of ways to divide the objects
// in groups.
int count(int pos, int prev, int objects, int groups)
{
    // Base Case
    if (pos == groups) {
        if (left == 0)
            return 1;
        else
            return 0;
    }
    // if objects is divides completely
    // into groups
    if (objects == 0)
        return 0;
 
    // If the subproblem has been
    // solved, use the value
    if (dp[pos][prev][objects] != -1)
        return dp[pos][prev][objects];
 
    int solution = 0;
    // put all possible values
    // greater equal to prev
    for (int i = prev; i <= objects; i++) {
        solution += count(pos + 1, i, objects - i, groups);
    }
 
    return dp[pos][prev][objects] = solution;
}
 
// Function to count the number of
// ways to divide into groups
int WaystoDivide(int objects, int groups)
{
    // Initialize DP Table as -1
    memset(dp, -1, sizeof(dp));
 
    return count(0, 1, objects, groups);
}
 
// Main Code
int main()
{
    int objects, groups;
    objects = 8;
    groups = 4;
    cout << WaystoDivide(objects, groups);
    return 0;
}

问题 2:删除 m 个项目后不同元素的最小数量

给定一个项目数组,第 i 个索引元素表示项目 id 并给定一个数字 m,任务是删除 m 个元素,以便应留下最小的不同 id。打印不同 ID 的数量。

例子:

Input : arr[] = { 2, 2, 1, 3, 3, 3}
           m = 3
Output : 1
Remove 1 and both 2's.So, only 3 will be
left that's why distinct id is 1.
Input : arr[] = { 2, 4, 1, 5, 3, 5, 1, 3}
           m = 2
Output : 3
Remove 2 and 4 completely. So, remaining ids
are 1, 3 and 5 i.e. 3

C++

#include 
using namespace std;
 
// Function to find distintc id's
int distIds(int a[], int n, int m)
{
    unordered_map mi;
    vector > v;
    int count = 0;
 
    // Store the occurrence of ids
    for (int i = 0; i < n; i++)
        mi[a[i]]++;
 
    // Store into the vector second as first and vice-versa
    for (auto it = mi.begin(); it != mi.end(); it++)
        v.push_back(make_pair(it->second, it->first));
 
    // Sort the vector
    sort(v.begin(), v.end());
 
    int size = v.size();
 
    // Start removing elements from the beginning
    for (int i = 0; i < size; i++) {
 
        // Remove if current value is less than
        // or equal to m
        if (v[i].first <= m) {
            m -= v[i].first;
            count++;
        }
 
        // Return the remaining size
        else
            return size - count;
    }
    return size - count;
}
 
// Main code
int main()
{
    int arr[] = { 2, 3, 1, 2, 3, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int m = 3;
 
    cout << distIds(arr, n, m);
    return 0;
}

时间复杂度: O(n log n)