ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

LeetCode 0128 Longest Consecutive Sequence

2022-05-19 08:03:49  阅读:174  来源: 互联网

标签:map right Sequence int sequence ranges Longest Consecutive left


原题传送门

1. 题目描述

2. Solution 1

1、思路分析
使用HashMap来记录连续序列长度,并把结果保存到边界点。如,给定序列{1, 2, 3, 4, 5}, map.get(1) 与 map.get(5)均得到5。
当一个新元素n插入到map时,做下面两件事

  1. 查询n-1 和 n+1是否存在在map中,若存在,则表示序列延伸至n。变量left、right分别表示上述两序列长度,其中0表示不存在序列。把n -> (left + right +1) 插入到map中。
  2. 用left、right来定位新序列的边界点,并更新边界的长度。

2、代码实现

package Q0199.Q0128LongestConsecutiveSequence;

import java.util.HashMap;

/*
   We will use HashMap. The key thing is to keep track of the sequence length and store that in the boundary points
   of the sequence. For example, as a result, for sequence {1, 2, 3, 4, 5}, map.get(1) and map.get(5) should both return 5.

   Whenever a new element n is inserted into the map, do two things:

   1) See if n - 1 and n + 1 exist in the map, and if so, it means there is an existing sequence next to n.
   Variables left and right will be the length of those two sequences, while 0 means there is no sequence and n will
   be the boundary point later. Store (left + right + 1) as the associated value to key n into the map.
   2) Use left and right to locate the other end of the sequences to the left and right of n respectively, and
   replace the value with the new length.
   Everything inside the for loop is O(1) so the total time is O(n).
 */
public class Solution {

    public int longestConsecutive(int[] nums) {
        int res = 0;
        HashMap<Integer, Integer> ranges = new HashMap<>();
        for (int n : nums) {
            if (ranges.containsKey(n)) continue;
            // 1. Find left and right num
            int left = ranges.getOrDefault(n - 1, 0);
            int right = ranges.getOrDefault(n + 1, 0);
            int sum = left + right + 1;
            ranges.put(n, sum);  // Keep each number in Map to de-duplicate
            res = Math.max(res, sum);

            // 2. Union by only updating boundary
            // Leave middle k-v dirty to avoid cascading update
            if (left > 0) ranges.put(n - left, sum);
            if (right > 0) ranges.put(n + right, sum);
        }
        return res;
    }
}

/*
漂亮的cpp解法
use a hash map to store boundary information of consecutive sequence for each element; there are 4 cases when a new
element i reached:
1) neither i+1 nor i-1 has been seen: m[i]=1;
2) both i+1 and i-1 have been seen: extend m[i+m[i+1]] and m[i-m[i-1]] to each other;
3) only i+1 has been seen: extend m[i+m[i+1]] and m[i] to each other;
4) only i-1 has been seen: extend m[i-m[i-1]] and m[i] to each other.

int longestConsecutive(vector<int> &num) {
    unordered_map<int, int> m;
    int r = 0;
    for (int i : num) {
        if (m[i]) continue;
        r = max(r, m[i] = m[i + m[i + 1]] = m[i - m[i - 1]] = m[i + 1] + m[i - 1] + 1);
    }
    return r;
}
 */

3、复杂度分析
时间复杂度: O(n)
空间复杂度: O(n)

标签:map,right,Sequence,int,sequence,ranges,Longest,Consecutive,left
来源: https://www.cnblogs.com/junstat/p/16287136.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有