/**
* @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的更多相关文章

  1. [LeetCode] Repeated DNA Sequences 求重复的DNA序列

    All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: "ACG ...

  2. 187. Repeated DNA Sequences

    题目: All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...

  3. Leetcode:Repeated DNA Sequences详细题解

    题目 All DNA is composed of a series of nucleotides abbreviated as A, C, G, and T, for example: " ...

  4. 【原创】leetCodeOj --- Repeated DNA Sequences 解题报告

    原题地址: https://oj.leetcode.com/problems/repeated-dna-sequences/ 题目内容: All DNA is composed of a series ...

  5. LeetCode哈希表

    1. Two Sum https://leetcode.com/problems/two-sum/description/ 不使用额外空间需要n*n的复杂度 class Solution { publ ...

  6. leetcode187. Repeated DNA Sequences

    https://leetcode.com/problems/repeated-dna-sequences/#/description   https://leetcode.com/problems/r ...

  7. 187 Repeated DNA Sequences 重复的DNA序列

    所有DNA由一系列缩写为A,C,G和 T 的核苷酸组成,例如:“ACGAATTCCG”.在研究DNA时,识别DNA中的重复序列有时非常有用.编写一个函数来查找DNA分子中所有出现超多一次的10个字母长 ...

  8. [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 ...

  9. 重复的DNA序列[哈希表] LeetCode.187

    所有 DNA 由一系列缩写为 A,C,G 和 T 的核苷酸组成,例如:"ACGAATTCCG".在研究 DNA 时,识别 DNA 中的重复序列有时会对研究非常有帮助. 编写一个函数 ...

随机推荐

  1. CodeMachine Debugger Extension DLL

    http://www.codemachine.com/downloads.html http://www.codemachine.com/tool_cmkd.html#stack

  2. careercup-中等难度 17.4

    17.4 编写一个方法,找出两个数字中最大的那一个.不得使用if-else或其他比较运算符. 解法: 我们可以通过一步步的分析来将需要用到的if-else和比较操作符去掉: If a > b, ...

  3. Jordan Lecture Note-7: Soft Margin SVM

    Soft Margin SVM  (1)Recall 之前分析到SVM的模型为: \begin{align}\mathop{\min}&\quad \frac{1}{2}w^\prime w\ ...

  4. MySQL(5):数据表操作

    表名的前缀: 两个student表,为了区分相同逻辑表名的不同应用,给逻辑表名,增加前缀 //学生管理 create table info_student ( name varchar(20), st ...

  5. Web页面向后台提交数据的方式和选择

    1.通过表单提交 这是HTML支持最传统的提交方法,需要创建表单,然后表单包含各种类型的表单元素,还要有一个提交按钮,通过提交按钮来提交到后台,这种方式提交后页面会刷新. 2.通过网页链接提交 可以在 ...

  6. JavaScript高级程序设计(第三版)学习笔记22、24、25章

    第22章,高级技巧 高级函数 安全的类型检测 typeof会出现无法预知的行为 instanceof在多个全局作用域中并不能正确工作 调用Object原生的toString方法,会返回[Object ...

  7. jQuery mouseover与mouseenter,mouseout与mouseleave的区别

    mouseover与mouseenter 不论鼠标指针穿过被选元素或其子元素,都会触发 mouseover 事件. 只有在鼠标指针穿过被选元素时,才会触发 mouseenter 事件. mouseou ...

  8. ASP防止盗链的一段代码

    <%@Language="vbscript"%> <% Response.Buffer = true Response.Expires = -1441 ' var ...

  9. xmlDoc.SelectNodes用法(获取不到节点时注意事项)

    注:以下举例仅针对xml自定义了命名空间的情况,如果是其他情况,请参照他人博客~ using System;using System.Collections.Generic;using System. ...

  10. 再议Unity 3D

    一年前,偶发冲动,翻译了<[译] Unity3D游戏和facebook绑定(1:简介)>系列文章. 现在看有2个明显的好处, 一:给这个不温不火的博客带了top 3的人气: 二:我个人由此 ...