Skip to content

Commit

Permalink
Added Solution for 1000-1100q/1090 1091
Browse files Browse the repository at this point in the history
  • Loading branch information
Garvit244 committed Jun 19, 2019
1 parent 5b2edf3 commit 43c329d
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 1 deletion.
61 changes: 61 additions & 0 deletions 1000-1100q/1090.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
'''
We have a set of items: the i-th item has value values[i] and label labels[i].
Then, we choose a subset S of these items, such that:
|S| <= num_wanted
For every label L, the number of items in S with label L is <= use_limit.
Return the largest possible sum of the subset S.
Example 1:
Input: values = [5,4,3,2,1], labels = [1,1,2,2,3], num_wanted = 3, use_limit = 1
Output: 9
Explanation: The subset chosen is the first, third, and fifth item.
Example 2:
Input: values = [5,4,3,2,1], labels = [1,3,3,3,2], num_wanted = 3, use_limit = 2
Output: 12
Explanation: The subset chosen is the first, second, and third item.
Example 3:
Input: values = [9,8,8,7,6], labels = [0,0,0,1,1], num_wanted = 3, use_limit = 1
Output: 16
Explanation: The subset chosen is the first and fourth item.
Example 4:
Input: values = [9,8,8,7,6], labels = [0,0,0,1,1], num_wanted = 3, use_limit = 2
Output: 24
Explanation: The subset chosen is the first, second, and fourth item.
Note:
1 <= values.length == labels.length <= 20000
0 <= values[i], labels[i] <= 20000
1 <= num_wanted, use_limit <= values.length
'''
class Solution(object):
def largestValsFromLabels(self, values, labels, num_wanted, use_limit):
"""
:type values: List[int]
:type labels: List[int]
:type num_wanted: int
:type use_limit: int
:rtype: int
"""
sorted_values = sorted([(i, j) for i, j in zip(values, labels)], key = lambda x : x[0]*-1)
label_used_count = {label: 0 for label in set(labels)}
result = 0
for s_v in sorted_values:
if num_wanted:
if label_used_count[s_v[1]] < use_limit:
result += s_v[0]
label_used_count[s_v[1]] +=1
num_wanted -= 1
else:
break
return result

52 changes: 52 additions & 0 deletions 1091.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'''
In an N by N square grid, each cell is either empty (0) or blocked (1).
A clear path from top-left to bottom-right has length k if and only if it is composed of cells C_1, C_2, ..., C_k such that:
Adjacent cells C_i and C_{i+1} are connected 8-directionally (ie., they are different and share an edge or corner)
C_1 is at location (0, 0) (ie. has value grid[0][0])
C_k is at location (N-1, N-1) (ie. has value grid[N-1][N-1])
If C_i is located at (r, c), then grid[r][c] is empty (ie. grid[r][c] == 0).
Return the length of the shortest such clear path from top-left to bottom-right. If such a path does not exist, return -1.
Example 1:
Input: [[0,1],[1,0]]
Output: 2
Example 2:
Input: [[0,0,0],[1,1,0],[1,1,0]]
Output: 4
Note:
1 <= grid.length == grid[0].length <= 100
grid[r][c] is 0 or 1
'''
class Solution(object):
def shortestPathBinaryMatrix(self, grid):
"""
:type grid: List[List[int]]
:rtype: int
"""
if not grid:
return -1

rows, cols = len(grid), len(grid[0])
if grid[0][0] or grid[rows-1][cols-1]:
return -1

queue = [[0, 0, 1]]
for row, col, dist in queue:
if row == rows-1 and col == cols-1:
return dist
for di, dj in [(-1, -1), (0, -1), (-1, 1), (-1, 0), (1, 0), (1, -1), (0, 1), (1, 1)]:
n_row, n_col = row + di, col + dj
if 0 <= n_row < rows and 0 <= n_col < cols and not grid[n_row][n_col]:
grid[n_row][n_col] = 1
queue.append([n_row, n_col, dist + 1])

return -1
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
##### [Problems 1000-1100](./1000-1100q/)
| # | Title | Solution | Difficulty |
|---| ----- | -------- | ---------- |
|1089|[]
|1091|[Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix)|[Python](./1000-1100q/1091.py)|Medium|
|1090|[Largest Values From Labels ](https://leetcode.com/problems/largest-values-from-labels)|[Python](./1000-1100q/1090.py)|Medium|
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros)|[Python](./1000-1100q/1089.py)|Easy|
|1088|[]
|1087|[Brace Expansion](https://leetcode.com/problems/brace-expansion)|[Python](./1000-1100q/1087.py)|Medium|
|1086|[High Five](https://leetcode.com/problems/high-five)|[Python](./1000-1100q/1086.py)|Medium|
Expand Down

0 comments on commit 43c329d

Please sign in to comment.