Skip to content

Commit

Permalink
update 18.01
Browse files Browse the repository at this point in the history
  • Loading branch information
YuriSpiridonov committed Jan 18, 2021
1 parent 02c9253 commit d1f2073
Show file tree
Hide file tree
Showing 7 changed files with 279 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Challenges/2021/January-LeetCoding-Challenge.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ None
|1646.|[Get Maximum in Generated Array](https://leetcode.com/problems/get-maximum-in-generated-array/)|[Python](/Easy/1646.GetMaximuminGeneratedArray.py)|Easy|
|215.|[Kth Largest Element in an Array](https://leetcode.com/problems/kth-largest-element-in-an-array/)|[Python](/Medium/215.KthLargestElementinanArray.py)|Medium|
|1641.|[Count Sorted Vowel Strings](https://leetcode.com/problems/count-sorted-vowel-strings/)|[Python](/Medium/1641.CountSortedVowelStrings.py)|Medium|

|1679.|[Max Number of K-Sum Pairs](https://leetcode.com/problems/max-number-of-k-sum-pairs/)|[Python](/Medium/1679.MaxNumberofK-SumPairs.py)|Medium|



Expand Down
2 changes: 1 addition & 1 deletion Challenges/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
- [December LeetCoding Challenge](/Challenges/2020/December-LeetCoding-Challenge.md) - 27/31

2021:
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 15/31
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 16/31
49 changes: 49 additions & 0 deletions Easy/1725.NumberOfRectanglesThatCanFormTheLargestSquare.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
'''
You are given an array rectangles where rectangles[i] = [li, wi]
represents the ith rectangle of length li and width wi.
You can cut the ith rectangle to form a square with a
side length of k if both k <= li and k <= wi. For
example, if you have a rectangle [4,6], you can cut it
to get a square with a side length of at most 4.
Let maxLen be the side length of the largest square you
can obtain from any of the given rectangles.
Return the number of rectangles that can make a square
with a side length of maxLen.
Example:
Input: rectangles = [[5,8],[3,9],[5,12],[16,5]]
Output: 3
Explanation: The largest squares you can get from each
rectangle are of lengths [5,3,5,5].
The largest possible square is of length 5,
and you can get it out of 3 rectangles.
Example:
Input: rectangles = [[2,3],[3,7],[4,3],[3,7]]
Output: 3
Constraints:
-1 <= rectangles.length <= 1000
-rectangles[i].length == 2
-1 <= li, wi <= 10^9
-li != wi
'''
#Difficulty: Easy
#68 / 68 test cases passed.
#Runtime: 176 ms
#Memory Usage: 14.8 MB

#Runtime: 176 ms, faster than 100.00% of Python3 online submissions for Number Of Rectangles That Can Form The Largest Square.
#Memory Usage: 14.8 MB, less than 66.67% of Python3 online submissions for Number Of Rectangles That Can Form The Largest Square.

from collections import defaultdict

class Solution:
def countGoodRectangles(self, rectangles: List[List[int]]) -> int:
squares = defaultdict(int)
for rectangle in rectangles:
squares[min(rectangle)] += 1
return squares[max(squares)]
69 changes: 69 additions & 0 deletions Medium/1019.NextGreaterNodeInLinkedList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
'''
We are given a linked list with head as the first node.
Let's number the nodes in the list: node_1, node_2,
node_3, ... etc.
Each node may have a next larger value: for node_i,
next_larger(node_i) is the node_j.val such that j > i,
node_j.val > node_i.val, and j is the smallest possible
choice. If such a j does not exist, the next larger
value is 0.
Return an array of integers answer, where answer[i] =
next_larger(node_{i+1}).
Note that in the example inputs (not outputs) below,
arrays such as [2,1,5] represent the serialization of
a linked list with a head node value of 2, second node
value of 1, and third node value of 5.
Example:
Input: [2,1,5]
Output: [5,5,0]
Example:
Input: [2,7,4,3,5]
Output: [7,0,5,5,0]
Example:
Input: [1,7,5,1,9,2,5,1]
Output: [7,9,9,9,0,5,0,0]
Note:
1. 1 <= node.val <= 10^9 for each node in the linked l
ist.
2. The given list has length in the range [0, 10000].
'''
#Difficulty: Medium
#76 / 76 test cases passed.
#Runtime: 320 ms
#Memory Usage: 18.6 MB

#Runtime: 320 ms, faster than 66.26% of Python3 online submissions for Next Greater Node In Linked List.
#Memory Usage: 18.6 MB, less than 66.04% of Python3 online submissions for Next Greater Node In Linked List.

# Definition for singly-linked list.
# class ListNode:
# def __init__(self, val=0, next=None):
# self.val = val
# self.next = next

class Solution:
def nextLargerNodes(self, head: ListNode) -> List[int]:
result = []
stack = []
length = 0
vals = []

while head:
result.append(0)
length += 1
vals.append(head.val)
head = head.next

for i in range(length):
if stack:
while stack and vals[stack[-1]] < vals[i]:
result[stack.pop()] = vals[i]
stack.append(i)
return result
74 changes: 74 additions & 0 deletions Medium/1448.CountGoodNodesinBinaryTree(optimized).py
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
'''
Given a binary tree root, a node X in the tree is named
good if in the path from root to X there are no nodes
with a value greater than X.
Return the number of good nodes in the binary tree.
Example:
3
/ \
1 4
/ / \
3 1 5
Input: root = [3,1,4,3,null,1,5]
Output: 4
Explanation: - Nodes in blue are good.
- Root Node (3) is always a good node.
- Node 4 -> (3,4) is the maximum value in
the path starting from the root.
- Node 5 -> (3,4,5) is the maximum value
in the path
- Node 3 -> (3,1,3) is the maximum value
in the path.
Example:
3
/
3
/ \
4 2
Input: root = [3,3,null,4,2]
Output: 3
Explanation: - Node 2 -> (3, 3, 2) is not good, because
"3" is higher than it.
Example:
Input: root = [1]
Output: 1
Explanation: Root is considered as good.
Constraints:
- The number of nodes in the binary tree is in the
range [1, 10^5].
- Each node's value is between [-10^4, 10^4].
'''
#Difficulty: Medium
#63 / 63 test cases passed.
#Runtime: 228 ms
#Memory Usage: 33.5 MB

#Runtime: 228 ms, faster than 92.42% of Python3 online submissions for Count Good Nodes in Binary Tree.
#Memory Usage: 33.5 MB, less than 43.74% of Python3 online submissions for Count Good Nodes in Binary Tree.

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right
class Solution:

def goodNodes(self, root: TreeNode) -> int:
self.count = 0
self.dfs(root, float(-inf))
return self.count

def dfs(self, root, max_val):
if not root:
return
if root.val >= max_val:
self.count += 1
max_val = root.val
self.dfs(root.left, max_val)
self.dfs(root.right, max_val)
79 changes: 79 additions & 0 deletions Medium/1448.CountGoodNodesinBinaryTree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'''
Given a binary tree root, a node X in the tree is named
good if in the path from root to X there are no nodes
with a value greater than X.
Return the number of good nodes in the binary tree.
Example:
3
/ \
1 4
/ / \
3 1 5
Input: root = [3,1,4,3,null,1,5]
Output: 4
Explanation: - Nodes in blue are good.
- Root Node (3) is always a good node.
- Node 4 -> (3,4) is the maximum value in
the path starting from the root.
- Node 5 -> (3,4,5) is the maximum value
in the path
- Node 3 -> (3,1,3) is the maximum value
in the path.
Example:
3
/
3
/ \
4 2
Input: root = [3,3,null,4,2]
Output: 3
Explanation: - Node 2 -> (3, 3, 2) is not good, because
"3" is higher than it.
Example:
Input: root = [1]
Output: 1
Explanation: Root is considered as good.
Constraints:
- The number of nodes in the binary tree is in the
range [1, 10^5].
- Each node's value is between [-10^4, 10^4].
'''
#Difficulty: Medium
#63 / 63 test cases passed.
#Runtime: 304 ms
#Memory Usage: 33 MB

#Runtime: 304 ms, faster than 16.81% of Python3 online submissions for Count Good Nodes in Binary Tree.
#Memory Usage: 33 MB, less than 79.45% of Python3 online submissions for Count Good Nodes in Binary Tree.

# Definition for a binary tree node.
# class TreeNode:
# def __init__(self, val=0, left=None, right=None):
# self.val = val
# self.left = left
# self.right = right

class Solution:

def goodNodes(self, root: TreeNode) -> int:
self.count = 1
self.dfs(root, float(-inf))
return self.count

def dfs(self, root, max_val):
if not root:
return
max_val = max(max_val, root.val)
if root.left:
if root.left.val >= max_val:
self.count += 1
self.dfs(root.left, max_val)
if root.right:
if root.right.val >= max_val:
self.count += 1
self.dfs(root.right, max_val)
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Python solutions of LeetCode problems.
![Language](https://img.shields.io/badge/language-Python-blue.svg)&nbsp;
![Problems Solved](https://img.shields.io/badge/problems%20solved-491%2F1567-orange)&nbsp;
![Problems Solved](https://img.shields.io/badge/problems%20solved-494%2F1571-orange)&nbsp;
[![License](https://img.shields.io/badge/license-MIT-green.svg)](./LICENSE)&nbsp;
![Update](https://img.shields.io/badge/update-Daily-brightgreen.svg)&nbsp;
<br><br>
Expand All @@ -19,7 +19,7 @@ In this repository provided my Python solutions of LeetCode problems.
- [December LeetCoding Challenge](/Challenges/2020/December-LeetCoding-Challenge.md) - 27/31

2021:
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 15/31
- [January LeetCoding Challenge](/Challenges/2021/January-LeetCoding-Challenge.md) - 16/31
<br><br>
## Solutions
*P.S. If you like this, please leave me a star.*
Expand Down Expand Up @@ -355,6 +355,7 @@ In this repository provided my Python solutions of LeetCode problems.
|1009.|[Complement of Base 10 Integer](https://leetcode.com/problems/complement-of-base-10-integer/)|[Python](/Easy/1009.ComplementofBase10Integer.py)|Easy||
|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||
|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|
|1019.|[Next Greater Node In Linked List](https://leetcode.com/problems/next-greater-node-in-linked-list/)|[Python](/Medium/1019.NextGreaterNodeInLinkedList.py)|Medium||
|1021.|[Remove Outermost Parentheses](https://leetcode.com/problems/remove-outermost-parentheses/)|[Python](/Easy/1021.RemoveOutermostParentheses.py)|Easy|Overcode|
|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`|
|1025.|[Divisor Game](https://leetcode.com/problems/divisor-game/)|[Python](/Easy/1025.DivisorGame.py)|Easy||
Expand Down Expand Up @@ -453,6 +454,8 @@ In this repository provided my Python solutions of LeetCode problems.
|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||
|1441.|[Build an Array With Stack Operations](https://leetcode.com/problems/build-an-array-with-stack-operations/)|[Python](/Easy/1441.BuildanArrayWithStackOperations.py)|Easy|`List`|
|1446.|[Consecutive Characters](https://leetcode.com/problems/consecutive-characters/)|[Python](/Easy/1446.ConsecutiveCharacters.py)|Easy|`Two pointers`|
|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`|
|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`|
|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`|
|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`|
|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`|
Expand Down Expand Up @@ -535,6 +538,7 @@ In this repository provided my Python solutions of LeetCode problems.
|1716.|[Calculate Money in Leetcode Bank](https://leetcode.com/problems/calculate-money-in-leetcode-bank/)|[Python](/Easy/1716.CalculateMoneyinLeetcodeBank.py)|Easy||
|1720.|[Decode XORed Array](https://leetcode.com/problems/decode-xored-array/)|[Python](/Easy/1720.DecodeXORedArray.py)|Easy||
|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`|
|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`|

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

0 comments on commit d1f2073

Please sign in to comment.