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 中的重复序列有时会对研究非常有帮助. 编写一个函数 ...
随机推荐
- Hadoop可视化与交互式工具:Zeppelin和Hue
https://yq.aliyun.com/articles/42282?spm=5176.team18.teamshow1.19.9TkKmZ#rd
- 三、Socket之UDP异步传输文件-多文件传输和文件MD5校验
本文接着上一篇文章二.Socket之UDP异步传输文件,在上一篇文章的基础上实现多文件的传输和文件传输完成后进行完整性校验. 要实现多文件的传输,必须要对文(2)中发送文件的数据格式进行改进,必须加入 ...
- nginx---reference
nginx (pronounced "engine x") is a free open source web server written by Igor Sysoev, a R ...
- python中HTMLParser简单理解
找一个网页,例如https://www.python.org/events/python-events/,用浏览器查看源码并复制,然后尝试解析一下HTML,输出Python官网发布的会议时间.名称和地 ...
- Mysql-5.6乱码问题
1 参考:http://www.testwo.com/blog/6930 mysql数据库默认的编码是:Latin1,要想支持中文需要修改为gbk/utf8的编码格式. 1.以root管理员身份查 ...
- jemalloc源码结构分析(三):arena_malloc_small内存分布
在arena_s结构中,由NBINS数组将bin按照不同规模等级分别存储,每一个等级对应一颗run树,即一颗以chunk_map_t为节点的红黑树,而这些chunk_map_t节点实际分布于各个chu ...
- 将Oracle表导入到Access方法,已经安装了Access但Win7提示 找不到access driver
已经安装了Access但Win7提示 找不到access driver: 1.打开 "C:\Windows\SysWOW64\odbcad32.exe" 2.管理员cmd运行 命令 ...
- xml的语法与创建
xml语法很简单,但很严格,如果出现错误则不能正常解析,而HTML如果出现局部的错误,照样解析 xml第一行必须写xml头<?xml version='1.0' encoding='utf8'? ...
- 【转】MyBatis学习总结(三)——优化MyBatis配置文件中的配置
[转]MyBatis学习总结(三)——优化MyBatis配置文件中的配置 一.连接数据库的配置单独放在一个properties文件中 之前,我们是直接将数据库的连接配置信息写在了MyBatis的con ...
- SQL*PLUS中批量执行SQL语句
SQL*PLUS中批量执行SQL语句 今天由于工作的需要,要在CMD中批量执行大量的SQL语句,对于Oracle学习还处在入门阶段的我,只能硬着头皮到处去寻找资料(主要是网络资料,也包括自己的电子书) ...