title
stringlengths
1
100
titleSlug
stringlengths
3
77
Java
int64
0
1
Python3
int64
1
1
content
stringlengths
28
44.4k
voteCount
int64
0
3.67k
question_content
stringlengths
65
5k
question_hints
stringclasses
970 values
[Python3] Divide and Conquer: Recursion + Memoization
different-ways-to-add-parentheses
0
1
```\nclass Solution(object):\n def diffWaysToCompute(self, s, memo=dict()):\n if s in memo:\n return memo[s]\n if s.isdigit(): # base case\n return [int(s)]\n calculate = {\'*\': lambda x, y: x * y,\n \'+\': lambda x, y: x + y,\n \'-\...
6
Given a string `expression` of numbers and operators, return _all possible results from computing all the different possible ways to group numbers and operators_. You may return the answer in **any order**. The test cases are generated such that the output values fit in a 32-bit integer and the number of different res...
null
Easy Solution Ever with Comment
different-ways-to-add-parentheses
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
4
Given a string `expression` of numbers and operators, return _all possible results from computing all the different possible ways to group numbers and operators_. You may return the answer in **any order**. The test cases are generated such that the output values fit in a 32-bit integer and the number of different res...
null
Detailed Python solution with comments.
different-ways-to-add-parentheses
0
1
```\nclass Solution(object):\n def diffWaysToCompute(self, expression):\n \n res=[]\n def fn(s,memo={}):\n res=[]\n if s in memo:\n return memo[s]\n if s.isdigit():\n return [int(s)] #if string containsonly numbers return that\n ...
1
Given a string `expression` of numbers and operators, return _all possible results from computing all the different possible ways to group numbers and operators_. You may return the answer in **any order**. The test cases are generated such that the output values fit in a 32-bit integer and the number of different res...
null
67% Tc and 55% Sc easy python solution
different-ways-to-add-parentheses
0
1
```\ndef diffWaysToCompute(self, ex: str) -> List[int]:\n\tcurr = ""\n\tarr = []\n\tfor i in ex:\n\t\tif(i in ["+","-","*"]):\n\t\t\tarr.append(curr)\n\t\t\tarr.append(i)\n\t\t\tcurr = ""\n\t\telse: curr += i\n\tarr.append(curr)\n\t@lru_cache(None)\n\tdef solve(i, j):\n\t\tif(i == j): return [int(arr[i])]\n\t\tif((i, j...
1
Given a string `expression` of numbers and operators, return _all possible results from computing all the different possible ways to group numbers and operators_. You may return the answer in **any order**. The test cases are generated such that the output values fit in a 32-bit integer and the number of different res...
null
Python | Recursive | Concise Solution
different-ways-to-add-parentheses
0
1
```\nclass Solution:\n def diffWaysToCompute(self, expression: str) -> List[int]:\n ops = {\n "+": lambda x, y : x + y,\n "-": lambda x, y : x - y,\n "*": lambda x, y : x * y\n }\n res = []\n for x, char in enumerate(expression):\n if char in op...
4
Given a string `expression` of numbers and operators, return _all possible results from computing all the different possible ways to group numbers and operators_. You may return the answer in **any order**. The test cases are generated such that the output values fit in a 32-bit integer and the number of different res...
null
[Python] Memoization Solution
different-ways-to-add-parentheses
0
1
```\nclass Solution:\n def diffWaysToCompute(self, expression: str) -> List[int]:\n \n def clac(a, b, operator):\n if operator == "+": return a + b\n if operator == "-": return a - b\n if operator == "*": return a * b\n \n memo = {} \n de...
1
Given a string `expression` of numbers and operators, return _all possible results from computing all the different possible ways to group numbers and operators_. You may return the answer in **any order**. The test cases are generated such that the output values fit in a 32-bit integer and the number of different res...
null
【Video】Give me 5 minutes - 4 solutions - How we think about a solution
valid-anagram
1
1
# Intuition\nCount all characters in each string\n\n---\n\n# Solution Video\n\nhttps://youtu.be/UR9geRGBvhk\n\n\u25A0 Timeline of the video\n\n`0:03` Explain Solution 1\n`2:52` Coding of Solution 1\n`4:38` Time Complexity and Space Complexity of Solution 1\n`5:04` Step by step algorithm of my solution code of Solution ...
42
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
💯Faster✅💯 Lesser✅4 Methods🔥Sorting🔥Hash Table🔥Character Array🔥Counter🔥Python🐍Java☕C++✅C📈
valid-anagram
1
1
# \uD83D\uDE80 Hi, I\'m [Mohammed Raziullah Ansari](https://leetcode.com/Mohammed_Raziullah_Ansari/), and I\'m excited to share 4 ways to solve this question with detailed explanation of each approach:\n\n# Problem Explaination: \nThe `Valid Anagram` problem typically asks us to determine whether two given strings are ...
35
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
Frequency Count vs 1-line||0ms Beats 100%
valid-anagram
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nFrequency Count. No hash\n# Approach\n<!-- Describe your approach to solving the problem. -->\ns is anagram of t $\\iff$ `freq(s)==freq(t)`\nIn other words, s is a permutation of t.\n \nC/C++/Python codes are implemented. The revised C co...
12
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
✅3 Method's 🤯 || C++ || JAVA || PYTHON || Beginner Friendly🔥🔥🔥
valid-anagram
1
1
\n# Intuition:\n\nThe Intuition is to determine if two strings are anagrams, compare the characters in both strings and check if they have the same characters but in a different order. By tracking the count of each character, if the counts match for all characters, the strings are anagrams; otherwise, they are not.\n\n...
906
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
3 Different Approaches | 700ms to 0ms 🔥| Beats 100% in both Time and Space 😎
valid-anagram
1
1
# Intuition\nThe given problem is to determine whether two strings, `s` and `t`, are anagrams of each other or not. An anagram is a word or phrase formed by rearranging the letters of another word or phrase. The code uses an array to keep track of the frequency of each letter in both strings and then checks if the freq...
5
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
🚀🚀🚀THREE LiNES OF CODE🚀🚀🚀by PRODONiK (Java, C++, Python, Go, JS, Ruby, C#)
valid-anagram
1
1
# Intuition\nThe intuition behind this solution is to determine if two strings are anagrams by comparing their sorted representations. Anagrams are words or phrases formed by rearranging the letters of another, and sorting the characters in each string allows us to easily check if they contain the same set of character...
5
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
Rust/C++/Python linear time. Count number of each character. Python 1 line
valid-anagram
0
1
# Intuition\nAll you need is to keep track how many times each character was seen in both strings. Then the values should be equal.\n\nYou can also do short-circuit to stop if lenghts are not equal. In python you can do everything in one line with counter.\n\n# Complexity\n- Time complexity: $O(n)$\n- Space complexity:...
2
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
Leetcode daily problems | C++ | PYTHON | 100% fast 🔥 solutions ✔| 4 processes
valid-anagram
0
1
# Intuition\nWhen we approach the anagram check problem, a few initial thoughts come to mind. The answer of an anagram lies in the rearrangement of characters, meaning that two anagrams will share the same set of characters with the same frequencies. Here are some intuitive strategies for solving this problem:\n\n***So...
2
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
✅☑[C++/C/Java/Python/JavaScript] || 3 Approaches || Beats 100% || EXPLAINED🔥
valid-anagram
1
1
# PLEASE UPVOTE IF IT HELPED\n\n---\n\n\n# Approaches\n*(Also explained in the code)*\n\n#### ***Approach 1(Sort)***\n1. **Sorting Strings:**\n\n - The code sorts both input strings `s` and `t` using the `sort` function. Sorting rearranges the characters in each string in ascending order.\n1. **Comparison:**\n\n ...
2
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
solution of Valid anagram
valid-anagram
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n<!-- Describe your approach to solving the problem. -->\nBasically An anagram is a word or phrase that is formed by rearranging the letters of another word or phrase. For example, "triangle" is an anagram of "integral". \n\nfirst convert ...
1
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
Easy Python. Beats 100% Time and Space. 😏🔥
valid-anagram
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
Easiest Solution || Python
valid-anagram
0
1
# Intuition\nA very simple way of solving this will be to check the length of the 2 strings and then checking if the frequency of letters in both the strings are equal.\n\n\n# Code\n```\nclass Solution:\n def isAnagram(self, s: str, t: str) -> bool:\n if len(s) == len(t):\n counts={}\n c...
1
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
Two approaches || Simple solution with HashTable in Python3
valid-anagram
0
1
# Intuition\nHere we have two strings, `s` and `t` respectively.\nOur goal is to check, if `t` is **anagram** of `s`.\n\n# Approach\nThere\'re **three things to notice**:\n- if strings have **different** lengths, **they\'re not equal**\n- you can sort them in any direction, and compare (**O(N log N)**)\n- you can use *...
1
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
Python3 Solution
valid-anagram
0
1
\n```\n"""class Solution:\n def isAnagram(self, s: str, t: str) -> bool:\n \n if len(s) !=len(t):\n return False \n countS,countT={},{}\n \n for i in range(len(s)):\n countS[s[i]]=1+countS.get(s[i],0)\n countT[t[i]]=1+countT.get(t[i],0) ...
1
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
Valid Anagram - Python One line, easy to understand
valid-anagram
0
1
# Approach\nSort the strings using sorted(), and compare them. If they are same after the sort, t is the valid anagram of the string s.\n**Example:**\ns = anagram t = nagaram\nsorted s = aaagmnr \nsorted t = aaagmnr \n\n\n# Code\n```\nclass Solution:\n def isAnagram(self, s: str, t: str) -> bool:\n return s...
1
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
1 line solutiuon
valid-anagram
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
✅✅✅ Very easy python3 solution.
valid-anagram
0
1
\n\n# Code\n```\nclass Solution:\n def isAnagram(self, s: str, t: str) -> bool:\n if sorted(s) == sorted(t):\n return True\n else:\n return False \n\n \n```
1
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
Python3 Best 100%
valid-anagram
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
[VIDEO] Visualization of Using Hash Tables Frequencies
valid-anagram
0
1
https://youtu.be/vYNRXZ4GXPg\n\nThe simplest approach would be to sort each string and compare them to each other. If they are the same, then they are anagrams. The drawback to this method is that since the cost of sorting can be no less than O(n log n), if the length of the longer string is n, then this strategy ru...
4
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
Very Easy || 100% || Fully Explained || C++, Java, Python, JavaScript, Python3
valid-anagram
1
1
# **Java Solution:**\n```\n// If two strings are anagrams then the frequency of every char in both of the strings are same.\nclass Solution {\n public boolean isAnagram(String s, String t) {\n // Base case: if the two strings are empty...\n if(s == null || t == null) return false;\n // In case o...
147
Given two strings `s` and `t`, return `true` _if_ `t` _is an anagram of_ `s`_, and_ `false` _otherwise_. An **Anagram** is a word or phrase formed by rearranging the letters of a different word or phrase, typically using all the original letters exactly once. **Example 1:** **Input:** s = "anagram", t = "nagaram" **...
null
Python Easy Solution || 100% || Recursion || Beats 98% ||
binary-tree-paths
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: $$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(n)$$\n<!-- Add your space complexity here...
2
Given the `root` of a binary tree, return _all root-to-leaf paths in **any order**_. A **leaf** is a node with no children. **Example 1:** **Input:** root = \[1,2,3,null,5\] **Output:** \[ "1->2->5 ", "1->3 "\] **Example 2:** **Input:** root = \[1\] **Output:** \[ "1 "\] **Constraints:** * The number of nodes ...
null
Python3 using queue (Level-Order Traversal)
binary-tree-paths
0
1
```\nclass Node:\n def __init__(self, value):\n self.value = value\n self.next = None\n \nclass LinkedList:\n def __init__(self):\n self.head = None\n self.tail = None\n self.length = 0\n\n def __len__(self):\n return self.length\n\n def add(self, value):\n ...
2
Given the `root` of a binary tree, return _all root-to-leaf paths in **any order**_. A **leaf** is a node with no children. **Example 1:** **Input:** root = \[1,2,3,null,5\] **Output:** \[ "1->2->5 ", "1->3 "\] **Example 2:** **Input:** root = \[1\] **Output:** \[ "1 "\] **Constraints:** * The number of nodes ...
null
😎Brute Force to Efficient Method🤩 | Java | C++ | Python | JavaScript | C#
binary-tree-paths
1
1
\n# 1st Method :- Brute Force\n<!-- Describe your first thoughts on how to solve this problem. -->\n> This brute force solution would be to perform a depth-first traversal of the binary tree and, at each node, generate all possible paths from the root to that node. We can use a recursive approach to traverse the tree a...
12
Given the `root` of a binary tree, return _all root-to-leaf paths in **any order**_. A **leaf** is a node with no children. **Example 1:** **Input:** root = \[1,2,3,null,5\] **Output:** \[ "1->2->5 ", "1->3 "\] **Example 2:** **Input:** root = \[1\] **Output:** \[ "1 "\] **Constraints:** * The number of nodes ...
null
Simple DFS in Python
binary-tree-paths
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\nTo find all the root-to-leaf paths, we can use a depth-first search (DFS) algorithm. We\'ll implement a recursive helper function `solve()` that takes two parameters: the current node and the current path string. The `solve()` function will traverse t...
8
Given the `root` of a binary tree, return _all root-to-leaf paths in **any order**_. A **leaf** is a node with no children. **Example 1:** **Input:** root = \[1,2,3,null,5\] **Output:** \[ "1->2->5 ", "1->3 "\] **Example 2:** **Input:** root = \[1\] **Output:** \[ "1 "\] **Constraints:** * The number of nodes ...
null
[Python 3] DFS + path backtracking
binary-tree-paths
0
1
# Code\n```python3 []\nclass Solution:\n def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:\n paths = []\n def dfs(node, path):\n if not node: return\n path.append(node.val)\n if not (node.left or node.right): # leaf\n paths.append(path.cop...
7
Given the `root` of a binary tree, return _all root-to-leaf paths in **any order**_. A **leaf** is a node with no children. **Example 1:** **Input:** root = \[1,2,3,null,5\] **Output:** \[ "1->2->5 ", "1->3 "\] **Example 2:** **Input:** root = \[1\] **Output:** \[ "1 "\] **Constraints:** * The number of nodes ...
null
257: Time 96.78%, Solution with step by step explanation
binary-tree-paths
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n Here\'s a step-by-step explanation:\n```\nclass Solution:\n def binaryTreePaths(self, root: Optional[TreeNode]) -> List[str]:\n ans = []\n```\nThe solution defines a class Solution with a method binaryTreePaths. It take...
8
Given the `root` of a binary tree, return _all root-to-leaf paths in **any order**_. A **leaf** is a node with no children. **Example 1:** **Input:** root = \[1,2,3,null,5\] **Output:** \[ "1->2->5 ", "1->3 "\] **Example 2:** **Input:** root = \[1\] **Output:** \[ "1 "\] **Constraints:** * The number of nodes ...
null
Binbin's another brilliant idea
binary-tree-paths
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
3
Given the `root` of a binary tree, return _all root-to-leaf paths in **any order**_. A **leaf** is a node with no children. **Example 1:** **Input:** root = \[1,2,3,null,5\] **Output:** \[ "1->2->5 ", "1->3 "\] **Example 2:** **Input:** root = \[1\] **Output:** \[ "1 "\] **Constraints:** * The number of nodes ...
null
Recursive solution by Leo
binary-tree-paths
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $...
3
Given the `root` of a binary tree, return _all root-to-leaf paths in **any order**_. A **leaf** is a node with no children. **Example 1:** **Input:** root = \[1,2,3,null,5\] **Output:** \[ "1->2->5 ", "1->3 "\] **Example 2:** **Input:** root = \[1\] **Output:** \[ "1 "\] **Constraints:** * The number of nodes ...
null
✔️ [Python3] =͟͟͞͞( ✌°∀° )☛ ITERATIVE, Explained
add-digits
0
1
**UPVOTE if you like (\uD83C\uDF38\u25E0\u203F\u25E0), If you have any question, feel free to ask.**\n\nThe simplest way to solve this problem is using bruteforce solution. We convert `num` into digits, sum them together and then repeate the process until the `num` becomes less than 10.\n\nTime: **O(ceil(log10(n))^2)**...
54
Given an integer `num`, repeatedly add all its digits until the result has only one digit, and return it. **Example 1:** **Input:** num = 38 **Output:** 2 **Explanation:** The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it. **Example 2:** **Input:** num = 0 **Output:** 0 *...
A naive implementation of the above process is trivial. Could you come up with other methods? What are all the possible results? How do they occur, periodically or randomly? You may find this Wikipedia article useful.
"🔢✨ Mastering the Digital Root: Quick Trick to Summarize Digits Using 3 lines of code! 💡🧮"| O(1)
add-digits
0
1
# Intuition\nWhen solving this problem, my first intuition was to iterate through the digits of the number and sum them up until i get a single-digit result. However, upon further analysis and observation, I noticed patterns in the digital roots of numbers and come up with the congruence formula as an elegant way to ca...
2
Given an integer `num`, repeatedly add all its digits until the result has only one digit, and return it. **Example 1:** **Input:** num = 38 **Output:** 2 **Explanation:** The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it. **Example 2:** **Input:** num = 0 **Output:** 0 *...
A naive implementation of the above process is trivial. Could you come up with other methods? What are all the possible results? How do they occur, periodically or randomly? You may find this Wikipedia article useful.
[Java/C++/Python] Efficient Digital Root Calculation with Math Insights Explained
add-digits
1
1
# EXPLANATION\n1. **Divisibility by 9 Rule**:\n - Any number whose digits add up to 9 is divisible by 9.\n - Examples include 18, 27, 36, 45, 54, 63, 72, 81, 90, etc.\n2. **Digital Root for Divisible by 9 Numbers:**\n - The digital root (sum of digits until a single digit is obtained) for any number divisible ...
1
Given an integer `num`, repeatedly add all its digits until the result has only one digit, and return it. **Example 1:** **Input:** num = 38 **Output:** 2 **Explanation:** The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it. **Example 2:** **Input:** num = 0 **Output:** 0 *...
A naive implementation of the above process is trivial. Could you come up with other methods? What are all the possible results? How do they occur, periodically or randomly? You may find this Wikipedia article useful.
My easy to understand method, Python3 Array Usage
add-digits
0
1
\n# Code\n```\nclass Solution:\n def addDigits(self, num: int) -> int:\n if len(str(num)) == 1:\n return num\n q = [num]\n while len(str(q[-1])) != 1:\n c = 0\n for i in str(q[-1]):\n c += int(i)\n q.append(c)\n return q[-1]\n```
1
Given an integer `num`, repeatedly add all its digits until the result has only one digit, and return it. **Example 1:** **Input:** num = 38 **Output:** 2 **Explanation:** The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it. **Example 2:** **Input:** num = 0 **Output:** 0 *...
A naive implementation of the above process is trivial. Could you come up with other methods? What are all the possible results? How do they occur, periodically or randomly? You may find this Wikipedia article useful.
Recursive Python Solution | Beats 93% Runtime
add-digits
0
1
# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution:\n def addDigits(self, num: int) -> int:\n if num >= 10:\n output = num // 10 + num % 10\n if output < 10:\n return output\n else:\n return self.add...
1
Given an integer `num`, repeatedly add all its digits until the result has only one digit, and return it. **Example 1:** **Input:** num = 38 **Output:** 2 **Explanation:** The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it. **Example 2:** **Input:** num = 0 **Output:** 0 *...
A naive implementation of the above process is trivial. Could you come up with other methods? What are all the possible results? How do they occur, periodically or randomly? You may find this Wikipedia article useful.
Python shortest 1-liner.
add-digits
0
1
# Approach\nTL;DR, Similar to the [Editorial solution](https://leetcode.com/problems/add-digits/editorial/) but condensed into short 1-liner.\n\n# Complexity\n- Time complexity: $$O(1)$$\n\n- Space complexity: $$O(1)$$\n\n# Code\n```python\nclass Solution:\n def addDigits(self, num: int) -> int:\n return mod(...
2
Given an integer `num`, repeatedly add all its digits until the result has only one digit, and return it. **Example 1:** **Input:** num = 38 **Output:** 2 **Explanation:** The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it. **Example 2:** **Input:** num = 0 **Output:** 0 *...
A naive implementation of the above process is trivial. Could you come up with other methods? What are all the possible results? How do they occur, periodically or randomly? You may find this Wikipedia article useful.
[c++ & Python] brute force
add-digits
0
1
\n# Complexity\n- Time complexity: $$O(log10(n))$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: $$O(1)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# C++\n```\nclass Solution {\npublic:\n int addDigits(int n) {\n int sum = 0;\n while(n) {\n su...
1
Given an integer `num`, repeatedly add all its digits until the result has only one digit, and return it. **Example 1:** **Input:** num = 38 **Output:** 2 **Explanation:** The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it. **Example 2:** **Input:** num = 0 **Output:** 0 *...
A naive implementation of the above process is trivial. Could you come up with other methods? What are all the possible results? How do they occur, periodically or randomly? You may find this Wikipedia article useful.
Speed is higher than 95% in Python3
add-digits
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
Given an integer `num`, repeatedly add all its digits until the result has only one digit, and return it. **Example 1:** **Input:** num = 38 **Output:** 2 **Explanation:** The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it. **Example 2:** **Input:** num = 0 **Output:** 0 *...
A naive implementation of the above process is trivial. Could you come up with other methods? What are all the possible results? How do they occur, periodically or randomly? You may find this Wikipedia article useful.
Three Language Python3,Go,Java
add-digits
1
1
# Python Solution\n```\nclass Solution:\n def addDigits(self, num: int) -> int:\n if num<10:\n return num\n if num%9==0:\n return 9\n return num%9\n```\n# Go Solution\n```\nfunc addDigits(num int) int {\n if num<10{\n return num\n }\n if num%9==0{\n r...
4
Given an integer `num`, repeatedly add all its digits until the result has only one digit, and return it. **Example 1:** **Input:** num = 38 **Output:** 2 **Explanation:** The process is 38 --> 3 + 8 --> 11 11 --> 1 + 1 --> 2 Since 2 has only one digit, return it. **Example 2:** **Input:** num = 0 **Output:** 0 *...
A naive implementation of the above process is trivial. Could you come up with other methods? What are all the possible results? How do they occur, periodically or randomly? You may find this Wikipedia article useful.
Python3 Alterative Counter solution One-Liner (Not for interview)
single-number-iii
0
1
\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def singleNumber(self, nums: List[int]) -> List[int]:\n a = []\n for i, j in Counter(nums)...
1
Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in **any order**. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space. ...
null
😎Brute Force to Efficient Method🤩 | Java | C++ | Python | JavaScript
single-number-iii
1
1
Java\nC++\nPython\nJavaScript\n\n# Java\n# Java 1st Method :- Brute force\n1. Initialize an empty set or list to keep track of elements with counts.\n2. Iterate through the given array.\n3. For each element in the array:\n - If it\'s not in the set or list, add it.\n - If it\'s already in the set or list, remove ...
13
Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in **any order**. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space. ...
null
Fastest And Smallest python code
single-number-iii
0
1
# Intuition\nBit Manipulation\n\n# Approach\nExplanation is too long I don\'t want to write it.\n# Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution:\n def singleNumber(self, nums: List[int]) -> List[int]:\n p=0\n q=0\n for i in nums :\n ...
3
Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in **any order**. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space. ...
null
Easy python solution for Beginners
single-number-iii
0
1
# Code\n```\nclass Solution:\n def singleNumber(self, nums: List[int]) -> List[int]:\n alt = []\n for num in nums:\n if num not in alt:\n alt.append(num)\n else:\n alt.remove(num)\n return alt\n```
1
Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in **any order**. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space. ...
null
Python O(1) Space | Easy | Clean Code with explaination | SR
single-number-iii
0
1
**PLEASE DO UPVOTE IF YOU GET THIS**\n```\nimport math\nclass Solution:\n \'\'\'\n say nums = [1,2,1,3,2,5]\n x = XOR(nums) -> 6\n Binary form of 6 is 110\n\n find the first set bit position and divide the array into 2 based on that bit position\n why?\n Becoz 6 is the r...
24
Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in **any order**. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space. ...
null
260: Time 90.93%, Solution with step by step explanation
single-number-iii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe can solve this problem in O(n) time complexity and O(1) space complexity by using bitwise operations.\n\n1. First, we XOR all the elements in the array to find the XOR of the two elements that appear only once. Let\'s cal...
8
Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in **any order**. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space. ...
null
use bit operators
single-number-iii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\nO(n)\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n...
1
Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in **any order**. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space. ...
null
Python Fast using counters
single-number-iii
0
1
```\nclass Solution:\n def singleNumber(self, nums: List[int]) -> List[int]:\n x = Counter(nums)\n return([y for y in x if x[y] == 1])\n```
2
Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in **any order**. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space. ...
null
PYTHON EASY SOLUTION || 100%
single-number-iii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: \n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -...
1
Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in **any order**. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space. ...
null
brute force with dictnory
single-number-iii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:n\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:n\n<!-- Add your space complexity here, e.g. $$O(n)$$ ...
5
Given an integer array `nums`, in which exactly two elements appear only once and all the other elements appear exactly twice. Find the two elements that appear only once. You can return the answer in **any order**. You must write an algorithm that runs in linear runtime complexity and uses only constant extra space. ...
null
Easy solution #Python3
ugly-number
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
I think that it is only harinadh's version but it is actual version of all 🤣🤣
ugly-number
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
0
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
Beats 100% Easy To Understand 0ms
ugly-number
1
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nJust do dry run on any number, you will get the working of this code.\n\n# Complexity\n- Time complexity:\nO(N)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n bool isUgly(int n) {\n if(n <...
3
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
Simple way, top 25% Python Solutions
ugly-number
0
1
\n# Code\n```\nclass Solution:\n def isUgly(self, n: int) -> bool:\n if n == 0:\n return False\n while n != 1:\n if n % 2 == 0:\n n /= 2\n elif n % 3 == 0:\n n /= 3\n elif n % 5 == 0:\n n /= 5\n else:\n ...
2
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
263: Solution with step by step explanation
ugly-number
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThe solution can be done by dividing the given number by 2, 3, or 5 as long as it is divisible, and then checking if the resulting number is 1 (which means it is an ugly number) or not.\n\nThe time complexity of this algorit...
5
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
Python ||Beats 99% ||Simple Math
ugly-number
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
2
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
Ugly Number
ugly-number
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
Logarithmic Solution (Best approach) in PYTHON/PYTHON3
ugly-number
0
1
\n\n# Approach\nThe solution can be done by dividing the given number by 2, 3, or 5 as long as it is divisible, and then checking if the resulting number is 1 (which means it is an ugly number) or not.\n\nThe time complexity of this algorithm is O(log n), since we divide the input number by 2, 3, or 5 repeatedly until ...
10
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
in log(n) time
ugly-number
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:log(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:1\n<!-- Add your space complexity here, e.g. $$O(...
9
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
2 Lines of Code--->Awesome Approach
ugly-number
0
1
# Math formula Approach\n```\nclass Solution:\n def isUgly(self, n: int) -> bool:\n if n<1:\n return False\n multiple=2*3*5\n return (multiple**20)%n==0\n\n //please upvote me it would encourage me alot\n\n\n```\n# Chack with each ugly factor\n```\nclass Solution:\n def isUgly(s...
6
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
Python || Easy || 96.74% Faster || O(log n) Solution
ugly-number
0
1
```\nfrom math import sqrt\nclass Solution:\n def isUgly(self, n: int) -> bool:\n if n<=0:\n return False\n while n%2==0 or n%3==0 or n%5==0:\n if n%2==0:\n n//=2\n elif n%3==0:\n n//=3\n elif n%5==0:\n n//=5\n ...
2
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
Python 93.76% faster | Simplest solution with explanation | Beg to Adv | Math
ugly-number
0
1
```python\nclass Solution:\n def isUgly(self, n: int) -> bool:\n \n prime = [2, 3, 5] # prime factors list provided in question againt which we have to check the provided number. \n \n if n == 0: # as we dont have factors for 0\n return False \n \n for p in prime:...
3
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
Binary Search Solution beats 86.__% time and 96.__% memory
ugly-number
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
✔ Python3 | Faster solution , Basic logic with maths
ugly-number
0
1
```\nclass Solution:\n def isUgly(self, n: int) -> bool:\n if n<=0: \n return False\n for i in [2,3,5]:\n while n%i==0:\n n=n//i\n return n==1\n```
17
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return `true` _if_ `n` _is an **ugly number**_. **Example 1:** **Input:** n = 6 **Output:** true **Explanation:** 6 = 2 \* 3 **Example 2:** **Input:** n = 1 **Output:** true **Explanation:** 1 has n...
null
Dynamic Programming Logic Python3
ugly-number-ii
0
1
\n\n# 1. Dynamic Programming\n```\nclass Solution:\n def nthUglyNumber(self, n: int) -> int:\n list1=[0]*n\n list1[0]=1\n a=b=c=0\n for i in range(1,n):\n list1[i]=min(list1[a]*2,list1[b]*3,list1[c]*5)\n if list1[a]*2==list1[i]:a+=1\n if list1[b]*3==list1[...
27
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return _the_ `nth` _**ugly number**_. **Example 1:** **Input:** n = 10 **Output:** 12 **Explanation:** \[1, 2, 3, 4, 5, 6, 8, 9, 10, 12\] is the sequence of the first 10 ugly numbers. **Example 2:** ...
The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number. The key is how to maintain the order of the ugly numbers. Try a simi...
264: Solution with step by step explanation
ugly-number-ii
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nWe will use a dynamic programming approach to solve this problem. We will start with the first ugly number, which is 1, and then generate all the other ugly numbers. We will use three pointers to keep track of the next multi...
17
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return _the_ `nth` _**ugly number**_. **Example 1:** **Input:** n = 10 **Output:** 12 **Explanation:** \[1, 2, 3, 4, 5, 6, 8, 9, 10, 12\] is the sequence of the first 10 ugly numbers. **Example 2:** ...
The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number. The key is how to maintain the order of the ugly numbers. Try a simi...
Python || 93.80% Faster || Iterative Approach || O(n) Solution
ugly-number-ii
0
1
```\nclass Solution:\n def nthUglyNumber(self, n: int) -> int:\n ans=[1]\n prod_2=prod_3=prod_5=0\n for i in range(1,n):\n a=ans[prod_2]*2\n b=ans[prod_3]*3\n c=ans[prod_5]*5\n m=min(a,b,c)\n ans.append(m)\n if m==a:\n ...
12
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return _the_ `nth` _**ugly number**_. **Example 1:** **Input:** n = 10 **Output:** 12 **Explanation:** \[1, 2, 3, 4, 5, 6, 8, 9, 10, 12\] is the sequence of the first 10 ugly numbers. **Example 2:** ...
The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number. The key is how to maintain the order of the ugly numbers. Try a simi...
[Python] Simple DP 9 Lines
ugly-number-ii
0
1
```\nclass Solution:\n def nthUglyNumber(self, n: int) -> int:\n k = [0] * n\n t1 = t2 = t3 = 0\n k[0] = 1\n for i in range(1,n):\n k[i] = min(k[t1]*2,k[t2]*3,k[t3]*5)\n if(k[i] == k[t1]*2): t1 += 1\n if(k[i] == k[t2]*3): t2 += 1\n if(k[i] == k[...
34
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return _the_ `nth` _**ugly number**_. **Example 1:** **Input:** n = 10 **Output:** 12 **Explanation:** \[1, 2, 3, 4, 5, 6, 8, 9, 10, 12\] is the sequence of the first 10 ugly numbers. **Example 2:** ...
The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number. The key is how to maintain the order of the ugly numbers. Try a simi...
Min Heap || with Dry Run
ugly-number-ii
0
1
\n# Code\n```\nclass Solution:\n def nthUglyNumber(self, n: int) -> int:\n # take a min heap and insert 1 into it as \n #there is no prime factor for 1\n min_heap = [1] \n #take one variable which records no of variable which have been\n # recorded already\n seen = set([1]) ...
3
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return _the_ `nth` _**ugly number**_. **Example 1:** **Input:** n = 10 **Output:** 12 **Explanation:** \[1, 2, 3, 4, 5, 6, 8, 9, 10, 12\] is the sequence of the first 10 ugly numbers. **Example 2:** ...
The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number. The key is how to maintain the order of the ugly numbers. Try a simi...
simple O(n) solution
ugly-number-ii
0
1
# Complexity\n- Time complexity:$$O(n)$$\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:$$O(n)$$\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution:\n def nthUglyNumber(self, n: int) -> int:\n ans = [1]\n i2, i3, i5 = 0, 0, 0\n wh...
2
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return _the_ `nth` _**ugly number**_. **Example 1:** **Input:** n = 10 **Output:** 12 **Explanation:** \[1, 2, 3, 4, 5, 6, 8, 9, 10, 12\] is the sequence of the first 10 ugly numbers. **Example 2:** ...
The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number. The key is how to maintain the order of the ugly numbers. Try a simi...
Python solution using dynamic programming
ugly-number-ii
0
1
```\nclass Solution:\n def nthUglyNumber(self, n: int) -> int:\n dp=[1]\n \n p2,p3,p5=0,0,0\n \n for i in range(n+1):\n t2=dp[p2]*2\n t3=dp[p3]*3\n t5=dp[p5]*5\n \n temp=min(t2,t3,t5)\n \n dp.append(temp)\...
4
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return _the_ `nth` _**ugly number**_. **Example 1:** **Input:** n = 10 **Output:** 12 **Explanation:** \[1, 2, 3, 4, 5, 6, 8, 9, 10, 12\] is the sequence of the first 10 ugly numbers. **Example 2:** ...
The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number. The key is how to maintain the order of the ugly numbers. Try a simi...
Python Heap Simple Solution
ugly-number-ii
0
1
# Intuition\nUse heap to store generated ugly numbers\n\n# Approach\ngenerate more ugly numbers with the min element from the heap, insert them into heap and append the min element to the n-ugly numbers list.(see code for better intuition)\n\n# Complexity\n- Time complexity:\nO(nlog(n))\n\n- Space complexity:\no(n)\n\n...
2
An **ugly number** is a positive integer whose prime factors are limited to `2`, `3`, and `5`. Given an integer `n`, return _the_ `nth` _**ugly number**_. **Example 1:** **Input:** n = 10 **Output:** 12 **Explanation:** \[1, 2, 3, 4, 5, 6, 8, 9, 10, 12\] is the sequence of the first 10 ugly numbers. **Example 2:** ...
The naive approach is to call isUgly for every number until you reach the nth one. Most numbers are not ugly. Try to focus your effort on generating only the ugly ones. An ugly number must be multiplied by either 2, 3, or 5 from a smaller ugly number. The key is how to maintain the order of the ugly numbers. Try a simi...
One liners | multiple solutions |
missing-number
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, return _the only number in the range that is missing from the array._ **Example 1:** **Input:** nums = \[3,0,1\] **Output:** 2 **Explanation:** n = 3 since there are 3 numbers, so all numbers are in the range \[0,3\]. 2 is the missing number...
null
math
missing-number
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
6
Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, return _the only number in the range that is missing from the array._ **Example 1:** **Input:** nums = \[3,0,1\] **Output:** 2 **Explanation:** n = 3 since there are 3 numbers, so all numbers are in the range \[0,3\]. 2 is the missing number...
null
Easy solution sum in Python3
missing-number
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
1
Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, return _the only number in the range that is missing from the array._ **Example 1:** **Input:** nums = \[3,0,1\] **Output:** 2 **Explanation:** n = 3 since there are 3 numbers, so all numbers are in the range \[0,3\]. 2 is the missing number...
null
90% using Math, simple 1 line
missing-number
0
1
\n# Code\n```py\nclass Solution:\n def missingNumber(self, nums: List[int]) -> int:\n return sum(range(len(nums) + 1)) - sum(nums)\n\n```\n# Line by line\n```py\nclass Solution:\n def missingNumber(self, nums: List[int]) -> int:\n # Calculate the expected sum of numbers from 0 to len(nums)\n ...
4
Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, return _the only number in the range that is missing from the array._ **Example 1:** **Input:** nums = \[3,0,1\] **Output:** 2 **Explanation:** n = 3 since there are 3 numbers, so all numbers are in the range \[0,3\]. 2 is the missing number...
null
Easy One Line. Math solution.
missing-number
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n**Use math.**\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1. We know that 1 number is missing, so let\'s assume that the length of the nums must be 1 more. \n2. Using a math formula we can find the sum of all ele...
6
Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, return _the only number in the range that is missing from the array._ **Example 1:** **Input:** nums = \[3,0,1\] **Output:** 2 **Explanation:** n = 3 since there are 3 numbers, so all numbers are in the range \[0,3\]. 2 is the missing number...
null
Easy approach with O(n) and O(1) complexities
missing-number
0
1
# Intuition\n<!-- -->The numbers in array are unique and the range of numbers is between 0 to size of array\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\nSo, a number is missing in range [0,n]\n\nMissing number is : (sum of all numbers [0 to n] ) - (sum of all numbers in array)\n\n\n# Complex...
10
Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, return _the only number in the range that is missing from the array._ **Example 1:** **Input:** nums = \[3,0,1\] **Output:** 2 **Explanation:** n = 3 since there are 3 numbers, so all numbers are in the range \[0,3\]. 2 is the missing number...
null
Python Easy to understand 99.5% memory
missing-number
0
1
**Plz Upvote ..if you got help from this.**\n\n# Code\n```\nclass Solution:\n def missingNumber(self, nums: List[int]) -> int:\n nums.sort()\n\n if nums[0] != 0:\n return 0\n\n for i in range(len(nums)-1):\n if nums[i] + 1 == nums[i+1]:\n continue\n ...
3
Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, return _the only number in the range that is missing from the array._ **Example 1:** **Input:** nums = \[3,0,1\] **Output:** 2 **Explanation:** n = 3 since there are 3 numbers, so all numbers are in the range \[0,3\]. 2 is the missing number...
null
very easy python solution, check it !
missing-number
0
1
# Intuition\n\n# Approach\n1. calculate sum for given range(0 to n) using formula n * (n + 1)//2\n2. and subtract sum of elements of nums\n3. resulting difference is your number\n# Complexity\n- Time complexity:\nO(n) time,because needs to iterate through the entire List.\n\n- Space complexity:\nO(1)\n\n# Code\n```\nc...
0
Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, return _the only number in the range that is missing from the array._ **Example 1:** **Input:** nums = \[3,0,1\] **Output:** 2 **Explanation:** n = 3 since there are 3 numbers, so all numbers are in the range \[0,3\]. 2 is the missing number...
null
✅ Python Easy One liners with Explanation
missing-number
0
1
1. #### Gaus Formula\n\nWe can find the sum of first `n` numbers using [Gaus formula](https://nrich.maths.org/2478).\n```\n(n * (n+1))/2 where `n` is the length of input `nums`.\n```\nThen just subtract this with the sum of input `nums` to find the missing number.\n\n```\nclass Solution:\n def missingNumber(self, nu...
59
Given an array `nums` containing `n` distinct numbers in the range `[0, n]`, return _the only number in the range that is missing from the array._ **Example 1:** **Input:** nums = \[3,0,1\] **Output:** 2 **Explanation:** n = 3 since there are 3 numbers, so all numbers are in the range \[0,3\]. 2 is the missing number...
null
Solution
integer-to-english-words
1
1
```C++ []\nclass Solution {\npublic:\n string one(int num) {\n switch(num) {\n case 1: return "One";\n case 2: return "Two";\n case 3: return "Three";\n case 4: return "Four";\n case 5: return "Five";\n case 6: return "Six";\n case 7: return "Seven";\n case 8: return "Eight...
436
Convert a non-negative integer `num` to its English words representation. **Example 1:** **Input:** num = 123 **Output:** "One Hundred Twenty Three " **Example 2:** **Input:** num = 12345 **Output:** "Twelve Thousand Three Hundred Forty Five " **Example 3:** **Input:** num = 1234567 **Output:** "One Million Tw...
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words. There are many edge cases. What are some good test cases? Does your code work ...
C++ || Recursion || Easiest Beginner Friendly Sol
integer-to-english-words
1
1
# Intuition of this Problem:\n<!-- Describe your first thoughts on how to solve this problem. -->\n**NOTE - PLEASE READ APPROACH FIRST THEN SEE THE CODE. YOU WILL DEFINITELY UNDERSTAND THE CODE LINE BY LINE AFTER SEEING THE APPROACH.**\n\n# Approach for this Problem:\n1. Declare three string arrays to hold words for nu...
66
Convert a non-negative integer `num` to its English words representation. **Example 1:** **Input:** num = 123 **Output:** "One Hundred Twenty Three " **Example 2:** **Input:** num = 12345 **Output:** "Twelve Thousand Three Hundred Forty Five " **Example 3:** **Input:** num = 1234567 **Output:** "One Million Tw...
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words. There are many edge cases. What are some good test cases? Does your code work ...
Python 100% faster, simple elegant solution.
integer-to-english-words
0
1
# Intuition\nAt first glance, this problem seems complex due to how many edge cases we might find trying to represent numbers. For example, numbers from 1 to 99, like "Thirteen," have unique representations. For larger numbers, we use the same rules but add a quantity descriptor like \'Billion\' or \'Hundred\' (e.g., \...
0
Convert a non-negative integer `num` to its English words representation. **Example 1:** **Input:** num = 123 **Output:** "One Hundred Twenty Three " **Example 2:** **Input:** num = 12345 **Output:** "Twelve Thousand Three Hundred Forty Five " **Example 3:** **Input:** num = 1234567 **Output:** "One Million Tw...
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words. There are many edge cases. What are some good test cases? Does your code work ...
Python || easy to understand
integer-to-english-words
0
1
# Approach\n<!-- Describe your approach to solving the problem. -->\n\nThe code you provided seems to be a solution for converting a given number into words based on the provided mapping. It follows a recursive approach to break down the number into its components and build the corresponding word representation.\n# Com...
7
Convert a non-negative integer `num` to its English words representation. **Example 1:** **Input:** num = 123 **Output:** "One Hundred Twenty Three " **Example 2:** **Input:** num = 12345 **Output:** "Twelve Thousand Three Hundred Forty Five " **Example 3:** **Input:** num = 1234567 **Output:** "One Million Tw...
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words. There are many edge cases. What are some good test cases? Does your code work ...
[Python] Solution with Interview Tip
integer-to-english-words
0
1
```\nclass Solution:\n def numberToWords(self, num: int) -> str:\n ## RC ##\n ## APPROACH : BRUTE FORCE ##\n ## Main intent of the interviewer when asked this question is , he is testing how you are handling the test cases and how elagantly you are using sub problems to get to the final solution...
36
Convert a non-negative integer `num` to its English words representation. **Example 1:** **Input:** num = 123 **Output:** "One Hundred Twenty Three " **Example 2:** **Input:** num = 12345 **Output:** "Twelve Thousand Three Hundred Forty Five " **Example 3:** **Input:** num = 1234567 **Output:** "One Million Tw...
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words. There are many edge cases. What are some good test cases? Does your code work ...
273: Time 86.78% and Space 94.53% , Solution with step by step explanation
integer-to-english-words
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nThis solution is similar to the previous one, but it defines the word lists and scales as instance variables of the class instead of local variables inside the function. It also defines a separate convert function to handle ...
6
Convert a non-negative integer `num` to its English words representation. **Example 1:** **Input:** num = 123 **Output:** "One Hundred Twenty Three " **Example 2:** **Input:** num = 12345 **Output:** "Twelve Thousand Three Hundred Forty Five " **Example 3:** **Input:** num = 1234567 **Output:** "One Million Tw...
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words. There are many edge cases. What are some good test cases? Does your code work ...
Easy To Follow Recursive Python
integer-to-english-words
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1-2 digits: Tens\n3 digits: Hundreds\n4-6 digits: Thousands\n7-9 digits: Millions\n10 digits: Billions\n\nUse recursion. Base case is when digits == 2. \n=\n# Code\n``...
1
Convert a non-negative integer `num` to its English words representation. **Example 1:** **Input:** num = 123 **Output:** "One Hundred Twenty Three " **Example 2:** **Input:** num = 12345 **Output:** "Twelve Thousand Three Hundred Forty Five " **Example 3:** **Input:** num = 1234567 **Output:** "One Million Tw...
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words. There are many edge cases. What are some good test cases? Does your code work ...
[Java/C++/Python/JavaScript/Kotlin/Swift]O(n)time/BEATS 99.97% MEMORY/SPEED 0ms APRIL 2022
integer-to-english-words
1
1
***Hello it would be my pleasure to introduce myself Darian.***\n\n***Java***\n```\nclass Solution {\n String[] tens = {"", "Ten", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"};\n String[] ones = {"", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "E...
12
Convert a non-negative integer `num` to its English words representation. **Example 1:** **Input:** num = 123 **Output:** "One Hundred Twenty Three " **Example 2:** **Input:** num = 12345 **Output:** "Twelve Thousand Three Hundred Forty Five " **Example 3:** **Input:** num = 1234567 **Output:** "One Million Tw...
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words. There are many edge cases. What are some good test cases? Does your code work ...
PYTHON SOLUTION || EASY_TO_UNDERSTAND
integer-to-english-words
0
1
```\nclass Solution:\n def numberToWords(self, num: int) -> str:\n if num==0:\n return "Zero"\n belowTwenty = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight",\n "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen",\n "...
3
Convert a non-negative integer `num` to its English words representation. **Example 1:** **Input:** num = 123 **Output:** "One Hundred Twenty Three " **Example 2:** **Input:** num = 12345 **Output:** "Twelve Thousand Three Hundred Forty Five " **Example 3:** **Input:** num = 1234567 **Output:** "One Million Tw...
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words. There are many edge cases. What are some good test cases? Does your code work ...
short & easy to understand Python code using dictionary
integer-to-english-words
0
1
```Python\nclass Solution: \n def numberToWords(self, num: int) -> str:\n if num == 0 : return \'Zero\' #if condition to handle zero\n d = {1000000000 : \'Billion\',1000000 : \'Million\',1000 : \'Thousand\',100 : \'Hundred\', \n\t\t90:\'Ninety\',80:\'Eighty\',70:\'Seventy\',60:\'Sixty\',50: \'Fi...
9
Convert a non-negative integer `num` to its English words representation. **Example 1:** **Input:** num = 123 **Output:** "One Hundred Twenty Three " **Example 2:** **Input:** num = 12345 **Output:** "Twelve Thousand Three Hundred Forty Five " **Example 3:** **Input:** num = 1234567 **Output:** "One Million Tw...
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words. There are many edge cases. What are some good test cases? Does your code work ...
Python 3, Runtime 28ms, Recursive approach
integer-to-english-words
0
1
```\nclass Solution:\n def numberToWords(self, num: int) -> str:\n self.lessThan20 = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"]\n self.tens = ["", "", "Twenty"...
9
Convert a non-negative integer `num` to its English words representation. **Example 1:** **Input:** num = 123 **Output:** "One Hundred Twenty Three " **Example 2:** **Input:** num = 12345 **Output:** "Twelve Thousand Three Hundred Forty Five " **Example 3:** **Input:** num = 1234567 **Output:** "One Million Tw...
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words. There are many edge cases. What are some good test cases? Does your code work ...
Elegant Recursive Solution in Python
integer-to-english-words
0
1
```Python\nclass Solution:\n def numberToWords(self, num: int) -> str: \n if num == 0:\n return \'Zero\'\n\n billion, million, thousand, hundred = 10 ** 9, 10 ** 6, 10 ** 3, 10 ** 2\n\n mapping = {\n 0: \'\',\n 1: \'One\',\n 2: \'Two\',\n 3...
1
Convert a non-negative integer `num` to its English words representation. **Example 1:** **Input:** num = 123 **Output:** "One Hundred Twenty Three " **Example 2:** **Input:** num = 12345 **Output:** "Twelve Thousand Three Hundred Forty Five " **Example 3:** **Input:** num = 1234567 **Output:** "One Million Tw...
Did you see a pattern in dividing the number into chunk of words? For example, 123 and 123000. Group the number by thousands (3 digits). You can write a helper function that takes a number less than 1000 and convert just that chunk to words. There are many edge cases. What are some good test cases? Does your code work ...
Python3 One line Code 🔥🔥🔥
h-index
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ --...
6
Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return _the researcher's h-index_. According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-index is defined as the maximum value of `h` such th...
An easy approach is to sort the array first. What are the possible values of h-index? A faster approach is to use extra space.
✅ Explained - Simple and Clear Python3 Code✅
h-index
0
1
# Intuition\nThe problem asks for the h-index of a researcher based on the number of citations received for their papers. The h-index is the maximum value of h such that the researcher has published at least h papers that have each been cited at least h times. To find the h-index, we need to sort the array of citations...
22
Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return _the researcher's h-index_. According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-index is defined as the maximum value of `h` such th...
An easy approach is to sort the array first. What are the possible values of h-index? A faster approach is to use extra space.
Simple and easy to understand python solution with 98% runtime and 57% memory beat
h-index
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity: O(n)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n\n- Space complexity: O(n)\n<!-- Add your space complexity here, e.g....
4
Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return _the researcher's h-index_. According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-index is defined as the maximum value of `h` such th...
An easy approach is to sort the array first. What are the possible values of h-index? A faster approach is to use extra space.
274: Time 90.70%, Solution with step by step explanation
h-index
0
1
# Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\nTo compute the researcher\'s h-index, we can sort the citations array in non-increasing order and then iterate through the sorted array. For each citation count, we compare it to the number of papers that have at least that ...
11
Given an array of integers `citations` where `citations[i]` is the number of citations a researcher received for their `ith` paper, return _the researcher's h-index_. According to the [definition of h-index on Wikipedia](https://en.wikipedia.org/wiki/H-index): The h-index is defined as the maximum value of `h` such th...
An easy approach is to sort the array first. What are the possible values of h-index? A faster approach is to use extra space.