mirror of
https://gitee.com/coder-xiaomo/leetcode-problemset
synced 2025-01-10 18:48:13 +08:00
57 lines
2.3 KiB
HTML
57 lines
2.3 KiB
HTML
<p>给你一个字符串 <code>target</code>、一个字符串数组 <code>words</code> 以及一个整数数组 <code>costs</code>,这两个数组长度相同。</p>
|
||
|
||
<p>设想一个空字符串 <code>s</code>。</p>
|
||
|
||
<p>你可以执行以下操作任意次数(包括<strong>零</strong>次):</p>
|
||
|
||
<ul>
|
||
<li>选择一个在范围 <code>[0, words.length - 1]</code> 的索引 <code>i</code>。</li>
|
||
<li>将 <code>words[i]</code> 追加到 <code>s</code>。</li>
|
||
<li>该操作的成本是 <code>costs[i]</code>。</li>
|
||
</ul>
|
||
|
||
<p>返回使 <code>s</code> 等于 <code>target</code> 的 <strong>最小</strong> 成本。如果不可能,返回 <code>-1</code>。</p>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong class="example">示例 1:</strong></p>
|
||
|
||
<div class="example-block">
|
||
<p><strong>输入:</strong> <span class="example-io">target = "abcdef", words = ["abdef","abc","d","def","ef"], costs = [100,1,1,10,5]</span></p>
|
||
|
||
<p><strong>输出:</strong> <span class="example-io">7</span></p>
|
||
|
||
<p><strong>解释:</strong></p>
|
||
|
||
<ul>
|
||
<li>选择索引 1 并以成本 1 将 <code>"abc"</code> 追加到 <code>s</code>,得到 <code>s = "abc"</code>。</li>
|
||
<li>选择索引 2 并以成本 1 将 <code>"d"</code> 追加到 <code>s</code>,得到 <code>s = "abcd"</code>。</li>
|
||
<li>选择索引 4 并以成本 5 将 <code>"ef"</code> 追加到 <code>s</code>,得到 <code>s = "abcdef"</code>。</li>
|
||
</ul>
|
||
</div>
|
||
|
||
<p><strong class="example">示例 2:</strong></p>
|
||
|
||
<div class="example-block">
|
||
<p><strong>输入:</strong> <span class="example-io">target = "aaaa", words = ["z","zz","zzz"], costs = [1,10,100]</span></p>
|
||
|
||
<p><strong>输出:</strong> <span class="example-io">-1</span></p>
|
||
|
||
<p><strong>解释:</strong></p>
|
||
|
||
<p>无法使 <code>s</code> 等于 <code>target</code>,因此返回 -1。</p>
|
||
</div>
|
||
|
||
<p> </p>
|
||
|
||
<p><strong>提示:</strong></p>
|
||
|
||
<ul>
|
||
<li><code>1 <= target.length <= 5 * 10<sup>4</sup></code></li>
|
||
<li><code>1 <= words.length == costs.length <= 5 * 10<sup>4</sup></code></li>
|
||
<li><code>1 <= words[i].length <= target.length</code></li>
|
||
<li>所有 <code>words[i].length</code> 的总和小于或等于 <code>5 * 10<sup>4</sup></code></li>
|
||
<li><code>target</code> 和 <code>words[i]</code> 仅由小写英文字母组成。</li>
|
||
<li><code>1 <= costs[i] <= 10<sup>4</sup></code></li>
|
||
</ul>
|