Skip to content

Commit a910190

Browse files
committed
Update
1 parent 5aa6d74 commit a910190

File tree

2 files changed

+130
-0
lines changed
  • leetcode

2 files changed

+130
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# coding=utf-8
2+
3+
'''
4+
Assume you are an awesome parent and want to give your children some cookies.
5+
But, you should give each child at most one cookie. Each child i has a greed
6+
factor gi, which is the minimum size of a cookie that the child will be content with;
7+
and each cookie j has a size sj. If sj >= gi, we can assign the cookie j to the
8+
child i, and the child i will be content. Your goal is to maximize the number of
9+
your content children and output the maximum number.
10+
11+
Note:
12+
You may assume the greed factor is always positive.
13+
You cannot assign more than one cookie to one child.
14+
15+
Example 1:
16+
Input: [1,2,3], [1,1]
17+
18+
Output: 1
19+
20+
Explanation: You have 3 children and 2 cookies. The greed factors of 3 children are
21+
1, 2, 3.
22+
And even though you have 2 cookies, since their size is both 1, you could only
23+
make the child whose greed factor is 1 content.
24+
You need to output 1.
25+
Example 2:
26+
Input: [1,2], [1,2,3]
27+
28+
Output: 2
29+
30+
Explanation: You have 2 children and 3 cookies. The greed factors of 2 children are 1, 2.
31+
You have 3 cookies and their sizes are big enough to gratify all of the children,
32+
You need to output 2.
33+
'''
34+
35+
'''
36+
此题主要是证明正确性
37+
Beat 57.31%
38+
'''
39+
40+
class Solution(object):
41+
def findContentChildren(self, g, s):
42+
"""
43+
:type g: List[int]
44+
:type s: List[int]
45+
:rtype: int
46+
"""
47+
g.sort()
48+
s.sort()
49+
g_cursor = 0
50+
s_cursor = 0
51+
result = 0
52+
53+
while g_cursor < len(g) and s_cursor < len(s):
54+
if g[g_cursor] > s[s_cursor]:
55+
s_cursor += 1
56+
else:
57+
g_cursor += 1
58+
s_cursor += 1
59+
result += 1
60+
61+
return result
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# coding=utf-8
2+
3+
'''
4+
Given a matrix of M x N elements (M rows, N columns), return all elements of the matrix in diagonal order as shown in the below image.
5+
6+
Example:
7+
Input:
8+
[
9+
[ 1, 2, 3 ],
10+
[ 4, 5, 6 ],
11+
[ 7, 8, 9 ]
12+
]
13+
Output: [1,2,4,7,5,3,6,8,9]
14+
Explanation:
15+
16+
'''
17+
18+
'''
19+
此题撞墙情况特别多,奇数轮和偶数轮坐标的移位都是有两种情况的,另外对角线打印的次数适合行列数总和有关的
20+
Beat 87.91%
21+
公司:Google
22+
'''
23+
24+
class Solution(object):
25+
def findDiagonalOrder(self, matrix):
26+
"""
27+
:type matrix: List[List[int]]
28+
:rtype: List[int]
29+
"""
30+
n = len(matrix)
31+
if not n:
32+
return []
33+
34+
if n == 1:
35+
return matrix[0]
36+
37+
m = len(matrix[0])
38+
if not m:
39+
return []
40+
41+
if m == 1:
42+
return [x[0] for x in matrix]
43+
44+
result = []
45+
cursor = [0, 0]
46+
47+
for time in xrange(n + m - 1):
48+
if time % 2 == 1:
49+
while cursor[0] + 1 < n and cursor[1] - 1 >= 0:
50+
result.append(matrix[cursor[0]][cursor[1]])
51+
cursor[0] += 1
52+
cursor[1] -= 1
53+
result.append(matrix[cursor[0]][cursor[1]])
54+
if cursor[0] + 1 < n:
55+
cursor[0] += 1
56+
else:
57+
cursor[1] += 1
58+
else:
59+
while cursor[0] - 1 >= 0 and cursor[1] + 1 < m:
60+
result.append(matrix[cursor[0]][cursor[1]])
61+
cursor[0] -= 1
62+
cursor[1] += 1
63+
result.append(matrix[cursor[0]][cursor[1]])
64+
if cursor[1] + 1 < m:
65+
cursor[1] += 1
66+
else:
67+
cursor[0] += 1
68+
69+
return result

0 commit comments

Comments
 (0)