分數排名問題https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem
下面是我一開始的寫法,過不了幾個隱藏測資,都是很大的數字導致runtime超過規定時間
科目以及知道最多科目的組合https://www.hackerrank.com/challenges/acm-icpc-team/problem
絕對值1的最大子序列 https://www.hackerrank.com/challenges/picking-numbers/problem
但目前過不了這個測資
公司考題:
Sherlock and Anagrams
最簡單的暴力解(但是會過不了太長的隱藏測資) 這裡有一個說明的
def sherlockAndAnagrams(s):
cnt=0
length=1
loop=True
while(loop==True):
ls = len(s) - length + 1
for i in range(len(s)):
for j in range(i+1,len(s)):
L = s[i:i+length]
R = s[j:j+length]
if sorted(L)==sorted(R):
print(L + R)
cnt+=1
length+=1
if(ls==1):
loop=False
print(cnt)
sherlockAndAnagrams("kkkk")
def circularArrayRotation(a, k, queries):
if k>len(a):
k = k%len(a)
k = len(a)-k
#k就是第幾個Index要來頭的位子
a = a[k:]+a[:k]
return [a[i] for i in queries]
Inserting a Node Into a Sorted Doubly Linked List
def sortedInsert(head, data):
node = DoublyLinkedListNode(data)
if head == None:
head = node
elif head.data>data:
node.next = head
head.prev = node
head = node
# insert at specific position or end
else:
cur = head
#traverse to specific position
while cur.next != None and cur.data<data:
cur = cur.next
#insert at the end
if cur.next == None and cur.data<data:
cur.next = node
node.prev = cur
#insert at specific position
else:
prev_node = cur.prev
#make changes at previous node
prev_node.next = node
node.prev = prev_node
#make changes for current node
node.next = cur
cur.prev = node
return head
def count_connect(i,j,matrix):
if any([i<0,j<0,i>=len(matrix),j>=len(matrix[0])]):
return 0
if matrix[i][j]==0:
return 0
cell_count = 1
matrix[i][j]=0
for x in range(i-1,i+2):
for y in range(j-1,j+2):
if any([x!=i,y!=j]):
cell_count += count_connect(x,y,matrix)
return cell_count
def connectedCell(matrix):
ans=0
for i in range(n):
for j in range(m):
if matrix[i][j]==1:
region_count = count_connect(i,j,matrix)
ans = max(ans,region_count)
return ans
前一半的Input都要替換成"-“,然後再照著順序加進去
def countSort(arr):
result=[[]for i in range(100)]
#first half "-"
for i in range(n//2):
result[int(arr[i][0])].append("-")
#second half
for i in range(n//2,n):
result[int(arr[i][0])].append(arr[i][1])
ans=""
for i in range(100):
for j in range(len(result[i])):
ans+=str(result[i][j])+" "
print ans
0223考試1
s = "a12b56a1c1"
#輸出要變成 a13b56c1
def isCha(i):
if ord(i)>=97 and ord(i)<=122:
return True
else:
return False
# print(s[0])
dict={}
i=0
while i < len(s):
# print("i"+str(i))
if isCha(s[i]):
cha=s[i]
# print(cha)
if cha not in dict.keys():
dict[cha]=0
num = 0
i+=1
# print("s["+str(i)+"]"+str(s[i]))
# s = "a12c56a1b54"
while not isCha(s[i]) and i<len(s)-1:
num = num*10+ int(s[i])
# print("num:"+str(num))
i+=1
if (isCha(s[i])) or i==len(s)-1:
break
# print("s["+str(i)+"]"+str(s[i]))
if i==len(s)-1:
num = num*10+int(s[i])
if cha in dict.keys():
# print(dict[cha])
dict[cha]=dict[cha]+num
else:
dict[cha]=dict[cha]+num
if i==len(s)-1:
break
ans=""
key_list = sorted(dict.keys())
for i in key_list:
print(i)
ans+= str(i) + str(dict[i])
print(ans)
20210628更簡單的做法
authentication tokens twitter hackerrank
0 1 1
1 1 4
1 2 5
expire = 4
command = [[0, 1, 1],[1, 1, 4],[1, 2, 5]]
def numberOfTokens(expiryLimit, commands):
# Write your code here
values = dict()
time = 0
for c in commands:
# extraction values
action = c[0]
token_id = c[1]
time = c[2]
# set token
if action == 0:
values[token_id] = expiryLimit + time
# reset token
elif action == 1:
# check if token exists
if token_id in values.keys():
expiry_time = values.get(token_id)
if expiry_time >= time:
values[token_id] = values.get(token_id) + expiryLimit - (expiry_time - time)
# counting values alive after reading all the values
count = sum(1 for i in values.values() if i >= time)
print( count )
numberOfTokens(expire ,command)
The Gird search
上圖是G,粗體的就是P,問題就是看在G裡面能不能找到P
def gridSearch(G, P):
linecheck = 0
for i in range(len(G)-len(P)+1):
for j in range(len(G[0])-len(P[0])+1):
if G[i][j:j+len(P[0])] == P[0]:
for x in range(1,len(P)):
if G[i+x][j:j+len(P[0])] == P[x]:
linecheck+=1
if linecheck == len(P)-1:
return "YES"
else:
linecheck = 0
return "NO"