Skip to content

Commit 6a97adc

Browse files
committed
Weekly Contest solutions
1 parent ccaf782 commit 6a97adc

File tree

5 files changed

+186
-0
lines changed

5 files changed

+186
-0
lines changed

Diff for: 1000-1100q/1051.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'''
2+
Students are asked to stand in non-decreasing order of heights for an annual photo.
3+
4+
Return the minimum number of students not standing in the right positions. (This is the number of students that must move in order for all students to be standing in non-decreasing order of height.)
5+
6+
7+
8+
Example 1:
9+
10+
Input: [1,1,4,2,1,3]
11+
Output: 3
12+
Explanation:
13+
Students with heights 4, 3 and the last 1 are not standing in the right positions.
14+
15+
16+
Note:
17+
18+
1 <= heights.length <= 100
19+
1 <= heights[i] <= 100
20+
'''
21+
22+
class Solution(object):
23+
def heightChecker(self, heights):
24+
"""
25+
:type heights: List[int]
26+
:rtype: int
27+
"""
28+
result = 0
29+
for new_h, hei in zip(heights, sorted(heights)):
30+
if new_h != hei:
31+
result += 1
32+
return result

Diff for: 1000-1100q/1052.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'''
2+
Today, the bookstore owner has a store open for customers.length minutes. Every minute, some number of customers (customers[i]) enter the store, and all those customers leave after the end of that minute.
3+
4+
On some minutes, the bookstore owner is grumpy. If the bookstore owner is grumpy on the i-th minute, grumpy[i] = 1, otherwise grumpy[i] = 0. When the bookstore owner is grumpy, the customers of that minute are not satisfied, otherwise they are satisfied.
5+
6+
The bookstore owner knows a secret technique to keep themselves not grumpy for X minutes straight, but can only use it once.
7+
8+
Return the maximum number of customers that can be satisfied throughout the day.
9+
10+
11+
12+
Example 1:
13+
14+
Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
15+
Output: 16
16+
Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes.
17+
The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
18+
19+
20+
Note:
21+
22+
1 <= X <= customers.length == grumpy.length <= 20000
23+
0 <= customers[i] <= 1000
24+
0 <= grumpy[i] <= 1
25+
'''
26+
class Solution(object):
27+
def maxSatisfied(self, customers, grumpy, X):
28+
"""
29+
:type customers: List[int]
30+
:type grumpy: List[int]
31+
:type X: int
32+
:rtype: int
33+
"""
34+
result = 0
35+
36+
prefix_sum = [0]*(len(customers)+1)
37+
index = 0
38+
for customer, grump in zip(customers, grumpy):
39+
prefix_sum[index+1] = prefix_sum[index]
40+
if grump == 0:
41+
result += customer
42+
else:
43+
prefix_sum[index+1] += customer
44+
index += 1
45+
# print prefix_sum
46+
curr_max = result + prefix_sum[X]
47+
# print curr_max
48+
for index in range(X+1, len(prefix_sum)):
49+
temp_max = result + prefix_sum[index] - prefix_sum[index-X]
50+
# print temp_max
51+
curr_max = max(curr_max, temp_max)
52+
return curr_max

Diff for: 1000-1100q/1053.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
Given an array A of positive integers (not necessarily distinct), return the lexicographically largest permutation that is smaller than A, that can be made with one swap (A swap exchanges the positions of two numbers A[i] and A[j]). If it cannot be done, then return the same array.
3+
4+
5+
6+
Example 1:
7+
8+
Input: [3,2,1]
9+
Output: [3,1,2]
10+
Explanation: Swapping 2 and 1.
11+
Example 2:
12+
13+
Input: [1,1,5]
14+
Output: [1,1,5]
15+
Explanation: This is already the smallest permutation.
16+
Example 3:
17+
18+
Input: [1,9,4,6,7]
19+
Output: [1,7,4,6,9]
20+
Explanation: Swapping 9 and 7.
21+
Example 4:
22+
23+
Input: [3,1,1,3]
24+
Output: [1,3,1,3]
25+
Explanation: Swapping 1 and 3.
26+
27+
28+
Note:
29+
30+
1 <= A.length <= 10000
31+
1 <= A[i] <= 10000
32+
'''
33+
class Solution(object):
34+
def prevPermOpt1(self, A):
35+
"""
36+
:type A: List[int]
37+
:rtype: List[int]
38+
"""
39+
40+
left, right = len(A)-2, len(A)-1
41+
for left in range(len(A)-2, -1, -1):
42+
if A[left] > A[left+1]:
43+
break
44+
else:
45+
return A
46+
right = A.index(max(ele for ele in A[left+1:] if ele < A[left]), left)
47+
A[left], A[right] = A[right], A[left]
48+
return A
49+

Diff for: 1000-1100q/1054.py

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
In a warehouse, there is a row of barcodes, where the i-th barcode is barcodes[i].
3+
4+
Rearrange the barcodes so that no two adjacent barcodes are equal. You may return any answer, and it is guaranteed an answer exists.
5+
6+
7+
8+
Example 1:
9+
10+
Input: [1,1,1,2,2,2]
11+
Output: [2,1,2,1,2,1]
12+
Example 2:
13+
14+
Input: [1,1,1,1,2,2,3,3]
15+
Output: [1,3,1,3,2,1,2,1]
16+
17+
18+
Note:
19+
20+
1 <= barcodes.length <= 10000
21+
1 <= barcodes[i] <= 10000
22+
'''
23+
24+
class Solution(object):
25+
def rearrangeBarcodes(self, barcodes):
26+
"""
27+
:type barcodes: List[int]
28+
:rtype: List[int]
29+
"""
30+
import heapq
31+
di = collections.Counter(barcodes)
32+
pq = [(-value, key) for key, value in di.items()]
33+
heapq.heapify(pq)
34+
# print pq
35+
result = []
36+
while len(pq) >= 2:
37+
freq1, barcode1 = heapq.heappop(pq)
38+
freq2, barcode2 = heapq.heappop(pq)
39+
result.extend([barcode1, barcode2])
40+
41+
if freq1 + 1:
42+
heapq.heappush(pq, (freq1 + 1, barcode1))
43+
if freq2 + 1:
44+
heapq.heappush(pq, (freq2 + 1, barcode2))
45+
46+
if pq:
47+
result.append(pq[0][1])
48+
49+
return result

Diff for: README.md

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1515
##### [Problems 1000-1100](./1000-1100q/)
1616
| # | Title | Solution | Difficulty |
1717
|---| ----- | -------- | ---------- |
18+
|1054|[Distant Barcodes](https://leetcode.com/problems/distant-barcodes)|[Python](./1000-1100q/1054.py)|Medium|
19+
|1053|[Previous Permutation With One Swap](https://leetcode.com/problems/previous-permutation-with-one-swap)|[Python](./1000-1100/1053.py)|Medium|
20+
|1052|[Grumpy Bookstore Owner](https://leetcode.com/problems/grumpy-bookstore-owner)|[Python](./1000-1100q/1052.py)|Medium|
21+
|1051|[Height Checker](https://leetcode.com/problems/height-checker)|[Python](./1000-1100q/1051.py)|Easy|
1822
|1048|[Longest String Chain](https://leetcode.com/problems/longest-string-chain)|[Python](./1000-1100q/1048.py)|Medium|
1923
|1047|[Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string)|[Python](./1000-1100q/1047.py)|Easy|
2024
|1046|[Last Stone Weight](https://leetcode.com/problems/last-stone-weight)|[Python](./1000-1100q/1046.py)|Easy|

0 commit comments

Comments
 (0)