Skip to content

Commit 3f07a44

Browse files
committed
243_Shortest_Word_Distance
252_Meeting_Rooms
1 parent 8fcdd2b commit 3f07a44

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,6 @@
7171
| 220 | [Contains Duplicate III](https://leetcode.com/problems/contains-duplicate-iii/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/220_Contains_Duplicate_III.py) | 1. Sort and binary Search <br>2. Bucket sort |
7272
| 221 | [Maximal Square](https://leetcode.com/problems/maximal-square/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/221_Maximal_Square.py) | 1. Brute force<br> 2. dp[i][j] = min(dp[i-1][j], dp[i-1][j-1], dp[i][j-1]) + 1, O(mn) and O(mn)<br>3. dp[j] = min([j], dp[j-1], prev) + 1, O(mn) and O(n)|
7373
| 228 | [Summary Ranges](https://leetcode.com/problems/summary-ranges/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/228_Summary_Ranges.py) | Detect start and jump, O(n) and O(1) |
74+
| 243 | [Shortest Word Distance](https://leetcode.com/problems/shortest-word-distance/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/243_Shortest_Word_Distance.py) | Update index1 and index2, and check distance, O(n) and O(1) |
75+
| 252 | [Meeting Rooms](https://leetcode.com/problems/meeting-rooms/) | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/252_Meeting_Rooms.py) | 1. Sort and compare intervals[i].end with intervals[i+1], O(nlogn) and O(1)<br>2. Sort and check intervals when count >= 2, O(nlogn) and O(n) |
7476
| 340 | [Longest Substring with At Most K Distinct Characters](https://leetcode.com/problems/longest-substring-with-at-most-k-distinct-characters/) &hearts; | [Python](https://github.com/qiyuangong/leetcode/blob/master/python/340_Longest_Substring_with_At_Most_K_Distinct_Characters.py) | Maintain a sliding window with at most k distinct characters and a count for this window. Note that the start position need a loop to update.|

python/243_Shortest_Word_Distance.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
class Solution(object):
2+
# def shortestDistance(self, words, word1, word2):
3+
# """
4+
# :type words: List[str]
5+
# :type word1: str
6+
# :type word2: str
7+
# :rtype: int
8+
# """
9+
# indexes = []
10+
# for index, word in enumerate(words):
11+
# if word1 == word:
12+
# indexes.append((index, 1))
13+
# elif word2 == word:
14+
# indexes.append((index, 2))
15+
# ls, min_range = len(indexes), len(words)
16+
# for i in range(ls - 1):
17+
# if indexes[i][1] == indexes[i + 1][1]:
18+
# continue
19+
# curr_range = abs(indexes[i][0] - indexes[i + 1][0])
20+
# if curr_range < min_range:
21+
# min_range = curr_range
22+
# return min_range
23+
24+
def shortestDistance(self, words, word1, word2):
25+
index1 = index2 = -1
26+
res = len(words)
27+
for index, word in enumerate(words):
28+
if word1 == word:
29+
index1 = index
30+
elif word2 == word:
31+
index2 = index
32+
if index1 != -1 and index2 != -1:
33+
res = min(res, abs(index1 - index2))
34+
return res

python/252_Meeting_Rooms.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Definition for an interval.
2+
# class Interval(object):
3+
# def __init__(self, s=0, e=0):
4+
# self.start = s
5+
# self.end = e
6+
7+
class Solution(object):
8+
# def canAttendMeetings(self, intervals):
9+
# """
10+
# :type intervals: List[Interval]
11+
# :rtype: bool
12+
# """
13+
# # if start then count += 1
14+
# # if end then count -= 1
15+
# # if count >= 2, then False
16+
# check = []
17+
# for it in intervals:
18+
# check.append((it.start, True))
19+
# check.append((it.end - 1, False))
20+
# check.sort(key=lambda x : x[0])
21+
# count = 0
22+
# for t in check:
23+
# if t[1]:
24+
# count += 1
25+
# if count > 1:
26+
# return False
27+
# else:
28+
# count -= 1
29+
# return True
30+
31+
def canAttendMeetings(self, intervals):
32+
intervals.sort(key=lambda x: x.start)
33+
ls = len(intervals)
34+
for i in range(ls - 1):
35+
if intervals[i].end > intervals[i + 1].start:
36+
return False
37+
return True

0 commit comments

Comments
 (0)