From 0faca374b2cbcf9c149af1ac6a208ae4bd925c62 Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Sun, 10 May 2020 01:31:37 +0300 Subject: [PATCH 01/13] insert-intervals --- README.md | 1 - intervals.md | 93 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 1 deletion(-) delete mode 100644 README.md create mode 100644 intervals.md diff --git a/README.md b/README.md deleted file mode 100644 index 4372b78..0000000 --- a/README.md +++ /dev/null @@ -1 +0,0 @@ -# algorithms \ No newline at end of file diff --git a/intervals.md b/intervals.md new file mode 100644 index 0000000..02d79f9 --- /dev/null +++ b/intervals.md @@ -0,0 +1,93 @@ +# Intervals + +## Non-overlapping intervals + +```python +class Solution(object): + @staticmethod + def eraseOverlapIntervals(intervals): + """ + :type intervals: List[List[int]] + :rtype: int + """ + + print(intervals) + intervals = sorted(intervals, key=lambda x: x[0]) + out = 0 + pstart = pend = float('-inf') + for start, end in intervals: + if start >= pend: + pstart, pend = start, end + else: + if end <= pend: + pstart, pend = start, end + out += 1 + return out + + +print(Solution.eraseOverlapIntervals([[1, 2], [2, 3], [3, 4], [1, 3]]), '\n') # [[1, 2], [2, 3], [3, 4], [1, 3]] \n1 + +print(Solution.eraseOverlapIntervals([[1, 2], [1, 2], [1, 2]]), '\n') # [[1, 2], [1, 2], [1, 2]] \n2 + +print(Solution.eraseOverlapIntervals([[1, 2], [2, 3]])) # [[1, 2], [2, 3]] \n0 + +``` + +## Insert intervals + +```python +class Solution(object): + @staticmethod + def insert(intervals, newInterval): + """ + :type intervals: List[List[int]] + :type newInterval: List[int] + :rtype: List[List[int]] + """ + + print(intervals, newInterval) + intervals.append(newInterval) + out = [] + for i in sorted(intervals, key=lambda x: x[0]): + if out and i[0] <= out[-1][1]: + out[-1][1] = max(i[1], out[-1][1]) + else: + out += [i] + return out + + +print(Solution.insert([[1, 3], [6, 9]], [2, 5]), '\n') # [[1, 3], [6, 9]] [2, 5] \n[[1, 5], [6, 9]] + +print(Solution.insert([[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]], + [4, 8])) # [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]] [4, 8] \n[[1, 2], [3, 10], [12, 16]] + +``` + +## Merge intervals + +```python +class Solution(object): + @staticmethod + def merge(intervals): + """ + :type intervals: List[List[int]] + :rtype: List[List[int]] + """ + + print(intervals) + out = [] + for i in sorted(intervals, key=lambda x: x[0]): + # print('out: {}, i[0]: {}'.format(out, i[0])) + if out and i[0] <= out[-1][1]: + out[-1][1] = max(i[1], out[-1][1]) + else: + out += [i] + return out + + +print(Solution.merge([[1, 3], [2, 6], [8, 10], [15, 18]]), + '\n') # [[1, 3], [2, 6], [8, 10], [15, 18]] \n[[1, 6], [8, 10], [15, 18]] + +print(Solution.merge([[1, 4], [4, 5]])) # [[1, 4], [4, 5]] \n[[1, 5]] + +``` \ No newline at end of file From 07ffd338401f93a6120aa3416f1999bc8a7c4a49 Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Sun, 10 May 2020 02:46:01 +0300 Subject: [PATCH 02/13] Added links, changed task order --- intervals.md | 38 ++++++++++++++++++++++---------------- 1 file changed, 22 insertions(+), 16 deletions(-) diff --git a/intervals.md b/intervals.md index 02d79f9..3a802b2 100644 --- a/intervals.md +++ b/intervals.md @@ -2,6 +2,8 @@ ## Non-overlapping intervals +https://leetcode.com/problems/non-overlapping-intervals + ```python class Solution(object): @staticmethod @@ -33,22 +35,23 @@ print(Solution.eraseOverlapIntervals([[1, 2], [2, 3]])) # [[1, 2], [2, 3]] \n0 ``` -## Insert intervals +## Merge intervals + +https://leetcode.com/problems/merge-intervals/ ```python class Solution(object): @staticmethod - def insert(intervals, newInterval): + def merge(intervals): """ :type intervals: List[List[int]] - :type newInterval: List[int] :rtype: List[List[int]] """ - print(intervals, newInterval) - intervals.append(newInterval) + print(intervals) out = [] for i in sorted(intervals, key=lambda x: x[0]): + # print('out: {}, i[0]: {}'.format(out, i[0])) if out and i[0] <= out[-1][1]: out[-1][1] = max(i[1], out[-1][1]) else: @@ -56,28 +59,31 @@ class Solution(object): return out -print(Solution.insert([[1, 3], [6, 9]], [2, 5]), '\n') # [[1, 3], [6, 9]] [2, 5] \n[[1, 5], [6, 9]] +print(Solution.merge([[1, 3], [2, 6], [8, 10], [15, 18]]), + '\n') # [[1, 3], [2, 6], [8, 10], [15, 18]] \n[[1, 6], [8, 10], [15, 18]] -print(Solution.insert([[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]], - [4, 8])) # [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]] [4, 8] \n[[1, 2], [3, 10], [12, 16]] +print(Solution.merge([[1, 4], [4, 5]])) # [[1, 4], [4, 5]] \n[[1, 5]] ``` -## Merge intervals +## Insert intervals + +https://leetcode.com/problems/insert-interval/ ```python class Solution(object): @staticmethod - def merge(intervals): + def insert(intervals, newInterval): """ :type intervals: List[List[int]] + :type newInterval: List[int] :rtype: List[List[int]] """ - print(intervals) + print(intervals, newInterval) + intervals.append(newInterval) out = [] for i in sorted(intervals, key=lambda x: x[0]): - # print('out: {}, i[0]: {}'.format(out, i[0])) if out and i[0] <= out[-1][1]: out[-1][1] = max(i[1], out[-1][1]) else: @@ -85,9 +91,9 @@ class Solution(object): return out -print(Solution.merge([[1, 3], [2, 6], [8, 10], [15, 18]]), - '\n') # [[1, 3], [2, 6], [8, 10], [15, 18]] \n[[1, 6], [8, 10], [15, 18]] +print(Solution.insert([[1, 3], [6, 9]], [2, 5]), '\n') # [[1, 3], [6, 9]] [2, 5] \n[[1, 5], [6, 9]] -print(Solution.merge([[1, 4], [4, 5]])) # [[1, 4], [4, 5]] \n[[1, 5]] +print(Solution.insert([[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]], + [4, 8])) # [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]] [4, 8] \n[[1, 2], [3, 10], [12, 16]] -``` \ No newline at end of file +``` From c375463ae0b6a473c52aea3f4897678ac180bd15 Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Wed, 13 May 2020 13:59:18 +0300 Subject: [PATCH 03/13] Removed prints and decorators --- .idea/.gitignore | 2 ++ .idea/algorithms.iml | 8 ++++++ .../inspectionProfiles/profiles_settings.xml | 6 +++++ .idea/misc.xml | 6 +++++ .idea/modules.xml | 8 ++++++ .idea/vcs.xml | 6 +++++ intervals.md | 25 ------------------- 7 files changed, 36 insertions(+), 25 deletions(-) create mode 100644 .idea/.gitignore create mode 100644 .idea/algorithms.iml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..5c98b42 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,2 @@ +# Default ignored files +/workspace.xml \ No newline at end of file diff --git a/.idea/algorithms.iml b/.idea/algorithms.iml new file mode 100644 index 0000000..d0876a7 --- /dev/null +++ b/.idea/algorithms.iml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 0000000..105ce2d --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..28a804d --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..5682d73 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/intervals.md b/intervals.md index 3a802b2..ed0cd4b 100644 --- a/intervals.md +++ b/intervals.md @@ -6,14 +6,12 @@ https://leetcode.com/problems/non-overlapping-intervals ```python class Solution(object): - @staticmethod def eraseOverlapIntervals(intervals): """ :type intervals: List[List[int]] :rtype: int """ - print(intervals) intervals = sorted(intervals, key=lambda x: x[0]) out = 0 pstart = pend = float('-inf') @@ -26,13 +24,6 @@ class Solution(object): out += 1 return out - -print(Solution.eraseOverlapIntervals([[1, 2], [2, 3], [3, 4], [1, 3]]), '\n') # [[1, 2], [2, 3], [3, 4], [1, 3]] \n1 - -print(Solution.eraseOverlapIntervals([[1, 2], [1, 2], [1, 2]]), '\n') # [[1, 2], [1, 2], [1, 2]] \n2 - -print(Solution.eraseOverlapIntervals([[1, 2], [2, 3]])) # [[1, 2], [2, 3]] \n0 - ``` ## Merge intervals @@ -41,14 +32,12 @@ https://leetcode.com/problems/merge-intervals/ ```python class Solution(object): - @staticmethod def merge(intervals): """ :type intervals: List[List[int]] :rtype: List[List[int]] """ - print(intervals) out = [] for i in sorted(intervals, key=lambda x: x[0]): # print('out: {}, i[0]: {}'.format(out, i[0])) @@ -58,12 +47,6 @@ class Solution(object): out += [i] return out - -print(Solution.merge([[1, 3], [2, 6], [8, 10], [15, 18]]), - '\n') # [[1, 3], [2, 6], [8, 10], [15, 18]] \n[[1, 6], [8, 10], [15, 18]] - -print(Solution.merge([[1, 4], [4, 5]])) # [[1, 4], [4, 5]] \n[[1, 5]] - ``` ## Insert intervals @@ -72,7 +55,6 @@ https://leetcode.com/problems/insert-interval/ ```python class Solution(object): - @staticmethod def insert(intervals, newInterval): """ :type intervals: List[List[int]] @@ -80,7 +62,6 @@ class Solution(object): :rtype: List[List[int]] """ - print(intervals, newInterval) intervals.append(newInterval) out = [] for i in sorted(intervals, key=lambda x: x[0]): @@ -90,10 +71,4 @@ class Solution(object): out += [i] return out - -print(Solution.insert([[1, 3], [6, 9]], [2, 5]), '\n') # [[1, 3], [6, 9]] [2, 5] \n[[1, 5], [6, 9]] - -print(Solution.insert([[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]], - [4, 8])) # [[1, 2], [3, 5], [6, 7], [8, 10], [12, 16]] [4, 8] \n[[1, 2], [3, 10], [12, 16]] - ``` From 08ae2bb3e0da959a5490d4076b8e6ef817d92f06 Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Wed, 13 May 2020 14:31:21 +0300 Subject: [PATCH 04/13] Final solution --- .idea/algorithms.iml | 2 +- .idea/misc.xml | 1 + intervals.md | 6 +++--- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.idea/algorithms.iml b/.idea/algorithms.iml index d0876a7..c444878 100644 --- a/.idea/algorithms.iml +++ b/.idea/algorithms.iml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml index 28a804d..8656114 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -3,4 +3,5 @@ + \ No newline at end of file diff --git a/intervals.md b/intervals.md index ed0cd4b..3e0f8ae 100644 --- a/intervals.md +++ b/intervals.md @@ -6,7 +6,7 @@ https://leetcode.com/problems/non-overlapping-intervals ```python class Solution(object): - def eraseOverlapIntervals(intervals): + def eraseOverlapIntervals(self, intervals): """ :type intervals: List[List[int]] :rtype: int @@ -32,7 +32,7 @@ https://leetcode.com/problems/merge-intervals/ ```python class Solution(object): - def merge(intervals): + def merge(self, intervals): """ :type intervals: List[List[int]] :rtype: List[List[int]] @@ -55,7 +55,7 @@ https://leetcode.com/problems/insert-interval/ ```python class Solution(object): - def insert(intervals, newInterval): + def insert(self, intervals, newInterval): """ :type intervals: List[List[int]] :type newInterval: List[int] From a0346fa075fe8e2189a81614ebfc6ae87b388d19 Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Wed, 13 May 2020 14:35:32 +0300 Subject: [PATCH 05/13] .idea changed --- .idea/workspace.xml | 115 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 115 insertions(+) create mode 100644 .idea/workspace.xml diff --git a/.idea/workspace.xml b/.idea/workspace.xml new file mode 100644 index 0000000..41f6a48 --- /dev/null +++ b/.idea/workspace.xml @@ -0,0 +1,115 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + 1589367377979 + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From eabab91b23da897ed8643df31cab3923400b7b1e Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Wed, 13 May 2020 14:39:22 +0300 Subject: [PATCH 06/13] .idea/workspace --- .idea/workspace.xml | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 41f6a48..0fc1751 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -1,11 +1,7 @@ - - - - - + From b242859500188aa26c4ccacfc78042d05c6dbeaa Mon Sep 17 00:00:00 2001 From: Igneaalis Date: Wed, 13 May 2020 14:45:14 +0300 Subject: [PATCH 07/13] blablabla --- .idea/workspace.xml | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 0fc1751..54bf3ed 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -76,7 +76,7 @@ @@ -84,22 +84,22 @@