题解 | 连续的牛群标签序列
连续的牛群标签序列
https://www.nowcoder.com/practice/5db36ae74c274176a0cf9274e9f9ed3e
import java.util.*; public class Solution { public int longestConsecutive (int[] tag) { Map<Integer, Integer> map = new HashMap<>(); int longest = 0; for (int t : tag) { if (map.containsKey(t)) { continue; } final int left = map.getOrDefault(t - 1, 0); final int right = map.getOrDefault(t + 1, 0); final int sum = left + right + 1; map.put(t, sum); map.put(t - left, sum); map.put(t + right, sum); longest = Math.max(longest, sum); } return longest; } }