LintCode 1166: Recommended Results are Scattered

1166. Recommended Results are Scattered

A recommendation system will recommend a list of video and picture elements. x is the element number, V_x is the video, and P_x is the image.Now you need to shatter these elements, and the rules are as follows:

 

The position of the first [picture P] remains unchanged;

Starting from the first [picture P], there is exactly 1 [picture P] in every n element;

The relative order between the pictures remains unchanged;

Elements that do not satisfy the shatter rule need to be discarded. Give you the list of elements and the value of n, please return the list of elements after scattering.

Example

Example 1:

Input sample:

elements=["V_0", "V_1", "V_2", "P_3", "P_4", "P_5", "V_6", "P_7", "V_8", "V_9"] , n=3

Output sample:

["V_0", "V_1", "V_2", "P_3", "V_6", "V_8", "P_4", "V_9"]

Sample explanation:

P_3 needs to appear in the 3rd bit starting from 0, since there is exactly 1 P in every 3 elements. P_5 and P_7 cannot meet the shatter requirements, so they are deleted.

 

Example 2:

Input sample:

elements=["V_0", "P_1", "V_2", "V_3", "V_4"] , n=2

Output sample:

["V_0", "P_1", "V_2"]

Sample explanation:

P_1 needs to appear in the first bit starting from 0, since there is exactly 1 P in every 2 elements. V_3 and V_4 cannot meet the shatter requirements, so they are deleted.

 

Notice

1 ≤ number of elements ≤10000

2 ≤ n ≤ number of elements

Make sure there's at least 1 [picture P] in the elements.

解法1:先把第一个P出现之前的V都存到结果里面,当出现第一个P之后,用2个数组分别存储P和V,然后打散存储,比如n=3,则1个P2个V,直到V数组全部用完。
 

class Solution {
public:
    /**
     * @param elements: A list of recommended elements.
     * @param n: [picture P] can appear at most 1 in every n
     * @return: Return the scattered result.
     */
    vector<string> scatter(vector<string> &elements, int n) {
        int len = elements.size();
        vector<string> pics, videos;
        vector<string> result;
        
        int index = 0;
        while(elements[index][0] == 'V') {
            result.push_back(elements[index]);
            index++;
        }
            
        while(index < len) {
            if (elements[index][0] == 'V') {
                videos.push_back(elements[index]);
            } else {
                pics.push_back(elements[index]);
            }
            index++;
        }
        
        int pics_len = pics.size(), videos_len = videos.size();
        int video_pos = 0;
 
        for (int i = 0; i < pics_len; ++i) {
            result.push_back(pics[i]);
            for (int j = 0; j < n - 1; ++j) {
                if (video_pos >= videos_len) return result; 
                result.push_back(videos[video_pos]);
                video_pos++;
            }
            
        }

        return result;
    }
};

解法2:用queue。思路跟解法1一样。
 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值