Skip to content

Commit d1f2073

Browse files
update 18.01
1 parent 02c9253 commit d1f2073

7 files changed

+279
-4
lines changed

Challenges/2021/January-LeetCoding-Challenge.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ None
3838
|1646.|[Get Maximum in Generated Array](https://leetcode.com/problems/get-maximum-in-generated-array/)|[Python](/Easy/1646.GetMaximuminGeneratedArray.py)|Easy|
3939
|215.|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)|[Python](/Medium/215.KthLargestElementinanArray.py)|Medium|
4040
|1641.|[Count Sorted Vowel Strings](https://leetcode.com/problems/count-sorted-vowel-strings/)|[Python](/Medium/1641.CountSortedVowelStrings.py)|Medium|
41-
41+
|1679.|[Max Number of K-Sum Pairs](https://leetcode.com/problems/max-number-of-k-sum-pairs/)|[Python](/Medium/1679.MaxNumberofK-SumPairs.py)|Medium|
4242

4343

4444

Challenges/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,4 @@
77
- [December LeetCoding Challenge](/Challenges/2020/December-LeetCoding-Challenge.md) - 27/31
88

99
2021:
10-
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 15/31
10+
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 16/31
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
'''
2+
You are given an array rectangles where rectangles[i] = [li, wi]
3+
represents the ith rectangle of length li and width wi.
4+
5+
You can cut the ith rectangle to form a square with a
6+
side length of k if both k <= li and k <= wi. For
7+
example, if you have a rectangle [4,6], you can cut it
8+
to get a square with a side length of at most 4.
9+
10+
Let maxLen be the side length of the largest square you
11+
can obtain from any of the given rectangles.
12+
13+
Return the number of rectangles that can make a square
14+
with a side length of maxLen.
15+
16+
Example:
17+
Input: rectangles = [[5,8],[3,9],[5,12],[16,5]]
18+
Output: 3
19+
Explanation: The largest squares you can get from each
20+
rectangle are of lengths [5,3,5,5].
21+
The largest possible square is of length 5,
22+
and you can get it out of 3 rectangles.
23+
24+
Example:
25+
Input: rectangles = [[2,3],[3,7],[4,3],[3,7]]
26+
Output: 3
27+
28+
Constraints:
29+
-1 <= rectangles.length <= 1000
30+
-rectangles[i].length == 2
31+
-1 <= li, wi <= 10^9
32+
-li != wi
33+
'''
34+
#Difficulty: Easy
35+
#68 / 68 test cases passed.
36+
#Runtime: 176 ms
37+
#Memory Usage: 14.8 MB
38+
39+
#Runtime: 176 ms, faster than 100.00% of Python3 online submissions for Number Of Rectangles That Can Form The Largest Square.
40+
#Memory Usage: 14.8 MB, less than 66.67% of Python3 online submissions for Number Of Rectangles That Can Form The Largest Square.
41+
42+
from collections import defaultdict
43+
44+
class Solution:
45+
def countGoodRectangles(self, rectangles: List[List[int]]) -> int:
46+
squares = defaultdict(int)
47+
for rectangle in rectangles:
48+
squares[min(rectangle)] += 1
49+
return squares[max(squares)]
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
'''
2+
We are given a linked list with head as the first node.
3+
Let's number the nodes in the list: node_1, node_2,
4+
node_3, ... etc.
5+
6+
Each node may have a next larger value: for node_i,
7+
next_larger(node_i) is the node_j.val such that j > i,
8+
node_j.val > node_i.val, and j is the smallest possible
9+
choice. If such a j does not exist, the next larger
10+
value is 0.
11+
12+
Return an array of integers answer, where answer[i] =
13+
next_larger(node_{i+1}).
14+
15+
Note that in the example inputs (not outputs) below,
16+
arrays such as [2,1,5] represent the serialization of
17+
a linked list with a head node value of 2, second node
18+
value of 1, and third node value of 5.
19+
20+
Example:
21+
Input: [2,1,5]
22+
Output: [5,5,0]
23+
24+
Example:
25+
Input: [2,7,4,3,5]
26+
Output: [7,0,5,5,0]
27+
28+
Example:
29+
Input: [1,7,5,1,9,2,5,1]
30+
Output: [7,9,9,9,0,5,0,0]
31+
32+
Note:
33+
1. 1 <= node.val <= 10^9 for each node in the linked l
34+
ist.
35+
2. The given list has length in the range [0, 10000].
36+
'''
37+
#Difficulty: Medium
38+
#76 / 76 test cases passed.
39+
#Runtime: 320 ms
40+
#Memory Usage: 18.6 MB
41+
42+
#Runtime: 320 ms, faster than 66.26% of Python3 online submissions for Next Greater Node In Linked List.
43+
#Memory Usage: 18.6 MB, less than 66.04% of Python3 online submissions for Next Greater Node In Linked List.
44+
45+
# Definition for singly-linked list.
46+
# class ListNode:
47+
# def __init__(self, val=0, next=None):
48+
# self.val = val
49+
# self.next = next
50+
51+
class Solution:
52+
def nextLargerNodes(self, head: ListNode) -> List[int]:
53+
result = []
54+
stack = []
55+
length = 0
56+
vals = []
57+
58+
while head:
59+
result.append(0)
60+
length += 1
61+
vals.append(head.val)
62+
head = head.next
63+
64+
for i in range(length):
65+
if stack:
66+
while stack and vals[stack[-1]] < vals[i]:
67+
result[stack.pop()] = vals[i]
68+
stack.append(i)
69+
return result
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'''
2+
Given a binary tree root, a node X in the tree is named
3+
good if in the path from root to X there are no nodes
4+
with a value greater than X.
5+
6+
Return the number of good nodes in the binary tree.
7+
8+
Example:
9+
3
10+
/ \
11+
1 4
12+
/ / \
13+
3 1 5
14+
Input: root = [3,1,4,3,null,1,5]
15+
Output: 4
16+
Explanation: - Nodes in blue are good.
17+
- Root Node (3) is always a good node.
18+
- Node 4 -> (3,4) is the maximum value in
19+
the path starting from the root.
20+
- Node 5 -> (3,4,5) is the maximum value
21+
in the path
22+
- Node 3 -> (3,1,3) is the maximum value
23+
in the path.
24+
25+
Example:
26+
3
27+
/
28+
3
29+
/ \
30+
4 2
31+
Input: root = [3,3,null,4,2]
32+
Output: 3
33+
Explanation: - Node 2 -> (3, 3, 2) is not good, because
34+
"3" is higher than it.
35+
36+
Example:
37+
Input: root = [1]
38+
Output: 1
39+
Explanation: Root is considered as good.
40+
41+
Constraints:
42+
- The number of nodes in the binary tree is in the
43+
range [1, 10^5].
44+
- Each node's value is between [-10^4, 10^4].
45+
'''
46+
#Difficulty: Medium
47+
#63 / 63 test cases passed.
48+
#Runtime: 228 ms
49+
#Memory Usage: 33.5 MB
50+
51+
#Runtime: 228 ms, faster than 92.42% of Python3 online submissions for Count Good Nodes in Binary Tree.
52+
#Memory Usage: 33.5 MB, less than 43.74% of Python3 online submissions for Count Good Nodes in Binary Tree.
53+
54+
# Definition for a binary tree node.
55+
# class TreeNode:
56+
# def __init__(self, val=0, left=None, right=None):
57+
# self.val = val
58+
# self.left = left
59+
# self.right = right
60+
class Solution:
61+
62+
def goodNodes(self, root: TreeNode) -> int:
63+
self.count = 0
64+
self.dfs(root, float(-inf))
65+
return self.count
66+
67+
def dfs(self, root, max_val):
68+
if not root:
69+
return
70+
if root.val >= max_val:
71+
self.count += 1
72+
max_val = root.val
73+
self.dfs(root.left, max_val)
74+
self.dfs(root.right, max_val)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'''
2+
Given a binary tree root, a node X in the tree is named
3+
good if in the path from root to X there are no nodes
4+
with a value greater than X.
5+
6+
Return the number of good nodes in the binary tree.
7+
8+
Example:
9+
3
10+
/ \
11+
1 4
12+
/ / \
13+
3 1 5
14+
Input: root = [3,1,4,3,null,1,5]
15+
Output: 4
16+
Explanation: - Nodes in blue are good.
17+
- Root Node (3) is always a good node.
18+
- Node 4 -> (3,4) is the maximum value in
19+
the path starting from the root.
20+
- Node 5 -> (3,4,5) is the maximum value
21+
in the path
22+
- Node 3 -> (3,1,3) is the maximum value
23+
in the path.
24+
25+
Example:
26+
3
27+
/
28+
3
29+
/ \
30+
4 2
31+
Input: root = [3,3,null,4,2]
32+
Output: 3
33+
Explanation: - Node 2 -> (3, 3, 2) is not good, because
34+
"3" is higher than it.
35+
36+
Example:
37+
Input: root = [1]
38+
Output: 1
39+
Explanation: Root is considered as good.
40+
41+
Constraints:
42+
- The number of nodes in the binary tree is in the
43+
range [1, 10^5].
44+
- Each node's value is between [-10^4, 10^4].
45+
'''
46+
#Difficulty: Medium
47+
#63 / 63 test cases passed.
48+
#Runtime: 304 ms
49+
#Memory Usage: 33 MB
50+
51+
#Runtime: 304 ms, faster than 16.81% of Python3 online submissions for Count Good Nodes in Binary Tree.
52+
#Memory Usage: 33 MB, less than 79.45% of Python3 online submissions for Count Good Nodes in Binary Tree.
53+
54+
# Definition for a binary tree node.
55+
# class TreeNode:
56+
# def __init__(self, val=0, left=None, right=None):
57+
# self.val = val
58+
# self.left = left
59+
# self.right = right
60+
61+
class Solution:
62+
63+
def goodNodes(self, root: TreeNode) -> int:
64+
self.count = 1
65+
self.dfs(root, float(-inf))
66+
return self.count
67+
68+
def dfs(self, root, max_val):
69+
if not root:
70+
return
71+
max_val = max(max_val, root.val)
72+
if root.left:
73+
if root.left.val >= max_val:
74+
self.count += 1
75+
self.dfs(root.left, max_val)
76+
if root.right:
77+
if root.right.val >= max_val:
78+
self.count += 1
79+
self.dfs(root.right, max_val)

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Python solutions of LeetCode problems.
22
![Language](https://img.shields.io/badge/language-Python-blue.svg)&nbsp;
3-
![Problems Solved](https://img.shields.io/badge/problems%20solved-491%2F1567-orange)&nbsp;
3+
![Problems Solved](https://img.shields.io/badge/problems%20solved-494%2F1571-orange)&nbsp;
44
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)&nbsp;
55
![Update](https://img.shields.io/badge/update-Daily-brightgreen.svg)&nbsp;
66
<br><br>
@@ -19,7 +19,7 @@ In this repository provided my Python solutions of LeetCode problems.
1919
- [December LeetCoding Challenge](/Challenges/2020/December-LeetCoding-Challenge.md) - 27/31
2020

2121
2021:
22-
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 15/31
22+
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 16/31
2323
<br><br>
2424
## Solutions
2525
*P.S. If you like this, please leave me a star.*
@@ -355,6 +355,7 @@ In this repository provided my Python solutions of LeetCode problems.
355355
|1009.|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/)|[Python](/Easy/1009.ComplementofBase10Integer.py)|Easy||
356356
|1010.|[Pairs of Songs With Total Durations Divisible by 60](https://leetcode.com/problems/pairs-of-songs-with-total-durations-divisible-by-60/)|[Python](/Medium/1010.PairsofSongsWithTotalDurationsDivisibleby60.py)|Medium||
357357
|1015.|[Smallest Integer Divisible by K](https://leetcode.com/problems/smallest-integer-divisible-by-k/)|[Python](/Medium/1015.SmallestIntegerDivisiblebyK.py)|Medium|counting lenght, Brute Force|
358+
|1019.|[Next Greater Node In Linked List](https://leetcode.com/problems/next-greater-node-in-linked-list/)|[Python](/Medium/1019.NextGreaterNodeInLinkedList.py)|Medium||
358359
|1021.|[Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses/)|[Python](/Easy/1021.RemoveOutermostParentheses.py)|Easy|Overcode|
359360
|1022.|[Sum of Root To Leaf Binary Numbers](https://leetcode.com/problems/sum-of-root-to-leaf-binary-numbers/)|[Python](/Easy/1022.SumofRootToLeafBinaryNumbers.py)|Easy|`DFS`, `Recursion`|
360361
|1025.|[Divisor Game](https://leetcode.com/problems/divisor-game/)|[Python](/Easy/1025.DivisorGame.py)|Easy||
@@ -453,6 +454,8 @@ In this repository provided my Python solutions of LeetCode problems.
453454
|1437.|[Check If All 1's Are at Least Length K Places Away](https://leetcode.com/problems/check-if-all-1s-are-at-least-length-k-places-away/)|[Python](/Medium/1437.CheckIfAll1sAreatLeastLengthKPlacesAway.py)|Medium||
454455
|1441.|[Build an Array With Stack Operations](https://leetcode.com/problems/build-an-array-with-stack-operations/)|[Python](/Easy/1441.BuildanArrayWithStackOperations.py)|Easy|`List`|
455456
|1446.|[Consecutive Characters](https://leetcode.com/problems/consecutive-characters/)|[Python](/Easy/1446.ConsecutiveCharacters.py)|Easy|`Two pointers`|
457+
|1448.|[Count Good Nodes in Binary Tree](https://leetcode.com/problems/count-good-nodes-in-binary-tree/)|[Python](/Medium/1448.CountGoodNodesinBinaryTree.py)|Medium|`DFS`, `Binary Tree`|
458+
|1448.|[Count Good Nodes in Binary Tree](https://leetcode.com/problems/count-good-nodes-in-binary-tree/)|[Python](/Medium/1448.CountGoodNodesinBinaryTree(optimized).py)|Medium|optimized `DFS`, `Binary Tree`|
456459
|1450.|[Number of Students Doing Homework at a Given Time](https://leetcode.com/problems/number-of-students-doing-homework-at-a-given-time/)|[Python](/Easy/1450.NumberofStudentsDoingHomeworkataGivenTime.py)|Easy|`Enumerate`|
457460
|1455.|[Check If a Word Occurs As a Prefix of Any Word in a Sentence](https://leetcode.com/problems/check-if-a-word-occurs-as-a-prefix-of-any-word-in-a-sentence/)|[Python](/Easy/1455.CheckIfaWordOccursAsaPrefixofAnyWordinaSentence.py)|Easy|`Enumerate`|
458461
|1457.|[Pseudo-Palindromic Paths in a Binary Tree](https://leetcode.com/problems/pseudo-palindromic-paths-in-a-binary-tree/)|[Python](/Medium/1457.Pseudo-PalindromicPathsinaBinaryTree.py)|Medium|`DFS`, `recursion`|
@@ -535,6 +538,7 @@ In this repository provided my Python solutions of LeetCode problems.
535538
|1716.|[Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank/)|[Python](/Easy/1716.CalculateMoneyinLeetcodeBank.py)|Easy||
536539
|1720.|[Decode XORed Array](https://leetcode.com/problems/decode-xored-array/)|[Python](/Easy/1720.DecodeXORedArray.py)|Easy||
537540
|1721.|[Swapping Nodes in a Linked List](https://leetcode.com/problems/swapping-nodes-in-a-linked-list/)|[Python](/Medium/1721.SwappingNodesinaLinkedList.py)|Medium|`Linked List`, `Queue`|
541+
|1725.|[Number Of Rectangles That Can Form The Largest Square](https://leetcode.com/problems/number-of-rectangles-that-can-form-the-largest-square/)|[Python](/Easy/1725.NumberOfRectanglesThatCanFormTheLargestSquare.py)|Easy|`collections.defaultdict`|
538542

539543
<div align="right">
540544
<b><a href="#python-solutions-of-leetcode-problems">Back to Top</a></b>

0 commit comments

Comments
 (0)