Skip to content

Commit e44871f

Browse files
author
Partho Biswas
committed
leetccode - google phone mock 6
1 parent fdf2e81 commit e44871f

File tree

5 files changed

+105
-12
lines changed

5 files changed

+105
-12
lines changed

README.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ I have solved quite a number of problems from several topics. See the below tabl
172172
|61| [731. My Calendar II](https://tinyurl.com/sde6smv) | [Python](https://tinyurl.com/wu6rdaw/731_My_Calendar_II.py), [Swift](https://tinyurl.com/wuja3c4/731_My_Calendar_II.swift) | | Medium | Merge interval |
173173
|62| [59. Spiral Matrix II](https://tinyurl.com/pqfjvm7) | [Python](https://tinyurl.com/wu6rdaw/59_Spiral_Matrix_II.py), [Swift](https://tinyurl.com/wuja3c4/59_Spiral_Matrix_II.swift) | | Medium | loved it |
174174
|63| **[525. Contiguous Array](https://tinyurl.com/txbs9wh)** | [Python](https://tinyurl.com/wu6rdaw/525_Contiguous_Array.py), [Swift](https://tinyurl.com/wuja3c4/525_Contiguous_Array.swift) | [Art 1](https://tinyurl.com/qmbd2vl) | Medium | loved it. Check again |
175+
|64| **[379. Design Phone Directory](https://tinyurl.com/ybs3848v)** | [Python](https://tinyurl.com/wu6rdaw/379_Design_Phone_Directory.py), [Swift](https://tinyurl.com/wuja3c4/379_Design_Phone_Directory.swift) | | Medium | |
176+
|65| **[280. Wiggle Sort](https://tinyurl.com/ybnjn6fs)** | [Python](https://tinyurl.com/wu6rdaw/280_Wiggle_Sort.py), [Swift](https://tinyurl.com/wuja3c4/280_Wiggle_Sort.swift) | | Medium | |
177+
|66| [246. Strobogrammatic Number](https://tinyurl.com/ycqwsozh) | [Python](https://tinyurl.com/wu6rdaw/246_Strobogrammatic_Number.py), [Swift](https://tinyurl.com/wuja3c4/246_Strobogrammatic_Number.swift) | | Easy | |
175178

176179

177180
</p>
@@ -516,7 +519,7 @@ BFS, DFS, Dijkstra, Floyd–Warshall, Bellman-Ford, Kruskal, Prim's, Minimum Spa
516519
|27| **[489. Robot Room Cleaner](https://tinyurl.com/yxpwjn7r)** | [Python](https://tinyurl.com/wu6rdaw/489_Robot_Room_Cleaner.py), [Swift](https://tinyurl.com/wuja3c4/489_Robot_Room_Cleaner.swift) | **[Vid 1](https://tinyurl.com/vahalvm)**, **[Vid 2](https://tinyurl.com/qpumkro)**, **[Art 1](https://tinyurl.com/rnu8679)** | Hard |📌 **TODO: CHECK again. Very important and interesting problem. I fucking loved it** |
517520
|28| **[1136. Parallel Courses](https://tinyurl.com/yxpwjn7r)** | [Python](https://tinyurl.com/wu6rdaw/11136_Parallel_Courses.py), [Swift](https://tinyurl.com/wuja3c4/11136_Parallel_Courses.swift) | --- | Hard |📌 Topological sort. |
518521
|29| **[947. Most Stones Removed with Same Row or Column](https://tinyurl.com/rrm6w2a)** | [Python](https://tinyurl.com/wu6rdaw/947_Most_Stones_Removed_with_Same_Row_or_Column.py), [Swift](https://tinyurl.com/wuja3c4/947_Most_Stones_Removed_with_Same_Row_or_Column.swift) | [Vid 1](https://tinyurl.com/sc2g6eg), [Art 1](https://tinyurl.com/sxyzxkr), [Vid 2](https://tinyurl.com/wocnrhn), [Art 2](https://tinyurl.com/sogsj74), [Art 3](https://tinyurl.com/w4pk463) | Medium |📌 DFS and Union FInd |
519-
|29| [1042. Flower Planting With No Adjacent](https://tinyurl.com/unwlzyy) | [Python](https://tinyurl.com/wu6rdaw/1042_Flower_Planting_With_No_Adjacent.py), [Swift](https://tinyurl.com/wuja3c4/1042_Flower_Planting_With_No_Adjacent.swift) | --- | Easy |📌 Could be done using DFS, BFS |
522+
|30| [1042. Flower Planting With No Adjacent](https://tinyurl.com/unwlzyy) | [Python](https://tinyurl.com/wu6rdaw/1042_Flower_Planting_With_No_Adjacent.py), [Swift](https://tinyurl.com/wuja3c4/1042_Flower_Planting_With_No_Adjacent.swift) | --- | Easy |📌 Could be done using DFS, BFS |
520523

521524

522525
</p>

algoexpert.io/python/Subarray_Sort.py

+9-11
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,27 @@
1+
import math
12

2-
# O(n) time | O(1) space
33
def subarraySort(array):
4-
minOutOfOrder = float('inf')
5-
maxOutOfOrder = float('-inf')
4+
minOutOfOrder = math.inf
5+
maxOutOfOrder = -math.inf
66
for i in range(len(array)):
77
num = array[i]
88
if isOutOfOrder(i, num, array):
9-
minOutOfOrder = min(num, minOutOfOrder)
10-
maxOutOfOrder = max(num, maxOutOfOrder)
11-
if minOutOfOrder == float('inf'):
9+
minOutOfOrder = min(minOutOfOrder, num)
10+
maxOutOfOrder = max(maxOutOfOrder, num)
11+
if minOutOfOrder == math.inf:
1212
return [-1, -1]
1313
subArrayLeftIdx = 0
14-
subArrayRightIdx = len(array) - 1
15-
while minOutOfOrder >= array[subArrayLeftIdx]:
14+
while array[subArrayLeftIdx] <= minOutOfOrder:
1615
subArrayLeftIdx += 1
16+
subArrayRightIdx = len(array) - 1
1717
while maxOutOfOrder <= array[subArrayRightIdx]:
1818
subArrayRightIdx -= 1
1919
return [subArrayLeftIdx, subArrayRightIdx]
2020

2121

22-
23-
2422
def isOutOfOrder(i, num, array):
2523
if i == 0:
2624
return num > array[i + 1]
2725
if i == len(array) - 1:
2826
return num < array[i - 1]
29-
return num > array[i + 1] or num < array[i - 1]
27+
return num < array[i - 1] or num > array[i + 1]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def isStrobogrammatic(self, num):
3+
"""
4+
:type num: str
5+
:rtype: bool
6+
"""
7+
nsg = ["2", "3", "4", "5", "7"]
8+
sg = {"0": "0", "1": "1", "6": "9", "8": "8", "9": "6"}
9+
newNum = ""
10+
for n in num:
11+
if n in nsg:
12+
return False
13+
else:
14+
newNum += sg[n]
15+
newNum = newNum[::-1]
16+
return num == newNum
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def wiggleSort(self, nums):
3+
"""
4+
:type nums: List[int]
5+
:rtype: None Do not return anything, modify nums in-place instead.
6+
"""
7+
nLen = len(nums) - 1
8+
nums.sort()
9+
fidx = nLen // 2
10+
sidx = nLen // 2 + 1
11+
12+
swapIdx = 0
13+
while swapIdx < nLen - 1:
14+
nums[swapIdx], nums[fidx] = nums[fidx], nums[swapIdx]
15+
swapIdx += 1
16+
fidx += 1
17+
18+
nums[swapIdx], nums[sidx] = nums[sidx], nums[swapIdx]
19+
swapIdx += 1
20+
sidx += 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import heapq
2+
3+
4+
class PhoneDirectory(object):
5+
6+
def __init__(self, maxNumbers):
7+
"""
8+
Initialize your data structure here
9+
@param maxNumbers - The maximum numbers that can be stored in the phone directory.
10+
:type maxNumbers: int
11+
"""
12+
self.phoneDictSts = {}
13+
self.availableNums = []
14+
for i in range(maxNumbers):
15+
self.phoneDictSts[i] = True
16+
heapq.heappush(self.availableNums, i)
17+
18+
def get(self):
19+
"""
20+
Provide a number which is not assigned to anyone.
21+
@return - Return an available number. Return -1 if none is available.
22+
:rtype: int
23+
"""
24+
if len(self.availableNums) > 0:
25+
num = heapq.heappop(self.availableNums)
26+
self.phoneDictSts[num] = False
27+
return num
28+
else:
29+
return -1
30+
31+
def check(self, number):
32+
"""
33+
Check if a number is available or not.
34+
:type number: int
35+
:rtype: bool
36+
"""
37+
if self.phoneDictSts[number] == True:
38+
return True
39+
else:
40+
return False
41+
42+
def release(self, number):
43+
"""
44+
Recycle or release a number.
45+
:type number: int
46+
:rtype: None
47+
"""
48+
if self.phoneDictSts[number] == False:
49+
self.phoneDictSts[number] = True
50+
heapq.heappush(self.availableNums, number)
51+
52+
# Your PhoneDirectory object will be instantiated and called as such:
53+
# obj = PhoneDirectory(maxNumbers)
54+
# param_1 = obj.get()
55+
# param_2 = obj.check(number)
56+
# obj.release(number)

0 commit comments

Comments
 (0)