Skip to content

Commit

Permalink
Weekly Contest solutions
Browse files Browse the repository at this point in the history
  • Loading branch information
Garvit244 committed May 27, 2019
1 parent ccaf782 commit 6a97adc
Show file tree
Hide file tree
Showing 5 changed files with 186 additions and 0 deletions.
32 changes: 32 additions & 0 deletions 1000-1100q/1051.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
'''
Students are asked to stand in non-decreasing order of heights for an annual photo.
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.)
Example 1:
Input: [1,1,4,2,1,3]
Output: 3
Explanation:
Students with heights 4, 3 and the last 1 are not standing in the right positions.
Note:
1 <= heights.length <= 100
1 <= heights[i] <= 100
'''

class Solution(object):
def heightChecker(self, heights):
"""
:type heights: List[int]
:rtype: int
"""
result = 0
for new_h, hei in zip(heights, sorted(heights)):
if new_h != hei:
result += 1
return result
52 changes: 52 additions & 0 deletions 1000-1100q/1052.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'''
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.
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.
The bookstore owner knows a secret technique to keep themselves not grumpy for X minutes straight, but can only use it once.
Return the maximum number of customers that can be satisfied throughout the day.
Example 1:
Input: customers = [1,0,1,2,1,1,7,5], grumpy = [0,1,0,1,0,1,0,1], X = 3
Output: 16
Explanation: The bookstore owner keeps themselves not grumpy for the last 3 minutes.
The maximum number of customers that can be satisfied = 1 + 1 + 1 + 1 + 7 + 5 = 16.
Note:
1 <= X <= customers.length == grumpy.length <= 20000
0 <= customers[i] <= 1000
0 <= grumpy[i] <= 1
'''
class Solution(object):
def maxSatisfied(self, customers, grumpy, X):
"""
:type customers: List[int]
:type grumpy: List[int]
:type X: int
:rtype: int
"""
result = 0

prefix_sum = [0]*(len(customers)+1)
index = 0
for customer, grump in zip(customers, grumpy):
prefix_sum[index+1] = prefix_sum[index]
if grump == 0:
result += customer
else:
prefix_sum[index+1] += customer
index += 1
# print prefix_sum
curr_max = result + prefix_sum[X]
# print curr_max
for index in range(X+1, len(prefix_sum)):
temp_max = result + prefix_sum[index] - prefix_sum[index-X]
# print temp_max
curr_max = max(curr_max, temp_max)
return curr_max
49 changes: 49 additions & 0 deletions 1000-1100q/1053.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'''
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.
Example 1:
Input: [3,2,1]
Output: [3,1,2]
Explanation: Swapping 2 and 1.
Example 2:
Input: [1,1,5]
Output: [1,1,5]
Explanation: This is already the smallest permutation.
Example 3:
Input: [1,9,4,6,7]
Output: [1,7,4,6,9]
Explanation: Swapping 9 and 7.
Example 4:
Input: [3,1,1,3]
Output: [1,3,1,3]
Explanation: Swapping 1 and 3.
Note:
1 <= A.length <= 10000
1 <= A[i] <= 10000
'''
class Solution(object):
def prevPermOpt1(self, A):
"""
:type A: List[int]
:rtype: List[int]
"""

left, right = len(A)-2, len(A)-1
for left in range(len(A)-2, -1, -1):
if A[left] > A[left+1]:
break
else:
return A
right = A.index(max(ele for ele in A[left+1:] if ele < A[left]), left)
A[left], A[right] = A[right], A[left]
return A

49 changes: 49 additions & 0 deletions 1000-1100q/1054.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'''
In a warehouse, there is a row of barcodes, where the i-th barcode is barcodes[i].
Rearrange the barcodes so that no two adjacent barcodes are equal. You may return any answer, and it is guaranteed an answer exists.
Example 1:
Input: [1,1,1,2,2,2]
Output: [2,1,2,1,2,1]
Example 2:
Input: [1,1,1,1,2,2,3,3]
Output: [1,3,1,3,2,1,2,1]
Note:
1 <= barcodes.length <= 10000
1 <= barcodes[i] <= 10000
'''

class Solution(object):
def rearrangeBarcodes(self, barcodes):
"""
:type barcodes: List[int]
:rtype: List[int]
"""
import heapq
di = collections.Counter(barcodes)
pq = [(-value, key) for key, value in di.items()]
heapq.heapify(pq)
# print pq
result = []
while len(pq) >= 2:
freq1, barcode1 = heapq.heappop(pq)
freq2, barcode2 = heapq.heappop(pq)
result.extend([barcode1, barcode2])

if freq1 + 1:
heapq.heappush(pq, (freq1 + 1, barcode1))
if freq2 + 1:
heapq.heappush(pq, (freq2 + 1, barcode2))

if pq:
result.append(pq[0][1])

return result
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
##### [Problems 1000-1100](./1000-1100q/)
| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|1054|[Distant Barcodes](https://leetcode.com/problems/distant-barcodes)|[Python](./1000-1100q/1054.py)|Medium|
|1053|[Previous Permutation With One Swap](https://leetcode.com/problems/previous-permutation-with-one-swap)|[Python](./1000-1100/1053.py)|Medium|
|1052|[Grumpy Bookstore Owner](https://leetcode.com/problems/grumpy-bookstore-owner)|[Python](./1000-1100q/1052.py)|Medium|
|1051|[Height Checker](https://leetcode.com/problems/height-checker)|[Python](./1000-1100q/1051.py)|Easy|
|1048|[Longest String Chain](https://leetcode.com/problems/longest-string-chain)|[Python](./1000-1100q/1048.py)|Medium|
|1047|[Remove All Adjacent Duplicates In String](https://leetcode.com/problems/remove-all-adjacent-duplicates-in-string)|[Python](./1000-1100q/1047.py)|Easy|
|1046|[Last Stone Weight](https://leetcode.com/problems/last-stone-weight)|[Python](./1000-1100q/1046.py)|Easy|
Expand Down

0 comments on commit 6a97adc

Please sign in to comment.