Skip to content

Commit 9a9e46c

Browse files
author
Partho Biswas
committed
659. Split Array into Consecutive Subsequences
1 parent 0c004a0 commit 9a9e46c

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,15 +255,14 @@ I have solved quite a number of problems from several topics. See the below tabl
255255
|06| **[480. Sliding Window Median](https://leetcode.com/problems/sliding-window-median/)** | [Python](https://tinyurl.com/wu6rdaw/480_Sliding_Window_Median.py)| [educative.io](https://tinyurl.com/rp96568), [Vid 1](https://tinyurl.com/uz66xbf) | Hard | 📌 Sliding window with 2 heap. very important |
256256
|07| **[4. Median of Two Sorted Arrays](https://leetcode.com/problems/median-of-two-sorted-arrays/)** | [Python](https://tinyurl.com/wu6rdaw/4_Median_of_Two_Sorted_Arrays.py)| [Article 01](https://tinyurl.com/wn5w5j2), [Art 2](https://tinyurl.com/qm3fbbu) [Video 1](https://www.youtube.com/watch?v=LPFhl65R7ww)| Hard | 📌 Classic problem |
257257
|08| [215. Kth Largest Element in an Array](https://tinyurl.com/y6cjzgbv) | [Python](https://tinyurl.com/wu6rdaw/215_Kth_Largest_Element_in_an_Array.py)| [Art 01](https://tinyurl.com/svnozo2), [Algoexpert.io](https://tinyurl.com/rjzx5hw), [educative.io](https://tinyurl.com/r34urrx)| Medium | 📌 Also check Quickselect method. |
258-
|09| [659. Split Array into Consecutive Subsequences](https://tinyurl.com/vsbddyl) | [Python](https://tinyurl.com/wu6rdaw/659_Split_Array_into_Consecutive_Subsequences.py)| [Art 0](https://tinyurl.com/qq5jucw), [Art 1](https://tinyurl.com/qk7m78s), [Art 2](https://tinyurl.com/sjkdtre), [Art 3](https://tinyurl.com/tnewgqa) | Medium | 📌 It's a fucking unclear problem. TODO: Not done. Check again later |
258+
|09| **[373. Find K Pairs with Smallest Sums](https://tinyurl.com/w9succr)** | [Python](https://tinyurl.com/wu6rdaw/373_Find_K_Pairs_with_Smallest_Sums.py)| [educative.io](https://tinyurl.com/r8ovn6v) | Hard | 📌 TODO: Check again. |
259259
|10| [973. K Closest Points to Origin](https://tinyurl.com/t4d3ge7) | [Python](https://tinyurl.com/wu6rdaw/973_K_Closest_Points_to_Origin.py)| [Algoexpert.io](https://tinyurl.com/rjzx5hw), [educative.io](https://tinyurl.com/vppeh5y), [Vid 1](https://tinyurl.com/wey8mub) | Medium | 📌 Also check Quickselect and heap method |
260260
|11| [451. Sort Characters By Frequency](https://tinyurl.com/y8x876bf) | [Python](https://tinyurl.com/wu6rdaw/451_Sort_Characters_By_Frequency.py)| [Algoexpert.io](https://tinyurl.com/rjzx5hw), [educative.io](https://tinyurl.com/vtk3de8) | Medium | 📌 Also check Quickselect and heap method |
261261
|12| [692. Top K Frequent Words](https://tinyurl.com/yx69p89s) | [Python](https://tinyurl.com/wu6rdaw/692_Top_K_Frequent_Words.py)| [Algoexpert.io](https://tinyurl.com/rjzx5hw), [educative.io](https://tinyurl.com/vtk3de8) | Medium | 📌 Also check Quickselect and heap method |
262262
|13| [703. Kth Largest Element in a Stream](https://tinyurl.com/wqrgost) | [Python](https://tinyurl.com/wu6rdaw/703_Kth_Largest_Element_in_a_Stream.py)| [educative.io](https://tinyurl.com/uajjww8) | Medium | 📌 --- |
263263
|14| [895. Maximum Frequency Stack](https://tinyurl.com/szdk34w) | [Python](https://tinyurl.com/wu6rdaw/895_Maximum_Frequency_Stack.py)| [educative.io](https://tinyurl.com/w5cd6yh) | Hard | 📌 TODO: Check again |
264264
|15| **[378. Kth Smallest Element in a Sorted Matrix](https://tinyurl.com/shz4289)** | [Python](https://tinyurl.com/wu6rdaw/378_Kth_Smallest_Element_in_a_Sorted_Matrix.py)| [educative.io](https://tinyurl.com/scbjgjd) | Hard | 📌 TODO: Check again the Binary Search approach. Very important |
265265
|16| **[632. Smallest Range Covering Elements from K Lists](https://tinyurl.com/v3fshwo)** | [Python](https://tinyurl.com/wu6rdaw/632_Smallest_Range_Covering_Elements_from_K_Lists.py)| [educative.io](https://tinyurl.com/ubfua7f) | Hard | 📌 TODO: Check again. Very important |
266-
|17| **[373. Find K Pairs with Smallest Sums](https://tinyurl.com/w9succr)** | [Python](https://tinyurl.com/wu6rdaw/373_Find_K_Pairs_with_Smallest_Sums.py)| [educative.io](https://tinyurl.com/r8ovn6v) | Hard | 📌 TODO: Check again. |
267266

268267
</p>
269268
</details>
@@ -518,6 +517,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
518517
|13| [252. Meeting Rooms](https://tinyurl.com/whmtgr7)| [Python](https://tinyurl.com/wu6rdaw/252_Meeting_Rooms.py)| [Official](https://leetcode.com/problems/meeting-rooms/solution/) | Easy | Greedy and uses heap |
519518
|14| [253. Meeting Rooms II](https://tinyurl.com/r8vr7qo)| [Python](https://tinyurl.com/wu6rdaw/253_Meeting_Rooms_II.py)| [Official](https://leetcode.com/problems/meeting-rooms-ii/solution/) | Medium | Greedy and uses heap |
520519
|15| [759. Employee Free Time](https://tinyurl.com/y5z73vre)| [Python](https://tinyurl.com/wu6rdaw/759_Employee_Free_Time.py)| [Educative.io](https://tinyurl.com/v42vmyr) | Hard | Greedy and uses heap. Not done. Check again |
520+
|16| [659. Split Array into Consecutive Subsequences](https://tinyurl.com/vsbddyl) | [Python](https://tinyurl.com/wu6rdaw/659_Split_Array_into_Consecutive_Subsequences.py)| [Art 0](https://tinyurl.com/qq5jucw), [Art 1](https://tinyurl.com/qk7m78s), [Art 2](https://tinyurl.com/sjkdtre), [Art 3](https://tinyurl.com/tnewgqa), **[Vid 1](https://tinyurl.com/u2hnq84)**, **[Art 4](https://tinyurl.com/w3j4yqp)** | Medium | 📌 It's a fucking unclear problem. TODO: Not done. Check again later |
521521

522522
</p>
523523
</details>
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Source: https://tinyurl.com/u2hnq84 and https://tinyurl.com/w3j4yqp
2+
from collections import defaultdict
3+
class Solution(object):
4+
def isPossible(self, nums):
5+
"""
6+
:type nums: List[int]
7+
:rtype: bool
8+
"""
9+
occurrences, nextNums = defaultdict(int), defaultdict(int)
10+
for num in nums:
11+
occurrences[num] += 1
12+
for num in nums:
13+
if occurrences[num] == 0:
14+
continue
15+
elif nextNums[num] > 0:
16+
nextNums[num] -= 1
17+
nextNums[num + 1] += 1
18+
elif occurrences[num + 1] > 0 and occurrences[num + 2] > 0:
19+
occurrences[num + 1] -= 1
20+
occurrences[num + 2] -= 1
21+
nextNums[num + 3] += 1
22+
else:
23+
return False
24+
occurrences[num] -= 1
25+
return True

0 commit comments

Comments
 (0)