forked from YuriSpiridonov/LeetCode
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
02c9253
commit d1f2073
Showing
7 changed files
with
279 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
Easy/1725.NumberOfRectanglesThatCanFormTheLargestSquare.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters