Skip to content

Commit

Permalink
Merge pull request #11 from 149ps/LC-problems
Browse files Browse the repository at this point in the history
New LC Problem Solutions
  • Loading branch information
149ps authored May 16, 2024
2 parents db43d79 + 3b94216 commit 0cd7dde
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 6 deletions.
44 changes: 44 additions & 0 deletions 165. Compare Version Numbers/165. Compare Version Numbers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
"""
Given two version numbers, version1 and version2, compare them.
Version numbers consist of one or more revisions joined by a dot '.'. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character. Revisions are 0-indexed from left to right, with the leftmost revision being revision 0, the next revision being revision 1, and so on. For example 2.5.33 and 0.1 are valid version numbers.
To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.
Return the following:
If version1 < version2, return -1.
If version1 > version2, return 1.
Otherwise, return 0.
Example 1:
Input: version1 = "1.01", version2 = "1.001"
Output: 0
Explanation: Ignoring leading zeroes, both "01" and "001" represent the same integer "1".
Example 2:
Input: version1 = "1.0", version2 = "1.0.0"
Output: 0
Explanation: version1 does not specify revision 2, which means it is treated as "0".
Example 3:
Input: version1 = "0.1", version2 = "1.1"
Output: -1
Explanation: version1's revision 0 is "0", while version2's revision 0 is "1". 0 < 1, so version1 < version2.
"""
class Solution:
def compareVersion(self, version1: str, version2: str) -> int:
version1_list = version1.split(".")
version2_list = version2.split(".")
for str1, str2 in itertools.zip_longest(version1_list, version2_list):
num1 = int(str1) if str1 else 0
num2 = int(str2) if str2 else 0
if num1 > num2:
return 1
elif num1 < num2:
return -1
else:
continue
return 0
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"""
You are given the root of a full binary tree with the following properties:
Leaf nodes have either the value 0 or 1, where 0 represents False and 1 represents True.
Non-leaf nodes have either the value 2 or 3, where 2 represents the boolean OR and 3 represents the boolean AND.
The evaluation of a node is as follows:
If the node is a leaf node, the evaluation is the value of the node, i.e. True or False.
Otherwise, evaluate the node's two children and apply the boolean operation of its value with the children's evaluations.
Return the boolean result of evaluating the root node.
A full binary tree is a binary tree where each node has either 0 or 2 children.
A leaf node is a node that has zero children.
Example 1:
Input: root = [2,1,3,null,null,0,1]
Output: true
Explanation: The above diagram illustrates the evaluation process.
The AND node evaluates to False AND True = False.
The OR node evaluates to True OR False = True.
The root node evaluates to True, so we return true.
Example 2:
Input: root = [0]
Output: false
Explanation: The root node is a leaf node and it evaluates to false, so we return false.
Constraints:
The number of nodes in the tree is in the range [1, 1000].
0 <= Node.val <= 3
Every node has either 0 or 2 children.
Leaf nodes have a value of 0 or 1.
Non-leaf nodes have a value of 2 or 3.
"""


# 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 evaluateTree(self, root: Optional[TreeNode]) -> bool:
if not root.left and not root.right:
return 0 if root.val == 0 else 1
left = self.evaluateTree(root.left)
right = self.evaluateTree(root.right)
return left | right if root.val == 2 else left & right


12 changes: 6 additions & 6 deletions 791. Custom Sort String/791. Custom Sort String.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@
class Solution:
def customSortString(self, order: str, s: str) -> str:
hmap, count = {}, collections.Counter(s)
for i,char in enumerate(order):
for i, char in enumerate(order):
hmap[char] = i
j = 0
result,temp = "",""
for key,val in hmap.items():
result, temp = "", ""
for key, val in hmap.items():
if key in count:
result += count[key] * key
for k,v in count.items():
for k, v in count.items():
if k not in hmap:
temp += k*v
return result + temp
temp += k * v
return result + temp

0 comments on commit 0cd7dde

Please sign in to comment.