Skip to content

Commit e141c4a

Browse files
committed
Solutions for context 121 and 123
1 parent e1eeff9 commit e141c4a

File tree

10 files changed

+415
-0
lines changed

10 files changed

+415
-0
lines changed

900-1000q/977.py

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
'''
2+
Given an array of integers A sorted in non-decreasing order, return an array of the squares of each number, also in sorted non-decreasing order.
3+
4+
5+
6+
Example 1:
7+
8+
Input: [-4,-1,0,3,10]
9+
Output: [0,1,9,16,100]
10+
Example 2:
11+
12+
Input: [-7,-3,2,3,11]
13+
Output: [4,9,9,49,121]
14+
'''
15+
16+
class Solution(object):
17+
def sortedSquares(self, A):
18+
"""
19+
:type A: List[int]
20+
:rtype: List[int]
21+
"""
22+
N = len(A)
23+
j = 0
24+
while j <N and A[j] < 0:
25+
j += 1
26+
i = j-1
27+
result = []
28+
while i >= 0 and j < N:
29+
if A[i]**2 < A[j]**2:
30+
result.append(A[i]**2)
31+
i -= 1
32+
else:
33+
result.append(A[j]**2)
34+
j += 1
35+
while i>= 0:
36+
result.append(A[i]**2)
37+
i -= 1
38+
39+
while j < N:
40+
result.append(A[j]**2)
41+
j += 1
42+
43+
return result

900-1000q/981.py

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'''
2+
Create a timebased key-value store class TimeMap, that supports two operations.
3+
4+
1. set(string key, string value, int timestamp)
5+
6+
Stores the key and value, along with the given timestamp.
7+
2. get(string key, int timestamp)
8+
9+
Returns a value such that set(key, value, timestamp_prev) was called previously, with timestamp_prev <= timestamp.
10+
If there are multiple such values, it returns the one with the largest timestamp_prev.
11+
If there are no values, it returns the empty string ("").
12+
'''
13+
14+
import bisect
15+
class TimeMap(object):
16+
17+
def __init__(self):
18+
"""
19+
Initialize your data structure here.
20+
"""
21+
self.time_dict = {}
22+
self.key_map = {}
23+
24+
25+
def set(self, key, value, timestamp):
26+
"""
27+
:type key: str
28+
:type value: str
29+
:type timestamp: int
30+
:rtype: None
31+
"""
32+
if key in self.time_dict:
33+
self.time_dict[key].append(timestamp)
34+
self.key_map[key].append(value)
35+
else:
36+
self.time_dict[key] = [timestamp]
37+
self.key_map[key] = [value]
38+
39+
40+
41+
42+
def get(self, key, timestamp):
43+
"""
44+
:type key: str
45+
:type timestamp: int
46+
:rtype: str
47+
"""
48+
if key in self.time_dict:
49+
t_values = self.time_dict[key]
50+
index = bisect.bisect_right(t_values, timestamp)
51+
if index-1 == len(t_values) or index == 0:
52+
return ''
53+
54+
return self.key_map[key][index-1]
55+
56+
57+
58+
# Your TimeMap object will be instantiated and called as such:
59+
# obj = TimeMap()
60+
# obj.set(key,value,timestamp)
61+
# param_2 = obj.get(key,timestamp)

900-1000q/983.py

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'''
2+
In a country popular for train travel, you have planned some train travelling one year in advance. The days of the year that you will travel is given as an array days. Each day is an integer from 1 to 365.
3+
4+
Train tickets are sold in 3 different ways:
5+
6+
a 1-day pass is sold for costs[0] dollars;
7+
a 7-day pass is sold for costs[1] dollars;
8+
a 30-day pass is sold for costs[2] dollars.
9+
The passes allow that many days of consecutive travel. For example, if we get a 7-day pass on day 2, then we can travel for 7 days: day 2, 3, 4, 5, 6, 7, and 8.
10+
11+
Return the minimum number of dollars you need to travel every day in the given list of days.
12+
13+
14+
15+
Example 1:
16+
17+
Input: days = [1,4,6,7,8,20], costs = [2,7,15]
18+
Output: 11
19+
'''
20+
21+
class Solution:
22+
def mincostTickets(self, days: 'List[int]', costs: 'List[int]') -> 'int':
23+
def get_days_ago(day, ago):
24+
for i in range(len(days)):
25+
if days[i] > days[day-1] - ago:
26+
return i
27+
out = [0] * (len(days) + 1)
28+
for i in range(1, len(days) + 1):
29+
out[i] = min(out[i-1] + costs[0], out[get_days_ago(i,7)] + costs[1], out[get_days_ago(i,30)] + costs[2])
30+
return out[-1]

900-1000q/984.py

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
'''
2+
Given two integers A and B, return any string S such that:
3+
4+
S has length A + B and contains exactly A 'a' letters, and exactly B 'b' letters;
5+
The substring 'aaa' does not occur in S;
6+
The substring 'bbb' does not occur in S.
7+
8+
9+
Example 1:
10+
11+
Input: A = 1, B = 2
12+
Output: "abb"
13+
Explanation: "abb", "bab" and "bba" are all correct answers.
14+
'''
15+
16+
class Solution(object):
17+
def strWithout3a3b(self, A, B):
18+
"""
19+
:type A: int
20+
:type B: int
21+
:rtype: str
22+
"""
23+
24+
result = ''
25+
if A > B:
26+
while B > 0 and A > 0:
27+
if A-B >= 3:
28+
if A > 1:
29+
result += 'aab'
30+
A -= 2
31+
else:
32+
result += 'ab'
33+
A -= 1
34+
B -= 1
35+
else:
36+
result += 'ab'
37+
A -= 1
38+
B -= 1
39+
if A > 0:
40+
result += 'a'*A
41+
if B > 0:
42+
result += 'b'*B
43+
else:
44+
while B > 0 and A > 0:
45+
if B-A >= 3:
46+
if B > 1:
47+
result += 'bba'
48+
B -= 2
49+
else:
50+
result += 'ba'
51+
B -= 1
52+
A -= 1
53+
else:
54+
result += 'ba'
55+
A -= 1
56+
B -= 1
57+
if A > 0:
58+
result += 'a'*A
59+
if B > 0:
60+
result += 'b'*B
61+
62+
return result

900-1000q/985.py

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
'''
2+
We have an array A of integers, and an array queries of queries.
3+
4+
For the i-th query val = queries[i][0], index = queries[i][1], we add val to A[index]. Then, the answer to the i-th query is the sum of the even values of A.
5+
6+
(Here, the given index = queries[i][1] is a 0-based index, and each query permanently modifies the array A.)
7+
8+
Return the answer to all queries. Your answer array should have answer[i] as the answer to the i-th query.
9+
10+
11+
12+
Example 1:
13+
14+
Input: A = [1,2,3,4], queries = [[1,0],[-3,1],[-4,0],[2,3]]
15+
Output: [8,6,2,4]
16+
'''
17+
18+
class Solution(object):
19+
def sumEvenAfterQueries(self, A, queries):
20+
"""
21+
:type A: List[int]
22+
:type queries: List[List[int]]
23+
:rtype: List[int]
24+
"""
25+
result = 0
26+
for val in A:
27+
if val%2 == 0:
28+
result += val
29+
30+
f_result = []
31+
for val_index in queries:
32+
val, index = val_index[0], val_index[1]
33+
prev_val = A[index]
34+
if prev_val%2 == 0:
35+
result -= prev_val
36+
new_val = prev_val + val
37+
if new_val %2 == 0:
38+
result += new_val
39+
A[index] = new_val
40+
f_result.append(result)
41+
return f_result
42+

900-1000q/988.py

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'''
2+
Given the root of a binary tree, each node has a value from 0 to 25 representing the letters 'a' to 'z': a value of 0 represents 'a', a value of 1 represents 'b', and so on.
3+
4+
Find the lexicographically smallest string that starts at a leaf of this tree and ends at the root.
5+
'''
6+
# Definition for a binary tree node.
7+
# class TreeNode(object):
8+
# def __init__(self, x):
9+
# self.val = x
10+
# self.left = None
11+
# self.right = None
12+
13+
class Solution(object):
14+
def smallestFromLeaf(self, root):
15+
"""
16+
:type root: TreeNode
17+
:rtype: str
18+
"""
19+
self.result = "~"
20+
21+
def dfs(node, A):
22+
if node:
23+
A.append(chr(node.val + ord('a')))
24+
if not node.left and not node.right:
25+
self.result = min(self.result, "".join(reversed(A)))
26+
27+
dfs(node.left, A)
28+
dfs(node.right, A)
29+
A.pop()
30+
dfs(root, [])
31+
return self.result

900-1000q/989.py

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
'''
2+
For a non-negative integer X, the array-form of X is an array of its digits in left to right order. For example, if X = 1231, then the array form is [1,2,3,1].
3+
4+
Given the array-form A of a non-negative integer X, return the array-form of the integer X+K.
5+
6+
7+
8+
Example 1:
9+
10+
Input: A = [1,2,0,0], K = 34
11+
Output: [1,2,3,4]
12+
Explanation: 1200 + 34 = 1234
13+
'''
14+
15+
class Solution(object):
16+
def addToArrayForm(self, A, K):
17+
"""
18+
:type A: List[int]
19+
:type K: int
20+
:rtype: List[int]
21+
"""
22+
arr_k = []
23+
while K >0:
24+
digit = K%10
25+
K /= 10
26+
arr_k.append(digit)
27+
28+
arr_k.reverse()
29+
if len(arr_k) > len(A):
30+
A, arr_k = arr_k, A
31+
32+
sum_arr = [0]*len(A)
33+
i, j = len(A)-1, len(arr_k)-1
34+
k = len(A) -1
35+
digit_sum, carry = 0, 0
36+
while j >= 0:
37+
curr_sum = A[i] + arr_k[j] + carry
38+
sum_arr[k] = (curr_sum%10)
39+
carry = curr_sum//10
40+
i -= 1
41+
k -= 1
42+
j -= 1
43+
44+
while i >= 0:
45+
curr_sum = A[i] + carry
46+
sum_arr[k] = (curr_sum%10)
47+
carry =curr_sum//10
48+
i -= 1
49+
k -= 1
50+
51+
if carry:
52+
sum_arr = [carry] + sum_arr
53+
return sum_arr

900-1000q/990.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
'''
2+
Given an array equations of strings that represent relationships between variables, each string equations[i] has length 4 and takes one of two different forms: "a==b" or "a!=b". Here, a and b are lowercase letters (not necessarily different) that represent one-letter variable names.
3+
4+
Return true if and only if it is possible to assign integers to variable names so as to satisfy all the given equations.
5+
6+
7+
8+
Example 1:
9+
10+
Input: ["a==b","b!=a"]
11+
Output: false
12+
Explanation: If we assign say, a = 1 and b = 1, then the first equation is satisfied, but not the second. There is no way to assign the variables to satisfy both equations.
13+
'''
14+
15+
class Solution(object):
16+
def equationsPossible(self, equations):
17+
"""
18+
:type equations: List[str]
19+
:rtype: bool
20+
"""
21+
equal_list, unequal_list = [], []
22+
for equation in equations:
23+
x, y = equation[0], equation[3]
24+
if '==' in equation:
25+
if not equal_list:
26+
equal_list.append(x+y)
27+
else:
28+
found = False
29+
for index in range(0, len(equal_list)):
30+
val = equal_list[index]
31+
if x in val or y in val:
32+
val = val+x+y
33+
equal_list[index] = val
34+
found = True
35+
if not found:
36+
equal_list.append(x+y)
37+
else:
38+
if x == y:
39+
return False
40+
unequal_list.append([x, y])
41+
42+
for val in unequal_list:
43+
for equal in equal_list:
44+
if val[0] in equal and val[1] in equal:
45+
return False
46+
return True

0 commit comments

Comments
 (0)