zoukankan      html  css  js  c++  java
  • 691. Stickers to Spell Word

    We are given N different types of stickers. Each sticker has a lowercase English word on it.

    You would like to spell out the given target string by cutting individual letters from your collection of stickers and rearranging them.

    You can use each sticker more than once if you want, and you have infinite quantities of each sticker.

    What is the minimum number of stickers that you need to spell out the target? If the task is impossible, return -1.

    Example 1:

    Input:

    ["with", "example", "science"], "thehat"
    

    Output:

    3
    

    Explanation:

    We can use 2 "with" stickers, and 1 "example" sticker.
    After cutting and rearrange the letters of those stickers, we can form the target "thehat".
    Also, this is the minimum number of stickers necessary to form the target string.
    

    Example 2:

    Input:

    ["notice", "possible"], "basicbasic"
    

    Output:

    -1
    

    Explanation:

    We can't form the target "basicbasic" from cutting letters from the given stickers.
    

    Note:

    • stickers has length in the range [1, 50].
    • stickers consists of lowercase English words (without apostrophes).
    • target has length in the range [1, 15], and consists of lowercase English letters.
    • In all test cases, all words were chosen randomly from the 1000 most common US English words, and the target was chosen as a concatenation of two random words.
    • The time limit may be more challenging than usual. It is expected that a 50 sticker test case can be solved within 35ms on average.

    Approach #1: DP. [C++]

    class Solution {
    public:
        int minStickers(vector<string>& stickers, string target) {
            int m = stickers.size();
            vector<vector<int>> mp(m, vector<int>(26, 0));
            unordered_map<string, int> dp;
            for (int i = 0; i < m; ++i) {
                for (char c : stickers[i])
                    mp[i][c-'a']++;
            }
            dp[""] = 0;
            return helper(dp, mp, target);
        }
        
    private:
        int helper(unordered_map<string, int>& dp, vector<vector<int>>& mp, string target) {
            if (dp.count(target)) return dp[target];
            int ans = INT_MAX, n = mp.size();
            vector<int> tar(26, 0);
            for (char c : target) tar[c-'a']++;
            for (int i = 0; i < n; ++i) {
                if (mp[i][target[0]-'a'] == 0) continue;
                string s;
                for (int j = 0; j < 26; ++j) 
                    if (tar[j] - mp[i][j] > 0)
                        s += string(tar[j]-mp[i][j], 'a'+j);
                int tmp = helper(dp, mp, s);
                if (tmp != -1) ans = min(ans, 1+tmp);
            }
            dp[target] = ans == INT_MAX ? -1 : ans;
            return dp[target];
        }
    };
    

      

    Analysis:

    https://leetcode.com/problems/stickers-to-spell-word/discuss/108318/C%2B%2BJavaPython-DP-%2B-Memoization-with-optimization-29-ms-(C%2B%2B)

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    导航控制器生产,push,pop,root,index
    DNSserver内置笔记本
    解决“Dynamic Web Module 3.0 requires Java 1.6 or newer.”错误
    ssh配置连接
    在UITouch事件中画圆圈-iOS8 Swift基础教程
    iOS之UITableViewCell左右滑动效果
    iOS UIView非常用方法及属性详解
    IOS用CGContextRef画各种图形(文字、圆、直线、弧线、矩形、扇形、椭圆、三角形、圆角矩形、贝塞尔曲线、图片)
    UIColor,CGColor,CIColor三者的区别和联系
    iOS中正确的截屏姿势
  • 原文地址:https://www.cnblogs.com/h-hkai/p/10354309.html
Copyright © 2011-2022 走看看