1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/最小代价构造字符串 [construct-string-with-minimum-cost].html
2024-07-16 16:03:30 +08:00

57 lines
2.3 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>给你一个字符串 <code>target</code>、一个字符串数组 <code>words</code> 以及一个整数数组 <code>costs</code>,这两个数组长度相同。</p>
<p>设想一个空字符串 <code>s</code></p>
<p>你可以执行以下操作任意次数(包括<strong></strong>次):</p>
<ul>
<li>选择一个在范围&nbsp; <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>&nbsp;</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>&nbsp;</p>
<p><strong>提示:</strong></p>
<ul>
<li><code>1 &lt;= target.length &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= words.length == costs.length &lt;= 5 * 10<sup>4</sup></code></li>
<li><code>1 &lt;= words[i].length &lt;= 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 &lt;= costs[i] &lt;= 10<sup>4</sup></code></li>
</ul>