LeetCode 659: Split Array into Consecutive Subsequence

Note:

1. If it does not belong any sequences : append.getOrDefault(num, 0) == 0, create a new sequence. It requires num + 1 and num + 2 count > 0.

2. Once it has sequence, move sequece to num + 1.

class Solution {
    public boolean isPossible(int[] nums) {
        if (nums.length < 3) {
            return false;
        }

        Map<Integer, Integer> count = new HashMap<>(), append = new HashMap<>();
        for (int num : nums) count.put(num, count.getOrDefault(num, 0) + 1);
        for (int num : nums) {
            if (count.get(num) == 0) continue;
            else if (append.getOrDefault(num, 0) > 0) {
                append.put(num, append.get(num) - 1);
                append.put(num + 1, append.getOrDefault(num + 1, 0) + 1);
            } else if (count.getOrDefault(num + 1, 0) > 0 && count.getOrDefault(num + 2, 0) > 0) {
                count.put(num + 1, count.get(num + 1) - 1);
                count.put(num + 2, count.get(num + 2) - 1);
                append.put(num + 3, append.getOrDefault(num + 3, 0) + 1);
            } else return false;
            count.put(num, count.get(num) - 1);
        }
        return true;
    }
}
时间: 2024-10-09 16:06:09

LeetCode 659: Split Array into Consecutive Subsequence的相关文章

leetcode 659. Split Array into Consecutive Subsequences

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example

[LeetCode] 659. Split Array into Consecutive Subsequences 将数组分割成连续子序列

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example

[LeetCode] Split Array into Consecutive Subsequences 将数组分割成连续子序列

You are given an integer array sorted in ascending order (may contain duplicates), you need to split them into several subsequences, where each subsequences consist of at least 3 consecutive integers. Return whether you can make such a split. Example

Leetcode 410. Split Array Largest Sum

Problem: Given an array which consists of non-negative integers and an integer m, you can split the array into m non-empty continuous subarrays. Write an algorithm to minimize the largest sum among these m subarrays. Note: If n is the length of array

LeetCode 548. Split Array with Equal Sum (分割数组使得子数组的和都相同)$

Given an array with n integers, you need to find if there are triplets (i, j, k) which satisfies following conditions: 0 < i, i + 1 < j, j + 1 < k < n - 1 Sum of subarrays (0, i - 1), (i + 1, j - 1), (j + 1, k - 1) and (k + 1, n - 1) should be

[LeetCode] 805. Split Array With Same Average 用相同均值拆分数组

In a given integer array A, we must move every element of A to either list B or list C. (B and C initially start empty.) Return true if and only if after such a move, it is possible that the average value of B is equal to the average value of C, and

LeetCode 842. Split Array into Fibonacci Sequence

原题链接在这里:https://leetcode.com/problems/split-array-into-fibonacci-sequence/ 题目: Given a string S of digits, such as S = "123456579", we can split it into a Fibonacci-like sequence [123, 456, 579]. Formally, a Fibonacci-like sequence is a list F o

leetcode659 Split Array into Consecutive Subsequences

思路: 对于每个数,尽量放在已有子序列的后面:如果不能,就创建一个新的子序列. 实现: 1 class Solution 2 { 3 public: 4 bool isPossible(vector<int>& nums) 5 { 6 unordered_map<int, int> cnt, end; 7 for (auto i : nums) cnt[i]++; 8 for (auto i : nums) 9 { 10 if (cnt[i] == 0) continue;

codeforce 977 F. Consecutive Subsequence

F. Consecutive Subsequence time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output You are given an integer array of length nn. You have to choose some subsequence of this array of maximum length