Skip to content

Commit 3c07146

Browse files
author
Partho Biswas
committed
leetcode 382
1 parent ffa2085 commit 3c07146

File tree

2 files changed

+131
-0
lines changed

2 files changed

+131
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
from collections import defaultdict
2+
from bisect import bisect_left
3+
4+
# Using Binary search
5+
class Solution(object):
6+
def createMap(self, s):
7+
# create a map. key is char. value is index of apperance in acending order.
8+
posMap = defaultdict(list)
9+
for i, char in enumerate(s):
10+
posMap[char].append(i)
11+
return posMap
12+
13+
def isSubsequence(self, s, t):
14+
"""
15+
:type s: str
16+
:type t: str
17+
:rtype: bool
18+
"""
19+
posMap = self.createMap(t)
20+
# lowBound is the minimum index the current char has to be at.
21+
lowBound = 0
22+
for char in s:
23+
if char not in posMap: return False
24+
charIndexList = posMap[char]
25+
# try to find an index that is larger than or equal to lowBound
26+
i = bisect_left(charIndexList, lowBound)
27+
if i == len(charIndexList): return False
28+
lowBound = charIndexList[i] + 1
29+
return True
30+
31+
32+
# Using Two Pointer
33+
# class Solution(object):
34+
# def isSubsequence(self, s, t):
35+
# """
36+
# :type s: str
37+
# :type t: str
38+
# :rtype: bool
39+
# """
40+
# if len(s) == 0:
41+
# return True
42+
# if len(t) == 0:
43+
# return False
44+
# i, j = 0, 0
45+
# while i < len(s) and j < len(t):
46+
# if s[i] == t[j]:
47+
# i += 1
48+
# j += 1
49+
# return True if i == len(s) else False
50+
51+
52+
53+
54+
sol = Solution()
55+
s = "abc"
56+
t = "ahbgdca"
57+
out = sol.isSubsequence(s, t)
58+
print('Res: ', out)
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from heapq import heappush, heappop
2+
from collections import Counter
3+
4+
5+
# Task Scheduler - https://leetcode.com/problems/task-scheduler/
6+
7+
class Solution:
8+
def inverse(self, num):
9+
return -1 * num
10+
11+
def leastInterval(self, tasks, n):
12+
"""
13+
:type tasks: List[str]
14+
:type n: int
15+
:rtype: int
16+
"""
17+
# What are the inputs here
18+
# - tasks - a list of strings the represent information we're processing
19+
# - n - the time between tasks of the exact same time. This would leave us room to process other tasks
20+
# we asking for here?
21+
# - We're looking for an int as an output. That's because we're returning a count
22+
# - We're looking for the minimum number of intervals that we're going to use for the problem
23+
24+
# How I think we're going to solve the problem
25+
# 1. We process the most important tasks often
26+
# - That's because we want to process everything quickly, and processing it quickly means processing the most frequent tasks immediately after the cooldown time is over is necessary.
27+
# 2. Determine my interval
28+
# - I want to be able to determine what step I'm at
29+
# 3. Create a dict of counts since I last processed my task
30+
31+
# Task map to store if we've seen the item before
32+
task_count = Counter(tasks)
33+
current_time = 0
34+
current_heap = []
35+
36+
# Sorting from least to greatest inside of the heap current_heap
37+
for k, v in task_count.items():
38+
heappush(current_heap,
39+
(self.inverse(v), k)) # Pushes item from least to greatest (hence the negative values)
40+
41+
# Here we're running through the entire heap and processing the sorted tasks
42+
while current_heap: # We're running until this list runs out because we intend to pop elements from it
43+
index, temp = 0, []
44+
while index <= n:
45+
current_time += 1 # We're counting the interval time here
46+
if current_heap:
47+
timing, taskid = heappop(current_heap)
48+
# We're checking to see if it's at the end of the overall count.
49+
# Remember that it was negative at the beginning
50+
if timing != -1:
51+
temp.append((timing + 1, taskid))
52+
# Checking to see if we're out of tasks. Exit the inner loop if both are true.
53+
# This will automatically exit out of the overall tasks
54+
if not current_heap and not temp:
55+
break
56+
else:
57+
index += 1
58+
# Because we transfered all of the items from the heap to temp, we're transferring them back to know if we should continue
59+
# heap -> If we're not out of tasks -> temp
60+
# temp -> Because we're not out -> heap
61+
for item in temp:
62+
heappush(current_heap, item)
63+
# We only stop if we're out of tasks
64+
# (Constantly checking the current_heap for if it's empty)
65+
return current_time
66+
67+
68+
69+
sol = Solution()
70+
tasks = ["A","A","A","B","B","B"]
71+
n = 2
72+
output = sol.leastInterval(tasks, n)
73+
print('Res: ', output)

0 commit comments

Comments
 (0)