Day 46 | 139. Word Break | Backpack Question Summary

这篇具有很好参考价值的文章主要介绍了Day 46 | 139. Word Break | Backpack Question Summary。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Day 1 | 704. Binary Search | 27. Remove Element | 35. Search Insert Position | 34. First and Last Position of Element in Sorted Array
Day 2 | 977. Squares of a Sorted Array | 209. Minimum Size Subarray Sum | 59. Spiral Matrix II
Day 3 | 203. Remove Linked List Elements | 707. Design Linked List | 206. Reverse Linked List
Day 4 | 24. Swap Nodes in Pairs| 19. Remove Nth Node From End of List| 160.Intersection of Two Lists
Day 6 | 242. Valid Anagram | 349. Intersection of Two Arrays | 202. Happy Numbe | 1. Two Sum
Day 7 | 454. 4Sum II | 383. Ransom Note | 15. 3Sum | 18. 4Sum
Day 8 | 344. Reverse String | 541. Reverse String II | 替换空格 | 151.Reverse Words in a String | 左旋转字符串
Day 9 | 28. Find the Index of the First Occurrence in a String | 459. Repeated Substring Pattern
Day 10 | 232. Implement Queue using Stacks | 225. Implement Stack using Queue
Day 11 | 20. Valid Parentheses | 1047. Remove All Adjacent Duplicates In String | 150. Evaluate RPN
Day 13 | 239. Sliding Window Maximum | 347. Top K Frequent Elements
Day 14 | 144.Binary Tree Preorder Traversal | 94.Binary Tree Inorder Traversal| 145.Binary Tree Postorder Traversal
Day 15 | 102. Binary Tree Level Order Traversal | 226. Invert Binary Tree | 101. Symmetric Tree
Day 16 | 104.MaximumDepth of BinaryTree| 111.MinimumDepth of BinaryTree| 222.CountComplete TreeNodes
Day 17 | 110. Balanced Binary Tree | 257. Binary Tree Paths | 404. Sum of Left Leaves
Day 18 | 513. Find Bottom Left Tree Value | 112. Path Sum | 105&106. Construct Binary Tree
Day 20 | 654. Maximum Binary Tree | 617. Merge Two Binary Trees | 700.Search in a Binary Search Tree
Day 21 | 530. Minimum Absolute Difference in BST | 501. Find Mode in Binary Search Tree | 236. Lowes
Day 22 | 235. Lowest Common Ancestor of a BST | 701. Insert into a BST | 450. Delete Node in a BST
Day 23 | 669. Trim a BST | 108. Convert Sorted Array to BST | 538. Convert BST to Greater Tree
Day 24 | 77. Combinations
Day 25 | 216. Combination Sum III | 17. Letter Combinations of a Phone Number
Day 27 | 39. Combination Sum | 40. Combination Sum II | 131. Palindrome Partitioning
Day 28 | 93. Restore IP Addresses | 78. Subsets | 90. Subsets II
Day 29 | 491. Non-decreasing Subsequences | 46. Permutations | 47. Permutations II
Day 30 | 332. Reconstruct Itinerary | 51. N-Queens | 37. Sudoku Solver
Day 31 | 455. Assign Cookies | 376. Wiggle Subsequence | 53. Maximum Subarray
Day 32 | 122. Best Time to Buy and Sell Stock II | 55. Jump Game | 45. Jump Game II
Day 34 | 1005. Maximize Sum Of Array After K Negations | 134. Gas Station | 135. Candy
Day 35 | 860. Lemonade Change | 406. Queue Reconstruction by Height | 452. Minimum Number of Arrows
Day 36 | 435. Non-overlapping Intervals | 763. Partition Labels | 56. Merge Intervals
Day 37 | 738. Monotone Increasing Digits | 714. Best Time to Buy and Sell Stock | 968. BT Camera
Day 38 | 509. Fibonacci Number | 70. Climbing Stairs | 746. Min Cost Climbing Stairs
Day 39 | 62. Unique Paths | 63. Unique Paths II
Day 41 | 343. Integer Break | 96. Unique Binary Search Trees
Day 42 | 0-1 Backpack Basic Theory(一)| 0-1 Backpack Basic Theory(二)| 416. Partition Equal Subset Sum
Day 43 | 1049. Last Stone Weight II | 494. Target Sum | 474. Ones and Zeroes
Day 44 | Full Backpack Basic Theory | 518. Coin Change II | 377. Combination Sum IV
Day 45 | 70. Climbing Stairs | 322. Coin Change | 279. Perfect Squares


139. Word Break

Question Link

class Solution {
    public boolean wordBreak(String s, List<String> wordDict) {
        boolean[] dp = new boolean[s.length() + 1];
        dp[0] = true;

        for(int i = 1; i <= s.length(); i++){
            for(String word : wordDict){
                int len = word.length();
                // substring(beginIndex, endIndex) return the content from beginIndex to endIndex-1
                if(i >= len && dp[i-len] && word.equals(s.substring(i-len, i)))
                    dp[i] = true;
            }
        }

        return dp[s.length()];
    }
}
  • dp[i]: demonstrate whether a string of length i could be segmented into one or more dictionary words.

  • dp[0] = true, cause dp[0] is the root of the recursion. Otherwise, all the following recursion will be false.

  • s.substring(beginIndex, endIndex) return the content from beginIndex to endIndex-1文章来源地址https://www.toymoban.com/news/detail-607976.html

Backpack Question Summary

Recursion Formula

  • When asking whether the backpack can be filled(or how much it can hold at most)
    • dp[j] = max(dp[j], dp[j - nums[i]] + nums[i])
  • When asking the number of method to fill the backpack
    • dp[j] += dp[j - nums[i]]
  • When asking the maximum value of the backpack
    • dp[j] = max(dp[j], dp[j - weight[i]] + value[i])
  • When asking the minimum number of items to fill the backpack
    • dp[j] = min(dp[j - coins[i]] + 1, dp[j])

Traversal Order

  • 01 backpack
    • If we use one dimension array, we must traverse items first, and the inner loop must traverses from large to small.
  • Full backpack
    • traverse items first and traverse capacity first are both fine.
    • The inner loop must traverses from small to large.
    • If we solve for the number of combinations, the outer loop traverses items, the inner loop traverses capacity.
    • If we solve for the number of permutations, the outer loop traverses capacity, the inner loop traverses items.

到了这里,关于Day 46 | 139. Word Break | Backpack Question Summary的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 算法|Day46 动态规划14

    LeetCode 1143- 最长公共子序列 题目链接:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台 题目描述 :给定两个字符串 text1 和 text2,返回这两个字符串的最长 公共子序列 的长度。如果不存在 公共子序列 ,返回 0 。 一个字符串的 子序列 是指这样一个新的字符串:它是由原

    2024年02月11日
    浏览(33)
  • 算法记录 | Day46 动态规划

    思路: 1.确定dp数组以及下标的含义 dp[i] : 字符串长度为i的话,dp[i]为true,表示可以拆分为一个或多个在字典中出现的单词 。 2.确定递推公式 如果 s[0: j] 可以拆分为单词(即 dp[j] == True ),并且字符串 s[j: i] 出现在字典中,则 dp[i] = True 。 如果 s[0: j] 不可以拆分为单词(即

    2024年02月02日
    浏览(29)
  • 日撸 Java 三百行day46

    闵老师的文章链接: 日撸 Java 三百行(总述)_minfanphd的博客-CSDN博客 自己也把手敲的代码放在了github上维护:https://github.com/fulisha-ok/sampledata 快速排序需要一个基准值,在这个基准值右边的数都比这个基准值 大 ,左边的数都比这个基准值 小 。一趟排序就可以确定一个数的

    2024年02月03日
    浏览(25)
  • Day46- 动态规划part14

    题目一:1143. 最长公共子序列 1143. 最长公共子序列 给定两个字符串  text1  和  text2 ,返回这两个字符串的最长  公共子序列  的长度。如果不存在  公共子序列  ,返回  0  。 一个字符串的  子序列   是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的

    2024年02月21日
    浏览(31)
  • 算法 DAY45 动态规划07 70. 爬楼梯 322. 零钱兑换 279. 完全平方数 139. 单词拆分 多重背包

    和377. 组合总和 Ⅳ (opens new window)基本就是一道题了。 本题代码不长,题目也很普通,但稍稍一进阶就可以考察完全背包 动态规划五部曲 1、确定dp[j]的含义 dp[j] 凑成 j 的最少硬币的个数 2、确定递推公式 比如想凑成3, 如果手里有1,那么最小个数就是dp[2]+1 如果手里有2,那

    2024年02月02日
    浏览(49)
  • LeetCode 每日一题 Day 46 ||枚举

    给你一个下标从 0 开始的数组 words ,数组中包含 互不相同 的字符串。 如果字符串 words[i] 与字符串 words[j] 满足以下条件,我们称它们可以匹配: 字符串 words[i] 等于 words[j] 的反转字符串。 0 = i j words.length 请你返回数组 words 中的 最大 匹配数目。 注意,每个字符串最多匹配

    2024年01月22日
    浏览(35)
  • 研习代码 day46 | 动态规划——子序列问题2

            1.1 题目         给定两个字符串  text1  和  text2 ,返回这两个字符串的最长  公共子序列  的长度。如果不存在  公共子序列  ,返回  0  。         一个字符串的  子序列   是指这样一个新的字符串:它是由原字符串在不改变字符的相对顺序的情况下

    2024年02月04日
    浏览(31)
  • Day 29 | 回溯 491.递增子序列 、 46.全排列 、47.全排列 II

    题目 文章讲解 视频讲解 思路:去重原则:元素,树层不可以重复取,树枝可以。hash这种去重方式不需要回溯 题目 文章讲解 视频讲解 思路:used[i]这种去重方式需要回溯 注意比较两种去重方式 permute(排列) 题目 文章讲解 视频讲解 思路:去重之前一定做排序,used[i-1] =

    2024年01月25日
    浏览(38)
  • English Learning - L3 作业打卡 Lesson7 Day46 2023.6.19 周一

    ⏰打卡时间:2023.6.19周一 6:00-17:00 训练技巧顺序: 【完全听写法】➡️【车轮法】➡️【影子跟读法】 ⏱【练习时间】60 mins /ɪf jɔː laɪf wɜː ə bʊk ənd jʊ wɜː ðiː ˈɔːθə haʊ wʊd juː wɒnt jɔː ˈstɔːrɪ tə gəʊ/ 语音现象描述+自身问题总结: (连读、重读、弱读、浊化、断

    2024年02月10日
    浏览(35)
  • HCIE-Security Day46:AC准入控制Dot1x

      802.1X协议是一种基于端口的网络接入控制协议(Port based networkaccess control protocol)。“基于端口的网络接入控制”是指在局域网接入设备的端口这一级验证用户身份并控制其访问权限。 二层协议:802.1x为2层协议,不需要到达三层,对接入设备的整体性能要求不高,即可以使

    2024年02月06日
    浏览(15)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包