Skip to content

Commit 4932eb5

Browse files
committed
If I'm not back in five minutes, just wait longer.
1 parent 8eb13fd commit 4932eb5

File tree

4 files changed

+68
-0
lines changed

4 files changed

+68
-0
lines changed
Binary file not shown.
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
from typing import List
2+
3+
4+
def arrayManipulation(n: int, queries: List[List[int]]):
5+
"""
6+
:param n: the number of elements in the array
7+
:param queries: a two dimensional array of queries where each queries[i] contains three integers, a, b, and k.
8+
:return: the maximum value in the resultant array
9+
"""
10+
arr = [0] * n
11+
12+
for query in queries:
13+
a, b, k = query
14+
# adds "k" to all subsequent elements in the array
15+
arr[a - 1] += k
16+
17+
# ignore if b is out of range
18+
if b < n:
19+
# subtracts "k" from all subsequent elements in the array
20+
arr[b] -= k
21+
22+
s = 0
23+
max_sum = 0
24+
25+
for item in arr:
26+
s += item
27+
28+
max_sum = max(s, max_sum)
29+
30+
return max_sum
31+
32+
33+
if __name__ == '__main__':
34+
nm = input().split()
35+
36+
_n = int(nm[0])
37+
38+
m = int(nm[1])
39+
40+
q = []
41+
42+
for _ in range(m):
43+
q.append(list(map(int, input().rstrip().split())))
44+
45+
print(arrayManipulation(_n, q))
46+
Binary file not shown.
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
from typing import List
2+
3+
4+
def minimumSwaps(arr: List[int]):
5+
"""
6+
:param arr: an unordered array of integers
7+
:return: the minimum number of swaps to sort the array
8+
"""
9+
swaps = 0
10+
11+
for i in range(len(arr) - 1):
12+
while arr[i] != i + 1:
13+
arr[arr[i] - 1], arr[i] = arr[i], arr[arr[i] - 1]
14+
swaps += 1
15+
16+
return swaps
17+
18+
19+
if __name__ == '__main__':
20+
n = int(input())
21+
22+
print(minimumSwaps(list(map(int, input().rstrip().split()))))

0 commit comments

Comments
 (0)