vendredi 10 juillet 2020

Explain the problem properly and then explain the solution

Can someone explain me the problem here in this gist code. I couldn't understand what actually the problem is neither the sample input/output.

https://gist.githubusercontent.com/k9982874/5f0de0475a41aa5c4eebedf4961e3f98/raw/282300813b706ce9faef1bfef6a4b4ef658bfbcf/AmazonVideo.cpp

Is this problem particularly related to data structure or binary tree or something else?

The problem and solution from the gist

class Solution {
public:
    /// <summary>
    /// You are working on developing a movie with Amazon Video and want to devise an application to easily break up individual shots (short sequence from a particular camera angle) in a video into scenes (a sequence of shots).  Each shot is labeled with a letter. There is already an algorithm that breaks the video up into shots and labels them.
    /// Write a function which will partition a sequence of shots into minimal subsequences so that no shot appears in more than one subsequence. The output should be the length of each subsequence. 
    /// Input
    /// The input to the function/method consists of an argument - inputList, a list of characters representing the sequence of shots.
    /// Output
    /// Return a list of integers representing the length of each scene, in the order in which it appears in the given sequence of shots.
    /// Example 1:
    /// inputList = [a, b, c]
    /// Output = [1, 1, 1]
    /// Explanation:
    /// Because there are no recurring shots, all shots can be in the minimal length 1 subsequence.
    /// Example 2:
    /// inputList  = [a, b, c, a]
    /// Output = [4]
    /// Explanation:
    /// Because ‘a’ appears more than once, everything between the first and last appearance of ‘a’ must be in the same list.
    /// Example 3:
    /// inputList = [a, b, a, b, c, b, a, c, a, d, e, f, e, g, d, e, h, i, j, h, k, l, i, j]
    /// Output = [9, 7, 8]
    /// Test 1:
    /// Input = [a, b, c, d, a, e, f, g, h, i, j, e]
    /// Output = 5 7
    /// Test 2:
    /// [z, z, c, b, z, c, h, f, i, h, i]
    /// Output = 6 5 
    /// </summary>
    vector<int> partitionLabels(string S) 
    {
        map<char, int> m;
        for (int i = 0; i < S.size(); i++)
        {
            m[S[i]] = i;
        }

        vector<int> result;
        int left = 0;
        int right = 0;
        for (int i = 0; i < S.size(); i++)
        {
            right = max(right, m[S[i]]);
            if (right == i)
            {
                result.push_back(1 + right - left);
                left = right + 1;
            }
        }
        return result;
    }
};

The commented section in above class Solution is a problem and you are asked to find the solution. Someone on gist has posted the solution and the solution is partitionLabels function.

I am looking for someone to explain me that problem and why that solution is good/bad or what should be the approach to solve that problem.

Aucun commentaire:

Enregistrer un commentaire