Skip to content

Commit 16d3ec3

Browse files
committed
Initial problems
1 parent 690f0d2 commit 16d3ec3

4 files changed

+329
-0
lines changed

11. Container With Most Water.ipynb

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
"cells": [
3+
{
4+
"metadata": {},
5+
"cell_type": "markdown",
6+
"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![](https://s3-lc-upload.s3.amazonaws.com/uploads/2018/07/17/question_11.jpg \"Problem visualization\")\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`"
14+
},
15+
{
16+
"metadata": {
17+
"trusted": true
18+
},
19+
"cell_type": "code",
20+
"source": "from itertools import combinations",
21+
"execution_count": 1,
22+
"outputs": []
23+
},
24+
{
25+
"metadata": {
26+
"trusted": true
27+
},
28+
"cell_type": "code",
29+
"source": "def volume(h1, h2):\n distance = abs(list(h1.keys())[0] - list(h2.keys())[0])\n height = min(list(h1.values())[0], list(h2.values())[0])\n return distance * height",
30+
"execution_count": 29,
31+
"outputs": []
32+
},
33+
{
34+
"metadata": {
35+
"trusted": true
36+
},
37+
"cell_type": "code",
38+
"source": "class Solution:\n def maxArea(self, height):\n \"\"\"\n :type height: List[int]\n :rtype: int\n \"\"\"\n from itertools import combinations\n dict_data = [{index: value} for index, value in enumerate(height)]\n comb = list(combinations(dict_data, 2))\n volume = lambda h1, h2: abs(list(h1.keys())[0] - list(h2.keys())[0]) * min(list(h1.values())[0], list(h2.values())[0])\n return max([volume(c[0], c[1]) for c in comb])",
39+
"execution_count": 47,
40+
"outputs": []
41+
},
42+
{
43+
"metadata": {
44+
"trusted": true
45+
},
46+
"cell_type": "code",
47+
"source": "sol = Solution()\ndata = [1,8,6,2,5,4,8,3,7]\nsol.maxArea(data)",
48+
"execution_count": 48,
49+
"outputs": [
50+
{
51+
"output_type": "execute_result",
52+
"execution_count": 48,
53+
"data": {
54+
"text/plain": "49"
55+
},
56+
"metadata": {}
57+
}
58+
]
59+
},
60+
{
61+
"metadata": {
62+
"trusted": true
63+
},
64+
"cell_type": "code",
65+
"source": "",
66+
"execution_count": null,
67+
"outputs": []
68+
}
69+
],
70+
"metadata": {
71+
"kernelspec": {
72+
"name": "python36",
73+
"display_name": "Python 3.6",
74+
"language": "python"
75+
},
76+
"language_info": {
77+
"mimetype": "text/x-python",
78+
"nbconvert_exporter": "python",
79+
"name": "python",
80+
"pygments_lexer": "ipython3",
81+
"version": "3.6.6",
82+
"file_extension": ".py",
83+
"codemirror_mode": {
84+
"version": 3,
85+
"name": "ipython"
86+
}
87+
}
88+
},
89+
"nbformat": 4,
90+
"nbformat_minor": 2
91+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
{
2+
"cells": [
3+
{
4+
"metadata": {},
5+
"cell_type": "markdown",
6+
"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."
14+
},
15+
{
16+
"metadata": {},
17+
"cell_type": "markdown",
18+
"source": "__Example__:\n\n`Input: \"23\"\nOutput: [\"ad\", \"ae\", \"af\", \"bd\", \"be\", \"bf\", \"cd\", \"ce\", \"cf\"].`"
19+
},
20+
{
21+
"metadata": {
22+
"trusted": true
23+
},
24+
"cell_type": "code",
25+
"source": "class Solution:\n def letterCombinations(self, digits):\n \"\"\"\n :type digits: str\n :rtype: List[str]\n \"\"\"\n if digits == \"\": return []\n \n from itertools import product\n mapping = {\n 2: \"abc\",\n 3: \"def\",\n 4: \"ghi\",\n 5: \"jkl\",\n 6: \"mno\",\n 7: \"pqrs\",\n 8: \"tuv\",\n 9: \"wxyz\"\n }\n \n lists = [mapping[int(i)] for i in digits]\n return list(product(*lists))",
26+
"execution_count": 43,
27+
"outputs": []
28+
},
29+
{
30+
"metadata": {
31+
"trusted": true
32+
},
33+
"cell_type": "code",
34+
"source": "sol = Solution()\nnum = \"23\"\nsol.letterCombinations(num)",
35+
"execution_count": 45,
36+
"outputs": [
37+
{
38+
"output_type": "execute_result",
39+
"execution_count": 45,
40+
"data": {
41+
"text/plain": "[('a', 'd'),\n ('a', 'e'),\n ('a', 'f'),\n ('b', 'd'),\n ('b', 'e'),\n ('b', 'f'),\n ('c', 'd'),\n ('c', 'e'),\n ('c', 'f')]"
42+
},
43+
"metadata": {}
44+
}
45+
]
46+
},
47+
{
48+
"metadata": {
49+
"trusted": true
50+
},
51+
"cell_type": "code",
52+
"source": "",
53+
"execution_count": null,
54+
"outputs": []
55+
}
56+
],
57+
"metadata": {
58+
"kernelspec": {
59+
"name": "python36",
60+
"display_name": "Python 3.6",
61+
"language": "python"
62+
},
63+
"language_info": {
64+
"mimetype": "text/x-python",
65+
"nbconvert_exporter": "python",
66+
"name": "python",
67+
"pygments_lexer": "ipython3",
68+
"version": "3.6.6",
69+
"file_extension": ".py",
70+
"codemirror_mode": {
71+
"version": 3,
72+
"name": "ipython"
73+
}
74+
}
75+
},
76+
"nbformat": 4,
77+
"nbformat_minor": 2
78+
}

68. Text Justification.ipynb

+85
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
{
2+
"cells": [
3+
{
4+
"metadata": {},
5+
"cell_type": "markdown",
6+
"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(' ', '*') + '|')",
45+
"execution_count": 113,
46+
"outputs": [
47+
{
48+
"output_type": "stream",
49+
"text": "|a****************|\n",
50+
"name": "stdout"
51+
}
52+
]
53+
},
54+
{
55+
"metadata": {
56+
"trusted": true
57+
},
58+
"cell_type": "code",
59+
"source": "",
60+
"execution_count": null,
61+
"outputs": []
62+
}
63+
],
64+
"metadata": {
65+
"kernelspec": {
66+
"name": "python36",
67+
"display_name": "Python 3.6",
68+
"language": "python"
69+
},
70+
"language_info": {
71+
"mimetype": "text/x-python",
72+
"nbconvert_exporter": "python",
73+
"name": "python",
74+
"pygments_lexer": "ipython3",
75+
"version": "3.6.6",
76+
"file_extension": ".py",
77+
"codemirror_mode": {
78+
"version": 3,
79+
"name": "ipython"
80+
}
81+
}
82+
},
83+
"nbformat": 4,
84+
"nbformat_minor": 2
85+
}

8. String to Integer (atoi).ipynb

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
{
2+
"cells": [
3+
{
4+
"metadata": {
5+
"collapsed": true
6+
},
7+
"cell_type": "markdown",
8+
"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 ",
26+
"execution_count": 12,
27+
"outputs": []
28+
},
29+
{
30+
"metadata": {
31+
"trusted": true
32+
},
33+
"cell_type": "code",
34+
"source": "s = \"23\"\nsol = Solution()\nnum = sol.myAtoi(s)\nprint(num)",
35+
"execution_count": 13,
36+
"outputs": [
37+
{
38+
"output_type": "stream",
39+
"text": "2\n",
40+
"name": "stdout"
41+
}
42+
]
43+
},
44+
{
45+
"metadata": {
46+
"trusted": true
47+
},
48+
"cell_type": "code",
49+
"source": "",
50+
"execution_count": null,
51+
"outputs": []
52+
}
53+
],
54+
"metadata": {
55+
"kernelspec": {
56+
"name": "python36",
57+
"display_name": "Python 3.6",
58+
"language": "python"
59+
},
60+
"language_info": {
61+
"mimetype": "text/x-python",
62+
"nbconvert_exporter": "python",
63+
"name": "python",
64+
"pygments_lexer": "ipython3",
65+
"version": "3.6.6",
66+
"file_extension": ".py",
67+
"codemirror_mode": {
68+
"version": 3,
69+
"name": "ipython"
70+
}
71+
}
72+
},
73+
"nbformat": 4,
74+
"nbformat_minor": 2
75+
}

0 commit comments

Comments
 (0)