LeetCode 2016 392,337,309,201,416
来源:互联网 发布:滤波器计算软件 编辑:程序博客网 时间:2024/05/21 11:28
392 Is Subsequence
class Solution {public: bool isSubsequence(string s, string t) { int ls = s.length(), lt= t.length(); if (ls==0) return true; int ps=0; bool flag=false; for(int i=0;i<lt;i++) { if (t[i]==s[ps]) { ps++; } if (ps==ls) { flag=true; break; } } return flag; }};
class Solution {public: int tryRob(TreeNode* root,int& l,int& r) { if (!root) return 0; int ll=0,lr=0,rl=0,rr=0; l=tryRob(root->left,ll,lr); r=tryRob(root->right,rl,rr); return max(root->val+ll+lr+rl+rr,l+r); } int rob(TreeNode* root) { int l,r; return tryRob(root,l,r); }};
309 Best Time to Buy and Sell Stock with Cooldown
1. 看了Disscuss明显题目中给了三种状态buy,sell,cooldown,就应该想到分成这三个情况来进行动态规划,我个脑残!The series of problems are typical dp. The key for dp is to find the variables to represent the states and deduce the transition function.Of course one may come up with a O(1) space solution directly, but I think it is better to be generous when you think and be greedy when you implement.The natural states for this problem is the 3 possible transactions : buy, sell, rest. Here rest means no transaction on that day (aka cooldown).Then the transaction sequences can end with any of these three states.For each of them we make an array, buy[n], sell[n] and rest[n].buy[i] means before day i what is the maxProfit for any sequence end with buy.sell[i] means before day i what is the maxProfit for any sequence end with sell.rest[i] means before day i what is the maxProfit for any sequence end with rest.Then we want to deduce the transition functions for buy sell and rest. By definition we have:buy[i] = max(rest[i-1]-price, buy[i-1]) sell[i] = max(buy[i-1]+price, sell[i-1])rest[i] = max(sell[i-1], buy[i-1], rest[i-1])Where price is the price of day i. All of these are very straightforward. They simply represents :(1) We have to `rest` before we `buy` and (2) we have to `buy` before we `sell`One tricky point is how do you make sure you sell before you buy, since from the equations it seems that [buy, rest, buy] is entirely possible.Well, the answer lies within the fact that buy[i] <= rest[i] which means rest[i] = max(sell[i-1], rest[i-1]). That made sure [buy, rest, buy] is never occurred.A further observation is that and rest[i] <= sell[i] is also true thereforerest[i] = sell[i-1]Substitute this in to buy[i] we now have 2 functions instead of 3:buy[i] = max(sell[i-2]-price, buy[i-1])sell[i] = max(buy[i-1]+price, sell[i-1])This is better than 3, butwe can do even better2. class Solution {public: int maxProfit(vector<int>& prices) { int buy(INT_MIN),sell(0),prev_sell(0),prev_buy; int len=prices.size(); for(int i=0;i<len;i++) { int price=prices[i]; prev_buy=buy; buy=max(prev_sell - price,buy); prev_sell = sell; sell=max(prev_buy+price,sell); } return sell; }};
201 Bitwise AND of Numbers Range
class Solution {public: int rangeBitwiseAnd(int m, int n) { if(m == 0){ return 0; } int moveFactor = 1; while(m != n){ m >>= 1; n >>= 1; moveFactor <<= 1; } return m * moveFactor; }};
class Solution {public: bool canPartition(vector<int>& nums) { int len=nums.size(); int sum=0,hsum=0; for(int i=0;i<len;i++) { sum+=nums[i]; } if (sum%2==1) return false; hsum=sum/2; int MAX_F=100*100+10; bool f[MAX_F]; f[0]=true; for(int i=0;i<len;i++) { for(int j=hsum;j>=nums[i];j--) { if (j-nums[i]>=0) { f[j]=f[j]||f[j-nums[i]]; } } } return f[hsum]; }};
0 0
- LeetCode 2016 392,337,309,201,416
- Leetcode 201
- leetcode 121 309
- LeetCode[392] Is Subsequence
- LeetCode 392 Is Subsequence
- 【leetcode 392】Is Subsequence
- [Leetcode] #392 Is Subsequence
- Leetcode 392(Java)
- LeetCode 392 Is Subsequence
- leetcode 392 Is Subsequence
- Leetcode 392 Is Subsequence
- Leetcode no. 337
- leetcode: (201) Count Primes
- Leetcode no. 201
- LeetCode 2016 282,241
- LeetCode 2016 213,109
- LeetCode 2016 47,31
- LeetCode 2016 167,382
- Android UI 初学笔记(一)
- Ember 翻译——教程七:创建一个简单的组件
- JPA 菜鸟教程 11 复合主键-2个@Id
- 如何成为一个月薪过三万的Java工程师
- 深度学习论文笔记之(一)HED边缘检测
- LeetCode 2016 392,337,309,201,416
- 初步探讨Zigbee之详细了解系统时钟(+电源管理)
- java--流程控制、数组、面向对象相关备忘知识点
- 为最佳性能调优 Nginx
- 第一次画PCB板的感想
- spark RDD写入 Hbase
- 缓存在高并发场景下的常见问题
- 1022. D进制的A+B
- Windows JDK环境配置