Skip to content

Commit b4f1431

Browse files
committed
some leetcode examples
1 parent 2d16410 commit b4f1431

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

leetcode/maxprofit.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution(object):
2+
def maxProfit(self, prices):
3+
"""
4+
:type prices: List[int]
5+
:rtype: int
6+
"""
7+
min_price = float('inf')
8+
max_profit = 0
9+
for i in range(len(prices)):
10+
if prices[i] < min_price:
11+
min_price = prices[i]
12+
elif prices[i] - min_price > max_profit:
13+
max_profit = prices[i] - min_price
14+
15+
return max_profit
16+

leetcode/romans.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution(object):
2+
def intToRoman(self, num):
3+
"""
4+
:type num: int
5+
:rtype: str
6+
"""
7+
thousand = ["", "M", "MM", "MMM"]
8+
hundred = ["", "C", "CC", "CCC", "CD", "D", "DC", "DCC", "DCCC", "CM"]
9+
ten = ["", "X", "XX", "XXX", "XL", "L", "LX", "LXX", "LXXX", "XC"]
10+
one = ["", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX"]
11+
return (thousand[num // 1000] + hundred[num % 1000 // 100]
12+
+ ten[num % 100 // 10] + one[num % 10])

leetcode/twolessthank.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class Solution(object):
2+
def twoSumLessThanK(self, nums, k):
3+
"""
4+
:type nums: List[int]
5+
:type k: int
6+
:rtype: int
7+
"""
8+
nums.sort()
9+
answer = -1
10+
left = 0
11+
right = len(nums) -1
12+
while left < right:
13+
sum = nums[left] + nums[right]
14+
if (sum < k):
15+
answer = max(answer, sum)
16+
left += 1
17+
else:
18+
right -= 1
19+
return answer
20+

0 commit comments

Comments
 (0)