forked from Garvit244/Leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
186 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters