Skip to content

Commit 43c329d

Browse files
committed
Added Solution for 1000-1100q/1090 1091
1 parent 5b2edf3 commit 43c329d

File tree

3 files changed

+116
-1
lines changed

3 files changed

+116
-1
lines changed

1000-1100q/1090.py

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'''
2+
We have a set of items: the i-th item has value values[i] and label labels[i].
3+
4+
Then, we choose a subset S of these items, such that:
5+
6+
|S| <= num_wanted
7+
For every label L, the number of items in S with label L is <= use_limit.
8+
Return the largest possible sum of the subset S.
9+
10+
11+
12+
Example 1:
13+
14+
Input: values = [5,4,3,2,1], labels = [1,1,2,2,3], num_wanted = 3, use_limit = 1
15+
Output: 9
16+
Explanation: The subset chosen is the first, third, and fifth item.
17+
Example 2:
18+
19+
Input: values = [5,4,3,2,1], labels = [1,3,3,3,2], num_wanted = 3, use_limit = 2
20+
Output: 12
21+
Explanation: The subset chosen is the first, second, and third item.
22+
Example 3:
23+
24+
Input: values = [9,8,8,7,6], labels = [0,0,0,1,1], num_wanted = 3, use_limit = 1
25+
Output: 16
26+
Explanation: The subset chosen is the first and fourth item.
27+
Example 4:
28+
29+
Input: values = [9,8,8,7,6], labels = [0,0,0,1,1], num_wanted = 3, use_limit = 2
30+
Output: 24
31+
Explanation: The subset chosen is the first, second, and fourth item.
32+
33+
34+
Note:
35+
36+
1 <= values.length == labels.length <= 20000
37+
0 <= values[i], labels[i] <= 20000
38+
1 <= num_wanted, use_limit <= values.length
39+
'''
40+
class Solution(object):
41+
def largestValsFromLabels(self, values, labels, num_wanted, use_limit):
42+
"""
43+
:type values: List[int]
44+
:type labels: List[int]
45+
:type num_wanted: int
46+
:type use_limit: int
47+
:rtype: int
48+
"""
49+
sorted_values = sorted([(i, j) for i, j in zip(values, labels)], key = lambda x : x[0]*-1)
50+
label_used_count = {label: 0 for label in set(labels)}
51+
result = 0
52+
for s_v in sorted_values:
53+
if num_wanted:
54+
if label_used_count[s_v[1]] < use_limit:
55+
result += s_v[0]
56+
label_used_count[s_v[1]] +=1
57+
num_wanted -= 1
58+
else:
59+
break
60+
return result
61+

1091.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'''
2+
In an N by N square grid, each cell is either empty (0) or blocked (1).
3+
4+
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:
5+
6+
Adjacent cells C_i and C_{i+1} are connected 8-directionally (ie., they are different and share an edge or corner)
7+
C_1 is at location (0, 0) (ie. has value grid[0][0])
8+
C_k is at location (N-1, N-1) (ie. has value grid[N-1][N-1])
9+
If C_i is located at (r, c), then grid[r][c] is empty (ie. grid[r][c] == 0).
10+
Return the length of the shortest such clear path from top-left to bottom-right. If such a path does not exist, return -1.
11+
12+
13+
14+
Example 1:
15+
16+
Input: [[0,1],[1,0]]
17+
Output: 2
18+
Example 2:
19+
20+
Input: [[0,0,0],[1,1,0],[1,1,0]]
21+
Output: 4
22+
23+
24+
Note:
25+
26+
1 <= grid.length == grid[0].length <= 100
27+
grid[r][c] is 0 or 1
28+
'''
29+
class Solution(object):
30+
def shortestPathBinaryMatrix(self, grid):
31+
"""
32+
:type grid: List[List[int]]
33+
:rtype: int
34+
"""
35+
if not grid:
36+
return -1
37+
38+
rows, cols = len(grid), len(grid[0])
39+
if grid[0][0] or grid[rows-1][cols-1]:
40+
return -1
41+
42+
queue = [[0, 0, 1]]
43+
for row, col, dist in queue:
44+
if row == rows-1 and col == cols-1:
45+
return dist
46+
for di, dj in [(-1, -1), (0, -1), (-1, 1), (-1, 0), (1, 0), (1, -1), (0, 1), (1, 1)]:
47+
n_row, n_col = row + di, col + dj
48+
if 0 <= n_row < rows and 0 <= n_col < cols and not grid[n_row][n_col]:
49+
grid[n_row][n_col] = 1
50+
queue.append([n_row, n_col, dist + 1])
51+
52+
return -1

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ Python solution of problems from [LeetCode](https://leetcode.com/).
1515
##### [Problems 1000-1100](./1000-1100q/)
1616
| # | Title | Solution | Difficulty |
1717
|---| ----- | -------- | ---------- |
18-
|1089|[]
18+
|1091|[Shortest Path in Binary Matrix](https://leetcode.com/problems/shortest-path-in-binary-matrix)|[Python](./1000-1100q/1091.py)|Medium|
19+
|1090|[Largest Values From Labels ](https://leetcode.com/problems/largest-values-from-labels)|[Python](./1000-1100q/1090.py)|Medium|
20+
|1089|[Duplicate Zeros](https://leetcode.com/problems/duplicate-zeros)|[Python](./1000-1100q/1089.py)|Easy|
1921
|1088|[]
2022
|1087|[Brace Expansion](https://leetcode.com/problems/brace-expansion)|[Python](./1000-1100q/1087.py)|Medium|
2123
|1086|[High Five](https://leetcode.com/problems/high-five)|[Python](./1000-1100q/1086.py)|Medium|

0 commit comments

Comments
 (0)