7 682
Suscriptores
-324 horas
-287 días
+3030 días
Archivo de publicaciones
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
from math import floor
from collections import defaultdict
def solution(balances, requests):
n = len(balances)
cashback_schedule = defaultdict(list)
last_timestamp = 0
for i, request in enumerate(requests):
parts = request.split()
action = parts[0]
timestamp = int(parts[1])
holder_id = int(parts[2]) - 1
if timestamp > last_timestamp:
for ts in range(last_timestamp + 1, timestamp + 1):
for acc_id, cashback in cashback_schedule.pop(ts, []):
if 0 <= acc_id < n:
balances[acc_id] += cashback
last_timestamp = timestamp
if holder_id < 0 or holder_id >= n:
return [-i - 1]
if action == "deposit":
amount = int(parts[3])
balances[holder_id] += amount
elif action == "withdraw":
amount = int(parts[3])
if balances[holder_id] < amount:
return [-i - 1]
balances[holder_id] -= amount
cashback = floor(amount * 0.02)
cashback_time = timestamp + 86400
cashback_schedule[cashback_time].append((holder_id, cashback))
return balances
//Triology✅✅
q2
def findOptimalPair(n, m, blockedPositions):
from collections import deque
grid = [[0] * m for _ in range(n)]
b = set()
for r, c in blockedPositions:
b.add((r - 1, c - 1))
grid[r - 1][c - 1] = -1
d = [[float('inf')] * m for _ in range(n)]
queue = deque()
for r, c in b:
queue.append((r, c))
d[r][c] = 0
directions = [(1, 0), (-1, 0), (0, 1), (0, -1)]
while queue:
r, c = queue.popleft()
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < n and 0 <= nc < m and (nr, nc) not in b:
if d[nr][nc] > d[r][c] + 1:
d[nr][nc] = d[r][c] + 1
queue.append((nr, nc))
def solve(min_strength):
visited = [[False] * m for _ in range(n)]
if (0, 0) in b or d[0][0] < min_strength:
return -1
q = deque()
q.append((0, 0, 1))
visited[0][0] = True
while q:
r, c, steps = q.popleft()
if (r, c) == (n - 1, m - 1):
return steps
for dr, dc in directions:
nr, nc = r + dr, c + dc
if 0 <= nr < n and 0 <= nc < m and not visited[nr][nc] and (nr, nc) not in b:
if d[nr][nc] >= min_strength:
visited[nr][nc] = True
q.append((nr, nc, steps + 1))
return -1
low, high = 0, n + m
bs, ms = -1, -1
while low <= high:
mid = (low + high) // 2
ans = solve(mid)
if ans != -1:
bs = mid
ms = ans
low = mid + 1
else:
high = mid - 1
if bs == -1:
return [-1, -1]
return [bs, ms]
#CISCO
findOptimalPair✅
¡Ya disponible! Investigación de Telegram 2025 — los principales insights del año 
