在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★ Given a list of Return the maximum score of any valid set of words formed by using the given letters ( It is not necessary to use all characters in Example 1: Input: words = ["dog","cat","dad","good"], letters = ["a","a","c","d","d","d","g","o","o"], score = [1,0,9,5,0,0,3,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0] Output: 23 Explanation: Score a=1, c=9, d=5, g=3, o=2 Given letters, we can form the words "dad" (5+1+5) and "good" (3+2+2+5) with a score of 23. Words "dad" and "dog" only get a score of 21. Example 2: Input: words = ["xxxz","ax","bx","cx"], letters = ["z","a","b","c","x","x","x"], score = [4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,10] Output: 27 Explanation: Score a=4, b=4, c=4, x=5, z=10 Given letters, we can form the words "ax" (4+5), "bx" (4+5) and "cx" (4+5) with a score of 27. Word "xxxz" only get a score of 25. Example 3: Input: words = ["leetcode"], letters = ["l","e","t","c","o","d"], score = [0,0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0] Output: 0 Explanation: Letter "e" can only be used once. Constraints:
你将会得到一份单词表 请你帮忙计算玩家在单词拼写游戏中所能获得的「最高得分」:能够由 单词拼写游戏的规则概述如下:
示例 1: 输入:words = ["dog","cat","dad","good"], letters = ["a","a","c","d","d","d","g","o","o"], score = [1,0,9,5,0,0,3,0,0,0,0,0,0,0,2,0,0,0,0,0,0,0,0,0,0,0] 输出:23 解释: 字母得分为 a=1, c=9, d=5, g=3, o=2 使用给定的字母表 letters,我们可以拼写单词 "dad" (5+1+5)和 "good" (3+2+2+5),得分为 23 。 而单词 "dad" 和 "dog" 只能得到 21 分。 示例 2: 输入:words = ["xxxz","ax","bx","cx"], letters = ["z","a","b","c","x","x","x"], score = [4,4,4,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,5,0,10] 输出:27 解释: 字母得分为 a=4, b=4, c=4, x=5, z=10 使用给定的字母表 letters,我们可以组成单词 "ax" (4+5), "bx" (4+5) 和 "cx" (4+5) ,总得分为 27 。 单词 "xxxz" 的得分仅为 25 。 示例 3: 输入:words = ["leetcode"], letters = ["l","e","t","c","o","d"], score = [0,0,1,1,1,0,0,0,0,0,0,1,0,0,1,0,0,0,0,1,0,0,0,0,0,0] 输出:0 解释: 字母 "e" 在字母表 letters 中只出现了一次,所以无法组成单词表 words 中的单词。 提示:
Runtime: 24 ms
Memory Usage: 21.4 MB
1 class Solution { 2 func maxScoreWords(_ words: [String], _ letters: [Character], _ score: [Int]) -> Int { 3 if words.isEmpty || words.count == 0 || letters.isEmpty || letters.count == 0 || score.isEmpty || score.count == 0 4 { 5 return 0 6 } 7 var count:[Int] = [Int](repeating: 0, count: score.count) 8 for ch in letters 9 { 10 count[Int(ch.asciiValue! - 97)] += 1 11 } 12 return backtrack(words, &count, score, 0) 13 } 14 15 func backtrack(_ words: [String], _ count: inout [Int], _ score: [Int],_ index:Int) -> Int 16 { 17 var maxNum:Int = 0 18 for i in index..<words.count 19 { 20 var res:Int = 0 21 var isValid:Bool = true 22 for ch in words[i] 23 { 24 count[Int(ch.asciiValue! - 97)] -= 1 25 res += score[Int(ch.asciiValue! - 97)] 26 if count[Int(ch.asciiValue! - 97)] < 0 27 { 28 isValid = false 29 } 30 } 31 if isValid 32 { 33 res += backtrack(words, &count, score, i + 1) 34 maxNum = max(res, maxNum) 35 } 36 for ch in words[i] 37 { 38 count[Int(ch.asciiValue! - 97)] += 1 39 res = 0 40 } 41 } 42 return maxNum 43 } 44 }
|
请发表评论