[Leetcode] Math & Dynamic
来源:互联网 发布:linux rpm安装命令jdk 编辑:程序博客网 时间:2024/05/29 19:09
[Leetcode] Math & Dynamic
In this blog, I try to explain how to solve two similar question about Math & Dynamic. The idea in these two question is pretty similar, if you have mastered how to solve one of these two questions, you will feel it easy to solve another.
Subarray Sum Equals K
Subarray Sum Equals K
Analysis
In this question, you have to get the number of continuous subarrays whose sum equals to K. Pay attention to that the description of question doesn’t emphasize the size of the subarray so it will be valid if the size of subarray just be 1. The easy solution is by brutal force. By looping the array in two-layer, you can have all of the subarrays the array generates, then just sum them up to check if they equal to K. But it takes a bit more time. The other solution is dynamic programming. By defining T(n) as the sum of subarray which starts at beginning of the array and ends at the index n, then we get know the sum of array[i : n] is T(n) - T(i). SO, things are easy now. We can just check if T(n) - T(i) equals to K. There is one more trick we need to know. We can store all of the T(i) in a map. The key in map stands for the sum of the subarray, and the value in the map represents the num of such sum. Then the num of subarray which equals to K is map[T(n) - T(i)].
Code
The detail of algorithm can be found in the code. K = 0
is a special situation, you have to consider carefully about how to dealing with the situation when K = 0.
class Solution {public: int subarraySum(vector<int>& nums, int k) { unordered_map<int, int> hash; int sum = 0; int result = 0; int mod = 0; for (int i = 0; i < nums.size(); i++) { sum += nums[i]; mod = k == 0 ? sum : sum - k; result += hash[mod]; hash[sum] += 1; if (sum == k) { result += 1; } } return result; }};
Time complexity: O(n)
Continuous Subarray Sum
Continuous Subarray Sum
Analysis
This question is similar to the above one. You can try to think about that by using the idea which I have described. What you have to know is the size of subarray must be at least 2. There is a trick which you have to store pre
in the map instead of storing sum
directly.
Code
class Solution {public: bool checkSubarraySum(vector<int>& nums, int k) { unordered_set<int> hash; int sum = 0, pre = 0; for (int i = 0; i < nums.size(); i++) { sum += nums[i]; int mod = k == 0 ? sum : sum % k; if (hash.count(mod)) { return true; } hash.insert(pre); pre = mod; } return false; }};
Time complexity: O(n)
- [Leetcode] Math & Dynamic
- 【leetcode】【Medium】【413. Arithmetic Slices】【Math】【Dynamic programming】
- leetcode最优解整理(dfs/String/Binary Search/Two pointers/Math/Dynamic programming)
- [leetcode] math
- LeetCode:Math
- leetcode dynamic programming
- LeetCode:Dynamic Programming
- LeetCode Summary Dynamic Programming
- LeetCode--Dynamic Programming
- [Leetcode]Dynamic Programming-note
- [Leetcode]Dynamic Programming
- [Leetcode]Dynamic Programming
- [Leetcode]Dynamic Programming
- [Leetcode]Dynamic Programming
- LeetCode[Math]: Sqrt(x)
- [leetcode][math] Palindrome Number
- [leetcode][math] Plus One
- [leetcode][math] Add Binary
- 数据库
- 【软考】各种策略和相关的代表性算法
- python爬虫——最基础笔记
- 软连接与硬链接的区别,以及如何删除软链接
- Oracle:in操作符
- [Leetcode] Math & Dynamic
- idea修改项目、模块、文件编码方法
- BottomTabBar依赖
- linux安装composer
- 前端时遇到的问题
- [故障诊断]TE田纳西-伊斯曼过程数据集
- IntelliJ IDEA 快捷键说明大全(中英对照、带图示详解)
- Gvoice语音接入
- 欢迎使用CSDN-markdown编辑器