Skip to content

Commit

Permalink
Sliding Window, Greedy Algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
uniqueimaginate committed Mar 6, 2021
1 parent 6416476 commit 0d7ccc4
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Coding Test/122.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class Solution:
def maxProfit(self, prices: List[int]) -> int:
result = 0

for i in range(len(prices) - 1):
if prices[i+1] > prices[i]:
result += prices[i+1] - prices[i]

return result
15 changes: 15 additions & 0 deletions Coding Test/134.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Solution:
def canCompleteCircuit(self, gas: List[int], cost: List[int]) -> int:
if sum(gas) < sum(cost):
return -1


start, fuel = 0, 0
for i in range(len(gas)):
if gas[i] + fuel < cost[i]:
start = i + 1
fuel = 0
else:
fuel += gas[i] - cost[i]

return start
20 changes: 20 additions & 0 deletions Coding Test/406.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import re
import collections
import itertools
import heapq
import bisect

class Solution:
def reconstructQueue(self, people: List[List[int]]) -> List[List[int]]:
result = []
heap = []

for person in people:
heapq.heappush(heap, (-person[0], person[1]))


while heap:
person = heapq.heappop(heap)
result.insert(person[1], (-person[0], person[1]))

return result
17 changes: 17 additions & 0 deletions Coding Test/424.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class Solution:
def characterReplacement(self, s: str, k: int) -> int:
left = right = 0

counts = collections.Counter()

for right in range(1, len(s) + 1):

counts[s[right-1]] += 1

max_char_n = counts.most_common(1)[0][1]

if right - left - max_char_n > k:
counts[s[left]] -= 1
left += 1

return right - left
13 changes: 13 additions & 0 deletions Coding Test/455.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class Solution:
def findContentChildren(self, g: List[int], s: List[int]) -> int:
g.sort()
s.sort()

i = j = 0

while i < len(g) and j < len(s):
if g[i] <= s[j]:
i += 1
j += 1

return i
30 changes: 30 additions & 0 deletions Coding Test/76.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import re
import collections
import itertools
import heapq
import bisect

class Solution:
def minWindow(self, s: str, t: str) -> str:
need = collections.Counter(t)
missing = len(t)
left = start = end = 0

for right, char in enumerate(s, 1):
missing -= need[char] > 0
need[char] -= 1

if missing == 0:

while left < right and need[s[left]] < 0:
need[s[left]] += 1
left += 1

if not end or right - left <= end - start:
start, end = left, right

# need[s[left]] += 1
# missing += 1
# left += 1

return s[start:end]

0 comments on commit 0d7ccc4

Please sign in to comment.