7 359
Подписчики
-324 часа
-237 дней
-11330 день
Архив постов
JAVA SE8 certification exam ✅✅
56 Java mcqs✅✅
Contact : @MLCODER2
+2
UBS exam done successfully ✅✅
100% clearance for any exam ✅
Contact : @MLCODER2
def getMaximumSum(matrix, k):
global idx
import heapq
from collections import deque
n = len(matrix)
rows = [deque(row) for row in matrix]
heap = []
for i in range(n):
if rows[i]:
heapq.heappush(heap, (-rows[i][0], i, True))
heapq.heappush(heap, (-rows[i][-1], i, False))
ans= 0
for _ in range(k):
while heap:
val, idx, is_left = heapq.heappop(heap)
if rows[idx]:
if is_left and rows[idx][0] == -val:
ans += rows[idx].popleft()
break
elif not is_left and rows[idx][-1] == -val:
ans += rows[idx].pop()
break
if rows[idx]:
heapq.heappush(heap, (-rows[idx][0], idx, True))
heapq.heappush(heap, (-rows[idx][-1], idx, False))
return ans
getMaximumSum✅✅
CISCO✅
class sof(StackOverFlow):
def init(self, decay_time):
super().init(decay_time)
self.questions = {}
self.authors = defaultdict(list)
self.current_time = 0
self.id_counter = 0
def _apply_decay(self, qid):
q = self.questions.get(qid)
if not q or q.deleted:
return
elapsed = self.current_time - q.my_decay_time
decay_count = elapsed // self.decay_time
if decay_count > 0:
q.vote -= decay_count
q.my_decay_time += decay_count * self.decay_time
def addQuestion(self, content, author):
self.current_time += 1
q = Question(content)
q.my_decay_time = self.current_time
q.author = author
self.questions[self.id_counter] = q
self.authors[author].append(self.id_counter)
print(f"Question with id: {self.id_counter} is added")
self.id_counter += 1
return self.id_counter - 1
def deleteQuestion(self, qid):
self.current_time += 1
q = self.questions.get(qid)
if q and not q.deleted:
q.deleted = True
print(f"Question with id: {qid} is deleted")
def upVote(self, qid):
self.current_time += 1
self._apply_decay(qid)
q = self.questions.get(qid)
if q and not q.deleted:
q.vote += 1
q.my_decay_time = self.current_time
print(f"Question with id: {qid} is upvoted")
def downVote(self, qid):
self.current_time += 1
self._apply_decay(qid)
q = self.questions.get(qid)
if q and not q.deleted:
q.vote -= 1
q.my_decay_time = self.current_time
print(f"Question with id: {qid} is downvoted")
def getQuestionById(self, qid):
self.current_time += 1 # ⏱ simulate time
self._apply_decay(qid)
q = self.questions.get(qid)
if q and not q.deleted:
print(f"Content: {q.content}, Vote: {q.vote}")
return Pair(q.content, q.vote)
else:
print("Content: null, Vote: 0")
return Pair("null", 0)
def getTop10QuestionsByAuthor(self, author):
self.current_time += 1
valid_questions = []
for qid in self.authors.get(author, []):
self._apply_decay(qid)
q = self.questions[qid]
if not q.deleted:
valid_questions.append((q.vote, qid, q.content))
valid_questions.sort(key=lambda x: (-x[0], x[1]))
for item in valid_questions[:10]:
print(item[2])
Platform queries ✅✅
All cases passed ✅
from collections import defaultdict
import heapq
class Question:
def init(self, id, content, author, created_time, decay_time):
self.id = id
self.content = content
self.author = author
self.votes = 0
self.deleted = False
self.last_updated = created_time
self.decay_time = decay_time
def decay(self, current_time):
if self.decay_time == 0:
return
elapsed = (current_time - self.last_updated) // self.decay_time
if elapsed > 0:
self.votes -= elapsed
self.last_updated += elapsed * self.decay_time
if self.votes < 0:
self.votes = 0
class StackOverFlow:
def init(self, decay_time):
self.decay_time = decay_time
self.questions = {}
self.author_questions = defaultdict(list)
self.id_counter = 0
self.time = 0
def addQuestion(self, content, author):
q = Question(self.id_counter, content, author, self.time, self.decay_time)
self.questions[self.id_counter] = q
self.author_questions[author].append(self.id_counter)
print(f"Question with id: {self.id_counter} is added")
self.id_counter += 1
def deleteQuestion(self, qid):
if qid in self.questions and not self.questions[qid].deleted:
self.questions[qid].deleted = True
print(f"Question with id: {qid} is deleted")
def upVote(self, qid):
if qid in self.questions:
q = self.questions[qid]
self._apply_decay(q)
if not q.deleted:
q.votes += 1
q.last_updated = self.time
print(f"Question with id: {qid} is upvoted")
def downVote(self, qid):
if qid in self.questions:
q = self.questions[qid]
self._apply_decay(q)
if not q.deleted:
q.votes -= 1
q.last_updated = self.time
print(f"Question with id: {qid} is downvoted")
def getQuestionById(self, qid):
if qid in self.questions:
q = self.questions[qid]
self._apply_decay(q)
if not q.deleted:
print(f"Content: {q.content}, Vote: {q.votes}")
def getTop10QuestionsByAuthor(self, author):
valid = []
for qid in self.author_questions.get(author, []):
q = self.questions[qid]
self._apply_decay(q)
if not q.deleted:
valid.append((q.votes, -qid, q.content))
top10 = heapq.nlargest(10, valid)
for vote, neg_id, content in top10:
print(content)
def _apply_decay(self, q):
q.decay(self.time)
Platfrom queries✅✅
import math
from collections import defaultdict, deque
def is_perfect_square(x):
root = int(math.isqrt(x))
return root * root == x
def solve(n, val, edge):
tree = defaultdict(list)
for u, v in edge:
tree[u].append(v)
tree[v].append(u)
parent = [-1] * n
path = [[] for _ in range(n)]
queue = deque([0])
visited = [False] * n
visited[0] = True
while queue:
node = queue.popleft()
for neighbor in tree[node]:
if not visited[neighbor]:
visited[neighbor] = True
parent[neighbor] = node
path[neighbor] = path[node] + [node]
queue.append(neighbor)
total_sum = 0
for i in range(1, n):
vi = 0
for anc in path[i]:
if is_perfect_square(val[i] * val[anc]):
vi += 1
total_sum += vi
return total_sum
n=int(input())
val=[int(i) for i in input().split()]
edge=[[int(i) for i in input().split()] for _ in range(n-1)]
print(solve(n,val,edge))
Tree Climber✅✅
def solution(crypt):
words = crypt
x = list(set(''.join(words)))
if len(x) > 10:
return 0
first_letters = set(word[0] for word in words)
d= {}
ud = set()
count = [0]
def solve(word):
return int(''.join(str(d[c]) for c in word))
def dfs(index):
if index == len(x):
for word in words:
if d[word[0]] == 0:
return
if solve(words[0]) + solve(words[1]) == solve(words[2]):
count[0] += 1
return
char = x[index]
for digit in range(10):
if digit in ud:
continue
d[char] = digit
ud.add(digit)
dfs(index + 1)
ud.remove(digit)
del d[char]
dfs(0)
return count[0]
//Triology
q1
