Tuesday, March 29, 2016

Best Time to Buy and Sell Stock


  1. Best Time to Buy and Sell Stock
Leetcode 121 (M)
只能买卖一次。
只能一次的话,就是要寻找最小和最大的值(同时必须满足最大值在最小值后面)
可以用两个值来存,maxcurr记录对于第i天可以拿到的最大利润,maxsofar记录到目前为止见过的最大利润(最大的maxcurr)。
所以唯一的问题是如何计算maxcurr,如果maxcurr小于0,那么就是0,对于第i天的maxcurr来说,如果maxcurr + (p[i] - p[i-1]) > 0, 那么更新maxcurr。

或者可以,一直寻找最小的minprice,然后用prices[i]-minprice,同时即时更新minprice。

code1:
public class Solution {
    public int maxProfit(int[] prices) {
        int maxcurr = 0;
        int maxsofar = 0;
        for (int i = 1; i < prices.length; i++) {
            maxcurr = Math.max(0, maxcurr + (prices[i] - prices[i-1]));
            maxsofar = Math.max(maxsofar, maxcurr);
        }
        return maxsofar;
    }
}

code2:
public int maxProfit(int[] prices) {
    int profit = 0;
    int minElement = Integer.MAX_VALUE;
    for(int i=0; i < prices.length; i++){
       profit = Math.max(profit, prices[i]-minElement);
       minElement = Math.min(minElement, prices[i]);
    }
    return profit;
}
  1. Best Time to Buy and Sell Stock II
Leetcode 122 (M)
可以买卖多次。这样的话很容易理解,就是把所以递增的都增量都加起来就可以了。因为如果是递增,那么price[j] - price[k] = price[j]-price[i] + price[i]-price[k]

public class Solution {
    public int maxProfit(int[] prices) {
        if (prices.length < 2) {
            return 0;
        }
        
        int total = 0;
        
        for (int i = 1; i < prices.length; i++) {
            total += Math.max(0, prices[i] - prices[i-1]);
        }
        return total;
    }
}
  1. Best Time to Buy and Sell Stock III
  2. Best Time to Buy and Sell Stock IV
  3. Best Time to Buy and Sell Stock with Cooldown

Dynamic Programming

Dynamic programming is a technique for solving problems with the following properties:
  1. An instance is solved using the solutions for smaller instances.
  2. The solution for a smaller instance might be needed multiple times.
  3. The solutions to smaller instances are stored in a table, so that each smaller instance is solved only once.
  4. Additional space is used to save time.
经典题目:
1. 买卖股票I II III IV

Tuesday, March 22, 2016

Java技能

Java:
1. OO, 常用Java API (collection, multithreads, I/O (NIO), Socket, JDBC, XML, reflection)。
2. JSP and Servlet (Java web). JSTL/EL。监听器,过滤器(Web组件)。MVC架构模式进行java web项目开发。
3. Spring IoC container。 AOP原理。Spring框架管理各种web组件。
4. Hibernate, MyBatis等ORM框架。熟悉他们的核心API。对hibernate的关联映射,继承映射,组件映射,缓存机制,事务管理,性能调优。
5. HTML/CSS/JavaScript web前端。JQuery和Bootstrap。Ajax在web项目中的应用。前端MVC框架(AngularJS)和JavaScript模板引擎(HandleBars)。
6. 常用的relatinal db (MySQL, Oracle)。熟练使用SQL和PL/SQL。

7. Design Pattern。GoF设计模式。UML。 TDD/DDD
8. Apache, NginX, Tomcat, WildFly, Weblogic等web服务器和应用服务器的使用。
9. 产品原型工具Axure。设计建模工具PowerDesigner和Enterprise Architect。Java开发环境Eclipse和IntelliJ。前端开发环境webstorm。版本控制svn和git。项目构建和管理工具Maven和Gradle。

项目:
本系统是X委托Y开发的用于Z的系统,系统包括A, B, C, D等模块。系统使用了java企业级开发的开源框架E以及前端技术F。表示层运用了G架构,使用H作为视图I作为控制器并实现了REST风格的请求;业务逻辑层运用了J模式,并通过K实现事务、日志和安全性等功能,通过L实现缓存服务;持久层使用了M封装CRUD操作,底层使用N实现数据存取。整个项目采用了P开发模型。

E: Spring
F: JQuery库及其插件,或者是Bootstrap框架。 SPA(单页应用)最佳方案是前端MVC框架(AngularJS)和JavaScript模板引擎(HandleBars)。
G: MVC (模型-视图-控制)。Spring MVC, Struts 2, JSF
J:事务脚本
K: AOP技术
L:memcached和Redis
M: 选择很多,Hibernate和MyBatis (一般来说增删改交给hibernate,复杂查询给MyBatis)
N: Relational DB (MySQL, Oracle, SQLServer etc.), 或者NoSQL(MongoDB, MemBase, BigTable),和其他大数据存取方案(GFS, HDFS)。
P: 瀑布模型,快速原型模型,增量模型,螺旋模型,喷泉模型,RAD模型。

版本控制: CVS/SVN/Git
自动构建:Ant/Maven/Ivy/Gradle
持续集成: Hudson/Jenkins

系统架构:
负载均衡服务器:F5, A10
应用服务器:
    Http: Apache, NginX
    Servlet: Tomcat, Resin
    EJB容器: WildFly(JBoss Application Server), GlassFish,Weblogic, WebSphere
数据库服务器: MySQL / Oracle

第三方工具(插件)应用
图表工具: 基于jQuery的图标插件(jQchart, Flot, Charted)
报表工具: Pentaho Reporting, iReport, DynamicReports等
文档处理: POI, iText
工作流引擎:

Monday, March 14, 2016

常用java API

HashSet:
https://docs.oracle.com/javase/7/docs/api/java/util/HashSet.html

All Implemented Interfaces:
SerializableCloneableIterable<E>, Collection<E>, Set<E>

Methods:
  boolean add(E e)
  boolean contains(O o)
  int size()
  boolean isEmpty()
用处:
  当不需要map的时候(比如只存数字便于查找),可以用HashSet。

Friday, March 4, 2016

Leetcode 52 - N-Queens II

Follow up for N-Queens problem.
Now, instead outputting board configurations, return the total number of distinct solutions.
Backtracking (recursive)
RecursiveNQueens(Q[1 .. n],r):
if r = n + 1
    print Q
else for j ← 1 to n
    legal ← True
    for i ← 1 to r − 1
        if (Q[i] = j) or (Q[i] = j + r − i) or (Q[i] = j − r + i)
        legal ← False
    if legal
        Q[r] ← j
        RecursiveNQueens(Q[1 .. n],r + 1)