ICode9

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

659. Split Array into Consecutive Subsequences

2022-08-19 13:33:05  阅读:143  来源: 互联网

标签:pq end nums into Subsequences Split subsequences split


You are given an integer array nums that is sorted in non-decreasing order.

Determine if it is possible to split nums into one or more subsequences such that both of the following conditions are true:

  • Each subsequence is a consecutive increasing sequence (i.e. each integer is exactly one more than the previous integer).
  • All subsequences have a length of 3 or more.

Return true if you can split nums according to the above conditions, or false otherwise.

A subsequence of an array is a new array that is formed from the original array by deleting some (can be none) of the elements without disturbing the relative positions of the remaining elements. (i.e., [1,3,5] is a subsequence of [1,2,3,4,5] while [1,3,2] is not).

 

Example 1:

Input: nums = [1,2,3,3,4,5]
Output: true
Explanation: nums can be split into the following subsequences:
[1,2,3,3,4,5] --> 1, 2, 3
[1,2,3,3,4,5] --> 3, 4, 5

Example 2:

Input: nums = [1,2,3,3,4,4,5,5]
Output: true
Explanation: nums can be split into the following subsequences:
[1,2,3,3,4,4,5,5] --> 1, 2, 3, 4, 5
[1,2,3,3,4,4,5,5] --> 3, 4, 5

Example 3:

Input: nums = [1,2,3,4,4,5]
Output: false
Explanation: It is impossible to split nums into consecutive increasing subsequences of length 3 or more.

 

Constraints:

  • 1 <= nums.length <= 104
  • -1000 <= nums[i] <= 1000
  • nums is sorted in non-decreasing order.
class Solution {
    public boolean isPossible(int[] nums) {
        int n = nums.length;
        if(n < 3) return false;
        PriorityQueue<Interval> pq = new PriorityQueue<>((a, b) -> a.end == b.end ? a.len - b.len : a.end - b.end);
        for(int i : nums) {
            while(!pq.isEmpty() && (pq.peek().end + 1 < i)) {
                if(pq.poll().len < 3) return false;
            }
            if(pq.isEmpty() || i == pq.peek().end) {
                pq.add(new Interval(i, 1));
            }
            else {
                Interval it = pq.poll();
                it.len++;
                it.end = i;
                pq.add(it);
            }
        }
        
        while(!pq.isEmpty()) {
            if(pq.poll().len <3) return false;
        }
        return true;
    }
    
    class Interval {
        int end;
        int len;
        public Interval(int end, int len) {
            this.end = end;
            this.len = len;
        }
    }
}

https://leetcode.com/problems/split-array-into-consecutive-subsequences/discuss/130452/20ms-Java-PriorityQueue-with-Explanations

标签:pq,end,nums,into,Subsequences,Split,subsequences,split
来源: https://www.cnblogs.com/wentiliangkaihua/p/16601666.html

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

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

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

ICode9版权所有