You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"source": "# 11. Container With Most Water\n\nhttps://leetcode.com/problems/container-with-most-water/description/"
7
+
},
8
+
{
9
+
"metadata": {
10
+
"collapsed": true
11
+
},
12
+
"cell_type": "markdown",
13
+
"source": "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.\n\n__Note:__ You may not slant the container and n is at least 2.\n\n\n\nThe above vertical lines are represented by array [1,8,6,2,5,4,8,3,7]. In this case, the max area of water (blue section) the container can contain is 49.\n\n__Example__\n\n`Input: [1,8,6,2,5,4,8,3,7]\nOutput: 49`"
"source": "# 17. Letter Combinations of a Phone Number\n\nhttps://leetcode.com/problems/letter-combinations-of-a-phone-number"
7
+
},
8
+
{
9
+
"metadata": {
10
+
"collapsed": true
11
+
},
12
+
"cell_type": "markdown",
13
+
"source": "Given a string containing digits from `2-9` inclusive, return all possible letter combinations that the number could represent.\n\nA mapping of digit to letters (just like on the telephone buttons) is given below. Note that 1 does not map to any letters."
"source": "# 68. Text Justification\n\nhttps://leetcode.com/problems/text-justification/description/"
7
+
},
8
+
{
9
+
"metadata": {
10
+
"collapsed": true
11
+
},
12
+
"cell_type": "markdown",
13
+
"source": "Given an array of words and a width maxWidth, format the text such that each line has exactly maxWidth characters and is fully (left and right) justified.\n\nYou should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces `' '` when necessary so that each line has exactly maxWidth characters.\n\nExtra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.\n\nFor the last line of text, it should be left justified and no __extra__ space is inserted between words.\n\nNote:\n\n- A word is defined as a character sequence consisting of non-space characters only.\n- Each word's length is guaranteed to be greater than 0 and not exceed maxWidth.\n- The input array `words` contains at least one word."
14
+
},
15
+
{
16
+
"metadata": {},
17
+
"cell_type": "markdown",
18
+
"source": "__Example 1__\n```\nInput:\nwords = [\"This\", \"is\", \"an\", \"example\", \"of\", \"text\", \"justification.\"]\nmaxWidth = 16\nOutput:\n[\n\"This is an\",\n\"example of text\",\n\"justification. \"\n]\n```"
19
+
},
20
+
{
21
+
"metadata": {},
22
+
"cell_type": "markdown",
23
+
"source": "__Example 2__\n\n```\nInput:\nwords = [\"What\",\"must\",\"be\",\"acknowledgment\",\"shall\",\"be\"]\nmaxWidth = 16\nOutput:\n[\n\"What must be\",\n\"acknowledgment \",\n\"shall be \"\n]\nExplanation: Note that the last line is \"shall be \" instead of \"shall be\",\n because the last line must be left-justified instead of fully-justified.\n Note that the second line is also left-justified becase it contains only one word.\n```"
24
+
},
25
+
{
26
+
"metadata": {},
27
+
"cell_type": "markdown",
28
+
"source": "__Example 3__\n\n```\nInput:\nwords = [\"Science\",\"is\",\"what\",\"we\",\"understand\",\"well\",\"enough\",\"to\",\"explain\",\n\"to\",\"a\",\"computer.\",\"Art\",\"is\",\"everything\",\"else\",\"we\",\"do\"]\nmaxWidth = 20\nOutput:\n[\n\"Science is what we\",\n\"understand well\",\n\"enough to explain to\",\n\"a computer. Art is\",\n\"everything else we\",\n\"do \"\n]\n```"
29
+
},
30
+
{
31
+
"metadata": {
32
+
"trusted": true
33
+
},
34
+
"cell_type": "code",
35
+
"source": "class Solution:\n def fullJustify(self, words, maxWidth):\n \"\"\"\n :type words: List[str]\n :type maxWidth: int\n :rtype: List[str]\n \"\"\"\n lines = []\n line = words[0]\n \n for word in words[1:]:\n space_available = maxWidth - len(line)\n if space_available > len(word):\n # Word fits in current line\n # Add word to current line\n line += \" \" + word\n else:\n # Word does not fit in current line\n line_words = line.split()\n gaps = len(line_words) - 1\n if gaps > 0:\n # Gaps found, justify current line\n padding = space_available // gaps\n extra_padding = space_available % gaps\n \n # Add padding\n line = line.replace(' ', ' ' * (padding + 1))\n \n # Add extra padding\n line_words = line.split(' ')\n for i in range(extra_padding):\n line_words[i] = line_words[i] + ' '\n line = ' '.join(line_words)\n else:\n # No gaps, add right padding\n line += ' ' * space_available\n\n # Add current line to result\n lines.append(line)\n \n # Empty current line\n # Add word to current line\n line = word\n # Special case: last line\n \n if lines == [] or lines[-1] != line:\n # Add right padding to last line\n space_available = maxWidth - len(line)\n line = line + ' ' * space_available\n # Add last line to result\n lines.append(line)\n \n \n return lines",
36
+
"execution_count": 111,
37
+
"outputs": []
38
+
},
39
+
{
40
+
"metadata": {
41
+
"trusted": true
42
+
},
43
+
"cell_type": "code",
44
+
"source": "words = [\"This\", \"is\", \"an\", \"example\", \"of\", \"text\", \"justification.\"]\n# words = [\"What\",\"must\",\"be\",\"acknowledgment\",\"shall\",\"be\"]\n# words = [\"Science\",\"is\",\"what\",\"we\",\"understand\",\"well\",\"enough\",\"to\",\"explain\",\"to\",\"a\",\"computer.\",\"Art\",\"is\",\"everything\",\"else\",\"we\",\"do\"]\nwords = [\"a\"]\nmaxWidth = 17\n\nsol = Solution()\nresult = sol.fullJustify(words, maxWidth)\nfor r in result:\n pass\n print('|' + r.replace(' ', '*') + '|')",
"source": "# 8. String to Integer (atoi)\n\nhttps://leetcode.com/problems/string-to-integer-atoi/description/"
9
+
},
10
+
{
11
+
"metadata": {},
12
+
"cell_type": "markdown",
13
+
"source": "Implement `atoi` which converts a string to an integer.\n\nThe function first discards as many whitespace characters as necessary until the first non-whitespace character is found. Then, starting from this character, takes an optional initial plus or minus sign followed by as many numerical digits as possible, and interprets them as a numerical value.\n\nThe string can contain additional characters after those that form the integral number, which are ignored and have no effect on the behavior of this function.\n\nIf the first sequence of non-whitespace characters in str is not a valid integral number, or if no such sequence exists because either str is empty or it contains only whitespace characters, no conversion is performed.\n\nIf no valid conversion could be performed, a zero value is returned.\n\nNote:\n\n - Only the space character `' '` is considered as whitespace character.\n - Assume we are dealing with an environment which could only store integers within the 32-bit signed integer range: [−2<sup>31</sup>, 2<sup>31</sup> − 1]. If the numerical value is out of the range of representable values, INT_MAX (2<sup>31</sup> − 1) or INT_MIN (−2<sup>31</sup>) is returned."
14
+
},
15
+
{
16
+
"metadata": {},
17
+
"cell_type": "markdown",
18
+
"source": "__Example 1:__\n\n```\nInput: \"42\"\nOutput: 42\nExample 2:\n```\n\n__Example 2:__\n\n```\nInput: \" -42\"\nOutput: -42\nExplanation: The first non-whitespace character is '-', which is the minus sign.\n Then take as many numerical digits as possible, which gets 42.\n```\n\n__Example 3:__\n\n```\nInput: \"4193 with words\"\nOutput: 4193\nExplanation: Conversion stops at digit '3' as the next character is not a numerical digit.\n```\n\n__Example 4:__\n\n```\nInput: \"words and 987\"\nOutput: 0\nExplanation: The first non-whitespace character is 'w', which is not a numerical \n digit or a +/- sign. Therefore no valid conversion could be performed.\n```\n\n__Example 5:__\n\n```\nInput: \"-91283472332\"\nOutput: -2147483648\nExplanation: The number \"-91283472332\" is out of the range of a 32-bit signed integer.\n Thefore INT_MIN (−2<sup>31</sup>) is returned.\n```"
19
+
},
20
+
{
21
+
"metadata": {
22
+
"trusted": true
23
+
},
24
+
"cell_type": "code",
25
+
"source": "class Solution:\n def myAtoi(self, str):\n\"\"\"\n :type str: str\n :rtype: int\n\"\"\"\n sign = 1\n valid_symbols = ['-', '+']\n valid_digits = ['-']\n valid_digits += [i.__str__() for i in range(10)]\n\n if str == '':\n # Empty string\n return 0\n\n if str[0] in valid_symbols:\n # Check for negative\n if str[0] == '-':\n sign = -1\n # Remove symbol\n str = str[1:]\n\n if str[0] in valid_digits:\n # Valid integer\n return str[0]\n else:\n # Invalid integer\n return 0\n",
0 commit comments