Try to solve problems in JavaScript, Python, and Java.
This is TIL(memo of key learnings) for solving problems in these languages,
but mostly python.

241205:
1. validate-binary-search-tree
2. contains-duplicate
3. valid-palindrome
4. top-k-frequent-elements
validate-binary-search-tree
An empty node (null node) in a Binary Search Tree (BST) is valid BST.
Because a BST's definition states that for any node, all values in the left subtree must be smaller, and all values in the right subtree must be larger.
An empty node doesn't violate this condition, as there are no values to compare.
if rootNode is None:
return True # empty node is valid BST
self in Python?validate-binary-search-tree:
In Python, self is used in instance methods of a class to refer to the current instance of the class.
It uses to access the instance's attributes and methods!
It's required as the first parameter in instance methods and is how the class instance is referenced within those methods.
If you declare a method in a class, you need to use self as the first parameter.
Long for declaring infinity in Java?validate-binary-search-tree:
github link
In Java, you can use Long, Double, Float, and Integer for declaring infinity.
| Type | Constant | Range/Precision |
|---|---|---|
| Long | Long.MAX_VALUE | 64-bit signed integer, max: 9223372036854775807 |
| Double | Double.POSITIVE_INFINITY | 64-bit floating point, positive/negative infinity |
| Float | Float.POSITIVE_INFINITY | 32-bit floating point, positive/negative infinity |
| Integer | Integer.MAX_VALUE | 32-bit signed integer, max: 2147483647 |
π Choose the appropriate type depends on your specific use case.
validate-binary-search-tree: github link
There was a test case that failed because of precision loss when passing a long value to a function expecting a float.
When you pass a long value to a function expecting a float, precision loss can occur.
public boolean checkBST(TreeNode node, float min, float max) {...} // I passed a long value to this function!
This happens because floating-point numbers have limited precision, especially when representing large integers, leading to potential inaccuracies in the comparison.
And passing a long value makes the comparison inaccurate.
π So, if there was a test case that handling a long value keep failing?
Do check if you passed a expected type to the function.
is)valid-palindrome: Github Comment Link
In Python, is checks for object identity, not equality.
To compare a string with an empty space (" "), you should use the equality operator (==) rather than is.
if s == " ": # π’ use this.
if s is " ": # π΄ don't use this.
valid-palindrome: Github Comment Link
isalnum(): https://www.w3schools.com/python/ref_string_isalnum.asp
isalnum() because of regex overhead.# Code A
def isPalindrome(self, s: str) -> bool:
if s == " ":
return True
reg = "[^a-z0-9]"
converted_s = re.sub(reg, "", s.lower())
return converted_s == converted_s[::-1]
isalnum()isalnum() method.def isPalindrome(self, s: str) -> bool:
if s == " ":
return True
s = s.lower()
converted_s = ''.join(c for c in s if c.isalnum())
# Convert the string to lowercase and keep only alphanumeric characters
# .isalnum(): check if the character is alphanumeric.
return converted_s == converted_s[::-1]
Great insights! It's intriguing how BST empty nodes and Java data type accuracy are related. Knowing when and how to utilize self in Python and the accuracy loss when passing data types helps write more robust programs. Your clarifications are much appreciated! Geometry Dash