Skip to content

Commit 5ed7519

Browse files
committed
solve 77-80
1 parent a2e0f72 commit 5ed7519

File tree

3 files changed

+179
-65
lines changed

3 files changed

+179
-65
lines changed

daily.py

Lines changed: 165 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1638,7 +1638,7 @@ def test_75():
16381638
test_75()
16391639

16401640
"""
1641-
question 76
1641+
question 76 TODO
16421642
You are given an N by M 2D matrix of lowercase letters. Determine the minimum
16431643
number of columns that can be removed to ensure that each row is ordered from
16441644
top to bottom lexicographically. That is, the letter at each column is
@@ -1686,4 +1686,167 @@ def test_76():
16861686
pass
16871687

16881688

1689-
test_76()
1689+
"""
1690+
question 77
1691+
Given a list of possibly overlapping intervals, return a new list of intervals
1692+
where all overlapping intervals have been merged.
1693+
1694+
The input list is not necessarily ordered in any way.
1695+
1696+
For example, given [(1, 3), (5, 8), (4, 10), (20, 25)], you should return [(1,
1697+
3), (4, 10), (20, 25)].
1698+
1699+
-------------------
1700+
1. Sort the interval list by start time.
1701+
2. use two pointers to iterate the sorted list.
1702+
p1 points to the last merged interval, p2 points to the interval to check.
1703+
if the two intervals are overlapped:
1704+
merge them and save the merged interval to first interval. p2 moves forward.
1705+
othewise,
1706+
p1 moves forward and save the second list to p1. p2 moves forward.
1707+
time O(n), space O(1)
1708+
"""
1709+
1710+
1711+
def mergeIntervals(intervals):
1712+
intervals.sort(key=lambda x: x[0])
1713+
merged = [intervals[0]]
1714+
p1 = 0
1715+
for p2 in range(1, len(intervals)):
1716+
s1, e1 = merged[p1]
1717+
s2, e2 = intervals[p2]
1718+
if s2 <= e1: # overlapped
1719+
merged[p1] = (s1, max(e1, e2))
1720+
else:
1721+
p1 += 1
1722+
merged.append(intervals[p2])
1723+
return merged
1724+
1725+
1726+
def test_77():
1727+
intervals = [(1, 3), (5, 8), (4, 10), (20, 25)]
1728+
assert mergeIntervals(intervals) == [(1, 3), (4, 10), (20, 25)]
1729+
intervals = [(1, 10), (15, 21), (3, 8), (20, 25), [11, 18], [30, 50]]
1730+
assert mergeIntervals(intervals) == [(1, 10), (11, 25), [30, 50]]
1731+
1732+
1733+
"""
1734+
question 78
1735+
Given k sorted singly linked lists, write a function to merge all the lists into
1736+
one sorted singly linked list.
1737+
1738+
-------------------
1739+
use heap
1740+
1. put k list to the heap, comparing by head's value
1741+
2. pop the list from the heap:
1742+
Add its head element to the return list.
1743+
move the head to next element. If head is not none, add it to the heap.
1744+
3. repeat #2 until heap is empty.
1745+
we can do some optimzation when the heap has only one elment.
1746+
1747+
time O(nlgk), n is the total number of element in the k list. space O(k)
1748+
"""
1749+
1750+
1751+
def question78():
1752+
pass
1753+
1754+
1755+
def test_78():
1756+
pass
1757+
1758+
1759+
"""
1760+
question 79
1761+
Given an array of integers, write a function to determine whether the array
1762+
could become non-decreasing by modifying at most 1 element.
1763+
1764+
For example, given the array [10, 5, 7], you should return true, since we can
1765+
modify the 10 into a 1 to make the array non-decreasing.
1766+
1767+
Given the array [10, 5, 1], you should return false, since we can't modify any
1768+
one element to get a non-decreasing array.
1769+
1770+
-------------------
1771+
Scan the array from left to right, there should be at most one decreaing happen.
1772+
If the decreasing happend at the second element, we can change the first element
1773+
to a value less than the second one. So return true.
1774+
If the decreasing happend at the last element, we can change the last element to
1775+
a value larger than its previous one. So return true.
1776+
But if the descreasing happens in the middle, it depends.
1777+
There is a valley we need to check the previous element and next element of the valley.
1778+
if pre <= next, we can change the valley to a vlue in between. otherwise we can not.
1779+
1780+
"""
1781+
1782+
1783+
def canBecomeNonincreasing(nums):
1784+
count = 0
1785+
valley_idx = 0
1786+
pre = nums[0]
1787+
for i in range(1, len(nums)):
1788+
if nums[i] < pre:
1789+
count += 1
1790+
valley_idx = i
1791+
pre = nums[i]
1792+
if count == 0:
1793+
return True
1794+
if count > 1:
1795+
return False
1796+
# count = 1
1797+
if (
1798+
valley_idx == 1
1799+
or valley_idx == len(nums) - 1
1800+
or nums[valley_idx - 1] <= nums[valley_idx + 1]
1801+
):
1802+
return True
1803+
else:
1804+
return False
1805+
1806+
1807+
def test_79():
1808+
nums = [10, 5, 7]
1809+
assert canBecomeNonincreasing(nums)
1810+
nums = [10, 5, 1]
1811+
assert not canBecomeNonincreasing(nums)
1812+
nums = [1, 5, 2, 7]
1813+
assert canBecomeNonincreasing(nums)
1814+
nums = [1, 5, 2, 3]
1815+
assert not canBecomeNonincreasing(nums)
1816+
nums = [1, 2, 3, -10]
1817+
assert canBecomeNonincreasing(nums)
1818+
1819+
1820+
"""
1821+
question 80
1822+
Given the root of a binary tree, return a deepest node. For example, in the
1823+
following tree, return d.
1824+
1825+
a
1826+
/ \
1827+
b c
1828+
/
1829+
d
1830+
-------------------
1831+
create a recursive function to travel the tree in depth-first traversal
1832+
and return a pair, first is the depth of the tree and second is the deepest node.
1833+
1834+
def dfs(tree, depth):
1835+
if not tree:
1836+
return (0, None)
1837+
# leaf node
1838+
if not tree.left and not tree.right:
1839+
return (depth, tree)
1840+
left = dfs(tree.left, depth+1)
1841+
right = dfs(trree.right, depth+1)
1842+
return left if left[0] > right[0] else right
1843+
1844+
"""
1845+
1846+
1847+
def question80():
1848+
pass
1849+
1850+
1851+
def test_80():
1852+
pass

output.txt

Lines changed: 6 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,12 @@
11
"""
2-
question 76
3-
You are given an N by M 2D matrix of lowercase letters. Determine the minimum
4-
number of columns that can be removed to ensure that each row is ordered from
5-
top to bottom lexicographically. That is, the letter at each column is
6-
lexicographically later as you go down each row. It does not matter whether each
7-
row itself is ordered lexicographically.
8-
9-
For example, given the following table:
10-
11-
cba
12-
daf
13-
ghi
14-
15-
This is not ordered because of the a in the center. We can remove the second
16-
column to make it ordered:
17-
ca
18-
df
19-
gi
20-
21-
So your function should return 1, since we only needed to remove 1 column.
22-
23-
As another example, given the following table:
24-
abcdef
25-
26-
Your function should return 0, since the rows are already ordered (there's only
27-
one row).
28-
29-
As another example, given the following table:
30-
zyx
31-
wvu
32-
tsr
33-
34-
Your function should return 3, since we would need to remove all the columns to
35-
order it.
2+
question 80
3+
Given the root of a binary tree, return a deepest node. For example, in the
4+
following tree, return d.
365

376
-------------------
387

398

409
"""
41-
def question76(): pass
42-
def test_76(): pass
43-
test_76()
10+
def question80(): pass
11+
def test_80(): pass
12+
test_80()

question.txt

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
1-
76
2-
You are given an N by M 2D matrix of lowercase letters. Determine the minimum number of columns that can be removed to ensure that each row is ordered from top to bottom lexicographically. That is, the letter at each column is lexicographically later as you go down each row. It does not matter whether each row itself is ordered lexicographically.
3-
4-
For example, given the following table:
5-
6-
cba
7-
daf
8-
ghi
9-
This is not ordered because of the a in the center. We can remove the second column to make it ordered:
10-
11-
ca
12-
df
13-
gi
14-
So your function should return 1, since we only needed to remove 1 column.
15-
16-
As another example, given the following table:
17-
18-
abcdef
19-
Your function should return 0, since the rows are already ordered (there's only one row).
20-
21-
As another example, given the following table:
22-
23-
zyx
24-
wvu
25-
tsr
26-
Your function should return 3, since we would need to remove all the columns to order it.
1+
80
2+
Given the root of a binary tree, return a deepest node. For example, in the following tree, return d.
3+
4+
a
5+
/ \
6+
b c
7+
/
8+
d

0 commit comments

Comments
 (0)