Skip to content

Commit e7e3bc3

Browse files
committed
9.29
1 parent c1b309b commit e7e3bc3

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

149.py

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class Solution:
2+
"""
3+
@param prices: Given an integer array
4+
@return: Maximum profit
5+
"""
6+
def maxProfit(self, prices):
7+
# write your code here
8+
if len(prices) <= 1:
9+
return 0
10+
max_pro = 0
11+
min_pri = 99999
12+
for num in prices:
13+
min_pri = min(min_pri, num)
14+
max_pro = max(max_pro, num - min_pri)
15+
return max_pro

411.py

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# 000
2+
# 001
3+
# 011
4+
# 010
5+
# 110
6+
# 111
7+
# 101
8+
# 100
9+
class Solution:
10+
"""
11+
@param n: a number
12+
@return: Gray code
13+
"""
14+
def grayCode(self, n):
15+
# write your code here
16+
res = [[0,0],[0,1],[1,1],[1,0]]
17+
res_ = []
18+
if n == 0:
19+
return [0]
20+
if n == 1:
21+
return [0, 1]
22+
while(n > 2):
23+
res_tmp = [arr[:] for arr in res]
24+
res_tmp.reverse()
25+
print(res)
26+
print(res_tmp)
27+
for arr in res:
28+
arr.insert(0, 0)
29+
print(res)
30+
print(res_tmp)
31+
for arr in res_tmp:
32+
arr.insert(0, 1)
33+
res.append(arr)
34+
n -= 1
35+
print(res)
36+
for num_arr in res:
37+
tmp = 1
38+
num = 0
39+
for index in num_arr[::-1]:
40+
num += tmp*index
41+
tmp *= 2
42+
res_.append(num)
43+
return res_
44+
45+
print(Solution.grayCode(2, 4))

46.py

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
class Solution:
2+
"""
3+
@param: nums: a list of integers
4+
@return: find a majority number
5+
"""
6+
def majorityNumber(self, nums):
7+
# write your code here
8+
max_res = 1
9+
count = 1
10+
nums.sort()
11+
res = nums[0]
12+
tmp = nums[0]
13+
for index in range(1, len(nums)):
14+
if nums[index] == tmp:
15+
count += 1
16+
else:
17+
if max_res < count:
18+
max_res = count
19+
res = tmp
20+
tmp = nums[index]
21+
count = 1
22+
if max_res < count:
23+
return tmp
24+
return res
25+
26+
A = [1,1,1,1,2,2,2]
27+
print(Solution.majorityNumber(A, A))

0 commit comments

Comments
 (0)