Skip to content

Commit 3413989

Browse files
5 new problems solutions (codedecks-in#33)
Co-authored-by: Yuri <[email protected]>
1 parent 4b069f1 commit 3413989

6 files changed

+121
-10
lines changed

Diff for: Python/100.SameTree.py

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#Time Complexity: O(n)
2+
#Space Complexity: O(n)
3+
#Speed: 98.83%
4+
#Memory: 13.23%
5+
6+
# Definition for a binary tree node.
7+
# class TreeNode:
8+
# def __init__(self, val=0, left=None, right=None):
9+
# self.val = val
10+
# self.left = left
11+
# self.right = right
12+
13+
class Solution:
14+
15+
def isSameTree(self, p: TreeNode, q: TreeNode) -> bool:
16+
result = []
17+
self.preorder(p, q, result)
18+
return min(result)
19+
20+
def preorder(self, p, q, result):
21+
if not p and not q:
22+
result.append(True)
23+
return
24+
if not p or not q:
25+
result.append(False)
26+
return
27+
if p.val != q.val:
28+
result.append(False)
29+
if p.val == q.val:
30+
result.append(True)
31+
self.preorder(p.left, q.left, result)
32+
self.preorder(p.right, q.right, result)

Diff for: Python/101.SymmetricTree.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#Time Complexity: O(n)
2+
#Space Complexity: O(n)
3+
#Speed: 95.44%
4+
#Memory: 23.72%
5+
6+
class Solution:
7+
def isSymmetric(self, root: TreeNode) -> bool:
8+
queue = [root]
9+
while queue:
10+
length = len(queue)
11+
level = []
12+
while length:
13+
node = queue.pop(0)
14+
level.append(node.val if node else None)
15+
length -= 1
16+
if node:
17+
queue.append(node.left)
18+
queue.append(node.right)
19+
i = 0
20+
j = len(level) - 1
21+
while i <= j:
22+
if level[i] != level[j]:
23+
return False
24+
i += 1
25+
j -= 1
26+
return True

Diff for: Python/150.EvaluateReversePolishNotation.py

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#Time Complexity: O(n)
2+
#Space Complexity: O(1)
3+
#Speed: 84.65%
4+
#Memory: 84.34%
5+
6+
class Solution:
7+
def evalRPN(self, tokens: List[str]) -> int:
8+
stack = []
9+
for token in tokens:
10+
if token.isdigit() or token[1:].isdigit():
11+
stack.append(int(token))
12+
else:
13+
y = stack.pop()
14+
x = stack.pop()
15+
if token == '+': stack.append(x + y)
16+
if token == '-': stack.append(x - y)
17+
if token == '*': stack.append(x * y)
18+
if token == '/': stack.append(int(x / y))
19+
return stack[0]

Diff for: Python/35.SearchInsertPosition.py

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#Time Complexity: O(log(n))
2+
#Space Complexity: O(1)
3+
#Speed: 91.93%
4+
#Memory: 5.97%
5+
6+
class Solution:
7+
def searchInsert(self, nums: List[int], target: int) -> int:
8+
l = 0
9+
r = len(nums)-1
10+
while l <= r:
11+
m = (l+r)//2
12+
if m == r and target > nums[m]:
13+
return m + 1
14+
if target > nums[m]:
15+
l = m + 1
16+
else:
17+
r = m - 1
18+
return m

Diff for: Python/50.Powxn.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#Time Complexity: O(n)
2+
#Space Complexity: O(1)
3+
#Speed: 92.57%
4+
#Memory: 15.17%
5+
6+
class Solution:
7+
def myPow(self, x: float, n: int) -> float:
8+
if not n: return 1
9+
power = self.myPow(x, int(n/2))
10+
if n % 2 == 0:
11+
return power * power
12+
else:
13+
return power * power / x if n < 0 else power * power * x

Diff for: README.md

+13-10
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
7272
| ---- | --------------------------------------------------------------------- | -------------------------------------------------------------------------- | ------ | ------ | ---------- | --- | --------- |
7373
| 0136 | [Single Number](https://leetcode.com/problems/single-number/) | [Java](./Java/single-number.java) <br> [Python](./Python/single-number.py) | _O(n)_ | _O(1)_ | Easy | | Using XOR |
7474
| 0260 | [Single Number III](https://leetcode.com/problems/single-number-iii/) | [Python](./Python/single-number-iii.py) | _O(n)_ | _O(1)_ | Medium | | |
75-
| 0520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py) | _O(n)_ | _O(1)_ | Easy | | |
75+
| 0520 | [Detect Capital Use](https://leetcode.com/problems/detect-capital/) | [Python](./Python/detect-capital.py) | _O(n)_ | _O(1)_ | Easy | | |
7676

7777
<br/>
7878
<div align="right">
@@ -130,7 +130,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
130130
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
131131
| --- | --------------------------------------------------------------------- | ----------------------------------------- | ------ | ------ | ---------- | ----- | ---- |
132132
| 020 | [Valid Parentheses](https://leetcode.com/problems/valid-parentheses/) | [Python](./Python/20_ValidParentheses.py) | _O(n)_ | _O(n)_ | Easy | Stack | |
133-
133+
| 150 | [Evaluate Reverse Polish Notation](https://leetcode.com/problems/evaluate-reverse-polish-notation/) | [Python](./Python/150.EvaluateReversePolishNotation.py) | _O(n)_ | _O(1)_ | Medium | Stack | |
134134
<br/>
135135
<div align="right">
136136
<b><a href="#algorithms">⬆️ Back to Top</a></b>
@@ -142,14 +142,15 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
142142
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
143143
| --- | ------------------------------------------------------------------------------------------------- | ---------------------------------------------------------- | --------- | --------- | ---------- | ---------------------------------------------- | ---- |
144144
| 094 | [Binary Tree Inorder Traversal](https://leetcode.com/problems/binary-tree-inorder-traversal/) | [Java](./Java/binary-tree-inorder-traversal.java) | _O(n)_ | _O(logn)_ | Medium | Binary Tree, Stack, HashTable | |
145-
| 101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | [Java](./Java/symmetric-tree.java) | _O(n)_ | _O(n)_ | Easy | Tree, Breadth-first Search, Depth-first Search | |
145+
| 100 | [Same Tree](https://leetcode.com/problems/same-tree/) | [Python](./Python/100.SymmetricTree.py) | _O(n)_ | _O(n)_ | Easy | Tree, Depth-first Search | |
146+
| 101 | [Symmetric Tree](https://leetcode.com/problems/symmetric-tree/) | [Java](./Java/symmetric-tree.java)<br>[Python](./Python/101.SymmetricTree.py)| _O(n)_ | _O(n)_ | Easy | Tree, Breadth-first Search, Depth-first Search | |
146147
| 144 | [Binary Tree Preorder Traversal](https://leetcode.com/problems/binary-tree-preorder-traversal/) | [Java](./Java/binary-tree-preorder-traversal.java) | _O(n)_ | _O(logn)_ | Medium | Binary Tree, Stack | |
147148
| 145 | [Binary Tree Postorder Traversal](https://leetcode.com/problems/binary-tree-postorder-traversal/) | [Java](./Java/binary-tree-postorder-traversal.java) | _O(n)_ | _O(logn)_ | Hard | Binary Tree, Stack | |
148149
| 103 | [ZigZag Level Order](https://leetcode.com/problems/binary-tree-zigzag-level-order-traversal/) | [JavaScript](./JavaScript/Binary-Tree-ZigZag-Traversal.js) | _O(n)_ | _O(n)_ | Medium | Binary Tree | |
149150
| 129 | [Sum Root to Leaf Numbers](https://leetcode.com/problems/sum-root-to-leaf-numbers/) | [Java](./Java/sum-root-to-leaf-numbers.java) | _O(n)_ | _O(logn)_ | Medium | Binary Tree, Depth First Search | |
150151
| 307 | [Range Sum Query - Mutable](https://leetcode.com/problems/range-sum-query-mutable/) | [Java](./Java/Range-Sum-Query-Mutable.java) | _O(logn)_ | _O(n)_ | Medium | Segment Tree | |
151152
| 919 | [Complete Binary Tree Inserter](https://leetcode.com/problems/complete-binary-tree-inserter/) | [Java](./Java/complete-binary-tree-inserter.java) | _O(n)_ | _O(n)_ | Medium | Tree | |
152-
| 124 | [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/) | [C++](./C++/Binary-Tree-Maximum-Path-Sum.cpp) | _O(n)_ | _O(n)_ | Hard | Tree | |
153+
| 124 | [Binary Tree Maximum Path Sum](https://leetcode.com/problems/binary-tree-maximum-path-sum/) | [C++](./C++/Binary-Tree-Maximum-Path-Sum.cpp) | _O(n)_ | _O(n)_ | Hard | Tree | |
153154
| 1028 | [Recover a Tree From Preorder Traversal](https://leetcode.com/problems/recover-a-tree-from-preorder-traversal/) | [C++](./C++/Recover-a-Tree-From-Preorder-Traversal.cpp) | _O(n)_ | _O(n)_ | Hard | Tree | |
154155

155156

@@ -189,6 +190,7 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
189190

190191
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
191192
| --- | ---------------------------------------------------------------------------------- | ------------------------------------------------------------------------- | ----------------- | ------ | ---------- | ------ | ---- |
193+
| 050 | [Pow(x, n)](https://leetcode.com/problems/powx-n/) | [Python](./Python/50.Powxn.py) | _O(n)_ | _O(1)_ | Medium | Math | |
192194
| 204 | [Count Primes](https://leetcode.com/problems/count-primes) | [C++](./C++/Count-Primes.cpp) | _O(n(log(logn)))_ | _O(n)_ | Easy | Math | |
193195
| 168 | [Excel Sheet Column Title](https://leetcode.com/problems/excel-sheet-column-title) | [C++](./C++/Excel-Sheet-Column-Title.cpp) | _O(n)_ | _O(n)_ | Easy | String | |
194196
| 007 | [Reverse Integer](https://leetcode.com/problems/reverse-integer) | [Java](./Java/reverse-integer.java) <br> [C++](./C++/Reverse-Integer.cpp) | _O(n)_ | _O(n)_ | Easy | Math | |
@@ -239,13 +241,13 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
239241

240242
# Dynamic Programming
241243

242-
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
243-
| --- | --------------------------------------------------------------------- | ----------------------------------------- | ------ | ------ | ---------- | ----- | ---- |
244-
| 416 | [ Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/)| [C++](./C++/Partition-Equal-Subset-Sum.cpp)| _O(n^2)_ | _O(n^2)_ | Medium | DP | |
245-
| 56 | [Wildcard Matching](https://leetcode.com/problems/wildcard-matching/) | [Python](./Python/wildcard-matching.py) | _O(n^2)_ | _O(n^2)_ | Hard | |
244+
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
245+
| --- | --------------------------------------------------------------------- | ----------------------------------------- | ------ | ------ | ---------- | ----- | ---- |
246+
| 416 | [ Partition Equal Subset Sum](https://leetcode.com/problems/partition-equal-subset-sum/)| [C++](./C++/Partition-Equal-Subset-Sum.cpp)| _O(n^2)_ | _O(n^2)_ | Medium | DP | |
247+
| 056 | [Wildcard Matching](https://leetcode.com/problems/wildcard-matching/) | [Python](./Python/wildcard-matching.py) | _O(n^2)_ | _O(n^2)_ | Hard | |
246248
| 343 | [Integer Break](https://leetcode.com/problems/integer-break/) | [C++](./C++/Integer-Break.cpp) | _O(n^2)_ | _O(n)_ | Medium | |
247249
| 139 | [Word Break](https://leetcode.com/problems/word-break/) | [Python](./Python/word-break-1.py) | _O(n^3)_ | _O(n)_ | Medium | DP | |
248-
| 1092 | [Shortest Common Supersequence](https://leetcode.com/problems/shortest-common-supersequence/) | [C++](./C++/Shortest-Common-Supersequence.cpp) | _O(n^2)_ | _O(n^2)_ | Hard | DP | |
250+
| 1092 | [Shortest Common Supersequence](https://leetcode.com/problems/shortest-common-supersequence/) | [C++](./C++/Shortest-Common-Supersequence.cpp) | _O(n^2)_ | _O(n^2)_ | Hard | DP | |
249251

250252

251253
<br/>
@@ -258,8 +260,9 @@ Check out ---> [Sample PR](https://github.com/codedecks-in/LeetCode-Solutions/pu
258260

259261
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
260262
| --- | ----------------------------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------- | --------- | ------ | ---------- | --- | ------------- |
263+
| 035 | [Search Insert Position](https://leetcode.com/problems/search-insert-position/) | [Python](./Python/35.SearchInsertPosition.py) | _O(logn)_ | _O(1)_ | Easy | | Binary Search |
261264
| 278 | [First Bad Version](https://leetcode.com/problems/first-bad-version/) | [Java](./Java/May-LeetCoding-Challenge/Day-1-First-Bad-Version.java) <br> [JavaScript](./JavaScript/First-Bad-Version.js) | _O(logn)_ | _O(1)_ | Easy | | Binary Search |
262-
| 33 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Python](./Python/search-in-rotated-sorted-array.py) | _O(logn)_ | _O(1)_ | Medium | | Binary Search |
265+
| 033 | [Search in Rotated Sorted Array](https://leetcode.com/problems/search-in-rotated-sorted-array/) | [Python](./Python/search-in-rotated-sorted-array.py) | _O(logn)_ | _O(1)_ | Medium | | Binary Search |
263266

264267
<br/>
265268
<div align="right">

0 commit comments

Comments
 (0)