zoukankan      html  css  js  c++  java
  • leetcode:longest-increasing

    1、

    Give an integer array,find the longest increasing continuous subsequence in this array.

    An increasing continuous subsequence:

    • Can be from right to left or from left to right.
    • Indices of the integers in the subsequence should be continuous.

    For [5, 4, 2, 1, 3], the LICS is [5, 4, 2, 1], return 4.

    For [5, 1, 2, 3, 4], the LICS is [1, 2, 3, 4], return 4.

    2、思路:

      1、从左到右判断

      2、从右到左判断

      3、初始化为一个参数为1,进行逐一判断

    3、

      

    public class Solution {
        public int longestIncreasingContinuousSubsequence(int[] A) {
            if (A == null || A.length == 0) {
                return 0;
            }
            int n = A.length;
            int answer = 1;
            
            // from left to right
            int length = 1; // just A[0] itself
            for (int i = 1; i < n; i++) {
                if (A[i] > A[i - 1]) {
                    length++;
                } else {
                    length = 1;
                }
                answer = Math.max(answer, length);
            }
            
            // from right to left
            length = 1;
            for (int i = n - 2; i >= 0; i--) {
                if (A[i] > A[i + 1]) {
                    length++;
                } else {
                    length = 1;
                }
                answer = Math.max(answer, length);
            }
            
            return answer;
        }
    }
    //暂无搞懂
    // version 2: Memorization Search, can not get accepted, just show you how to write a memorization search code
    public class Solution {
        private int[] flag;
        private int[] dp;
        private int[] A;
        private int n;
        
        public static int VISITED = 1;
        /**
         * @param A an array of Integer
         * @return  an integer
         */
        public int longestIncreasingContinuousSubsequence(int[] A) {
            if (A == null) {
                return 0;
            }
            
            // initialize
            n = A.length;
            flag = new int[n];
            dp = new int[n];
            this.A = A;
    
            // memorization search
            int ans = 0;
            for (int i = 0; i < n; i++) {
                dp[i] = dfs(i);
                ans = Math.max(ans, dp[i]);
            }
            
            return ans;
        }
        
        int dfs(int index)   {
            if (flag[index] == VISITED) {
                return dp[index];
            }
            
            int ans = 1;
            if (index - 1 >= 0 && A[index] < A[index - 1]) {
                ans = dfs(index - 1) + 1;
            } 
            if (index + 1 < n && A[index] < A[index + 1]) {
                ans = Math.max(ans,  dfs(index + 1) + 1);
            }
            flag[index] = VISITED;
            dp[index] = ans;
            return ans;
        }
    }
    工作小总结,有错请指出,谢谢。
  • 相关阅读:
    群发邮件2
    谈谈C#中的三个关键词new , virtual , override
    一个简单的jQuery插件ajaxfileupload实现ajax上传文件例子
    网站静态化结构
    第四十七章 天神的邀请
    asp.net 异步群发邮件时遭遇到的问题 ddddddddd
    第四十章 远方的消息
    商用群发p2p网络
    第四十八章 三大客卿
    第四十五章 你没让我失望
  • 原文地址:https://www.cnblogs.com/zilanghuo/p/5292889.html
Copyright © 2011-2022 走看看