Skip to content

Commit e58d73a

Browse files
committed
[main] Added files
1 parent 62c736a commit e58d73a

File tree

14 files changed

+201
-55
lines changed

14 files changed

+201
-55
lines changed
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
Binary file not shown.
Binary file not shown.
0 Bytes
Binary file not shown.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
from __future__ import annotations
2+
from typing import List
3+
4+
class Task:
5+
"""
6+
The Task class, represents an task with an required time
7+
"""
8+
9+
def __init__(self, id: int, required: int):
10+
"""
11+
Constructs a new activity instance
12+
13+
Args:
14+
id (int): The id of the task
15+
required (int): Time required to complete
16+
"""
17+
self.id = id
18+
self.required = required
19+
20+
def to_string(self):
21+
return f'[Id {self.id} | Required {self.required}]'
22+
23+
def to_string_total(self, total):
24+
print(f'{self.to_string()} total {total}')
25+
26+
def calculate_total_cost(tasks: List[Task]) -> float: ## couldn't we just sort the array of tasks?
27+
## sort tasks by
28+
sorted_tasks = sorted(tasks, key=lambda x: x.required)
29+
running_total = 0
30+
for eachtask in sorted_tasks:
31+
running_total = running_total + (eachtask.required + running_total)
32+
print(running_total)
33+
return running_total / len(tasks)
34+
## choose task, find most compatible, choosing the task involves finding it's least counterpart,
35+
## aka if a + (b + a) < b + (a + b), or if a = 3 and b = 5, we want 3 + 8 vs 5 + 8
36+
37+
38+
39+
if __name__ == "__main__":
40+
activity_values = [5, 3, 2, 1, 10]
41+
activity_array = [Task(taskId, taskCost) for taskId, taskCost in enumerate(activity_values)]
42+
print(calculate_total_cost(activity_array))

pythonProblems/dp/longestvalid.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,44 @@
11
class Solution:
22
def longestValidParentheses(self, s: str) -> int:
3-
dp = [0] * len(s)
4-
for i in range(len(s)):
5-
if s[i] == ')':
6-
if i - 1 < 0:
7-
continue
8-
elif s[i - 1] == '(':
9-
dp[i] = dp[i - 2] + 2
10-
elif i - dp[i - 1] - 1 < 0:
11-
continue
12-
elif s[i - dp[i - 1] - 1] == '(':
13-
## matching over the current pair
14-
dp[i] = dp[i - 1] + 2 + dp[i - dp[i - 1] - 2]
15-
return max(dp)
16-
3+
sums = []
4+
stack = []
5+
count = 0
6+
for ind, eachparen in enumerate(s):
7+
if eachparen == '(':
8+
stack.append('(')
9+
count += 1
10+
if ind < len(s) - 1 and len(stack) > 1 and len(s[ind + 1:]) < len(stack):
11+
print('in if', ind)
12+
sums.append(0)
13+
sums.append(count - len(stack))
14+
sums.append(0)
15+
stack = []
16+
count = 0
17+
elif eachparen == ')':
18+
if len(stack) == 1:
19+
stack.pop()
20+
sums.append(count + 1)
21+
count = 0
22+
elif len(stack) > 0:
23+
stack.pop()
24+
count += 1
25+
else:
26+
sums.append(0)
27+
if len(stack) > 0:
28+
sums.append(0)
29+
sums.append(count - len(stack))
30+
print(sums)
31+
running_total = 0
32+
running_sums = []
33+
for eachnum in sums:
34+
if eachnum == 0:
35+
running_sums.append(running_total)
36+
running_total = 0
37+
else:
38+
running_total += eachnum
39+
running_sums.append(running_total)
40+
return max(running_sums) if len(running_sums) > 0 else 0
1741

1842
if __name__ == '__main__':
1943
sol = Solution()
20-
print(sol.longestValidParentheses("(()))())("
21-
))
44+
print(sol.longestValidParentheses("(()")) # 4
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class NList:
2+
3+
def __init__(self):
4+
occupied = 0
5+
elements = []
6+
7+
def add_elem(self, elem: int):
8+
elements = list(bin(elem)[2:])
9+
10+
11+
class

pythonProblems/dp/rotArrSearch.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from typing import List
2+
3+
4+
class Solution:
5+
6+
def search(self, nums: List[int], target: int) -> int:
7+
l = 0
8+
r = len(nums) - 1
9+
while l != r:
10+
if nums[l] == target:
11+
return l
12+
elif nums[r] == target:
13+
return r
14+
return -1
15+
16+
17+
if __name__ == '__main__':
18+
sol = Solution()
19+
sol.search([4, 5, 6, 7, 0, 1, 2], 0)

0 commit comments

Comments
 (0)