Skip to content

Commit dc1e25d

Browse files
committed
Mathematics, Data Structures
1 parent afa6fa7 commit dc1e25d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1539
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
# queue = []
2+
#
3+
# for i in range(int(input())):
4+
# query = list(map(int, input().split()))
5+
# if query[0] == 1:
6+
# queue.append(query[1])
7+
# elif query[0] == 2:
8+
# if len(queue) != 0:
9+
# queue.pop(0)
10+
# else:
11+
# print(queue[0])
12+
13+
# Using two stacks:
14+
stack_in, stack_out = [], []
15+
16+
for i in range(int(input())):
17+
curr_inst = list(map(int, input().split(" ")))
18+
19+
if curr_inst[0] == 1:
20+
stack_in.append(curr_inst[1])
21+
22+
elif curr_inst[0] == 2:
23+
# If stack_out is not empty then pop from it
24+
if stack_out:
25+
stack_out.pop()
26+
continue
27+
else:
28+
# Move everything to the out stack:
29+
while stack_in:
30+
stack_out.append(stack_in.pop())
31+
stack_out.pop()
32+
continue
33+
34+
else:
35+
if not stack_out:
36+
while stack_in:
37+
stack_out.append(stack_in.pop())
38+
print(stack_out[-1])
+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
def isBalanced(s):
2+
bracket_dict = {')': '(', ']': '[', '}': '{'}
3+
stack = []
4+
for x in s:
5+
if x in bracket_dict and stack and bracket_dict.get(x) == stack[-1]:
6+
# bracket_dict.get(x) similar to bracket_dict[x] -> both return the value for key
7+
stack.pop()
8+
else:
9+
stack.append(x)
10+
11+
return "NO" if stack else "YES"
12+
13+
14+
# def isBalanced(s):
15+
# n = -1
16+
# while len(s) != n:
17+
# n = len(s)
18+
# s = s.replace("()", "")
19+
# s = s.replace("[]", "")
20+
# s = s.replace("{}", "")
21+
#
22+
# if len(s) == 0:
23+
# return "YES"
24+
# else:
25+
# return "NO"
26+
27+
28+
if __name__ == '__main__':
29+
t = int(input())
30+
31+
for t_itr in range(t):
32+
s = input()
33+
34+
result = isBalanced(s)
35+
print(result)

Data Structures/Stacks/EqualStacks.py

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# def equalStacks(h1, h2, h3):
2+
# sum1, sum2, sum3 = map(sum, (h1, h2, h3))
3+
# while h1 and h2 and h3:
4+
# minimum = min(sum1, sum2, sum3)
5+
# while sum1 > minimum:
6+
# sum1 -= h1.pop(0)
7+
# while sum2 > minimum:
8+
# sum2 -= h2.pop(0)
9+
# while sum3 > minimum:
10+
# sum3 -= h3.pop(0)
11+
# if sum1 == sum2 == sum3:
12+
# return sum1
13+
# return 0
14+
15+
16+
def equalStacks(h1, h2, h3):
17+
h1 = h1[::-1]
18+
h2 = h2[::-1]
19+
h3 = h3[::-1]
20+
21+
sum_h1 = sum(h1)
22+
sum_h2 = sum(h2)
23+
sum_h3 = sum(h3)
24+
25+
while not sum_h1 == sum_h2 == sum_h3:
26+
if sum_h1 > sum_h2 or sum_h1 > sum_h3:
27+
t = h1.pop()
28+
sum_h1 -= t
29+
if sum_h2 > sum_h1 or sum_h2 > sum_h3:
30+
t = h2.pop()
31+
sum_h2 -= t
32+
if sum_h3 > sum_h1 or sum_h3 > sum_h2:
33+
t = h3.pop()
34+
sum_h3 -= t
35+
36+
return sum_h1
37+
38+
39+
if __name__ == '__main__':
40+
first_multiple_input = input().rstrip().split()
41+
42+
n1 = int(first_multiple_input[0])
43+
n2 = int(first_multiple_input[1])
44+
n3 = int(first_multiple_input[2])
45+
46+
h1 = list(map(int, input().rstrip().split()))
47+
h2 = list(map(int, input().rstrip().split()))
48+
h3 = list(map(int, input().rstrip().split()))
49+
50+
result = equalStacks(h1, h2, h3)
51+
print(result)
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
def twoStacks(x, a, b):
2+
a.reverse()
3+
b.reverse()
4+
stack = []
5+
total, score = 0, 0
6+
7+
while a:
8+
item = a.pop()
9+
if (total + item) <= x:
10+
total += item
11+
score += 1
12+
stack.append(item)
13+
else:
14+
break
15+
16+
max_score = score
17+
18+
while b:
19+
# print("b ", b)
20+
item = b.pop()
21+
# print("item ", item)
22+
total += item
23+
score += 1
24+
while total > x and stack:
25+
total -= stack.pop()
26+
# print("total ", total)
27+
score -= 1
28+
# print("score ", score)
29+
if total <= x and score > max_score:
30+
max_score = score
31+
# print("max ", max_score)
32+
33+
return max_score
34+
35+
36+
# def twoStacks(x, a, b):
37+
# total, count, i, j = 0, 0, 0, 0
38+
#
39+
# while i < len(a) and total + a[i] <= x: # considering only first stack and calculating count
40+
# total += a[i]
41+
# i += 1
42+
#
43+
# count = i
44+
#
45+
# while j < m and i >= 0: # now adding one element of second stack at a time and subtracting the top element of
46+
# # first stack and calculating the count
47+
# total += b[j]
48+
# j += 1
49+
# while total > x and i > 0:
50+
# i -= 1
51+
# total -= a[i]
52+
# if total <= x and i + j > count:
53+
# count = i + j
54+
#
55+
# return count
56+
57+
58+
if __name__ == '__main__':
59+
g = int(input())
60+
61+
for g_itr in range(g):
62+
nmx = input().split()
63+
64+
n = int(nmx[0])
65+
m = int(nmx[1])
66+
x = int(nmx[2])
67+
68+
a = list(map(int, input().rstrip().split()))
69+
b = list(map(int, input().rstrip().split()))
70+
71+
result = twoStacks(x, a, b)
72+
print(result)
+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Using two lists:
2+
items = []
3+
maximum = []
4+
5+
for i in range(int(input())):
6+
nums = list(map(int, input().split()))
7+
if nums[0] == 1:
8+
if len(items) == 0:
9+
items.append(nums[1])
10+
maximum.append(nums[1])
11+
else:
12+
if nums[1] > maximum[-1]:
13+
maximum.append(nums[1])
14+
else:
15+
maximum.append(maximum[-1])
16+
items.append(nums[1])
17+
elif nums[0] == 2:
18+
if len(items) != 0:
19+
items.pop()
20+
maximum.pop()
21+
else:
22+
print(maximum[-1])
23+
24+
# Using single list:
25+
# items = [0]
26+
#
27+
# for i in range(int(input())):
28+
# nums = list(map(int, input().split()))
29+
# if nums[0] == 1:
30+
# items.append(max(nums[1], items[-1]))
31+
# elif nums[0] == 2:
32+
# items.pop()
33+
# else:
34+
# print(items[-1])
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
test = int(input())
2+
3+
my_string = ""
4+
stack = []
5+
6+
for _ in range(test):
7+
operation = input().strip().split(" ")
8+
9+
if operation[0] == "1":
10+
stack.append(my_string)
11+
my_string = my_string + operation[1]
12+
elif operation[0] == "2":
13+
stack.append(my_string)
14+
del_pos = int(operation[1])
15+
my_pos = len(my_string) - del_pos
16+
my_string = my_string[0:my_pos]
17+
elif operation[0] == "3":
18+
print_pos = int(operation[1]) - 1
19+
print(my_string[print_pos])
20+
else:
21+
my_string = stack.pop()
22+
# print(stack)
23+
# print(my_string)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
class Node:
2+
def __init__(self, info):
3+
self.info = info
4+
self.left = None
5+
self.right = None
6+
self.level = None
7+
8+
def __str__(self):
9+
return str(self.info)
10+
11+
12+
class BinarySearchTree:
13+
def __init__(self):
14+
self.root = None
15+
16+
def create(self, val):
17+
if self.root is None:
18+
self.root = Node(val)
19+
else:
20+
current = self.root
21+
22+
while True:
23+
if val < current.info:
24+
if current.left:
25+
current = current.left
26+
else:
27+
current.left = Node(val)
28+
break
29+
elif val > current.info:
30+
if current.right:
31+
current = current.right
32+
else:
33+
current.right = Node(val)
34+
break
35+
else:
36+
break
37+
38+
39+
# Enter your code here. Read input from STDIN. Print output to STDOUT
40+
'''
41+
class Node:
42+
def __init__(self,info):
43+
self.info = info
44+
self.left = None
45+
self.right = None
46+
47+
48+
// this is a node of the tree , which contains info as data, left , right
49+
'''
50+
51+
52+
def height(root):
53+
left_height = 0
54+
right_height = 0
55+
if root.left:
56+
left_height = 1 + height(root.left)
57+
58+
if root.right:
59+
right_height = 1 + height(root.right)
60+
61+
if root is None:
62+
return -1
63+
return max(left_height, right_height)
64+
65+
66+
# def height(root):
67+
# if root is None:
68+
# return -1
69+
# return max(1 + height(root.left), 1 + height(root.right))
70+
71+
72+
# From Editorial:
73+
# def height(root):
74+
# if root is None:
75+
# return -1
76+
# else:
77+
# return 1 + max(height(root.left), height(root.right))
78+
79+
80+
tree = BinarySearchTree()
81+
t = int(input())
82+
83+
arr = list(map(int, input().split()))
84+
85+
for i in range(t):
86+
tree.create(arr[i])
87+
88+
print(height(tree.root))

0 commit comments

Comments
 (0)