Skip to content

Commit 44ef4ca

Browse files
committed
170_Two_Sum_III-Data_structure_design
296_Best_Meeting_Point
1 parent bb4b45a commit 44ef4ca

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@
7171
| 163 | [Missing Ranges](https://leetcode.com/problems/missing-ranges/) ♥ | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/163_Missing_Ranges.py) | Add -1 to lower for special case, then check if curr - prev >= 2|
7272
| 166 | [Fraction to Recurring Decimal](https://leetcode.com/problems/fraction-to-recurring-decimal/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/166_Fraction_to_Recurring_Decimal.py) | % and Hash to find duplicate |
7373
| 167 | [Two Sum II - Input array is sorted](https://leetcode.com/problems/two-sum-ii-input-array-is-sorted/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/167_Two_Sum_II_Input_array_is_sorted.py) | Two points O(n) and O(1) |
74+
| 170 | [Two Sum III - Data structure design](https://leetcode.com/problems/two-sum-iii-data-structure-design/)
75+
&hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/170_Two_Sum_III-Data_structure_design.py) | 1. Hash, O(1) for add, O(n) for find, O(n) space<br>2. sorted list, O(logn) for add, O(n) for find, O(n) space<br> 3. Sort before find, O(1) for add, O(nlogn) for find, O(n) space|
7476
| 186 | [Reverse Words in a String II](https://leetcode.com/problems/reverse-words-in-a-string-ii/) &hearts;| [Python](https://github.com/qiyuangong/leetcode/blob/master/python/186_Reverse_Words_in_a_String_II.py) | Reverse all and reverse each words |
7577
| 198 | [House Robber](https://leetcode.com/problems/house-robber/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/198_House_Robber.py) | f(k) = max(f(k – 2) + num[k], f(k – 1)), O(n) and O(1) |
7678
| 200 | [Number of Islands](https://leetcode.com/problems/number-of-islands/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/200_Number_of_Islands.py) | 1. Quick union find, O(nlogn) and O(n^2)<br>2. BFS with marks, O(n^2) and O(1) |
@@ -93,6 +95,7 @@
9395
| 280 | [Wiggle Sort](https://leetcode.com/problems/wiggle-sort/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/280_Wiggle_Sort.py) &hearts; | 1. Sort and insert (n - 1) / 2 from tail to correct position, O(nlogn) and O(1)<br>2. Sort and swap(i, i + 1) from 1 to n - 1, O(nlogn)<br>3. Iteratively check order and reverse order, if not satisfied, then swap i with i + 1, O(n) |
9496
| 286 | [Walls and Gates](https://leetcode.com/problems/walls-and-gates/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/286_Walls_and_Gates.py) | BFS with queue, O(mn) and O(mn) |
9597
| 288 | [Unique Word Abbreviation](https://leetcode.com/problems/unique-word-abbreviation/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/288_Unique_Word_Abbreviation.py) | hash |
98+
| 296 | [Best Meeting Point](https://leetcode.com/problems/best-meeting-point/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/296_Best_Meeting_Point.py) | Think hard about Manhattan Distance in 1D case. Sort and find mean, O(mnlogmn) and O(1) |
9699
| 298 | [Binary Tree Longest Consecutive Sequence](https://leetcode.com/problems/binary-tree-longest-consecutive-sequence/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/298_Binary_Tree_Longest_Consecutive_Sequence.py) | Bottom-up or top-down recursion, O(n) and O(n) |
97100
| 305 | [Number of Islands II](https://leetcode.com/problems/number-of-islands-ii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/305_Number_of_Islands_II.py) | Quick union find with weights, O(nlogn) and O(n) |
98101
| 322 | [Coin Change](https://leetcode.com/problems/coin-change/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/322_Coin_Change.py) | Bottom-up or top-down DP, dp[n] = min(dp[n], dp[n - v_i]), where v_i is the coin, O(amount * n) and O(amount) |
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
class TwoSum(object):
2+
3+
def __init__(self):
4+
"""
5+
initialize your data structure here
6+
"""
7+
self.internal = []
8+
self.dic = {}
9+
10+
def add(self, number):
11+
"""
12+
Add the number to an internal data structure.
13+
:rtype: nothing
14+
"""
15+
self.internal.append(number)
16+
if number in self.dic:
17+
# more than once
18+
self.dic[number] = True
19+
return
20+
# once
21+
self.dic[number] = False
22+
23+
def find(self, value):
24+
"""
25+
Find if there exists any pair of numbers which sum is equal to the value.
26+
:type value: int
27+
:rtype: bool
28+
"""
29+
for v in self.internal:
30+
if value - v in self.dic:
31+
if v << 1 == value and not self.dic[v]:
32+
continue
33+
return True
34+
return False
35+
36+
37+
# Your TwoSum object will be instantiated and called as such:
38+
# twoSum = TwoSum()
39+
# twoSum.add(number)
40+
# twoSum.find(value)

python/296_Best_Meeting_Point.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
class Solution(object):
2+
# def minTotalDistance(self, grid):
3+
# """
4+
# :type grid: List[List[int]]
5+
# :rtype: int
6+
# """
7+
# # sort
8+
# rows = []
9+
# cols = []
10+
# for i in range(len(grid)):
11+
# for j in range(len(grid[0])):
12+
# if grid[i][j] == 1:
13+
# rows.append(i)
14+
# cols.append(j)
15+
# row = rows[len(rows) / 2]
16+
# cols.sort()
17+
# col = cols[len(cols) / 2]
18+
# return self.minDistance1D(rows, row) + self.minDistance1D(cols, col)
19+
20+
# def minDistance1D(self, points, origin):
21+
# distance = 0
22+
# for point in points:
23+
# distance += abs(point - origin)
24+
# return distance
25+
26+
def minDistance1D(self, points):
27+
# two points
28+
distance = 0
29+
i, j = 0, len(points) - 1
30+
while i < j:
31+
distance += points[j] - points[i]
32+
i += 1
33+
j -= 1
34+
return distance
35+
36+
def minTotalDistance(self, grid):
37+
rows = self.collectRows(grid)
38+
cols = self.collectCols(grid)
39+
row = rows[len(rows) / 2]
40+
col = cols[len(cols) / 2]
41+
return self.minDistance1D(rows) + self.minDistance1D(cols)
42+
43+
def collectRows(self, grid):
44+
rows = []
45+
for i in range(len(grid)):
46+
for j in range(len(grid[0])):
47+
if grid[i][j] == 1:
48+
rows.append(i)
49+
return rows
50+
51+
def collectCols(self, grid):
52+
cols = []
53+
for j in range(len(grid[0])):
54+
for i in range(len(grid)):
55+
if grid[i][j] == 1:
56+
cols.append(j)
57+
return cols

0 commit comments

Comments
 (0)