RepeatedDNASequences BestTime_to_Buy_and_SellStockIV
/**
* @Author: weblee
* @Email: likaiweb@163.com
* @Blog: http://www.cnblogs.com/lkzf/
* @Time: 2015年2月18日上午10:03:42
*
************* function description ***************
*
****************************************************
*/ public class BestTime_to_Buy_and_SellStockIV {
public int maxProfit(int k, int[] prices) {
if (prices.length == 0) {
return 0;
} if (k >= prices.length) {
return solveMaxProfit(prices);
} /*
* 局部最优值是比较前一天并少交易一次的全局最优加上大于0的差值,
* 和前一天的局部最优加上差值后相比,两者之中取较大值,
* 而全局最优比较局部最优和前一天的全局最优
*/
int[] global = new int[k + 1];
int[] local = new int[k + 1]; for (int i = 0; i < prices.length - 1; ++i) {
int diff = prices[i + 1] - prices[i]; for (int j = k; j >= 1; --j) {
local[j] = Math.max(global[j - 1] + Math.max(diff, 0), local[j]
+ diff); global[j] = Math.max(global[j], local[j]);
}
} return global[k];
} public static int solveMaxProfit(int[] prices) {
int result = 0; for (int i = 1; i < prices.length; i++) {
if (prices[i] - prices[i - 1] > 0) {
result += (prices[i] - prices[i - 1]);
}
} return result;
} }
package com.weblee.Medium; import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map; /**
* @Author: weblee
* @Email: likaiweb@163.com
* @Blog: http://www.cnblogs.com/lkzf/
* @Time: 2015年2月18日上午9:20:12
*
************* function description ***************
*
****************************************************
*/ public class RepeatedDNASequences {
public List<String> findRepeatedDnaSequences(String s) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
List<String> result = new ArrayList<String>(); if (s.length() <= 10) {
return result;
} // encode
char[] convert = new char[26];
convert[0] = 0;
convert[2] = 1;
convert[6] = 2;
convert[19] = 3; int hashValue = 0; /*
* the first 10 characters string
*/
for (int pos = 0; pos < 10; ++pos) {
hashValue <<= 2;
hashValue |= convert[s.charAt(pos) - 'A'];
} // first 10-letter-long sequences encode -> value
map.put(hashValue, 1); // remove duplicate
HashSet<Integer> set = new HashSet<>(); for (int pos = 10; pos < s.length(); ++pos) {
// left 2 bit, equal to multiply 4
hashValue <<= 2;
// the 2 right bit valued encode of s.char(pos)
hashValue |= convert[s.charAt(pos) - 'A'];
// 最高两位置0
hashValue &= ~(0x300000); if (!map.containsKey(hashValue)) {
map.put(hashValue, 1);
} else {
if (!set.contains(hashValue)) {
// 10-letter-long sequences
result.add(s.substring(pos - 9, pos + 1)); set.add(hashValue);
} map.replace(hashValue, 1 + map.get(hashValue));
}
} return result;
} }
RepeatedDNASequences BestTime_to_Buy_and_SellStockIV的更多相关文章
- [LeetCode] Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 187. Repeated DNA Sequences
题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...
- Leetcode:Repeated DNA Sequences详细题解
题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...
- 【原创】leetCodeOj --- Repeated DNA Sequences 解题报告
原题地址: https://oj.leetcode.com/problems/repeated-dna-sequences/ 题目内容: All DNA is composed of a series ...
- LeetCode哈希表
1. Two Sum https://leetcode.com/problems/two-sum/description/ 不使用额外空间需要n*n的复杂度 class Solution { publ ...
- leetcode187. Repeated DNA Sequences
https://leetcode.com/problems/repeated-dna-sequences/#/description https://leetcode.com/problems/r ...
- 187 Repeated DNA Sequences 重复的DNA序列
所有DNA由一系列缩写为A,C,G和 T 的核苷酸组成,例如:“ACGAATTCCG”.在研究DNA时,识别DNA中的重复序列有时非常有用.编写一个函数来查找DNA分子中所有出现超多一次的10个字母长 ...
- [LeetCode] 187. Repeated DNA Sequences 求重复的DNA序列
All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...
- 重复的DNA序列[哈希表] LeetCode.187
所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助. 编写一个函数 ...
随机推荐
- ibdata文件增大的原因
http://blog.itpub.net/22664653/viewspace-1994016/
- UNIX标准化及实现之标准之间的冲突
就整体而言,这些不同的标准之间配合得相当好.但是我们也很关注它们之间的差别,特别是ISO C标准和POSIX.1之间的差别. ISO C定义了函数clock,它返回进程使用的CPU时间,返回值类型是c ...
- php关于日期时间 php日期 php时间
strtotime 的牛逼用法: $a='-4 days '.date('Y-m-d');$day = date('Y-m-d', strtotime($a));var_dump($day); /** ...
- Android实现资料收藏
1,调web浏览器 Uri myBlogUri = Uri.parse("http://xxxxx.com"); returnIt = new Intent(Intent.ACTI ...
- 如何在 iOS 中解决循环引用的问题
稍有常识的人都知道在 iOS 开发时,我们经常会遇到循环引用的问题,比如两个强指针相互引用,但是这种简单的情况作为稍有经验的开发者都会轻松地查找出来. 但是遇到下面这样的情况,如果只看其实现代码,也很 ...
- C# 之 后台加载图片Image
命名空间为 System.Drawing ,Image.FromFile 一旦使用后,对应的文件在一直调用其生成的Image对象被Disponse前都不会被解除锁定,这就造成了一个问题,就是在这个图 ...
- jsp HTTP Status 405 - HTTP method GET is not supported by this URL
package myservlet.control; import java.io.IOException; import java.io.PrintWriter; import javax.serv ...
- 关于Git远程版本库
Git作为分布式版本库控制系统,每个人都是本地版本库的主人,可以在本地的版本库中随心所欲的创建分支和里程碑. 当需要多人协作时,问题就出现了: 1.如何避免因为用户把所有的本地分支都推送到了共享版本库 ...
- ubuntu下使用quick2wire控制RespberryPi2的I2C
首先,开启树莓派的I2C驱动: 查看I2C驱动是否已经被加载:ls /dev -l | grep i2c,如果有形如 i2c-x 的显示结果表明驱动已经加载,否则驱动没有加载,需要进行如下操作: 修改 ...
- .Net 动态代理,AOP
.Net 动态代理,AOP 直接上代码了. /***************************************** * author:jinshuai * * E-mail:redfox ...