Skip to content

Commit

Permalink
add 2
Browse files Browse the repository at this point in the history
  • Loading branch information
SophieGarden committed Mar 1, 2020
1 parent 6e18fbf commit 4984151
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 0 deletions.
33 changes: 33 additions & 0 deletions 11_container_with_most_water.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
11. Container With Most Water
Medium

4942

519

Add to List

Share
Given n non-negative integers a1, a2, ..., an , where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water.

Note: You may not slant the container and n is at least 2.


def maxArea(self, height: List[int]) -> int:
max_water = 0
for i, h1 in enumerate(height):
for j, h2 in enumerate(height):
max_water = max(max_water, (j-i) * min(h1, h2))
return max_water

class Solution:
def maxArea(self, height: List[int]) -> int:
max_area = 0
l, r = 0, len(height)-1
while l < r:
max_area = max(max_area, (r-l) * min(height[l], height[r]))
if height[l] < height[r]:
l += 1
else:
r -= 1
return max_area
77 changes: 77 additions & 0 deletions 14_integer_to_roman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
12. Integer to Roman
Medium

855

2408

Add to List

Share
Roman numerals are represented by seven different symbols: I, V, X, L, C, D and M.

Symbol Value
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
For example, two is written as II in Roman numeral, just two one's added together. Twelve is written as, XII, which is simply X + II. The number twenty seven is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

I can be placed before V (5) and X (10) to make 4 and 9.
X can be placed before L (50) and C (100) to make 40 and 90.
C can be placed before D (500) and M (1000) to make 400 and 900.
Given an integer, convert it to a roman numeral. Input is guaranteed to be within the range from 1 to 3999.

Example 1:

Input: 3
Output: "III"
Example 2:

Input: 4
Output: "IV"
Example 3:

Input: 9
Output: "IX"
Example 4:

Input: 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.
Example 5:

Input: 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.
Accepted
313,609
Submissions
584,957

class Solution:
def intToRoman(self, num: int) -> str:
'''can be clean a bit more by writing the number order from large to small so we can just use index to refer to symbols
'''
value = [1, 4, 5, 9,
10, 40, 50, 90,
100, 400, 500, 900,
1000]
symbol = ['I', 'IV', 'V', 'IX',
'X', 'XL', 'L', 'XC',
'C', 'CD', 'D', 'CM',
'M']
d = {}
for v, s in zip(value, symbol):
d[v] = s
roman = ''
for n in value[::-1]:
while num >= n:
roman += d[n]
num -= n
return roman

0 comments on commit 4984151

Please sign in to comment.