├── .gitignore
├── Data Structure
├── Graph
│ ├── BFS
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ ├── DFS
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ └── Shortest Path
│ │ ├── Dijkstra
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ └── main.js
│ │ ├── Floyd
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ └── main.js
│ │ └── graph.png
├── Search
│ ├── Binary Search
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ └── Pattern Match
│ │ ├── Brute Force
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ │ └── KMP
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
├── Sort
│ ├── Bubble Sort
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.go
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ ├── Heap Sort
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.go
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ ├── Insertion Sort
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.go
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ ├── Merge Sort
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.go
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ ├── Quick Sort
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.go
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ ├── Radix Sort
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.go
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ ├── Selection Sort
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.go
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ └── Shell Sort
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.go
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
└── Tree
│ ├── Traversal
│ ├── In Order
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.go
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ ├── Level Order
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ ├── Post Order
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.go
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ └── Pre Order
│ │ ├── Main.java
│ │ ├── main.cpp
│ │ ├── main.go
│ │ ├── main.js
│ │ ├── main.kt
│ │ └── main.py
│ └── Union Find
│ ├── UnionFind.cpp
│ ├── UnionFind.go
│ ├── UnionFind.java
│ ├── UnionFind.kt
│ ├── union-find.js
│ └── union_find.py
├── LICENSE
├── LeetCode.md
├── LeetCode
├── 0001. Two Sum
│ ├── README.md
│ ├── Solution.go
│ ├── Solution.java
│ ├── Solution.kt
│ ├── solution.cpp
│ ├── solution.js
│ ├── solution.py
│ └── solution.rs
├── 0002. Add Two Numbers
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0003. Longest Substring Without Repeating Characters
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0004. Median of Two Sorted Arrays
│ ├── README.md
│ └── Solution.kt
├── 0005. Longest Palindromic Substring
│ ├── README.md
│ ├── Solution.cpp
│ ├── Solution.java
│ ├── Solution.kt
│ └── solution.py
├── 0006. ZigZag Conversion
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0007. Reverse Integer
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0009. Palindrome Number
│ ├── README.md
│ └── solution.kt
├── 0011. Container With Most Water
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0012. Integer to Roman
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0013. Roman to Integer
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0014. Longest Common Prefix
│ ├── README.md
│ └── solution.cpp
├── 0015. 3Sum
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0017. Letter Combinations of a Phone Number
│ ├── README.md
│ ├── Solution.kt
│ └── solution.py
├── 0018. 4Sum
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0020. Valid Parentheses
│ ├── README.md
│ ├── Solution.cpp
│ ├── Solution.kt
│ └── solution.py
├── 0021. Merge Two Sorted Lists
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0022. Generate Parentheses
│ ├── README.md
│ ├── Solution.cpp
│ └── solution.py
├── 0024. Swap Nodes in Pairs
│ ├── README.md
│ ├── Solution.cpp
│ └── Solution.java
├── 0026. Remove Duplicates from Sorted Array
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0027. Remove Element
│ ├── README.md
│ ├── Solution.cpp
│ ├── Solution.kt
│ └── solution.py
├── 0028. Implement strStr()
│ ├── README.md
│ └── solution.cpp
├── 0029. Divide Two Integers
│ ├── README.md
│ └── solution.cpp
├── 0030. Substring with Concatenation of All Words
│ ├── README.md
│ └── solution.py
├── 0031. Next Permutation
│ ├── README.md
│ └── solution.py
├── 0033. Search in Rotated Sorted Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0034. Search for a Range
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0035. Search Insert Position
│ ├── README.md
│ └── Solution.kt
├── 0036. Valid Sudoku
│ ├── README.md
│ └── solution.cpp
├── 0038. Count and Say
│ ├── README.md
│ └── solution.cpp
├── 0039. Combination Sum
│ ├── README.md
│ └── solution.py
├── 0040. Combination Sum II
│ ├── README.md
│ └── solution.py
├── 0041. First Missing Positive
│ ├── README.md
│ └── solution.cpp
├── 0042. Trapping Rain Water
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0045. Jump Game II
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0046. Permutations
│ ├── README.md
│ └── Solution.kt
├── 0047. Permutations II
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0048. Rotate Image
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0050. Pow(x, n)
│ ├── README.md
│ └── Solution.kt
├── 0051. N Queens
│ ├── README.md
│ └── Solution.kt
├── 0053. Maximum Subarray
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0054. Spiral Matrix
│ ├── README.md
│ └── solution.cpp
├── 0055. Jump Game
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0056. Merge Intervals
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ └── solution.py
├── 0058. Length of Last Word
│ ├── README.md
│ ├── Solution.cpp
│ ├── Solution.kt
│ ├── solution.go
│ └── solution.py
├── 0059. Spiral Matrix II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0061. Rotate List
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0062. Unique Paths
│ ├── README.md
│ └── solution.py
├── 0063. Unique Paths II
│ ├── README.md
│ └── solution.py
├── 0064. Minimum Path Sum
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.kt
│ └── solution.py
├── 0066. Plus One
│ ├── README.md
│ ├── Solution.cpp
│ └── Solution.py
├── 0067. Add Binary
│ ├── README.md
│ └── Solution.kt
├── 0068. Text Justification
│ ├── README.md
│ └── solution.cpp
├── 0069. Sqrt(x)
│ ├── README.md
│ └── Solution.kt
├── 0070. Climbing Stairs
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0071. Simplify Path
│ ├── README.md
│ ├── Solution.kt
│ ├── Solution.py
│ ├── solution.cpp
│ └── solution.go
├── 0072. Edit Distance
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0073. Set Matrix Zeroes
│ ├── README.md
│ └── solution.cpp
├── 0074. Search a 2D Matrix
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0075. Sort Colors
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.js
│ ├── Solution.kt
│ └── Solution.py
├── 0077. Combinations
│ ├── README.md
│ └── Solution.kt
├── 0078. Subsets
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0079. Word Search
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0080. Remove Duplicates from Sorted Array II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0081. Search in Rotated Sorted Array II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0082. Remove Duplicates from Sorted List II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0083. Remove Duplicates from Sorted List
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0087. Scramble String
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0088. Merge Sorted Array
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0089. Gray Code
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0090. Subsets II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0091. Decode Ways
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0092. Reverse Linked List II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0094. Binary Tree Inorder Traversal
│ ├── README.md
│ └── Solution.kt
├── 0096. Unique Binary Search Trees
│ ├── README.md
│ └── solution.py
├── 0097. Interleaving String
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0098. Validate Binary Search Tree
│ ├── README.md
│ └── Solution.cpp
├── 0100. Same Tree
│ ├── README.md
│ └── Solution.kt
├── 0101. Symmetric Tree
│ ├── README.md
│ └── solution.py
├── 0102. Binary Tree Level Order Traversal
│ ├── README.md
│ ├── Solution.cpp
│ └── Solution.kt
├── 0103. Binary Tree Zigzag Level Order Traversal
│ ├── README.md
│ └── Solution.kt
├── 0104. Maximum Depth of Binary Tree
│ ├── README.md
│ ├── Solution.cpp
│ ├── Solution.kt
│ └── solution.py
├── 0105. Construct Binary Tree from Preorder and Inorder Traversal
│ ├── README.md
│ ├── Solution.java
│ └── Solution.kt
├── 0106. Construct Binary Tree from Inorder and Postorder Traversal
│ ├── README.md
│ ├── Solution.java
│ └── Solution.kt
├── 0107. Binary Tree Level Order Traversal II
│ ├── README.md
│ └── solution.py
├── 0108. Convert Sorted Array to Binary Search Tree
│ ├── README.md
│ └── solution.py
├── 0110. Balanced Binary Tree
│ ├── README.md
│ └── solution.py
├── 0111. Minimum Depth of Binary Tree
│ ├── README.md
│ ├── Solution.cpp
│ └── Solution.kt
├── 0112. Path Sum
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0115. Distinct Subsequences
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0117. Populating Next Right Pointers in Each Node II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0118. Pascal's Triangle
│ ├── REDME.md
│ ├── Solution.java
│ ├── Solution.kt
│ ├── Solution.py
│ └── solution.cpp
├── 0119. Pascal's Triangle II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0120. Triangle
│ ├── README.md
│ ├── Solution.cpp
│ ├── Solution.kt
│ └── solution.py
├── 0121. Best Time to Buy and Sell Stock
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ ├── solution.cpp
│ ├── solution.go
│ ├── solution.js
│ ├── solution.py
│ └── solution.rs
├── 0122. Best Time to Buy and Sell Stock II
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0125. Valid Palindrome
│ ├── README.md
│ └── Solution.kt
├── 0127. Word Ladder
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ └── solution.cpp
├── 0128. Longest Consecutive Sequence
│ ├── README.md
│ └── Solution.kt
├── 0129. Sum Root to Leaf Numbers
│ ├── README.md
│ └── solution.py
├── 0130. Surrounded Regions
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0131. Palindrome Partitioning
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0132. Palindrome Partitioning II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0136. Single Number
│ ├── README.md
│ └── Solution.kt
├── 0137. Single Number II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0139. Word Break
│ ├── README.md
│ └── solution.py
├── 0141. Linked List Cycle
│ ├── README.md
│ └── Solution.cpp
├── 0142. Linked List Cycle II
│ ├── README.md
│ └── Solution.cpp
├── 0144. Binary Tree Preorder Traversal
│ ├── README.md
│ └── Solution.kt
├── 0145. Binary Tree Postorder Traversal
│ ├── README.md
│ └── Solution.kt
├── 0148. Sort List
│ ├── README.md
│ ├── Solution.java
│ └── Solution.kt
├── 0149. Max Points on a Line
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0150. Evaluate Reverse Polish Notation
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.js
│ ├── Solution.kt
│ ├── Solution.py
│ └── solution.cpp
├── 0151. Reverse Words in a String
│ ├── README.md
│ └── Solution.java
├── 0152. Maximum Product Subarray
│ ├── README.md
│ └── solution.py
├── 0153. Find Minimum in Rotated Sorted Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0154. Find Minimum in Rotated Sorted Array II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0160. Intersection of Two Linked Lists
│ ├── README.md
│ ├── Solution.cpp
│ └── solution.py
├── 0162. Find Peak Element
│ ├── README.md
│ └── solution.cpp
├── 0165. Compare Version Numbers
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0166. Fraction to Recurring Decimal
│ ├── README.md
│ └── solution.cpp
├── 0167. Two Sum II - Input array is sorted
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ ├── Solution.py
│ └── solution.cpp
├── 0168. Excel Sheet Column Title
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0169. Majority Element
│ ├── README.md
│ └── solution.go
├── 0172. Factorial Trailing Zeroes
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ ├── solution.py
│ └── solution.rs
├── 0173. Binary Search Tree Iterator
│ ├── README.md
│ └── solution.cpp
├── 0179. Largest Number
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0187. Repeated DNA Sequences
│ ├── README.md
│ └── solution.cpp
├── 0189. Rotate Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.go
├── 0190. Reverse Bits
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0191. Number of 1 Bits
│ ├── README.md
│ └── solution.cpp
├── 0198. House Robber
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0199. Binary Tree Right Side View
│ ├── README.md
│ └── Solution.kt
├── 0200. Number of Islands
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0201. Bitwise AND of Numbers Range
│ ├── README.md
│ └── solution.py
├── 0202. Happy Number
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0203. Remove Linked List Elements
│ ├── README.md
│ └── Solution.kt
├── 0204. Count Primes
│ ├── README.md
│ └── Solution.kt
├── 0205. Isomorphic Strings
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0206. Reverse Linked List
│ ├── README.md
│ ├── Solution.cpp
│ ├── Solution.java
│ └── Solution.kt
├── 0208. Implement Trie (Prefix Tree)
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0209. Minimum Size Subarray Sum
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ ├── Solution.py
│ └── solution.cpp
├── 0211. Design Add and Search Words Data Structure
│ ├── README.md
│ └── solution.cpp
├── 0212. Word Search II
│ ├── README.md
│ └── solution.cpp
├── 0213. House Robber II
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0215. Kth Largest Element in an Array
│ ├── README.md
│ └── Solution.kt
├── 0217. Contains Duplicate
│ ├── README.md
│ ├── Solution.cpp
│ ├── Solution.kt
│ └── solution.py
├── 0219. Contains Duplicate II
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0220. Contains Duplicate III
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0221. Maximal Square
│ ├── README.md
│ └── solution.py
├── 0223. Rectangle Area
│ ├── README.md
│ └── solution.cpp
├── 0224. Basic Calculator
│ ├── README.md
│ └── solution.cpp
├── 0225. Implement Stack using Queues
│ ├── README.md
│ └── Solution.cpp
├── 0226. Invert Binary Tree
│ ├── README.md
│ └── Solution.kt
├── 0227. Basic Calculator II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0228. Summary Ranges
│ ├── README.md
│ ├── Solution.java
│ └── solution.cpp
├── 0229. Majority Element II
│ ├── README.md
│ └── solution.cpp
├── 0230. Kth Smallest Element in a BST
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0231. Power of Two
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0232. Implement Queue using Stacks
│ ├── README.md
│ ├── Solution.cpp
│ └── solution.py
├── 0233. Number of Digit One
│ ├── README.md
│ └── solution.cpp
├── 0235. Lowest Common Ancestor of a Binary Search Tree
│ ├── README.md
│ └── Solution.java
├── 0237. Delete Node in a Linked List
│ ├── README.md
│ ├── Solution.go
│ └── solution.cpp
├── 0240. Search a 2D Matrix II
│ ├── README.md
│ └── solution.cpp
├── 0241. Different Ways to Add Parentheses
│ ├── README.md
│ └── Solution.kt
├── 0242. Valid Anagram
│ ├── README.md
│ └── Solution.kt
├── 0257. Binary Tree Paths
│ ├── README.md
│ └── Solution.kt
├── 0258. Add Digits
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0260. Single Number III
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ └── solution.cpp
├── 0263. Ugly Number
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0264. Ugly Number II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0268. Missing Number
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0273. Integer to English Words
│ ├── README.md
│ └── solution.cpp
├── 0278. First Bad Version
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0279. Perfect Squares
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0282. Expression Add Operators
│ ├── README.md
│ └── solution.cpp
├── 0283. Move Zeroes
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.js
│ ├── Solution.kt
│ └── Solution.py
├── 0284. Peeking Iterator
│ ├── README.md
│ └── solution.cpp
├── 0287. Find the Duplicate Number
│ ├── README.md
│ └── Solution.cpp
├── 0290. Word Pattern
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0292. Nim Game
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ ├── solution.js
│ ├── solution.kt
│ └── solution.py
├── 0295. Find Median from Data Stream
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0299. Bulls and Cows
│ ├── README.md
│ └── solution.cpp
├── 0300. Longest Increasing Subsequence
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0301. Remove Invalid Parentheses
│ ├── README.md
│ └── solution.cpp
├── 0303. Range Sum Query - Immutable
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0304. Range Sum Query 2D - Immutable
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0306. Additive Number
│ ├── README.md
│ └── solution.cpp
├── 0307. Range Sum Query - Mutable
│ ├── README.md
│ └── solution.cpp
├── 0309. Best Time to Buy and Sell Stock with Cooldown
│ ├── README.md
│ └── solution.py
├── 0310. Minimum Height Trees
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0316. Remove Duplicate Letters
│ ├── README.md
│ └── solution.cpp
├── 0318. Maximum Product of Word Lengths
│ ├── README.md
│ └── solution.cpp
├── 0322. Coin Change
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0326. Power of Three
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0329. Longest Increasing Path in a Matrix
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0331. Verify Preorder Serialization of a Binary Tree
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0334. Increasing Triplet Subsequence
│ ├── README.md
│ └── solution.cpp
├── 0338. Counting Bits
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0341. Flatten Nested List Iterator
│ ├── README.md
│ └── solution.cpp
├── 0342. Power of Four
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0343. Integer Break
│ ├── README.md
│ ├── solution.kt
│ └── solution.py
├── 0344. Reverse String
│ ├── README.md
│ └── Solution.kt
├── 0345. Reverse Vowels of a String
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0347. Top K Frequent Elements
│ ├── README.md
│ └── Solution.kt
├── 0349. Intersection of Two Arrays
│ ├── README.md
│ └── Solution.kt
├── 0350. Intersection of Two Arrays II
│ ├── README.md
│ └── Solution.kt
├── 0352. Data Stream as Disjoint Intervals
│ ├── README.md
│ └── solution.cpp
├── 0354. Russian Doll Envelopes
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0357. Count Numbers with Unique Digits
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0363. Max Sum of Rectangle No Larger Than K
│ └── README.md
├── 0365. Water and Jug Problem
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0367. Valid Perfect Square
│ ├── README.md
│ └── solution.cpp
├── 0368. Largest Divisible Subset
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0371. Sum of Two Integers
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ └── solution.cpp
├── 0372. Super Pow
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ └── solution.cpp
├── 0373. Find K Pairs with Smallest Sums
│ ├── README.md
│ └── solution.cpp
├── 0374. Guess Number Higher or Lower
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0376. Wiggle Subsequence
│ ├── README.md
│ └── solution.py
├── 0377. Combination Sum IV
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0380. Insert Delete GetRandom O(1)
│ ├── README.md
│ └── solution.cpp
├── 0382. Linked List Random Node
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0383. Ransom Note
│ ├── README.md
│ └── solution.cpp
├── 0384. Shuffle an Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0385. Mini Parser
│ ├── README.md
│ └── solution.cpp
├── 0386. Lexicographical Numbers
│ ├── README.md
│ └── solution.cpp
├── 0388. Longest Absolute File Path
│ ├── README.md
│ └── solution.py
├── 0389. Find the Difference
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0390. Elimination Game
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0391. Perfect Rectangle
│ ├── README.md
│ └── solution.cpp
├── 0392. Is Subsequence
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0393. UTF-8 Validation
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0395. Longest Substring with At Least K Repeating Characters
│ ├── README.md
│ └── solution.cpp
├── 0396. Rotate Function
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0397. Integer Replacement
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0398. Random Pick Index
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0400. Nth Digit
│ ├── README.md
│ └── solution.cpp
├── 0401. Binary Watch
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0405. Convert a Number to Hexadecimal
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ └── solution.cpp
├── 0409. Longest Palindrome
│ ├── README.md
│ └── solution.go
├── 0412. Fizz Buzz
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0413. Arithmetic Slices
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0414. Third Maximum Number
│ ├── README.md
│ └── solution.cpp
├── 0415. Add Strings
│ ├── README.md
│ └── Solution.kt
├── 0416. Partition Equal Subset Sum
│ ├── README.md
│ ├── Soution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0417. Pacific Atlantic Water Flow
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0421. Maximum XOR of Two Numbers in an Array
│ ├── README.md
│ └── solution.cpp
├── 0424. Longest Repeating Character Replacement
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0429. N-ary Tree Level Order Traversal
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0430. Flatten a Multilevel Doubly Linked List
│ ├── README.md
│ └── solution.cpp
├── 0432. All O one Data Structure
│ ├── README.md
│ └── solution.cpp
├── 0433. Minimum Genetic Mutation
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0434. Number of Segments in a String
│ ├── README.md
│ └── solution.cpp
├── 0435. Non overlapping Intervals
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0436. Find Right Interval
│ ├── README.md
│ └── solution.cpp
├── 0437. Path Sum III
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0438. Find All Anagrams in a String
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0441. Arranging Coins
│ ├── README.md
│ ├── Solution.cpp
│ └── solution.py
├── 0442. Find All Duplicates in an Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0443. String Compression
│ ├── README.md
│ └── solution.cpp
├── 0447. Number of Boomeranges
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0448. Find All Numbers Disappeared in an Array
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0449. Serialize and Deserialize BST
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0450. Delete Node in a BST
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0451. Sort Characters By Frequency
│ ├── README.md
│ └── Solution.kt
├── 0453. Minimum Moves to Equal Array Elements
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ └── solution.py
├── 0454. 4Sum II
│ ├── README.md
│ └── Solution.kt
├── 0455. Assign Cookies
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0456. 132 Pattern
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0457. Circular Array Loop
│ ├── README.md
│ └── solution.cpp
├── 0461. Hamming Distance
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0462. Minimum Moves to Equal Array Elements II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0464. Can I Win
│ ├── README.md
│ └── solution.cpp
├── 0468. Validate IP Address
│ ├── README.md
│ └── solution.py
├── 0470. Implement Rand10() Using Rand7()
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0476. Number Complement
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0477. Total Hamming Distance
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0478. Generate Random Point in a Circle
│ ├── README.md
│ └── solution.py
├── 0480. Sliding Window Median
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ └── solution.py
├── 0482. License Key Formatting
│ ├── README.md
│ └── solution.cpp
├── 0485. Max Consecutive Ones
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0492. Construct the Rectangle
│ ├── README.md
│ └── solution.cpp
├── 0494. Target Sum
│ ├── README.md
│ └── solution.cpp
├── 0495. Teemo Attacking
│ ├── README.md
│ └── solution.cpp
├── 0496. Next Greater Element I
│ ├── README.md
│ ├── Solution.cpp
│ └── solution.py
├── 0498. Diagonal Traverse
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0500. Keyboard Row
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0502. IPO
│ ├── README.md
│ └── solution.cpp
├── 0503. Next Greater Element II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0504. Base 7
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0506. Relative Ranks
│ ├── README.md
│ └── solution.cpp
├── 0507. Perfect Number
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0508. Most Frequent Subtree Sum
│ ├── README.md
│ └── solution.py
├── 0509. Fibonacci Number
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ ├── solution.js
│ ├── solution.kt
│ └── solution.py
├── 0513. Find Bottom Left Tree Value
│ ├── README.md
│ ├── Solution.cpp
│ ├── Solution.kt
│ └── solution.py
├── 0515. Find Largest Value in Each Tree Row
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0516. Longest Palindromic Subsequence
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0517. Super Washing Machines
│ ├── README.md
│ └── solution.cpp
├── 0518. Coin Change 2
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0520. Detect Capital
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ └── solution.cpp
├── 0521. Longest Uncommon Subsequence I
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0523. Continuous Subarray Sum
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0524. Longest Word in Dictionary through Deleting
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0525. Contiguous Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0526. Beautiful Arrangement
│ ├── README.md
│ └── solution.cpp
├── 0530. Minimum Absolute Difference in BST
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0532. K-diff Pairs in an Array
│ ├── README.md
│ └── solution.py
├── 0535. Encode and Decode TinyURL
│ ├── README.md
│ └── solution.py
├── 0537. Complex Number Multiplication
│ ├── README.md
│ ├── Solution.java
│ └── solution.cpp
├── 0539. Minimum Time Difference
│ ├── README.md
│ └── solution.cpp
├── 0540. Single Element in a Sorted Array
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0541. Reverse String II
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0542. 01 Matrix
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0543. Diameter of Binary Tree
│ ├── LeetCode.md
│ ├── README.md
│ └── solution.go
├── 0547. Number of Provinces
│ ├── README.md
│ ├── Solution.cpp
│ ├── Solution.java
│ ├── Solution.kt
│ └── solution.py
├── 0551. Student Attendance Record I
│ ├── README.md
│ └── solution.cpp
├── 0552. Student Attendance Record II
│ ├── README.md
│ └── solution.cpp
├── 0553. Optimal Division
│ ├── README.md
│ └── solution.cpp
├── 0554. Brick Wall
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0560. Subarray Sum Equals K
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ └── solution.py
├── 0561. Array Partition I
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0566. Reshpae the Matrix
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0567. Permutation in String
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0572. Subtree of Another Tree
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0575. Distribute Candies
│ ├── README.md
│ └── solution.cpp
├── 0576. Out of Boundary Paths
│ ├── README.md
│ └── solution.cpp
├── 0581. Shortest Unsorted Continuous Subarray
│ ├── README.md
│ └── solution.cpp
├── 0583. Delete Operation for Two Strings
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0589. N-ary Tree Preorder Traversal
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0590. N-ary Tree Postorder Traversal
│ ├── README.md
│ └── solution.py
├── 0592. Fraction Addition and Subtraction
│ ├── README.md
│ └── Solution.kt
├── 0594. Longest Harmonious Subsequence
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0598. Range Addition II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.go
├── 0599. Minimum Index Sum of Two Lists
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0605. Can Place Flowers
│ ├── README.md
│ └── solution.cpp
├── 0606. Construct String from Binary Tree
│ ├── README.md
│ └── solution.cpp
├── 0611. Valid Triangle Number
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0622. Design Circular Queue
│ ├── README.md
│ └── solution.py
├── 0623. Add One Row to Tree
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0628. Maximum Product of Three Numbers
│ ├── README.md
│ └── solution.py
├── 0633. Sum of Square Numbers
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0636. Exclusive Time of Functions
│ ├── README.md
│ └── solution.py
├── 0638. Shopping Offers
│ ├── README.md
│ └── solution.cpp
├── 0639. Decode Ways II
│ ├── README.md
│ └── solution.cpp
├── 0640. Solve the Equation
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0641. Design Circular Deque
│ ├── README.md
│ └── solution.py
├── 0643. Maximum Average Subarray I
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0646. Maximum Length of Pair Chain
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0648. Replace Words
│ ├── README.md
│ └── Solution.kt
├── 0650. 2 Keys Keyboard
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ ├── solution.js
│ ├── solution.kt
│ └── solution.py
├── 0652. Find Duplicate Subtrees
│ ├── README.md
│ └── solution.cpp
├── 0653. Two Sum IV - Input is a BST
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0654. Maximum Binary Tree
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0655. Print Binary Tree
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0657. Robot Return to Origin
│ ├── README.md
│ ├── Solution.go
│ └── solution.cpp
├── 0658. Find K Closest Elements
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0661. Image Smoother
│ ├── README.md
│ ├── solution.cpp
│ └── solution.rs
├── 0662. Maximum Width of Binary Tree
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0665. Non-decreasing Array
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0667. Beautiful Arrangement II
│ ├── README.md
│ └── solution.cpp
├── 0669. Trim a Binary Search Tree
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0670. Maximum Swap
│ ├── README.md
│ └── solution.py
├── 0672. Bulb Switcher II
│ ├── README.md
│ └── solution.py
├── 0673. Number of Longest Increasing Subsequence
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0674. Longest Continuous Increasing Subsequence
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0678. Valid Parenthesis String
│ ├── README.md
│ └── solution.cpp
├── 0682. Baseball Game
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0684. Redundant Connection
│ ├── README.md
│ ├── Solution.cpp
│ ├── Solution.java
│ ├── Solution.kt
│ └── Solution.py
├── 0686. Repeated String Match
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0687. Longest Univalue Path
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0690. Employee Importance
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0692. Top K Frequent Words
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0693. Binary Number with Alternating Bits
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ ├── solution.cpp
│ ├── solution.go
│ ├── solution.py
│ └── solution.rs
├── 0695. Max Area of Island
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0697. Degree of an Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0698. Partition to K Equal Sum Subsets
│ ├── README.md
│ └── Solution.kt
├── 0700. Search in a Binary Search Tree
│ ├── README.md
│ └── solution.cpp
├── 0703. Kth Largest Element in a Stream
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0704. Binary Search
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0705. Design HashSet
│ ├── README.md
│ └── solution.cpp
├── 0706. Design HashMap
│ ├── README.md
│ └── solution.cpp
├── 0707. Design Linked List
│ ├── README.md
│ └── solution.py
├── 0709. To Lower Case
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0710. Random Pick with Blacklist
│ ├── README.md
│ └── solution.py
├── 0713. Subarray Product Less Than K
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0714. Best Time to Buy and Sell Stock with Transaction Fee
│ ├── README.md
│ └── solution.py
├── 0717. 1-bit and 2-bit Characters
│ ├── README.md
│ └── solution.cpp
├── 0720. Longest Word in Dictionary
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0721. Accounts Merge
│ ├── README.md
│ ├── Solution.java
│ └── Solution.kt
├── 0724. Find Pivot Index
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0725. Split Linked List in Parts
│ ├── README.md
│ └── solution.cpp
├── 0728. Self Dividing Numbers
│ ├── README.md
│ ├── Solution.kt
│ └── solution.py
├── 0729. My Calendar I
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0733. Flood Fill
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0738. Monotone Increasing Digits
│ ├── README.md
│ └── Solution.kt
├── 0740. Delete and Earn
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0743. Network Delay Time
│ ├── README.md
│ └── solution.cpp
├── 0744. Find Smallest Letter Greater Than Target
│ ├── README.md
│ └── solution.py
├── 0745. Prefix and Suffix Search
│ ├── README.md
│ └── Solution.kt
├── 0746. Min Cost Climbing Stairs
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0747. Largest Number At Least Twice of Others
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0752. Open the Lock
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0760. Find Anagram Mappings
│ ├── README.md
│ └── Solution.kt
├── 0762. Prime Number of Set Bits in Binary Representation
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0765. Couples Holding Hands
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0766. Toeplitz Matrix
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0768. Max Chunks To Make Sorted II
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ └── solution.py
├── 0771. Jewels and Stones
│ ├── README.md
│ └── Solution.kt
├── 0777. Swap Adjacent in LR String
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0780. Reaching Points
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ ├── solution.go
│ ├── solution.py
│ └── solution.rs
├── 0781. Rabbits in Forest
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0783. Minimum Distance Between BST Nodes
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0784. Letter Case Permutation
│ ├── README.md
│ └── Solution.kt
├── 0785. Is Graph Bipartite
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0788. Rotated Digits
│ ├── README.md
│ └── Solution.kt
├── 0789. Escape The Ghosts
│ ├── README.md
│ ├── Solution.kt
│ └── solution.cpp
├── 0791. Custom Sort String
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0796. Rotate String
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ ├── solution.go
│ ├── solution.py
│ └── solution.rs
├── 0797. All Paths From Source to Target
│ ├── README.md
│ ├── Solution.kt
│ ├── solution.cpp
│ └── solution.py
├── 0802. Find Eventual Safe States
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0804. Unique Morse Code Words
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0806. Number of Lines To Write String
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.rs
├── 0807. Max Increase to Keep City Skyline
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0810. Chalkboard XOR Game
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0811. Subdomain Visit Count
│ ├── README.md
│ └── solution.py
├── 0812. Largest Triangle Area
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0814. Binary Tree Pruning
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0819. Most Common Word
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0821. Shortest Distance to a Character
│ ├── README.md
│ └── solution.cpp
├── 0824. Goat Latin
│ ├── README.md
│ ├── Solution.cpp
│ └── solution.py
├── 0825. Friends Of Appropriate Ages
│ ├── README.md
│ └── solution.cpp
├── 0832. Flipping an Image
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0836. Rectangle Overlap
│ ├── README.md
│ └── solution.py
├── 0838. Push Dominoes
│ ├── README.md
│ └── solution.cpp
├── 0841. Keys and Rooms
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0844. Backspace String Compare
│ ├── README.md
│ └── solution.cpp
├── 0846. Hand of Straights
│ ├── README.md
│ └── solution.cpp
├── 0847. Shortest Path Visiting All Nodes
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0850. Rectangle Area II
│ ├── README.md
│ └── solution.py
├── 0852. Peak Index in a Mountain Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0856. Score of Parentheses
│ ├── README.md
│ └── solution.py
├── 0860. Lemonade Change
│ ├── README.md
│ └── Solution.cpp
├── 0867. Transpose Matrix
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0868. Binary Gap
│ ├── README.md
│ └── solution.cpp
├── 0869. Reordered Power of 2
│ ├── README.md
│ └── solution.cpp
├── 0870. Advantage Shuffle
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0872. Leaf-Similar Trees
│ ├── README.md
│ ├── Solution.cpp
│ └── solution.py
├── 0875. Koko Eating Bananas
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0876. Middle of the Linked List
│ ├── README.md
│ └── solution.go
├── 0883. Projection Area of 3D Shapes
│ ├── README.md
│ └── solution.py
├── 0884. Uncommon Words from Two Sentences
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0886. Possible Bipartition
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0888. Fair Candy Swap
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0890. Find and Replace Pattern
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0896. Monotonic Array
│ ├── README.md
│ └── solution.cpp
├── 0897. Increasing Order Search Tree
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0905. Sort Array By Parity
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0908. Smallest Range I
│ ├── README.md
│ └── solution.py
├── 0913. Cat and Mouse
│ ├── README.md
│ └── solution.cpp
├── 0917. Reverse Only Letters
│ ├── README.md
│ └── solution.cpp
├── 0918. Maximum Sum Circular Subarray
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0921. Minimum Add to Make Parentheses Valid
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0926. Flip String to Monotone Increasing
│ ├── README.md
│ └── solution.py
├── 0929. Unique Email Addresses
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0931. Minimum Falling Path Sum
│ ├── README.md
│ └── solution.py
├── 0933. Number of Recent Calls
│ ├── README.md
│ └── solution.py
├── 0934. Shortest Bridge
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0938. Range Sum of BST
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0942. DI String Match
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0944. Delete Columns to Make Sorted
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0945. Minimum Increment to Make Array Unique
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0946. Validate Stack Sequences
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0951. Flip Equivalent Binary Trees
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0953. Verifying an Alien Dictionary
│ ├── README.md
│ └── solution.cpp
├── 0954. Array of Doubled Pairs
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0961. N-Repeated Element in Size 2N Array
│ ├── README.md
│ ├── Solution.cpp
│ └── solution.py
├── 0965. Univalued Binary Tree
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0969. Pancake Sorting
│ ├── README.md
│ └── solution.cpp
├── 0977. Squares of a Sorted Array
│ ├── README.md
│ └── solution.cpp
├── 0978. Longest Turbulent Subarray
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0986. Interval List Intersections
│ ├── README.md
│ └── solution.cpp
├── 0989. Add to Array-Form of Integer
│ ├── README.md
│ └── solution.py
├── 0992. Subarrays with K Different Integers
│ ├── README.md
│ └── solution.cpp
├── 0993. Cousins in Binary Tree
│ ├── README.md
│ └── solution.cpp
├── 0995. Minimum Number of K Consecutive Bit Flips
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 0997. Find the Town Judge
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 0998. Maximum Binary Tree II
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1004. Max Consecutive Ones III
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1005. Maximize Sum Of Array After K Negations
│ ├── README.md
│ └── solution.cpp
├── 1006. Clumsy Factorial
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1009. Complement of Base 10 Integer
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.kt
│ └── solution.py
├── 1011. Capacity To Ship Packages Within D Days
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1013. Partition Array Into Three Parts With Equal Sum
│ ├── README.md
│ └── solution.go
├── 1014. Best Sightseeing Pair
│ ├── README.md
│ └── solution.py
├── 1020. Number of Enclaves
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1021. Remove Outermost Parentheses
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1022. Sum of Root To Leaf Binary Numbers
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1029. Two City Scheduling
│ ├── README.md
│ └── solution.cpp
├── 1035. Uncrossed Lines
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1036. Escape a Large Maze
│ ├── README.md
│ └── solution.cpp
├── 1037. Valid Boomerang
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1042. Flower Planting With No Adjacent
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1047. Remove All Adjacent Duplicates In String
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1051. Height Checker
│ ├── README.md
│ └── solution.py
├── 1052. Grumpy Bookstore Owner
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1078. Occurrences After Bigram
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1081. Smallest Subsequence of Distinct Characters
│ ├── README.md
│ └── solution.cpp
├── 1089. Duplicate Zeros
│ ├── README.md
│ └── solution.py
├── 1091. Shortest Path in Binary Matrix
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1103. Distribute Candies to People
│ ├── README.md
│ ├── Solution.java
│ └── solution.cpp
├── 1108. Defanging an IP Address
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1109. Corporate Flight Bookings
│ ├── README.md
│ └── solution.cpp
├── 1128. Number of Equivalent Domino Pairs
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1129. Shortest Path with Alternating Colors
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1137. N-th Tribonacci Number
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1143. Longest Common Subsequence
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1154. Day of the Year
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1160. Find Words That Can Be Formed by Characters
│ ├── README.md
│ └── solution.go
├── 1161. Maximum Level Sum of a Binary Tree
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1162. As Far from Land as Possible
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1178. Number of Valid Words for Each Puzzle
│ ├── README.md
│ └── solution.cpp
├── 1184. Distance Between Bus Stops
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1185. Day of the Week
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1189. Maximum Number of Balloons
│ ├── README.md
│ └── solution.cpp
├── 1190. Reverse Substrings Between Each Pair of Parentheses
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1200. Minimum Absolute Difference
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1206. Design Skiplist
│ ├── README.md
│ └── solution.cpp
├── 1208. Get Equal Substrings Within Budget
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1217. Minimum Cost to Move Chips to The Same Position
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1218. Longest Arithmetic Subsequence of Given Difference
│ ├── README.md
│ └── solution.cpp
├── 1220. Count Vowels Permutation
│ ├── README.md
│ └── solution.cpp
├── 1221. Split a String in Balanced Strings
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1232. Check If It Is a Straight Line
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ └── solution.py
├── 1254. Number of Closed Islands
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1260. Shift 2D Grid
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1281. Subtract the Product and Sum of Digits of an Integer
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1282. Group the People Given the Group Size They Belong To
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ └── solution.py
├── 1290. Convert Binary Number in a Linked List to Integer
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1295. Find Numbers with Even Number of Digits
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1302. Deepest Leaves Sum
│ ├── README.md
│ └── solution.py
├── 1305. All Elements in Two Binary Search Trees
│ ├── README.md
│ └── solution.py
├── 1306. Jump Game III
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1309. Decrypt String from Alphabet to Integer Mapping
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1310. XOR Queries of a Subarray
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1313. Decompress Run-Length Encoded List
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1314. Matrix Block Sum
│ ├── README.md
│ └── solution.py
├── 1319. Number of Operations to Make Network Connected
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1331. Rank Transform of an Array
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1337. The K Weakest Rows in a Matrix
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1342. Number of Steps to Reduce a Number to Zero
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1365. How Many Numbers Are Smaller Than the Current Number
│ ├── README.md
│ └── solution.cpp
├── 1370. Increasing Decreasing String
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1374. Generate a String With Characters That Have Odd Counts
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1376. Time Needed to Inform All Employees
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1379. Find a Corresponding Node of a Binary Tree in a Clone of That Tree
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1380. Lucky Numbers in a Matrix
│ ├── README.md
│ └── solution.cpp
├── 1389. Create Target Array in the Given Order
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1403. Minimum Subsequence in Non-Increasing Order
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1408. String Matching in an Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1413. Minimum Value to Get Positive Step by Step Sum
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1414. Find the Minimum Number of Fibonacci Numbers Whose Sum Is K
│ ├── README.md
│ └── solution.cpp
├── 1417. Reformat The String
│ ├── README.md
│ └── solution.py
├── 1422. Maximum Score After Splitting a String
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1423. Maximum Points You Can Obtain from Cards
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1431. Kids With the Greatest Number of Candies
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ └── solution.py
├── 1436. Destination City
│ ├── README.md
│ └── solution.cpp
├── 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1441. Build an Array With Stack Operations
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1442. Count Triplets That Can Form Two Arrays of Equal XOR
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1446. Consecutive Characters
│ ├── README.md
│ └── solution.cpp
├── 1447. Simplified Fractions
│ ├── README.md
│ └── solution.cpp
├── 1450. Number of Students Doing Homework at a Given Time
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1460. Make Two Arrays Equal by Reversing Sub-arrays
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1464. Maximum Product of Two Elements in an Array
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ └── solution.py
├── 1466. Reorder Routes to Make All Paths Lead to the City Zero
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1470. Shuffle the Array
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1475. Final Prices With a Special Discount in a Shop
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1480. Running Sum of 1d Array
│ ├── README.md
│ └── solution.cpp
├── 1486. XOR Operation in an Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1512. Number of Good Pairs
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1518. Water Bottles
│ ├── README.md
│ └── solution.cpp
├── 1528. Shuffle String
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1557. Minimum Number of Vertices to Reach All Nodes
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1567. Maximum Length of Subarray With Positive Product
│ ├── README.md
│ └── solution.py
├── 1572. Matrix Diagonal Sum
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1576. Replace All s to Avoid Consecutive Repeating Characters
│ ├── README.md
│ └── solution.cpp
├── 1582. Special Positions in a Binary Matrix
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1583. Count Unhappy Friends
│ ├── README.md
│ └── solution.cpp
├── 1584. Min Cost to Connect All Points
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1588. Sum of All Odd Length Subarrays
│ ├── README.md
│ └── solution.cpp
├── 1592. Rearrange Spaces Between Words
│ ├── README.md
│ └── solution.py
├── 1598. Crawler Log Folder
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1603. Design Parking System
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1608. Special Array With X Elements Greater Than or Equal X
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1609. Even Odd Three
│ ├── README.md
│ └── solution.cpp
├── 1614. Maximum Nesting Depth of the Parentheses
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1615. Maximal Network Rank
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1619. Mean of Array After Removing Some Elements
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1624. Largest Substring Between Two Equal Characters
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1629. Slowest Key
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1636. Sort Array by Increasing Frequency
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1646. Get Maximum in Generated Array
│ ├── README.md
│ └── solution.cpp
├── 1652. Defuse the Bomb
│ ├── README.md
│ └── solution.py
├── 1654. Minimum Jumps to Reach Home
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1656. Design an Ordered Stream
│ ├── README.md
│ └── solution.py
├── 1662. Check If Two String Arrays are Equivalent
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ └── solution.py
├── 1672. Richest Customer Wealth
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.py
│ └── solution.rs
├── 1678. Goal Parser Interpretation
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1684. Count the Number of Consistent Strings
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ └── solution.py
├── 1688. Count of Matches in Tournament
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1694. Reformat Phone Number
│ ├── README.md
│ └── solution.py
├── 1704. Determine if String Halves Are Alike
│ ├── README.md
│ └── solution.py
├── 1710. Maximum Units on a Truck
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1716. Calculate Money in Leetcode Bank
│ ├── README.md
│ └── solution.cpp
├── 1720. Decode XORed Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1723. Find Minimum Time to Finish All Jobs
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1725. Number Of Rectangles That Can Form The Largest Square
│ ├── README.md
│ └── solution.cpp
├── 1732. Find the Highest Altitude
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1734. Decode XORed Permutation
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1738. Find Kth Largest XOR Coordinate Value
│ ├── README.md
│ └── solution.cpp
├── 1748. Sum of Unique Elements
│ ├── README.md
│ └── solution.cpp
├── 1763. Longest Nice Substring
│ ├── README.md
│ └── solution.cpp
├── 1768. Merge Strings Alternately
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1773. Count Items Matching a Rule
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1779. Find Nearest Point That Has the Same X or Y Coordinate
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1790. Check if One String Swap Can Make Strings Equal
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1791. Find Center of Star Graph
│ ├── README.md
│ └── solution.cpp
├── 1800. Maximum Ascending Subarray Sum
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1812. Determine Color of a Chessboard Square
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1816. Truncate Sentence
│ ├── README.md
│ └── solution.cpp
├── 1822. Sign of the Product of an Array
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ └── solution.py
├── 1823. Find the Winner of the Circular Game
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1827. Minimum Operations to Make the Array Increasing
│ ├── README.md
│ └── solution.cpp
├── 1828. Queries on Number of Points Inside a Circle
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1832. Check if the Sentence Is Pangram
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1844. Replace All Digits with Characters
│ ├── README.md
│ └── solution.cpp
├── 1880. Check if Word Equals Summation of Two Words
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1894. Find the Student that Will Replace the Chalk
│ ├── README.md
│ └── solution.cpp
├── 1905. Count Sub Islands
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1920. Build Array from Permutation
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1926. Nearest Exit from Entrance in Maze
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1929. Concatenation of Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1941. Check if All Characters Have Equal Number of Occurrences
│ ├── README.md
│ └── solution.py
├── 1967. Number of Strings That Appear as Substrings in Word
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1984. Minimum Difference Between Highest and Lowest of K Scores
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 1995. Count Special Quadruplets
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 1996. The Number of Weak Characters in the Game
│ ├── README.md
│ └── solution.cpp
├── 2000. Reverse Prefix of Word
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2006. Count Number of Pairs With Absolute Difference K
│ ├── README.md
│ └── solution.cpp
├── 2011. Final Value of Variable After Performing Operations
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2013. Detect Squares
│ ├── README.md
│ └── solution.cpp
├── 2016. Maximum Difference Between Increasing Elements
│ ├── README.md
│ └── solution.cpp
├── 2022. Convert 1D Array Into 2D Array
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 2024. Maximize the Confusion of an Exam
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2027. Minimum Moves to Convert String
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2028. Find Missing Observations
│ ├── README.md
│ └── solution.cpp
├── 2032. Two Out of Three
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2037. Minimum Number of Moves to Seat Everyone
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2038. Remove Colored Pieces if Both Neighbors are the Same Color
│ ├── README.md
│ └── solution.cpp
├── 2039. The Time When the Network Becomes Idle
│ ├── README.md
│ └── solution.py
├── 2042. Check if Numbers Are Ascending in a Sentence
│ ├── README.md
│ └── solution.py
├── 2043. Simple Bank System
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ ├── solution.py
│ └── solution.rs
├── 2044. Count Number of Maximum Bitwise-OR Subsets
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2047. Number of Valid Words in a Sentence
│ ├── README.md
│ └── solution.cpp
├── 2049. Count Nodes With the Highest Score
│ ├── README.md
│ └── solution.py
├── 2053. Kth Distinct String in an Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2055. Plates Between Candles
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2089. Find Target Indices After Sorting Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2114. Maximum Number of Words Found in Sentences
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2160. Minimum Sum of Four Digit Number After Splitting Digits
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2169. Count Operations to Obtain Zero
│ ├── README.md
│ └── solution.cpp
├── 2180. Count Integers With Even Digit Sum
│ ├── README.md
│ └── solution.cpp
├── 2181. Merge Nodes in Between Zeros
│ ├── README.md
│ └── solution.cpp
├── 2182. Construct String With Repeat Limit
│ ├── README.md
│ └── solution.cpp
├── 2183. Count Array Pairs Divisible by K
│ ├── README.md
│ └── solution.cpp
├── 2185. Counting Words With a Given Prefix
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2210. Count Hills and Valleys in an Array
│ ├── README.md
│ └── solution.py
├── 2215. Find the Difference of Two Arrays
│ ├── README.md
│ └── solution.py
├── 2216. Minimum Deletions to Make Array Beautiful
│ ├── README.md
│ └── solution.py
├── 2217. Find Palindrome With Fixed Length
│ ├── README.md
│ └── solution.py
├── 2220. Minimum Bit Flips to Convert Number
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2224. Minimum Number of Operations to Convert Time
│ ├── README.md
│ └── solution.py
├── 2225. Find Players With Zero or One Losses
│ ├── README.md
│ └── solution.py
├── 2235. Add Two Integers
│ ├── README.md
│ ├── Solution.java
│ ├── Solution.kt
│ ├── solution.cpp
│ ├── solution.go
│ ├── solution.js
│ ├── solution.py
│ └── solution.rs
├── 2236. Root Equals Sum of Children
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2278. Percentage of Letter in String
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2283. Check if Number Has Equal Digit Count and Digit Value
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2287. Rearrange Characters to Make Target String
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2288. Apply Discount to Prices
│ ├── README.md
│ └── solution.py
├── 2290. Minimum Obstacle Removal to Reach Corner
│ ├── README.md
│ └── solution.py
├── 2293. Min Max Game
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2303. Calculate Amount Paid in Taxes
│ ├── README.md
│ └── solution.py
├── 2309. Greatest English Letter in Upper and Lower Case
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2315. Count Asterisks
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 2319. Check if Matrix Is X-Matrix
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2325. Decode the Message
│ ├── README.md
│ └── solution.py
├── 2331. Evaluate Boolean Binary Tree
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2335. Minimum Amount of Time to Fill Cups
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 2341. Maximum Number of Pairs in Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2347. Best Poker Hand
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2351. First Letter to Appear Twice
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2357. Make Array Zero by Subtracting Equal Amounts
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 2363. Merge Similar Items
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2367. Number of Arithmetic Triplets
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2373. Largest Local Values in a Matrix
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 2379. Minimum Recolors to Get K Consecutive Black Blocks
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2396. Strictly Palindromic Number
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2413. Smallest Even Multiple
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2418. Sort the People
│ ├── README.md
│ └── solution.py
├── 2469. Convert the Temperature
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2485. Find the Pivot Integer
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2490. Circular Sentence
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2496. Maximum Value of a String in an Array
│ ├── README.md
│ └── solution.py
├── 2500. Delete Greatest Value in Each Row
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2520. Count the Digits That Divide a Number
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2529. Maximum Count of Positive Integer and Negative Integer
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2535. Difference Between Element Sum and Digit Sum of an Array
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 2544. Alternating Digit Sum
│ ├── README.md
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── 2553. Separate the Digits in an Array
│ ├── README.md
│ ├── solution.cpp
│ └── solution.py
├── 2574. Left and Right Sum Differences
│ ├── README.md
│ ├── Solution.java
│ ├── solution.cpp
│ ├── solution.go
│ └── solution.py
├── add-solution.py
├── generate_leetcode_list.go
├── generate_leetcode_list.py
└── solution-readme-template.md
├── README.md
└── template
├── algorithm
├── Binary Search.md
├── Fast Power.md
├── Fraction.md
├── Permutation.md
├── Shortest Path.md
└── combination_number.cpp
└── language
├── Cpp Template.md
├── Java Template.md
└── Python Template.md
/Data Structure/Graph/Shortest Path/graph.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/InnoFang/algo-set/65ed98dfc84802db627868e8ad648e2b6444ded9/Data Structure/Graph/Shortest Path/graph.png
--------------------------------------------------------------------------------
/Data Structure/Search/Binary Search/main.js:
--------------------------------------------------------------------------------
1 | function binary_search(arr, target) {
2 | let left = 0, right = arr.length - 1
3 | while (left <= right) {
4 | let mid = left + (right - left) / 2
5 | if (arr[mid] == target) return mid
6 | else if (arr[mid] > target) right = mid - 1
7 | else left = mid + 1
8 | }
9 | return -1
10 | }
11 |
12 | const arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
13 | console.log("Where is number 3? " + binary_search(arr, 3))
14 |
--------------------------------------------------------------------------------
/Data Structure/Search/Binary Search/main.py:
--------------------------------------------------------------------------------
1 | def binary_search(arr, target):
2 | left, right = 0, len(arr) - 1
3 | while left <= right:
4 | mid = left + (right - left) // 2
5 | if arr[mid] == target: return mid
6 | elif arr[mid] > target: right = mid - 1
7 | else: left = mid + 1
8 | return -1
9 |
10 | def main():
11 | arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
12 | print('Where is number 3? %d' % binary_search(arr, 3))
13 |
14 | if __name__ == '__main__':
15 | main()
--------------------------------------------------------------------------------
/LeetCode/0001. Two Sum/Solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 29 / 29 test cases passed
3 | * Status: Accepted
4 | * Runtime: 8ms
5 | */
6 | func twoSum(nums []int, target int) []int {
7 | store := make(map[int]int)
8 | for idx, n := range nums {
9 | if diffIdx, ok := store[target - n]; ok {
10 | return []int{diffIdx, idx}
11 | } else {
12 | store[n] = idx
13 | }
14 | }
15 | return []int{}
16 | }
17 |
--------------------------------------------------------------------------------
/LeetCode/0001. Two Sum/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 29 / 29 test cases passed.
3 | Status: Accepted
4 | Runtime: 72 ms
5 | """
6 | class Solution:
7 | def twoSum(self, nums: List[int], target: int) -> List[int]:
8 | store = dict()
9 | for idx, elm in enumerate(nums):
10 | if (target - elm) in store:
11 | return [store[target - elm], idx]
12 | store[elm] = idx
13 |
14 |
--------------------------------------------------------------------------------
/LeetCode/0003. Longest Substring Without Repeating Characters/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 987 / 987 test cases passed.
3 | Runtime: 60 ms
4 | Memory Usage: 15.1 MB
5 | """
6 | class Solution:
7 | def lengthOfLongestSubstring(self, s: str) -> int:
8 | l, ans = 0, 0
9 | store = {}
10 | for r, c in enumerate(s):
11 | if c in store and store[c] >= l:
12 | l = store[c] + 1
13 | ans = max(ans, r - l + 1)
14 | store[c] = r
15 | return ans
16 |
--------------------------------------------------------------------------------
/LeetCode/0007. Reverse Integer/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 1032 / 1032 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.9 MB
5 | */
6 | class Solution {
7 | public:
8 | int reverse(int x) {
9 | int ans = 0;
10 | while (x) {
11 | if (ans < INT_MIN / 10 || ans > INT_MAX / 10) return 0;
12 | ans = ans * 10 + x % 10;
13 | x /= 10;
14 | }
15 | return ans;
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/LeetCode/0012. Integer to Roman/README.md:
--------------------------------------------------------------------------------
1 | # [12. Integer to Roman](https://leetcode.com/problems/integer-to-roman/description/)
2 |
3 | 
4 |
5 |
6 | Topics
7 |
8 | * [`Math`](https://leetcode.com/tag/math/)
9 | * [`String`](https://leetcode.com/tag/string/)
10 |
11 |
12 |
13 |
14 |
15 | Given an integer, convert it to a roman numeral.
16 |
17 | Input is guaranteed to be within the range from 1 to 3999.
--------------------------------------------------------------------------------
/LeetCode/0022. Generate Parentheses/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 8 / 8 test cases passed.
3 | Runtime: 44 ms
4 | Memory Usage: 15.1 MB
5 | """
6 | class Solution:
7 | def generateParenthesis(self, n: int) -> List[str]:
8 | ans = []
9 | def dfs(s, l, r):
10 | if l == 0 and r == 0:
11 | ans.append(s[:])
12 | if l > 0:
13 | dfs(s + "(", l - 1, r)
14 | if l < r:
15 | dfs(s + ")", l, r - 1)
16 | dfs("", n, n)
17 | return ans
18 |
--------------------------------------------------------------------------------
/LeetCode/0026. Remove Duplicates from Sorted Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 161 / 161 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 15.6 MB
5 | """
6 | class Solution:
7 | def removeDuplicates(self, nums: List[int]) -> int:
8 | if not nums:
9 | return 0
10 | size = 0
11 | for num in nums[1:]:
12 | if nums[size] != num:
13 | size += 1
14 | nums[size] = num
15 | return size + 1
16 |
--------------------------------------------------------------------------------
/LeetCode/0027. Remove Element/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 113 / 113 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def removeElement(self, nums: List[int], val: int) -> int:
8 | size = 0
9 | for num in nums:
10 | if num != val:
11 | nums[size] = num
12 | size += 1
13 | return size
14 |
--------------------------------------------------------------------------------
/LeetCode/0045. Jump Game II/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 106 / 106 test cases passed.
3 | * Runtime: 12 ms
4 | * Memory Usage: 5.5 MB
5 | */
6 | func jump(nums []int) int {
7 | farthest, ans, end := 0, 0, 0
8 | for i, num := range nums[:len(nums)-1] {
9 | if i + num > farthest { farthest = num + i }
10 | if i == end {
11 | end = farthest
12 | ans ++
13 | }
14 | }
15 | return ans
16 | }
17 |
--------------------------------------------------------------------------------
/LeetCode/0045. Jump Game II/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 106 / 106 test cases passed.
3 | Runtime: 48 ms
4 | Memory Usage: 15.5 MB
5 | """
6 | class Solution:
7 | def jump(self, nums: List[int]) -> int:
8 | farthest, ans, end = 0, 0, 0
9 | for i, num in enumerate(nums[:-1]):
10 | farthest = max(farthest, i + num)
11 | if i == end:
12 | end = farthest
13 | ans += 1
14 | return ans
15 |
--------------------------------------------------------------------------------
/LeetCode/0053. Maximum Subarray/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 209 / 209 test cases passed.
3 | * Runtime: 1 ms
4 | * Memory Usage: 50.4 MB
5 | */
6 | class Solution {
7 | public int maxSubArray(int[] nums) {
8 | int ans = nums[0], prev = 0;
9 | for (int i = 0; i < nums.length; ++ i) {
10 | int curr = Math.max(nums[i], prev + nums[i]);
11 | ans = Math.max(ans, curr);
12 | prev = curr;
13 | }
14 | return ans;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/LeetCode/0053. Maximum Subarray/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 202 / 202 test cases passed.
3 | * Status: Accepted
4 | * Runtime: 16 ms
5 | */
6 | class Solution {
7 | public:
8 | int maxSubArray(vector& nums) {
9 | vector dp( nums.size(), 0 );
10 | dp[0] = nums[0];
11 | int ans = dp[0];
12 | for ( int i = 1; i < nums.size(); ++ i) {
13 | dp[i] = max( dp[i - 1] + nums[i], nums[i] );
14 | ans = max(ans, dp[i]);
15 | }
16 | return ans;
17 | }
18 | };
19 |
--------------------------------------------------------------------------------
/LeetCode/0053. Maximum Subarray/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 203 / 203 test cases passed.
3 | * Runtime: 4 ms
4 | * Memory Usage: 3.4 MB
5 | */
6 | func maxSubArray(nums []int) int {
7 | dp := make([]int, len(nums)+1)
8 | ans := nums[0]
9 | for i, num := range nums {
10 | dp[i+1] = max(dp[i]+num, num)
11 | ans = max(ans, dp[i+1])
12 | }
13 | return ans
14 | }
15 |
16 | func max(a, b int) int {
17 | if a > b {
18 | return a
19 | }
20 | return b
21 | }
22 |
--------------------------------------------------------------------------------
/LeetCode/0055. Jump Game/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 166 / 166 test cases passed.
3 | * Runtime: 60 ms
4 | * Memory Usage: 47.2 MB
5 | */
6 | class Solution {
7 | public:
8 | bool canJump(vector& nums) {
9 | int farthest = 0;
10 | for (int i = 0; i < nums.size(); ++ i) {
11 | if (i > farthest) return false;
12 | farthest = max(farthest, i + nums[i]);
13 | }
14 | return true;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/0055. Jump Game/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 166 / 166 test cases passed.
3 | * Runtime: 56 ms
4 | * Memory Usage: 6.9 MB
5 | */
6 | func canJump(nums []int) bool {
7 | farthest := 0
8 | for i, num := range nums {
9 | if i > farthest { return false }
10 | if i + num > farthest { farthest = i + num }
11 | }
12 | return true
13 | }
14 |
--------------------------------------------------------------------------------
/LeetCode/0055. Jump Game/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 166 / 166 test cases passed.
3 | Runtime: 92 ms
4 | Memory Usage: 15.4 MB
5 | """
6 | class Solution:
7 | def canJump(self, nums: List[int]) -> bool:
8 | farthest = 0
9 | for i, num in enumerate(nums):
10 | if i > farthest:
11 | return False
12 | farthest = max(farthest, i + num)
13 | return True
14 |
--------------------------------------------------------------------------------
/LeetCode/0058. Length of Last Word/Solution.kt:
--------------------------------------------------------------------------------
1 | /**
2 | * 59 / 59 test cases passed.
3 | * Status: Accepted
4 | * Runtime: 380 ms
5 | */
6 | import java.util.regex.Pattern
7 | class Solution {
8 | fun lengthOfLastWord(s: String): Int {
9 | return s.trim().split(Pattern.compile("\\s+")).last().length
10 | }
11 | }
--------------------------------------------------------------------------------
/LeetCode/0058. Length of Last Word/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 58 / 58 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 2.1 MB
5 | */
6 | func lengthOfLastWord(s string) int {
7 | i, ans := len(s) - 1, 0
8 | for ; i >= 0 && s[i] == ' '; i-- {}
9 | for ; i >= 0 && s[i] != ' '; i-- { ans++ }
10 | return ans
11 | }
12 |
--------------------------------------------------------------------------------
/LeetCode/0070. Climbing Stairs/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 45 / 45 test cases passed.
3 | * Status: Accepted
4 | * Runtime: 0 ms
5 | */
6 | class Solution {
7 | public:
8 | int climbStairs(int n) {
9 | if (n <= 2) return n;
10 | int p = 1, q = 2, ans;
11 | for (int i = 3; i <= n; ++ i) {
12 | ans = p + q;
13 | p = q;
14 | q = ans;
15 | }
16 | return ans;
17 | }
18 | };
19 |
--------------------------------------------------------------------------------
/LeetCode/0078. Subsets/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 10 / 10 test cases passed.
3 | Runtime: 52 ms
4 | Memory Usage: 15.1 MB
5 | """
6 | class Solution:
7 | def subsets(self, nums: List[int]) -> List[List[int]]:
8 | ans, path = [], []
9 | def backtrack(start, path):
10 | ans.append(path[:])
11 | for i in range(start, len(nums)):
12 | path.append(nums[i])
13 | backtrack(i + 1, path)
14 | path.pop();
15 | backtrack(0, path)
16 | return ans
17 |
--------------------------------------------------------------------------------
/LeetCode/0080. Remove Duplicates from Sorted Array II/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 164 / 164 test cases passed.
3 | Runtime: 44 ms
4 | Memory Usage: 14.6 MB
5 | """
6 | class Solution:
7 | def removeDuplicates(self, nums: List[int]) -> int:
8 | size = 0
9 | for num in nums:
10 | if size < 2 or nums[size - 2] != num:
11 | nums[size] = num
12 | size += 1
13 | return size
--------------------------------------------------------------------------------
/LeetCode/0088. Merge Sorted Array/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 59 / 59 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 8.9 MB
5 | */
6 | class Solution {
7 | public:
8 | void merge(vector& nums1, int m, vector& nums2, int n) {
9 | int i = m - 1, j = n - 1, k = m + n - 1;
10 | while (j >= 0) {
11 | nums1[k--] = i >= 0 && nums1[i] > nums2[j] ? nums1[i--] : nums2[j--];
12 | }
13 | }
14 | };
15 |
--------------------------------------------------------------------------------
/LeetCode/0089. Gray Code/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 16 / 16 test cases passed.
3 | * Runtime: 4 ms
4 | * Memory Usage: 18.3 MB
5 | */
6 | class Solution {
7 | public:
8 | vector grayCode(int n) {
9 | vector ans(1 << n);
10 | for (int i = 0; i < ans.size(); ++ i) {
11 | ans[i] = i ^ (i >> 1);
12 | }
13 | return ans;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/LeetCode/0089. Gray Code/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 16 / 16 test cases passed.
3 | * Runtime: 8 ms
4 | * Memory Usage: 6.7 MB
5 | */
6 | func grayCode(n int) []int {
7 | ans := make([]int, 0, 1 << n)
8 | for i := 0; i < cap(ans); i++ {
9 | ans = append(ans, i ^ (i >> 1))
10 | }
11 | return ans
12 | }
13 |
--------------------------------------------------------------------------------
/LeetCode/0089. Gray Code/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 16 / 16 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 18.3 MB
5 | """
6 | class Solution:
7 | def grayCode(self, n: int) -> List[int]:
8 | return [i ^ i>>1 for i in range(1 << n)]
9 |
--------------------------------------------------------------------------------
/LeetCode/0096. Unique Binary Search Trees/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 19 / 19 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def numTrees(self, n: int) -> int:
8 | dp = [0] * (n + 1)
9 | dp[0] = dp[1] = 1
10 | for i in range(2, n + 1):
11 | for j in range(i + 1):
12 | dp[i] += dp[j - 1] * dp[i - j]
13 | return dp[n]
14 |
--------------------------------------------------------------------------------
/LeetCode/0104. Maximum Depth of Binary Tree/solution.py:
--------------------------------------------------------------------------------
1 | # Definition for a binary tree node.
2 | # class TreeNode:
3 | # def __init__(self, val=0, left=None, right=None):
4 | # self.val = val
5 | # self.left = left
6 | # self.right = right
7 |
8 | """
9 | 39 / 39 test case passed
10 | Status: Accepted
11 | Runtime: 56 ms
12 | """
13 | class Solution:
14 | def maxDepth(self, root: TreeNode) -> int:
15 | return 0 if root is None else max(self.maxDepth(root.left), self.maxDepth(root.right)) + 1
--------------------------------------------------------------------------------
/LeetCode/0119. Pascal's Triangle II/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 34 / 34 test cases passed.
3 | * Status: Accepted
4 | * Runtime: 0 ms
5 | */
6 | class Solution {
7 | public:
8 | vector getRow(int rowIndex) {
9 | vector row(rowIndex + 1);
10 | row[0] = 1;
11 | for (int i = 1; i < row.size(); ++ i) {
12 | row[i] = 1LL * row[i - 1] * (rowIndex - i + 1) / i;
13 | }
14 | return row;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/0119. Pascal's Triangle II/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 34 / 34 test cases passed.
3 | Status: Accepted
4 | Runtime: 40 ms
5 | """
6 | class Solution:
7 | def getRow(self, rowIndex: int) -> List[int]:
8 | row = [1]
9 | for i in range(1, rowIndex + 1):
10 | row.append(row[i - 1] * (rowIndex - i + 1) // i)
11 | return row
12 |
--------------------------------------------------------------------------------
/LeetCode/0120. Triangle/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 44 / 44 test cases passed.
3 | Runtime: 56 ms
4 | Memory Usage: 15.8 MB
5 | """
6 | class Solution:
7 | def minimumTotal(self, triangle: List[List[int]]) -> int:
8 | for i in range(len(triangle) - 2, -1, -1):
9 | for j in range(len(triangle[i])):
10 | triangle[i][j] += min(triangle[i + 1][j], triangle[i + 1][j + 1])
11 | return triangle[0][0]
12 |
--------------------------------------------------------------------------------
/LeetCode/0121. Best Time to Buy and Sell Stock/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 200 / 200 test cases passed
3 | * Status: Accepted
4 | * Runtime: 4 ms
5 | */
6 | func maxProfit(prices []int) int {
7 | min, profit := int(^uint(0) >> 1), 0
8 | for _, p := range prices {
9 | if min > p {
10 | min = p
11 | } else {
12 | diff := p - min
13 | if diff > profit {
14 | profit = diff
15 | }
16 | }
17 | }
18 | return profit
19 | }
--------------------------------------------------------------------------------
/LeetCode/0121. Best Time to Buy and Sell Stock/solution.js:
--------------------------------------------------------------------------------
1 | /**
2 | * 200 / 200 test cases passed
3 | * Status: Accepted
4 | * Runtime: 64 ms
5 | *
6 | * @param {number[]} prices
7 | * @return {number}
8 | */
9 | var maxProfit = function(prices) {
10 | [min, profit] = [Number.MAX_SAFE_INTEGER, 0]
11 | for (let p of prices) {
12 | min = Math.min(min, p)
13 | profit = Math.max(profit, p - min)
14 | }
15 | return profit
16 | };
--------------------------------------------------------------------------------
/LeetCode/0121. Best Time to Buy and Sell Stock/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 200 / 200 test cases passed.
3 | Status: Accepted
4 | Runtime: 36 ms
5 | """
6 | class Solution:
7 | def maxProfit(self, prices: List[int]) -> int:
8 | minP, profit = float('inf'), 0
9 | for i in range(len(prices)):
10 | minP = min(minP, prices[i])
11 | profit = max(profit, prices[i] - minP)
12 | return profit
--------------------------------------------------------------------------------
/LeetCode/0121. Best Time to Buy and Sell Stock/solution.rs:
--------------------------------------------------------------------------------
1 | /**
2 | * 211 / 211 test cases passed.
3 | * Runtime: 12 ms
4 | * Memory Usage: 2.9 MB
5 | */
6 | impl Solution {
7 | pub fn max_profit(prices: Vec) -> i32 {
8 | let (mut buy, mut sell) = (prices[0], 0);
9 | for i in (1..prices.len()) {
10 | sell = sell.max(prices[i] - buy);
11 | buy = buy.min(prices[i]);
12 | }
13 | sell
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/LeetCode/0122. Best Time to Buy and Sell Stock II/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 200 / 200 test cases passed.
3 | * Runtime: 8 ms
4 | * Memory Usage: 12.8 MB
5 | */
6 | class Solution {
7 | public:
8 | int maxProfit(vector& prices) {
9 | int ans = 0;
10 | for (int i = 1; i < prices.size(); ++ i) {
11 | if (prices[i] > prices[i - 1]) {
12 | ans += (prices[i] - prices[i - 1]);
13 | }
14 | }
15 | return ans;
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/LeetCode/0122. Best Time to Buy and Sell Stock II/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 200 / 200 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 15.9 MB
5 | """
6 | class Solution:
7 | def maxProfit(self, prices: List[int]) -> int:
8 | ans = 0
9 | for i in range(1, len(prices)):
10 | if prices[i] > prices[i - 1]:
11 | ans += prices[i] - prices[i - 1]
12 | return ans
13 |
--------------------------------------------------------------------------------
/LeetCode/0137. Single Number II/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 14 / 14 test cases passed.
3 | Runtime: 100 ms
4 | Memory Usage: 15.1 MB
5 | """
6 | class Solution:
7 | def singleNumber(self, nums: List[int]) -> int:
8 | return Counter(nums).most_common()[-1][0]
9 |
--------------------------------------------------------------------------------
/LeetCode/0148. Sort List/README.md:
--------------------------------------------------------------------------------
1 | # [148. Sort List](https://leetcode.com/problems/sort-list/description/)
2 |
3 | 
4 |
5 |
6 | Topics
7 |
8 | * [`Linked List`](https://leetcode.com/tag/linked-list/)
9 | * [`Sort`](https://leetcode.com/tag/sort)
10 |
11 |
12 |
13 |
14 |
15 | Sort a linked list in O(_n_ log _n_) time using constant space complexity.
--------------------------------------------------------------------------------
/LeetCode/0152. Maximum Product Subarray/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 188 / 188 test cases passed.
3 | Runtime: 72 ms
4 | Memory Usage: 15.5 MB
5 | """
6 | class Solution:
7 | def maxProduct(self, nums: List[int]) -> int:
8 | dp = float('-inf')
9 | imax = imin = 1
10 | for num in nums:
11 | if num < 0:
12 | imax, imin = imin, imax
13 | imax = max(imax * num, num)
14 | imin = min(imin * num, num)
15 | dp = max(dp, imax)
16 | return dp
17 |
--------------------------------------------------------------------------------
/LeetCode/0165. Compare Version Numbers/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 81 / 81 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def compareVersion(self, version1: str, version2: str) -> int:
8 | for v1, v2 in zip_longest(version1.split('.'), version2.split('.'), fillvalue=0):
9 | x, y = int(v1), int(v2)
10 | if x != y:
11 | return 1 if x > y else -1
12 | return 0
13 |
--------------------------------------------------------------------------------
/LeetCode/0168. Excel Sheet Column Title/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 18 / 18 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.9 MB
5 | */
6 | class Solution {
7 | public:
8 | string convertToTitle(int columnNumber) {
9 | vector ans;
10 | while (columnNumber != 0) {
11 | columnNumber --;
12 | ans.emplace_back(char('A' + (columnNumber % 26)));
13 | columnNumber /= 26;
14 | }
15 | return string(ans.rbegin(), ans.rend());
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/LeetCode/0168. Excel Sheet Column Title/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 18 / 18 test cases passed.
3 | Runtime: 28 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def convertToTitle(self, columnNumber: int) -> str:
8 | ans = []
9 | while columnNumber != 0:
10 | columnNumber -= 1
11 | ans.append(chr(65 + columnNumber % 26))
12 | columnNumber //= 26
13 | return ''.join(ans[::-1])
14 |
--------------------------------------------------------------------------------
/LeetCode/0172. Factorial Trailing Zeroes/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 500 / 500 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 38.7 MB
5 | */
6 | class Solution {
7 | public int trailingZeroes(int n) {
8 | int ans = 0;
9 | while (n > 0) {
10 | ans += n / 5;
11 | n /= 5;
12 | }
13 | return ans;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/LeetCode/0172. Factorial Trailing Zeroes/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 500 / 500 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.9 MB
5 | */
6 | class Solution {
7 | public:
8 | int trailingZeroes(int n) {
9 | int ans = 0;
10 | while (n > 0) {
11 | ans += n / 5;
12 | n /= 5;
13 | }
14 | return ans;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/0172. Factorial Trailing Zeroes/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 500 / 500 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 1.8 MB
5 | */
6 | func trailingZeroes(n int) int {
7 | ans := 0
8 | for n > 0 {
9 | ans += n / 5
10 | n /= 5
11 | }
12 | return ans
13 | }
14 |
--------------------------------------------------------------------------------
/LeetCode/0172. Factorial Trailing Zeroes/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 500 / 500 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def trailingZeroes(self, n: int) -> int:
8 | ans = 0
9 | while n > 0:
10 | ans += n // 5
11 | n //= 5
12 | return ans
13 |
--------------------------------------------------------------------------------
/LeetCode/0172. Factorial Trailing Zeroes/solution.rs:
--------------------------------------------------------------------------------
1 | /**
2 | * 500 / 500 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 2 MB
5 | */
6 | impl Solution {
7 | pub fn trailing_zeroes(mut n: i32) -> i32 {
8 | let mut ans = 0;
9 | while n > 0 {
10 | ans += n / 5;
11 | n /= 5;
12 | }
13 | ans
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/LeetCode/0179. Largest Number/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 229 / 229 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def largestNumber(self, nums: List[int]) -> str:
8 | from functools import cmp_to_key
9 | cmp = lambda a, b: int(b + a) - int(a + b)
10 | ans = sorted(map(str, nums), key=cmp_to_key(cmp))
11 | return '0' if ans[0] == '0' else ''.join(ans)
12 |
--------------------------------------------------------------------------------
/LeetCode/0190. Reverse Bits/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 600 / 600 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.9 MB
5 | */
6 | class Solution {
7 | public:
8 | uint32_t reverseBits(uint32_t n) {
9 | uint32_t ans = 0;
10 | for (int i = 32; i --> 0;) {
11 | ans = ans << 1 | n & 1;
12 | n >>= 1;
13 | }
14 | return ans;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/0190. Reverse Bits/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 600 / 600 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 14.7 MB
5 | """
6 | class Solution:
7 | def reverseBits(self, n: int) -> int:
8 | ans = 0
9 | for i in range(32):
10 | ans = ans << 1 | n & 1
11 | n >>= 1
12 | return ans
13 |
--------------------------------------------------------------------------------
/LeetCode/0201. Bitwise AND of Numbers Range/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 8268 / 8268 test cases passed.
3 | Runtime: 52 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def rangeBitwiseAnd(self, left: int, right: int) -> int:
8 | shift = 0
9 | while left < right:
10 | left >>= 1
11 | right >>= 1
12 | shift += 1
13 | return right << shift
14 |
--------------------------------------------------------------------------------
/LeetCode/0204. Count Primes/README.md:
--------------------------------------------------------------------------------
1 | # [204. Count Primes](https://leetcode.com/problems/count-primes/description/)
2 |
3 | 
4 |
5 |
6 | Topics
7 |
8 | * [`Hash Table`](https://leetcode.com/tag/hash-table/)
9 | * [`Math`](https://leetcode.com/tag/math/)
10 |
11 |
12 |
13 |
14 |
15 | **Description:**
16 |
17 | Count the number of prime numbers less than a non-negative number, _**n**_.
--------------------------------------------------------------------------------
/LeetCode/0205. Isomorphic Strings/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 32 / 32 test cases passed.
3 | * Status: Accepted
4 | * Runtime: 4 ms
5 | */
6 | class Solution {
7 | public:
8 | bool isIsomorphic(string s, string t) {
9 | for ( int i = 0; i < s.size(); ++ i )
10 | if ( s.find( s[i] ) != t.find( t[i] ) )
11 | return false;
12 | return true;
13 | }
14 | };
15 |
16 |
--------------------------------------------------------------------------------
/LeetCode/0206. Reverse Linked List/README.md:
--------------------------------------------------------------------------------
1 | # [206. Reverse Linked List](https://leetcode.com/problems/reverse-linked-list/description/)
2 |
3 | 
4 |
5 |
6 | Related Topics
7 |
8 | * [`Linked List`](https://leetcode.com/tag/linked-list/)
9 |
10 |
11 |
12 |
13 | Reverse a singly linked list.
--------------------------------------------------------------------------------
/LeetCode/0217. Contains Duplicate/Solution.kt:
--------------------------------------------------------------------------------
1 | /**
2 | * 18 / 18 test cases passed.
3 | * Status: Accepted
4 | * Runtime: 496 ms
5 | */
6 | class Solution {
7 | fun containsDuplicate(nums: IntArray): Boolean {
8 | return nums.toHashSet().size < nums.size
9 | }
10 | }
--------------------------------------------------------------------------------
/LeetCode/0217. Contains Duplicate/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 16 / 16 test cases passed.
3 | Runtime: 56 ms
4 | Memory Usage: 20.2 MB
5 | """
6 | class Solution:
7 | def containsDuplicate(self, nums: List[int]) -> bool:
8 | return len(set(nums)) != len(nums)
9 |
10 |
--------------------------------------------------------------------------------
/LeetCode/0219. Contains Duplicate II/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 23 / 23 test cases passed.
3 | Runtime: 48 ms
4 | Memory Usage: 22.3 MB
5 | """
6 | class Solution:
7 | def containsNearbyDuplicate(self, nums: List[int], k: int) -> bool:
8 | store = {}
9 | for i, num in enumerate(nums):
10 | if num in store and i - store[num] <= k:
11 | return True
12 | store[num] = i
13 | return False
14 |
--------------------------------------------------------------------------------
/LeetCode/0231. Power of Two/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 1108 / 1108 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.8 MB
5 | */
6 | class Solution {
7 | public:
8 | bool isPowerOfTwo(int n) {
9 | return n > 0 && (n & (n - 1)) == 0;
10 | }
11 | };
12 |
--------------------------------------------------------------------------------
/LeetCode/0231. Power of Two/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 1108 / 1108 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | larger = 2 << 30
8 | def isPowerOfTwo(self, n: int) -> bool:
9 | return n > 0 and Solution.larger % n == 0
10 |
11 |
12 | """
13 | 1108 / 1108 test cases passed.
14 | Runtime: 44 ms
15 | Memory Usage: 14.6 MB
16 | """
17 | class Solution2:
18 | def isPowerOfTwo(self, n: int) -> bool:
19 | return n > 0 and n & n - 1 == 0
20 |
--------------------------------------------------------------------------------
/LeetCode/0237. Delete Node in a Linked List/Solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for singly-linked list.
3 | * type ListNode struct {
4 | * Val int
5 | * Next *ListNode
6 | * }
7 | */
8 |
9 | /**
10 | * 41 / 41 test cases passed
11 | * Status: Accepted
12 | * Runtime: 4 ms
13 | */
14 | func deleteNode(node *ListNode) {
15 | node.Val = node.Next.Val
16 | node.Next = node.Next.Next
17 | }
18 |
--------------------------------------------------------------------------------
/LeetCode/0258. Add Digits/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 1101 / 1101 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.8 MB
5 | */
6 | class Solution {
7 | public:
8 | int addDigits(int num) {
9 | int tmp = 0;
10 | while (num >= 10) {
11 | tmp = 0;
12 | while (num) {
13 | tmp += num % 10;
14 | num /= 10;
15 | }
16 | num = tmp;
17 | }
18 | return num;
19 | }
20 | };
21 |
--------------------------------------------------------------------------------
/LeetCode/0258. Add Digits/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 1101 / 1101 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def addDigits(self, num: int) -> int:
8 | while num >= 10:
9 | tmp = 0
10 | while num:
11 | tmp += num % 10
12 | num //= 10
13 | num = tmp
14 | return num
15 |
--------------------------------------------------------------------------------
/LeetCode/0263. Ugly Number/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 1013 / 1013 test cases passed.
3 | * Runtime: 4 ms
4 | * Memory Usage: 5.8 MB
5 | */
6 | class Solution {
7 | public:
8 | bool isUgly(int n) {
9 | if (n <= 0) return false;
10 | while (n % 5 == 0) n /= 5;
11 | while (n % 3 == 0) n /= 3;
12 | while (n % 2 == 0) n /= 2;
13 | return n == 1;
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/LeetCode/0263. Ugly Number/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 1013 / 1013 test cases passed.
3 | Runtime: 44 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def isUgly(self, n: int) -> bool:
8 | if n <= 0:
9 | return False
10 | while not n % 5:
11 | n /= 5
12 | while not n % 3:
13 | n /= 3
14 | while not n % 2:
15 | n /= 2
16 | return n == 1
17 |
--------------------------------------------------------------------------------
/LeetCode/0279. Perfect Squares/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 588 / 588 test cases passed.
3 | Runtime: 3848 ms
4 | Memory Usage: 15.3 MB
5 | """
6 | class Solution:
7 | def numSquares(self, n: int) -> int:
8 | dp = list(range(n + 1))
9 | for i in range(1, n):
10 | if i * i > n: break
11 | for j in range(i * i, n + 1):
12 | dp[j] = min(dp[j], dp[j - i * i] + 1)
13 | return dp[-1]
14 |
--------------------------------------------------------------------------------
/LeetCode/0290. Word Pattern/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 36 / 36 test cases passed.
3 | Status: Accepted
4 | Runtime: 44 ms
5 | """
6 | class Solution:
7 | def wordPattern(self, pattern: str, s: str) -> bool:
8 | s = s.split()
9 | return list(map(pattern.index, pattern)) == list(map(s.index, s))
10 |
--------------------------------------------------------------------------------
/LeetCode/0292. Nim Game/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 60 / 60 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 34.9 MB
5 | */
6 | class Solution {
7 | public boolean canWinNim(int n) {
8 | return n % 4 != 0;
9 | }
10 | }
--------------------------------------------------------------------------------
/LeetCode/0292. Nim Game/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 60 / 60 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.7 MB
5 | */
6 | class Solution {
7 | public:
8 | bool canWinNim(int n) {
9 | return n % 4 != 0;
10 | }
11 | };
12 |
--------------------------------------------------------------------------------
/LeetCode/0292. Nim Game/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 60 / 60 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.9 MB
5 | */
6 | func twoSum(nums []int, target int) []int {
7 | store := make(map[int]int)
8 | for idx, n := range nums {
9 | if diffIdx, ok := store[target - n]; ok {
10 | return []int{diffIdx, idx}
11 | } else {
12 | store[n] = idx
13 | }
14 | }
15 | return []int{}
16 | }
17 |
--------------------------------------------------------------------------------
/LeetCode/0292. Nim Game/solution.js:
--------------------------------------------------------------------------------
1 | /**
2 | * 60 / 60 test cases passed.
3 | * Runtime: 60 ms
4 | * Memory Usage: 37.6 MB
5 | *
6 | * @param {number} n
7 | * @return {boolean}
8 | */
9 | var canWinNim = function(n) {
10 | return n % 4 != 0;
11 | };
12 |
--------------------------------------------------------------------------------
/LeetCode/0292. Nim Game/solution.kt:
--------------------------------------------------------------------------------
1 | /**
2 | * 60 / 60 test cases passed.
3 | * Runtime: 132 ms
4 | * Memory Usage: 32.2 MB
5 | */
6 | class Solution {
7 | fun canWinNim(n: Int): Boolean {
8 | return n % 4 != 0
9 | }
10 | }
--------------------------------------------------------------------------------
/LeetCode/0292. Nim Game/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 60 / 60 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def canWinNim(self, n: int) -> bool:
8 | return n % 4 != 0
9 |
10 |
--------------------------------------------------------------------------------
/LeetCode/0300. Longest Increasing Subsequence/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 54 / 54 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def lengthOfLIS(self, nums: List[int]) -> int:
8 | record = [nums[0]]
9 | for num in nums[1:]:
10 | if num > record[-1]:
11 | record.append(num)
12 | else:
13 | record[ bisect.bisect_left(record, num) ] = num
14 | return len(record)
15 |
--------------------------------------------------------------------------------
/LeetCode/0322. Coin Change/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 188 / 188 test cases passed.
3 | Runtime: 1116 ms
4 | Memory Usage: 15.1 MB
5 | """
6 | class Solution:
7 | def coinChange(self, coins: List[int], amount: int) -> int:
8 | dp = [amount + 1] * (amount + 1)
9 | dp[0] = 0
10 | for coin in coins:
11 | for x in range(coin, amount + 1):
12 | dp[x] = min(dp[x], dp[x - coin] + 1)
13 | return dp[amount] if dp[amount] <= amount else -1
14 |
--------------------------------------------------------------------------------
/LeetCode/0326. Power of Three/README.md:
--------------------------------------------------------------------------------
1 | # [326. Power of Three](https://leetcode.com/problems/power-of-three/description/)
2 |
3 | 
4 |
5 |
6 | Topics
7 |
8 | * [`Math`](https://leetcode.com/tag/math/)
9 |
10 |
11 |
12 |
13 |
14 | Given an integer, write a function to determine if it is a power of three.
15 |
16 | **Follow up:**
17 |
18 | Could you do it without using any loop / recursion?
--------------------------------------------------------------------------------
/LeetCode/0326. Power of Three/Solution.kt:
--------------------------------------------------------------------------------
1 | /**
2 | * Created by Inno Fang on 2018/4/10.
3 | */
4 |
5 | class Solution {
6 | fun isPowerOfThree(n: Int): Boolean {
7 | if (n == 0) return false
8 | val res = Math.log10(n.toDouble()) / Math.log10(3.0)
9 | return (res - res.toInt()) < 1e-10
10 | }
11 | }
--------------------------------------------------------------------------------
/LeetCode/0338. Counting Bits/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 15 / 15 test cases passed.
3 | Runtime: 84 ms
4 | Memory Usage: 21.4 MB
5 | """
6 | class Solution:
7 | def countBits(self, num: int) -> List[int]:
8 | dp = [0] * (num + 1)
9 | for i in range(num + 1):
10 | dp[i] = dp[i >> 1] + (i & 1)
11 | return dp
12 |
--------------------------------------------------------------------------------
/LeetCode/0342. Power of Four/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 1061 / 1061 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 6 MB
5 | */
6 | class Solution {
7 | public:
8 | bool isPowerOfFour(int n) {
9 | if (n <= 0) return false;
10 | double res = log(n) / log(4.0);
11 | return (res - int(res)) < 1e-6;
12 | }
13 | };
14 |
--------------------------------------------------------------------------------
/LeetCode/0342. Power of Four/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 1061 / 1061 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def isPowerOfFour(self, n: int) -> bool:
8 | if n <= 0:
9 | return False
10 | res = math.log(n) / math.log(4)
11 | return (res - int(res)) < 1e-6
12 |
--------------------------------------------------------------------------------
/LeetCode/0343. Integer Break/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 50 / 50 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def integerBreak(self, n: int) -> int:
8 | dp = [0] * (n + 1)
9 | for i in range(2, n + 1):
10 | for j in range(i):
11 | dp[i] = max(dp[i], j * (i - j), j * dp[i - j])
12 | return dp[n]
13 |
--------------------------------------------------------------------------------
/LeetCode/0349. Intersection of Two Arrays/Solution.kt:
--------------------------------------------------------------------------------
1 | class Solution {
2 | fun intersection(nums1: IntArray, nums2: IntArray): IntArray {
3 | val res = nums1.toHashSet()
4 | res.retainAll(nums2.toHashSet())
5 | return res.toIntArray()
6 | }
7 | }
8 |
9 | fun main(args: Array) {
10 | Solution().intersection(intArrayOf(1, 2, 3, 1), intArrayOf(2, 3)).forEach { print("$it ") };println()
11 | }
12 |
--------------------------------------------------------------------------------
/LeetCode/0357. Count Numbers with Unique Digits/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 9 / 9 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def countNumbersWithUniqueDigits(self, n: int) -> int:
8 | if n == 0: return 1
9 | if n == 1: return 10
10 | return 9 * math.perm(9, n - 1) + self.countNumbersWithUniqueDigits(n - 1
11 |
--------------------------------------------------------------------------------
/LeetCode/0365. Water and Jug Problem/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 34 / 34 test case passed
3 | * Status: Accepted
4 | * Runtime: 0 ms
5 | */
6 | func canMeasureWater(x int, y int, z int) bool {
7 | if x + y < z {
8 | return false
9 | }
10 | if x == 0 || y == 0 {
11 | return z == 0 || x + y == 0
12 | }
13 | return z % gcd(x, y) == 0
14 | }
15 |
16 | func gcd(x, y int) int {
17 | if y == 0 {
18 | return x
19 | }
20 | return gcd(y, x % y)
21 | }
22 |
--------------------------------------------------------------------------------
/LeetCode/0365. Water and Jug Problem/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 28 / 28 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def canMeasureWater(self, x: int, y: int, z: int) -> bool:
8 | if x + y < z: return False
9 | if x == 0 or y == 0: return z == 0 or x + y == z
10 | return z % math.gcd(x, y) == 0
11 |
--------------------------------------------------------------------------------
/LeetCode/0371. Sum of Two Integers/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 13 / 13 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 34.9 MB
5 | */
6 | class Solution {
7 | public int getSum(int a, int b) {
8 | return a == 0 ? b : getSum((a & b) << 1, a ^ b);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/LeetCode/0371. Sum of Two Integers/Solution.kt:
--------------------------------------------------------------------------------
1 | /**
2 | * Created by Inno Fang on 2017/12/8.
3 | */
4 | class Solution {
5 | fun getSum(a: Int, b: Int): Int {
6 | var sum = a
7 | var carry = b
8 | while (carry != 0) {
9 | val temp = sum
10 | sum = temp xor carry
11 | carry = (temp and carry) shl 1
12 | }
13 | return sum
14 | }
15 | }
16 |
17 | fun main(args: Array) {
18 | Solution().getSum(2, 4).let(::println)
19 | }
--------------------------------------------------------------------------------
/LeetCode/0377. Combination Sum IV/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 15 / 15 test cases passed.
3 | Runtime: 48 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def combinationSum4(self, nums: List[int], target: int) -> int:
8 | dp = [1] + [0] * target
9 | for i in range(1, target + 1):
10 | for num in nums:
11 | if i >= num:
12 | dp[i] += dp[i - num]
13 | return dp[target]
14 |
--------------------------------------------------------------------------------
/LeetCode/0390. Elimination Game/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 3377 / 3377 test cases passed.
3 | * Runtime: 8 ms
4 | * Memory Usage: 2.7 MB
5 | */
6 | func lastRemaining(n int) int {
7 | head, step, left := 1, 1, true
8 | for ; n > 1; n >>= 1 {
9 | if left || n & 1 == 1 {
10 | head += step
11 | }
12 | step <<= 1
13 | left = !left
14 | }
15 | return head
16 | }
17 |
--------------------------------------------------------------------------------
/LeetCode/0390. Elimination Game/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 3377 / 3377 test cases passed.
3 | Runtime: 44 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def lastRemaining(self, n: int) -> int:
8 | head, step, left = 1, 1, True
9 | while n > 1:
10 | if left or n & 1 == 1:
11 | head += step
12 | step <<= 1
13 | n >>= 1
14 | left = not left
15 | return head
16 |
--------------------------------------------------------------------------------
/LeetCode/0392. Is Subsequence/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 17 / 17 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 6.3 MB
5 | */
6 | class Solution {
7 | public:
8 | bool isSubsequence(string s, string t) {
9 | int i = 0, j = 0;
10 | while (i < s.size() && j < t.size()) {
11 | if (s[i] == t[j]) ++ i;
12 | ++ j;
13 | }
14 | return i == s.size();
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/0396. Rotate Function/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 58 / 58 test cases passed.
3 | Runtime: 304 ms
4 | Memory Usage: 21.9 MB
5 | """
6 | class Solution:
7 | def maxRotateFunction(self, nums: List[int]) -> int:
8 | n = len(nums)
9 | if n == 1: return 0
10 | total = sum(nums)
11 | temp = sum(i * nums[i] for i in range(n))
12 | ans = temp
13 | for i in range(n - 1):
14 | temp = temp - total + nums[i] * n
15 | ans = max(ans, temp)
16 | return ans
17 |
--------------------------------------------------------------------------------
/LeetCode/0412. Fizz Buzz/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 8 / 8 test cases passed.
3 | Runtime: 28 ms
4 | Memory Usage: 15.2 MB
5 | """
6 | class Solution:
7 | def fizzBuzz(self, n: int) -> List[str]:
8 | return ["FizzBuzz" if i % 3 == 0 and i % 5 == 0 else \
9 | "Fizz" if i % 3 == 0 else \
10 | "Buzz" if i % 5 == 0 else \
11 | str(i) for i in range(1, n + 1)]
12 |
--------------------------------------------------------------------------------
/LeetCode/0413. Arithmetic Slices/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 15 / 15 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def numberOfArithmeticSlices(self, nums: List[int]) -> int:
8 | n = len(nums)
9 | if n < 3:
10 | return 0
11 | dp = [0] * n
12 | for i in range(1, n - 1):
13 | if nums[i] - nums[i - 1] == nums[i + 1] - nums[i]:
14 | dp[i + 1] = dp[i] + 1
15 | return sum(dp)
16 |
--------------------------------------------------------------------------------
/LeetCode/0435. Non overlapping Intervals/solution.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/InnoFang/algo-set/65ed98dfc84802db627868e8ad648e2b6444ded9/LeetCode/0435. Non overlapping Intervals/solution.cpp
--------------------------------------------------------------------------------
/LeetCode/0441. Arranging Coins/Solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 1335 / 1335 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.9 MB
5 | */
6 | class Solution {
7 | public:
8 | int arrangeCoins(int n) {
9 | return static_cast((-1 + sqrt(1 + 8.0 * n)) / 2);
10 | }
11 | };
12 |
--------------------------------------------------------------------------------
/LeetCode/0441. Arranging Coins/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 1335 / 1335 test cases passed.
3 | Runtime: 28 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def arrangeCoins(self, n: int) -> int:
8 | return int((-1 + math.sqrt(1 + 8.0 * n)) / 2)
9 |
--------------------------------------------------------------------------------
/LeetCode/0442. Find All Duplicates in an Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 28 / 28 test cases passed.
3 | Runtime: 96 ms
4 | Memory Usage: 21.1 MB
5 | """
6 | class Solution:
7 | def findDuplicates(self, nums: List[int]) -> List[int]:
8 | ans = []
9 | for num in nums:
10 | num = abs(num)
11 | if nums[num - 1] < 0:
12 | ans.append(num)
13 | else:
14 | nums[num - 1] *= -1
15 | return ans
16 |
--------------------------------------------------------------------------------
/LeetCode/0453. Minimum Moves to Equal Array Elements/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 84 / 84 test cases passed.
3 | * Runtime: 7 ms
4 | * Memory Usage: 38.7 MB
5 | */
6 | class Solution {
7 | public int minMoves(int[] nums) {
8 | int sum = Arrays.stream(nums).sum();
9 | int min = Arrays.stream(nums).min().getAsInt();
10 | return sum - nums.length * min;
11 | }
12 | }
13 |
--------------------------------------------------------------------------------
/LeetCode/0453. Minimum Moves to Equal Array Elements/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 84 / 84 test cases passed.
3 | * Runtime: 28 ms
4 | * Memory Usage: 27.6 MB
5 | */
6 | class Solution {
7 | public:
8 | int minMoves(vector& nums) {
9 | int sum = accumulate(nums.begin(), nums.end(), 0);
10 | int min = *min_element(nums.begin(), nums.end());
11 | return sum - nums.size() * min;
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/LeetCode/0453. Minimum Moves to Equal Array Elements/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 84 / 84 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 15.8 MB
5 | """
6 | class Solution:
7 | def minMoves(self, nums: List[int]) -> int:
8 | return sum(nums) - len(nums) * min(nums)
9 |
--------------------------------------------------------------------------------
/LeetCode/0455. Assign Cookies/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 21 / 21 test cases passed.
3 | * Status: Accepted
4 | * Runtime: 80 ms
5 | */
6 | class Solution {
7 | public:
8 | int findContentChildren(vector& g, vector& s) {
9 | sort(g.begin(), g.end());
10 | sort(s.begin(), s.end());
11 | int i = 0, j = 0;
12 | while (i < g.size() && j < s.size()) {
13 | if (g[i] <= s[j]) i++;
14 | j++;
15 | }
16 | return i;
17 | }
18 | };
19 |
--------------------------------------------------------------------------------
/LeetCode/0456. 132 Pattern/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 102 / 102 test cases passed.
3 | Runtime: 68 ms
4 | Memory Usage: 27.1 MB
5 | """
6 | class Solution:
7 | def find132pattern(self, nums: List[int]) -> bool:
8 | minn = float('-inf')
9 | stk = []
10 | for num in reversed(nums):
11 | if num < minn: return True
12 | while stk and stk[-1] < num:
13 | minn = stk.pop()
14 | stk.append(num)
15 | return False
16 |
--------------------------------------------------------------------------------
/LeetCode/0461. Hamming Distance/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 149 / 149 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.7 MB
5 | */
6 | class Solution {
7 | public:
8 | int hammingDistance(int x, int y) {
9 | return __builtin_popcount(x ^ y);
10 | }
11 | };
12 |
--------------------------------------------------------------------------------
/LeetCode/0462. Minimum Moves to Equal Array Elements II/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 30 / 30 test cases passed.
3 | * Runtime: 8 ms
4 | * Memory Usage: 10.6 MB
5 | */
6 | class Solution {
7 | public:
8 | int minMoves2(vector& nums) {
9 | sort(nums.begin(), nums.end());
10 | int n = nums.size(), mid = nums[n / 2], ans = 0;
11 | for (int i = 0; i < n; ++ i) {
12 | ans += abs(nums[i] - mid);
13 | }
14 | return ans;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/0462. Minimum Moves to Equal Array Elements II/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 30 / 30 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 16 MB
5 | """
6 | class Solution:
7 | def minMoves2(self, nums: List[int]) -> int:
8 | nums.sort()
9 | mid, ans = nums[len(nums) // 2], 0
10 | return sum(abs(num - mid) for num in nums)
11 |
--------------------------------------------------------------------------------
/LeetCode/0470. Implement Rand10() Using Rand7()/solution.cpp:
--------------------------------------------------------------------------------
1 |
2 | // The rand7() API is already defined for you.
3 | // int rand7();
4 | // @return a random integer in the range 1 to 7
5 |
6 | /**
7 | * 12 / 12 test cases passed.
8 | * Runtime: 4 ms
9 | * Memory Usage: 8 MB
10 | */
11 | class Solution {
12 | public:
13 | int rand10() {
14 | for (;;) {
15 | int ans = (rand7() - 1) * 7 + (rand7() - 1);
16 | if (1 <= ans && ans <= 40) return (ans % 10) + 1;
17 | }
18 | }
19 | };
20 |
--------------------------------------------------------------------------------
/LeetCode/0470. Implement Rand10() Using Rand7()/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 12 / 12 test cases passed.
3 | * Runtime: 8 ms
4 | * Memory Usage: 5.5 MB
5 | */
6 | func rand10() int {
7 | for {
8 | ans := (rand7()-1)*7 + (rand7() - 1)
9 | if 1 <= ans && ans <= 40 {
10 | return ans%10 + 1
11 | }
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/LeetCode/0470. Implement Rand10() Using Rand7()/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 12 / 12 test cases passed.
3 | Runtime: 312 ms
4 | Memory Usage: :17.4 MB
5 | """
6 | class Solution:
7 | def twoSum(self, nums: List[int], target: int) -> List[int]:
8 | store = dict()
9 | for idx, elm in enumerate(nums):
10 | if (target - elm) in store:
11 | return [store[target - elm], idx]
12 | store[elm] = idx
13 |
14 |
--------------------------------------------------------------------------------
/LeetCode/0476. Number Complement/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 149 / 149 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def findComplement(self, num: int) -> int:
8 | mask = num
9 | mask |= mask >> 1;
10 | mask |= mask >> 2;
11 | mask |= mask >> 4;
12 | mask |= mask >> 8;
13 | mask |= mask >> 16;
14 | return mask ^ num
15 |
--------------------------------------------------------------------------------
/LeetCode/0477. Total Hamming Distance/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 46 / 46 test cases passed.
3 | Runtime: 360 ms
4 | Memory Usage: 15.7 MB
5 | """
6 | class Solution:
7 | def totalHammingDistance(self, nums: List[int]) -> int:
8 | n = len(nums)
9 | ans = 0
10 | for i in range(30):
11 | c = 0
12 | for num in nums:
13 | c += (num >> i) & 1
14 | ans += c * (n - c)
15 | return ans
16 |
17 |
--------------------------------------------------------------------------------
/LeetCode/0485. Max Consecutive Ones/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 41 / 41 test cases passed.
3 | Status: Accepted
4 | Runtime: 380 ms
5 | """
6 | class Solution:
7 | def twoSum(self, nums: List[int], target: int) -> List[int]:
8 | store = dict()
9 | for idx, elm in enumerate(nums):
10 | if (target - elm) in store:
11 | return [store[target - elm], idx]
12 | store[elm] = idx
13 |
14 |
--------------------------------------------------------------------------------
/LeetCode/0492. Construct the Rectangle/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 52 / 52 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 6 MB
5 | */
6 | class Solution {
7 | public:
8 | vector constructRectangle(int area) {
9 | int width = static_cast(sqrt(area));
10 | for (; area % width != 0; width--) { }
11 | return {static_cast(area / width), width};
12 | }
13 | };
14 |
--------------------------------------------------------------------------------
/LeetCode/0496. Next Greater Element I/README.md:
--------------------------------------------------------------------------------
1 | # [496. Next Greater Element I](https://leetcode-cn.com/problems/next-greater-element-i/)
2 |
3 | 
4 |
5 |
6 |
7 | Topics
8 |
9 | * [`Stack`](https://leetcode.com/tag/stack/)
10 |
11 |
12 |
13 |
--------------------------------------------------------------------------------
/LeetCode/0504. Base 7/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 241 / 241 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def convertToBase7(self, num: int) -> str:
8 | if num == 0: return '0'
9 | neg = num < 0
10 | num = abs(num)
11 | digits = ''
12 | while num > 0:
13 | digits += str(num % 7)
14 | num //= 7
15 | if neg:
16 | digits += '-'
17 | return digits[::-1]
18 |
--------------------------------------------------------------------------------
/LeetCode/0507. Perfect Number/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 98 / 98 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 1.9 MB
5 | */
6 | func checkPerfectNumber(num int) bool {
7 | if num == 1 {
8 | return false
9 | }
10 |
11 | sum := 1
12 | for i := 2; i * i <= num; i++ {
13 | if num % i == 0 {
14 | sum += i
15 | if i * i != num {
16 | sum += num / i
17 | }
18 | }
19 | }
20 | return sum == num
21 | }
22 |
--------------------------------------------------------------------------------
/LeetCode/0509. Fibonacci Number/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 31 / 31 test cases passed
3 | * Status: Accepted
4 | * Runtime: 0 ms
5 | */
6 | class Solution {
7 | public int fib(int N) {
8 | int a = 0, b = 1, ans = 0;
9 | for (int i = 1; i <= N; i++) {
10 | ans = b;
11 | b = a + b;
12 | a = ans;
13 | }
14 | return ans;
15 | }
16 | }
17 |
--------------------------------------------------------------------------------
/LeetCode/0509. Fibonacci Number/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 31 / 31 test cases passed
3 | * Status: Accepted
4 | * Runtime: 0 ms
5 | */
6 | class Solution {
7 | public:
8 | int fib(int N) {
9 | int a = 0, b = 1, ans = 0;
10 | for (int i = 1; i <= N; i++) {
11 | ans = b;
12 | b = a + b;
13 | a = ans;
14 | }
15 | return ans;
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/LeetCode/0509. Fibonacci Number/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 31 / 31 test cases passed
3 | * Status: Accepted
4 | * Runtime: 0 ms
5 | */
6 | func fib(N int) int {
7 | if N < 2 {
8 | return N
9 | }
10 | f1, f2 := 0, 1
11 | for i := 2; i <= N; i++ {
12 | f2 = f1 + f2
13 | f1 = f2 - f1
14 | }
15 | return f2
16 | }
17 |
--------------------------------------------------------------------------------
/LeetCode/0509. Fibonacci Number/solution.js:
--------------------------------------------------------------------------------
1 | /**
2 | * 31 / 31 test cases passed
3 | * Status: Accepted
4 | * Runtime: 64 ms
5 | * @param {number} N
6 | * @return {number}
7 | */
8 | var fib = function(N) {
9 | if (N < 2) return N
10 | let f1 = 0, f2 = 1
11 | for (let i = 2; i <= N; i++) {
12 | f2 = f1 + f2
13 | f1 = f2 - f1
14 | }
15 | return f2
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/0509. Fibonacci Number/solution.kt:
--------------------------------------------------------------------------------
1 | /**
2 | * 31 / 31 test cases passed
3 | * Status: Accepted
4 | * Runtime: 164 ms
5 | */
6 | class Solution {
7 | fun fib(N: Int): Int {
8 | if (N < 2) return N
9 | val memo = MutableList(N + 1) { 0 }
10 | memo[0] = 0
11 | memo[1] = 1
12 | return (2..N).forEach { memo[it] = memo[it - 1] + memo[it - 2]}.let{ memo[N]}
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/LeetCode/0509. Fibonacci Number/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 31 / 31 test cases passed.
3 | Status: Accepted
4 | Runtime: 44 ms
5 | """
6 | class Solution:
7 | def fib(self, N: int) -> int:
8 | a, b, ans = 0, 1, 0
9 | for i in range(N):
10 | ans = b
11 | b = a + b
12 | a = ans
13 | return ans
14 |
--------------------------------------------------------------------------------
/LeetCode/0518. Coin Change 2/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 28 / 28 test cases passed.
3 | * Runtime: 8 ms
4 | * Memory Usage: 6.9 MB
5 | */
6 | class Solution {
7 | public:
8 | int change(int amount, vector& coins) {
9 | vector dp(amount + 1);
10 | dp[0] = 1;
11 | for (auto& coin: coins) {
12 | for (int i = coin; i <= amount; ++ i) {
13 | dp[i] += dp[i - coin];
14 | }
15 | }
16 | return dp[amount];
17 | }
18 | };
19 |
--------------------------------------------------------------------------------
/LeetCode/0518. Coin Change 2/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 28 / 28 test cases passed.
3 | Runtime: 124 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def change(self, amount: int, coins: List[int]) -> int:
8 | dp = [1] + [0] * amount
9 | for coin in coins:
10 | for i in range(coin, amount + 1):
11 | dp[i] += dp[i - coin]
12 | return dp[amount]
13 |
--------------------------------------------------------------------------------
/LeetCode/0521. Longest Uncommon Subsequence I/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 40 / 40 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 6 MB
5 | */
6 | class Solution {
7 | public:
8 | int findLUSlength(string a, string b) {
9 | return a != b ? max(a.length(), b.length()) : -1;;
10 | }
11 | };
12 |
--------------------------------------------------------------------------------
/LeetCode/0521. Longest Uncommon Subsequence I/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 40 / 40 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def findLUSlength(self, a: str, b: str) -> int:
8 | return max(len(a), len(b)) if a != b else -1
9 |
--------------------------------------------------------------------------------
/LeetCode/0525. Contiguous Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 564 / 564 test cases passed.
3 | Runtime: 236 ms
4 | Memory Usage: 19.6 MB
5 | """
6 | class Solution:
7 | def findMaxLength(self, nums: List[int]) -> int:
8 | mp = { 0:-1 }
9 | ans, s = 0, 0
10 | for i, num in enumerate(nums):
11 | s += 1 if num == 1 else -1
12 | if s in mp:
13 | ans = max(ans, i - mp[s])
14 | else:
15 | mp[s] = i
16 | return ans
17 |
18 |
--------------------------------------------------------------------------------
/LeetCode/0554. Brick Wall/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 87 / 87 test cases passed.
3 | Runtime: 72 ms
4 | Memory Usage: 18 MB
5 | """
6 | from itertools import accumulate
7 | from collections import Counter
8 | class Solution:
9 | def leastBricks(self, wall: List[List[int]]) -> int:
10 | through = []
11 | for bricks in wall:
12 | through.extend(accumulate(bricks[:-1]))
13 | max_through = Counter(through).most_common()
14 | return len(wall) - (max_through[0][-1] if len(max_through) else 0)
15 |
--------------------------------------------------------------------------------
/LeetCode/0560. Subarray Sum Equals K/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 92 / 92 test cases passed.
3 | Runtime: 120 ms
4 | Memory Usage: 17.3 MB
5 | """
6 | class Solution:
7 | def subarraySum(self, nums: List[int], k: int) -> int:
8 | rec = collections.defaultdict(int)
9 | rec[0] = 1
10 | ans = psum = 0
11 | for num in nums:
12 | psum += num
13 | if (psum - k) in rec:
14 | ans += rec[psum - k]
15 | rec[psum] += 1
16 | return ans
17 |
--------------------------------------------------------------------------------
/LeetCode/0561. Array Partition I/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 83 / 83 test cases passed.
3 | * Status: Accepted
4 | * Runtime: 56 ms
5 | */
6 | class Solution {
7 | public:
8 | int arrayPairSum(vector& nums) {
9 | sort(nums.begin(), nums.end());
10 | int sum = 0;
11 | for(int i = 0; i < nums.size(); i += 2) {
12 | sum += nums[i];
13 | }
14 | return sum;
15 | }
16 | };
17 |
18 |
--------------------------------------------------------------------------------
/LeetCode/0561. Array Partition I/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 83 / 83 test cases passed.
3 | Status: Accepted
4 | Runtime: 76 ms
5 | """
6 | class Solution:
7 | def arrayPairSum(self, nums: List[int]) -> int:
8 | return sum(sorted(nums)[::2])
9 |
--------------------------------------------------------------------------------
/LeetCode/0566. Reshpae the Matrix/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 56 / 56 test cases passed.
3 | Runtime: 100 ms
4 | Memory Usage: 15.1 MB
5 | """
6 | class Solution:
7 | def matrixReshape(self, nums: List[List[int]], r: int, c: int) -> List[List[int]]:
8 | return nums if len(nums) * len(nums[0]) != r * c else zip(*[iter(i for row in nums for i in row)] * c)
9 |
--------------------------------------------------------------------------------
/LeetCode/0575. Distribute Candies/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 209 / 209 test cases passed.
3 | * Runtime: 280 ms
4 | * Memory Usage: 107.7 MB
5 | */
6 | class Solution {
7 | public:
8 | int distributeCandies(vector& candyType) {
9 | int limit = candyType.size() >> 1;
10 | unordered_set types(candyType.begin(), candyType.end());
11 | return types.size() > limit ? limit : types.size();
12 | }
13 | };
14 |
--------------------------------------------------------------------------------
/LeetCode/0594. Longest Harmonious Subsequence/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 206 / 206 test cases passed.
3 | Runtime: 296 ms
4 | Memory Usage: 16.5 MB
5 | """
6 | class Solution:
7 | def findLHS(self, nums: List[int]) -> int:
8 | count = defaultdict(int)
9 | for num in nums:
10 | count[num] += 1
11 | ans = 0
12 | for key in count:
13 | if key + 1 in count:
14 | ans = max(ans, count[key] + count[key + 1])
15 | return ans
16 |
--------------------------------------------------------------------------------
/LeetCode/0598. Range Addition II/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 69 / 69 test cases passed.
3 | * Runtime: 12 ms
4 | * Memory Usage: 10.8 MB
5 | */
6 | class Solution {
7 | public:
8 | int maxCount(int m, int n, vector>& ops) {
9 | int min_x = m, min_y = n;
10 | for (auto &item: ops) {
11 | min_x = min(min_x, item[0]);
12 | min_y = min(min_y, item[1]);
13 | }
14 | return min_x * min_y;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/0598. Range Addition II/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 69 / 69 test cases passed.
3 | * Runtime: 4 ms
4 | * Memory Usage: 3.7 MB
5 | */
6 | func maxCount(m int, n int, ops [][]int) int {
7 | min_x, min_y := m, n
8 | for _, item := range ops {
9 | if min_x > item[0] { min_x = item[0] }
10 | if min_y > item[1] { min_y = item[1] }
11 | }
12 | return min_x * min_y
13 | }
14 |
--------------------------------------------------------------------------------
/LeetCode/0628. Maximum Product of Three Numbers/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 91 / 91 test cases passed.
3 | Status: Accepted
4 | Runtime: 64 ms
5 | """
6 | class Solution:
7 | def maximumProduct(self, nums: List[int]) -> int:
8 | nums.sort()
9 | return max(nums[0] * nums[1] * nums[-1], nums[-1] * nums[-2] * nums[-3])
10 |
11 |
--------------------------------------------------------------------------------
/LeetCode/0633. Sum of Square Numbers/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 124 / 124 test cases passed.
3 | Runtime: 164 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def judgeSquareSum(self, c: int) -> bool:
8 | l, r = 0, int(sqrt(c))
9 | while l * l + r * r != c and l <= r:
10 | if l * l + r * r > c:
11 | r -= 1
12 | else:
13 | l += 1
14 | return True if l <= r else False
15 |
--------------------------------------------------------------------------------
/LeetCode/0643. Maximum Average Subarray I/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 123 / 123 test cases passed.
3 | Status: Accepted
4 | Runtime: 916 ms
5 | """
6 | class Solution:
7 | def findMaxAverage(self, nums: List[int], k: int) -> float:
8 | sumk = sum(nums[:k])
9 | max_aveg = sumk / k
10 | for i, j in zip(nums[:-k], nums[k:]):
11 | sumk = sumk - i + j
12 | max_aveg = max(max_aveg, sumk / k)
13 | return max_aveg
14 |
--------------------------------------------------------------------------------
/LeetCode/0646. Maximum Length of Pair Chain/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 205 / 205 test cases passed.
3 | Runtime: 52 ms
4 | Memory Usage: 15.2 MB
5 | """
6 | class Solution:
7 | def findLongestChain(self, pairs: List[List[int]]) -> int:
8 | prev, ans = -inf, 0
9 | for a, b in sorted(pairs, key=lambda p: p[1]):
10 | if a > prev:
11 | prev = b
12 | ans += 1
13 | return ans
14 |
--------------------------------------------------------------------------------
/LeetCode/0650. 2 Keys Keyboard/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 126 / 126 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 35.3 MB
5 | */
6 | class Solution {
7 | public int minSteps(int n) {
8 | if (n == 1) return 0;
9 | int x;
10 | for (x = n >> 1; n % x != 0; x--) {}
11 | return minSteps(x) + (n / x);
12 | }
13 | }
--------------------------------------------------------------------------------
/LeetCode/0650. 2 Keys Keyboard/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 126 / 126 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.9 MB
5 | */
6 | class Solution {
7 | public:
8 | int minSteps(int n) {
9 | if (n == 1) return 0;
10 | int x;
11 | for (x = n >> 1; n % x != 0; x--) {}
12 | return minSteps(x) + (n / x);
13 | }
14 | };
15 |
--------------------------------------------------------------------------------
/LeetCode/0650. 2 Keys Keyboard/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 126 / 126 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 1.9 MB
5 | */
6 | func minSteps(n int) int {
7 | if n == 1 {
8 | return 0
9 | }
10 | x := n >> 1
11 | for ; n%x != 0; x-- {}
12 | return minSteps(x) + (n / x)
13 | }
14 |
--------------------------------------------------------------------------------
/LeetCode/0650. 2 Keys Keyboard/solution.js:
--------------------------------------------------------------------------------
1 | /**
2 | * 126 / 126 test cases passed.
3 | * Runtime: 64 ms
4 | * Memory Usage: 37.8 MB
5 | *
6 | * @param {number} n
7 | * @return {number}
8 | */
9 | var minSteps = function(n) {
10 | if (n == 1) return 0;
11 | let x;
12 | for (x = n >> 1; n % x != 0; x--) {}
13 | return minSteps(x) + (n / x);
14 | };
15 |
--------------------------------------------------------------------------------
/LeetCode/0650. 2 Keys Keyboard/solution.kt:
--------------------------------------------------------------------------------
1 | /**
2 | * 126 / 126 test cases passed.
3 | * Runtime: 148 ms
4 | * Memory Usage: 32 MB
5 | */
6 | class Solution {
7 | fun minSteps(n: Int): Int {
8 | if (n == 1) return 0
9 | var x: Int = n shr 1;
10 | while (n % x != 0) x -= 1
11 | return minSteps(x) + (n / x)
12 | }
13 | }
14 |
--------------------------------------------------------------------------------
/LeetCode/0650. 2 Keys Keyboard/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 126 / 126 test cases passed.
3 | Runtime: 28 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def minSteps(self, n: int) -> int:
8 | if n == 1:
9 | return 0
10 | x = n >> 1
11 | while n % x != 0:
12 | x -= 1
13 | return self.minSteps(x) + (n // x)
14 |
--------------------------------------------------------------------------------
/LeetCode/0657. Robot Return to Origin/Solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 72 / 72 test cases passed.
3 | * Status: Accepted
4 | * Runtime: 4 ms
5 | */
6 | func judgeCircle(moves string) bool {
7 | hor, vec := 0, 0
8 | for _, mov := range moves {
9 | switch mov {
10 | case 'U': vec += 1
11 | case 'D': vec -= 1
12 | case 'R': hor += 1
13 | case 'L': hor -= 1
14 | }
15 | }
16 | return hor == 0 && vec == 0
17 | }
18 |
--------------------------------------------------------------------------------
/LeetCode/0670. Maximum Swap/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 111 / 111 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 14.7 MB
5 | """
6 | class Solution:
7 | def maximumSwap(self, num: int) -> int:
8 | ans = num
9 | s = list(str(num))
10 | for i in range(len(s)):
11 | for j in range(i):
12 | s[i], s[j] = s[j], s[i]
13 | ans = max(ans, int(''.join(s)))
14 | s[i], s[j] = s[j], s[i]
15 | return ans
16 |
--------------------------------------------------------------------------------
/LeetCode/0674. Longest Continuous Increasing Subsequence/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 36 / 36 test cases passed.
3 | Status: Accepted
4 | Runtime: 56 ms
5 | """
6 | class Solution:
7 | def findLengthOfLCIS(self, nums: List[int]) -> int:
8 | start, ans = 0, 0
9 | for i in range(len(nums)):
10 | if i > 0 and nums[i] <= nums[i-1]:
11 | start = i
12 | ans = max(ans, i - start + 1)
13 | return ans
14 |
15 |
--------------------------------------------------------------------------------
/LeetCode/0682. Baseball Game/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 39 / 39 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 15.2 MB
5 | """
6 | class Solution:
7 | def calPoints(self, ops: List[str]) -> int:
8 | stk = []
9 | for op in ops:
10 | if op == 'C': stk.pop()
11 | elif op == '+': stk.append(stk[-2] + stk[-1])
12 | elif op == 'D': stk.append(stk[-1] * 2)
13 | else: stk.append(int(op))
14 | return sum(stk)
15 |
--------------------------------------------------------------------------------
/LeetCode/0686. Repeated String Match/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 57 / 57 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 2.3 MB
5 | */
6 | func repeatedStringMatch(a string, b string) int {
7 | times := (len(b) - 1) / len(a) + 1
8 | temp := strings.Repeat(a, times)
9 | for i := 0; i <= 1; i++ {
10 | if strings.Contains(temp, b) {
11 | return times + i
12 | }
13 | temp += a
14 | }
15 | return -1
16 | }
17 |
--------------------------------------------------------------------------------
/LeetCode/0692. Top K Frequent Words/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 110 / 110 test cases passed.
3 | Runtime: 64 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def topKFrequent(self, words: List[str], k: int) -> List[str]:
8 | return [freq[0] for freq in Counter(sorted(words)).most_common(k)]
9 |
--------------------------------------------------------------------------------
/LeetCode/0693. Binary Number with Alternating Bits/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 204 / 204 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 38.4 MB
5 | */
6 | class Solution {
7 | public boolean hasAlternatingBits(int n) {
8 | int x = n ^ (n >> 1);
9 | return (x & (x + 1)) == 0;
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/LeetCode/0693. Binary Number with Alternating Bits/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 204 / 204 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.7 MB
5 | */
6 | class Solution {
7 | public:
8 | bool hasAlternatingBits(int n) {
9 | long x = n ^ (n >> 1);
10 | return (x & (x + 1)) == 0;
11 | }
12 | };
13 |
--------------------------------------------------------------------------------
/LeetCode/0693. Binary Number with Alternating Bits/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 204 / 204 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 1.8 MB
5 | */
6 | func hasAlternatingBits(n int) bool {
7 | x := n ^ (n >> 1)
8 | return (x & (x + 1)) == 0
9 | }
10 |
--------------------------------------------------------------------------------
/LeetCode/0693. Binary Number with Alternating Bits/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 204 / 204 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def hasAlternatingBits(self, n: int) -> bool:
8 | x = (n ^ (n >> 1))
9 | return (x & (x + 1)) == 0
10 |
11 |
--------------------------------------------------------------------------------
/LeetCode/0693. Binary Number with Alternating Bits/solution.rs:
--------------------------------------------------------------------------------
1 | /**
2 | * 204 / 204 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 1.9 MB
5 | */
6 | impl Solution {
7 | pub fn has_alternating_bits(n: i32) -> bool {
8 | let x = n ^ (n >> 1);
9 | x & (x + 1) == 0
10 | }
11 | }
--------------------------------------------------------------------------------
/LeetCode/0703. Kth Largest Element in a Stream/solution.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/InnoFang/algo-set/65ed98dfc84802db627868e8ad648e2b6444ded9/LeetCode/0703. Kth Largest Element in a Stream/solution.cpp
--------------------------------------------------------------------------------
/LeetCode/0704. Binary Search/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 46 / 46 test cases passed.
3 | * Runtime: 32 ms
4 | * Memory Usage: 6.7 MB
5 | */
6 | func search(nums []int, target int) int {
7 | lo, hi := 0, len(nums)-1
8 | for lo <= hi {
9 | mid := lo + (hi-lo)/2
10 | switch {
11 | case nums[mid] > target:
12 | hi = mid - 1
13 | case nums[mid] < target:
14 | lo = mid + 1
15 | default:
16 | return mid
17 | }
18 | }
19 | return -1
20 | }
21 |
--------------------------------------------------------------------------------
/LeetCode/0709. To Lower Case/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 114 / 114 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 6 MB
5 | */
6 | class Solution {
7 | public:
8 | string toLowerCase(string s) {
9 | for (auto &c : s) {
10 | if ('A' <= c && c <= 'Z') {
11 | c += 32;
12 | }
13 | }
14 | return s;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/0709. To Lower Case/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 114 / 114 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 2 MB
5 | */
6 | func toLowerCase(s string) string {
7 | bytes := []byte(s)
8 | for i, c := range s {
9 | if 'A' <= c && c <= 'Z' {
10 | bytes[i] += 32
11 | }
12 | }
13 | return string(bytes)
14 | }
15 |
--------------------------------------------------------------------------------
/LeetCode/0709. To Lower Case/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 114 / 114 test cases passed.
3 | Runtime: 28 ms
4 | Memory Usage: 15.1 MB
5 | """
6 | class Solution:
7 | def toLowerCase(self, s: str) -> str:
8 | return ''.join([c.lower() if 'A' <= c <= 'Z' else c for c in s])
9 |
10 |
--------------------------------------------------------------------------------
/LeetCode/0717. 1-bit and 2-bit Characters/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 93 / 93 test cases passed.
3 | * Runtime: 8 ms
4 | * Memory Usage: 9.7 MB
5 | */
6 | class Solution {
7 | public:
8 | bool isOneBitCharacter(vector& bits) {
9 | int pos = 0, n = bits.size();
10 | while (pos < n - 1) {
11 | pos += bits[pos] == 1 ? 2 : 1;
12 | }
13 | return pos == bits.size() - 1;
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/LeetCode/0724. Find Pivot Index/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 742 / 742 test cases passed.
3 | Status: Accepted
4 | Runtime: 60 ms
5 | """
6 | class Solution:
7 | def pivotIndex(self, nums: List[int]) -> int:
8 | total = sum(nums)
9 | part_sum = 0
10 | for i, num in enumerate(nums):
11 | if 2 * part_sum + num == total:
12 | return i
13 | part_sum += num
14 | return -1
15 |
--------------------------------------------------------------------------------
/LeetCode/0740. Delete and Earn/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 47 / 47 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def deleteAndEarn(self, nums: List[int]) -> int:
8 | freq = [0] * (max(nums) + 1)
9 | for num in nums:
10 | freq[num] += num
11 | last, curr = 0, 0
12 | for cnt in freq:
13 | last, curr = curr, max(curr, last + cnt)
14 | return curr
15 |
--------------------------------------------------------------------------------
/LeetCode/0744. Find Smallest Letter Greater Than Target/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 165 / 165 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 16.8 MB
5 | """
6 | class Solution:
7 | def nextGreatestLetter(self, letters: List[str], target: str) -> str:
8 | idx = bisect.bisect_right(letters, target)
9 | return letters[idx] if idx < len(letters) else letters[0]
10 |
--------------------------------------------------------------------------------
/LeetCode/0760. Find Anagram Mappings/Solution.kt:
--------------------------------------------------------------------------------
1 | /**
2 | * Created by Inno Fang on 2018/1/7.
3 | */
4 | class Solution {
5 | fun anagramMappings(A: IntArray, B: IntArray): IntArray {
6 | val p = IntArray(A.size)
7 | A.forEachIndexed{ index, i -> p[index] = B.indexOf(i) }
8 | return p
9 | }
10 | }
11 |
12 | fun main(args: Array) {
13 | Solution().anagramMappings(intArrayOf(12, 28, 46, 32, 50), intArrayOf(50, 12, 32, 46, 28)).forEach { print("$it,") }
14 | println()
15 | }
--------------------------------------------------------------------------------
/LeetCode/0762. Prime Number of Set Bits in Binary Representation/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 202 / 202 test cases passed.
3 | Runtime: 104 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def countPrimeSetBits(self, left: int, right: int) -> int:
8 | return sum((1 << x.bit_count()) & 0b10100010100010101100 != 0 for x in range(left, right + 1))
9 |
--------------------------------------------------------------------------------
/LeetCode/0765. Couples Holding Hands/solution.cpp:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/InnoFang/algo-set/65ed98dfc84802db627868e8ad648e2b6444ded9/LeetCode/0765. Couples Holding Hands/solution.cpp
--------------------------------------------------------------------------------
/LeetCode/0766. Toeplitz Matrix/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 483 / 483 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def isToeplitzMatrix(self, matrix: List[List[int]]) -> bool:
8 | for i in range(len(matrix) - 1):
9 | if matrix[i][:-1] != matrix[i + 1][1:]:
10 | return False
11 | return True
12 |
--------------------------------------------------------------------------------
/LeetCode/0771. Jewels and Stones/Solution.kt:
--------------------------------------------------------------------------------
1 | class Solution {
2 | fun numJewelsInStones(J: String, S: String): Int {
3 | return S.count { J.contains(it) }
4 | }
5 | }
6 |
7 | fun main(args: Array) {
8 | Solution().numJewelsInStones("aA", "aAAbbbb").let(::println)
9 | Solution().numJewelsInStones("z", "ZZ").let(::println)
10 | }
11 |
--------------------------------------------------------------------------------
/LeetCode/0781. Rabbits in Forest/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 54 / 54 test cases passed.
3 | Runtime: 44 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def numRabbits(self, answers: List[int]) -> int:
8 | count = collections.Counter(answers)
9 | return sum((count[x] + x) // (x + 1) * (x + 1) for x in count)
10 |
--------------------------------------------------------------------------------
/LeetCode/0791. Custom Sort String/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 40 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def customSortString(self, order: str, s: str) -> str:
7 | prior = defaultdict(int)
8 | for i, c in enumerate(order):
9 | prior[c] = i
10 | return "".join(sorted(s, key=lambda x: prior[x]))
11 |
--------------------------------------------------------------------------------
/LeetCode/0796. Rotate String/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 47 / 47 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.8 MB
5 | */
6 | class Solution {
7 | public:
8 | bool rotateString(string s, string goal) {
9 | return s.size() == goal.size() && (s + s).find(goal) != string::npos;
10 | }
11 | };
12 |
--------------------------------------------------------------------------------
/LeetCode/0796. Rotate String/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 47 / 47 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 1.8 MB
5 | */
6 | func rotateString(s string, goal string) bool {
7 | return len(s) == len(goal) && strings.Contains(s + s, goal)
8 | }
9 |
--------------------------------------------------------------------------------
/LeetCode/0796. Rotate String/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 47 / 47 test cases passed.
3 | Runtime: 48 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def rotateString(self, s: str, goal: str) -> bool:
8 | return len(s) == len(goal) and goal in s + s
9 |
--------------------------------------------------------------------------------
/LeetCode/0796. Rotate String/solution.rs:
--------------------------------------------------------------------------------
1 | /**
2 | * 47 / 47 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 2.2 MB
5 | */
6 | impl Solution {
7 | pub fn rotate_string(s: String, goal: String) -> bool {
8 | s.len() == goal.len() && s.repeat(2).contains(&goal)
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/LeetCode/0806. Number of Lines To Write String/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 27 / 27 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 1.9 MB
5 | */
6 | func numberOfLines(widths []int, s string) []int {
7 | level, k := 1, 0
8 | for _, c := range s {
9 | if w := widths[c - 'a']; k + w > 100 {
10 | level += 1
11 | k = w
12 | } else {
13 | k += w
14 | }
15 | }
16 | return []int{level, k}
17 | }
18 |
--------------------------------------------------------------------------------
/LeetCode/0810. Chalkboard XOR Game/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 168 / 168 test cases passed.
3 | * Runtime: 8 ms
4 | * Memory Usage: 12.5 MB
5 | */
6 | class Solution {
7 | public:
8 | bool xorGame(vector& nums) {
9 | if (nums.size() % 2 == 0) return true;
10 | int sum = 0;
11 | for (auto& num: nums) sum ^= num;
12 | return sum == 0;
13 | }
14 | };
15 |
--------------------------------------------------------------------------------
/LeetCode/0810. Chalkboard XOR Game/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 168 / 168 test cases passed.
3 | Runtime: 88 ms
4 | Memory Usage: 15.1 MB
5 | """
6 | class Solution:
7 | def xorGame(self, nums: List[int]) -> bool:
8 | return True if len(nums) % 2 == 0 else reduce(xor, nums) == 0
9 |
--------------------------------------------------------------------------------
/LeetCode/0812. Largest Triangle Area/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 57 / 57 test cases passed.
3 | Runtime: 104 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def largestTriangleArea(self, points: List[List[int]]) -> float:
8 | def triangleArea(x1, y1, x2, y2, x3, y3):
9 | return abs(x1 * y2 + x2 * y3 + x3 * y1 - x1 * y3 - x2 * y1 - x3 * y2) / 2
10 | return max(triangleArea(x1, y1, x2, y2, x3, y3) for (x1, y1), (x2, y2), (x3, y3) in combinations(points, 3))
11 |
--------------------------------------------------------------------------------
/LeetCode/0819. Most Common Word/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 47 / 47 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def mostCommonWord(self, paragraph: str, banned: List[str]) -> str:
8 | ban = set(banned)
9 | return Counter(word for word in re.split('[^\w]+', paragraph.lower()) if word not in ban).most_common(1)[0][0]
10 |
--------------------------------------------------------------------------------
/LeetCode/0832. Flipping an Image/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 82 / 82 test cases passed.
3 | Runtime: 52 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def flipAndInvertImage(self, A: List[List[int]]) -> List[List[int]]:
8 | return [map(lambda x : x ^ 1, row[::-1]) for row in A]
9 |
--------------------------------------------------------------------------------
/LeetCode/0836. Rectangle Overlap/solution.py:
--------------------------------------------------------------------------------
1 | class Solution:
2 | def isRectangleOverlap(self, rec1: List[int], rec2: List[int]) -> bool:
3 | return min(rec1[2], rec2[2]) > max(rec1[0], rec2[0]) and min(rec1[3], rec2[3]) > max(rec1[1], rec2[1])
4 |
--------------------------------------------------------------------------------
/LeetCode/0850. Rectangle Area II/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 60 / 60 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def canWinNim(self, n: int) -> bool:
8 | return n % 4 != 0
9 |
10 |
--------------------------------------------------------------------------------
/LeetCode/0852. Peak Index in a Mountain Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 34 / 34 test cases passed.
3 | Runtime: 28 ms
4 | Memory Usage: 15.6 MB
5 | """
6 | class Solution:
7 | def peakIndexInMountainArray(self, arr: List[int]) -> int:
8 | l, r = 1, len(arr) - 1
9 | while l < r:
10 | mid = l + r + 1 >> 1
11 | if arr[mid - 1] < arr[mid]:
12 | l = mid
13 | else:
14 | r = mid - 1
15 | return l
16 |
--------------------------------------------------------------------------------
/LeetCode/0856. Score of Parentheses/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 86 / 86 test cases passed.
3 | Runtime: 44 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def scoreOfParentheses(self, s: str) -> int:
8 | stk = [0]
9 | for c in s:
10 | if c == '(':
11 | stk.append(0)
12 | else:
13 | cur = stk.pop()
14 | stk.append(stk.pop() + max(cur * 2, 1))
15 | return stk[-1]
16 |
--------------------------------------------------------------------------------
/LeetCode/0867. Transpose Matrix/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 36 / 36 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def transpose(self, matrix: List[List[int]]) -> List[List[int]]:
8 | return list(zip(*matrix))
9 |
--------------------------------------------------------------------------------
/LeetCode/0875. Koko Eating Bananas/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 121 / 121 test cases passed.
3 | Runtime: 396 ms
4 | Memory Usage: 16.3 MB
5 | """
6 | class Solution:
7 | def minEatingSpeed(self, piles: List[int], h: int) -> int:
8 | check = lambda k : sum(math.ceil(p / k) for p in piles) <= h
9 | l, r = 1, int(1e9)
10 | while l < r:
11 | mid = l + (r - l) // 2
12 | if check(mid):
13 | r = mid
14 | else:
15 | l = mid + 1
16 | return r
17 |
--------------------------------------------------------------------------------
/LeetCode/0876. Middle of the Linked List/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for singly-linked list.
3 | * type ListNode struct {
4 | * Val int
5 | * Next *ListNode
6 | * }
7 | */
8 |
9 | /**
10 | * 15 / 15 test case passed
11 | * Status: Accepted
12 | * Runtime: 0 ms
13 | */
14 | func middleNode(head *ListNode) *ListNode {
15 | p, q := head, head
16 | for q != nil && q.Next != nil {
17 | p = p.Next
18 | q = q.Next.Next
19 | }
20 | return p
21 | }
22 |
--------------------------------------------------------------------------------
/LeetCode/0884. Uncommon Words from Two Sentences/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 55 / 55 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def uncommonFromSentences(self, s1: str, s2: str) -> List[str]:
8 | record = defaultdict(int)
9 | for word in split(' ', s1 + " " + s2):
10 | record[word] += 1
11 | return list(filter(lambda k: record[k] == 1, record))
12 |
--------------------------------------------------------------------------------
/LeetCode/0888. Fair Candy Swap/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 75 / 75 test cases passed.
3 | Status: Accepted
4 | Runtime: 404 ms
5 | """
6 | class Solution:
7 | def fairCandySwap(self, A: List[int], B: List[int]) -> List[int]:
8 | delta = (sum(A) - sum(B)) // 2
9 | setA = set(A)
10 | for b in B:
11 | if (b + delta) in setA:
12 | return [b + delta, b]
13 |
14 |
--------------------------------------------------------------------------------
/LeetCode/0890. Find and Replace Pattern/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 47 / 47 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 15.1 MB
5 | """
6 | class Solution:
7 | def findAndReplacePattern(self, words: List[str], pattern: str) -> List[str]:
8 | def match(w, p):
9 | return len(w) == len(p) and len(set(w)) == len(set(p)) == len(set(zip(w, p)))
10 | return [w for w in words if match(w, pattern)]
11 |
--------------------------------------------------------------------------------
/LeetCode/0905. Sort Array By Parity/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 285 / 285 test cases passed.
3 | * Runtime: 12 ms
4 | * Memory Usage: 15.7 MB
5 | */
6 | class Solution {
7 | public:
8 | vector sortArrayByParity(vector& nums) {
9 | int l = 0;
10 | for (int r = 0; r < nums.size(); ++ r) {
11 | if (nums[r] % 2 == 0) {
12 | swap(nums[l], nums[r]);
13 | ++ l;
14 | }
15 | }
16 | return nums;
17 | }
18 | };
19 |
--------------------------------------------------------------------------------
/LeetCode/0905. Sort Array By Parity/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 285 / 285 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 15.6 MB
5 | """
6 | class Solution:
7 | def sortArrayByParity(self, nums: List[int]) -> List[int]:
8 | l = 0
9 | for r in range(len(nums)):
10 | if nums[r] % 2 == 0:
11 | nums[l], nums[r] = nums[r], nums[l]
12 | l += 1
13 | return nums
14 |
--------------------------------------------------------------------------------
/LeetCode/0908. Smallest Range I/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 68 / 68 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 15.9 MB
5 | """
6 | class Solution:
7 | def smallestRangeI(self, nums: List[int], k: int) -> int:
8 | return max(0, max(nums) - min(nums) - 2 * k
9 |
--------------------------------------------------------------------------------
/LeetCode/0921. Minimum Add to Make Parentheses Valid/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 115 / 115 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 6 MB
5 | */
6 | class Solution {
7 | public:
8 | int minAddToMakeValid(string s) {
9 | int ans = 0, cnt = 0;
10 | for (auto &c : s) {
11 | if (c == '(') ++ cnt;
12 | else if (cnt > 0) -- cnt;
13 | else ++ ans;
14 | }
15 | return ans + cnt;
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/LeetCode/0921. Minimum Add to Make Parentheses Valid/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 115 / 115 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def minAddToMakeValid(self, s: str) -> int:
8 | ans = cnt = 0
9 | for c in s:
10 | if c == '(':
11 | cnt += 1
12 | elif cnt > 0:
13 | cnt -= 1
14 | else:
15 | ans += 1
16 | return ans + cnt
17 |
--------------------------------------------------------------------------------
/LeetCode/0926. Flip String to Monotone Increasing/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 93 / 93 test cases passed.
3 | Runtime: 176 ms
4 | Memory Usage: 15.5 MB
5 | """
6 | class Solution:
7 | def minFlipsMonoIncr(self, s: str) -> int:
8 | dp0 = dp1 = 0
9 | for c in s:
10 | dp0New, dp1New = dp0, min(dp0, dp1)
11 | if c == '1':
12 | dp0New += 1
13 | else:
14 | dp1New += 1
15 | dp0, dp1 = dp0New, dp1New
16 | return min(dp0, dp1)
17 |
--------------------------------------------------------------------------------
/LeetCode/0929. Unique Email Addresses/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 185 / 185 test cases passed.
3 | Runtime: 56 ms
4 | Memory Usage: 15.1 MB
5 | """
6 | class Solution:
7 | def numUniqueEmails(self, emails: List[str]) -> int:
8 | email_set = set()
9 | for email in emails:
10 | name, domain = email.split('@')
11 | name = name.split('+')[0].replace('.', '')
12 | email_set.add(name + '@' + domain)
13 | return len(email_set)
14 |
--------------------------------------------------------------------------------
/LeetCode/0942. DI String Match/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 95 / 95 test cases passed.
3 | Runtime: 52 ms
4 | Memory Usage: 95 MB
5 | """
6 | class Solution:
7 | def diStringMatch(self, s: str) -> List[int]:
8 | lo, hi = 0, len(s)
9 | ans = [0] * (hi + 1)
10 | for i, c in enumerate(s):
11 | if c == 'I':
12 | ans[i] = lo
13 | lo += 1
14 | else:
15 | ans[i] = hi
16 | hi -= 1
17 | ans[-1] = lo
18 | return ans
19 |
--------------------------------------------------------------------------------
/LeetCode/0945. Minimum Increment to Make Array Unique/solution.go:
--------------------------------------------------------------------------------
1 | import (
2 | "sort"
3 | )
4 |
5 | /**
6 | * 59 / 59 test case passed
7 | * Status: Accepted
8 | * Runtime: 84 ms
9 | */
10 | func minIncrementForUnique(A []int) int {
11 | sort.Ints(A)
12 | ans := 0
13 | for i := 1; i < len(A); i++ {
14 | if A[i] <= A[i - 1] {
15 | ans += A[i - 1] - A[i] + 1
16 | A[i] = A[i - 1] + 1
17 | }
18 | }
19 | return ans
20 | }
21 |
--------------------------------------------------------------------------------
/LeetCode/0945. Minimum Increment to Make Array Unique/solution.py:
--------------------------------------------------------------------------------
1 | class Solution:
2 | """
3 | 59 / 59 test case passed
4 | Status: Accepted
5 | Runtime: 412 ms
6 | """
7 | def minIncrementForUnique(self, A: List[int]) -> int:
8 | A.sort()
9 | ans = 0
10 | for i in range(1, len(A)):
11 | if A[i] <= A[i-1]:
12 | ans += A[i - 1] - A[i] + 1
13 | A[i] = A[i - 1] + 1
14 | return ans
15 |
--------------------------------------------------------------------------------
/LeetCode/0946. Validate Stack Sequences/README.md:
--------------------------------------------------------------------------------
1 | # [946. Validate Stack Sequences](https://leetcode.cn/problems/validate-stack-sequences/)
2 |
3 | 
4 |
5 |
6 | Topics
7 |
8 | * [`Array`](https://leetcode.com/tag/array/)
9 | * [`Stack`](https://leetcode.com/tag/stack/)
10 | * [`Simulation`](https://leetcode.com/tag/simulation/)
11 |
12 |
13 |
14 |
--------------------------------------------------------------------------------
/LeetCode/0946. Validate Stack Sequences/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 151 / 151 test cases passed.
3 | * Runtime: 4 ms
4 | * Memory Usage: 3.6 MB
5 | */
6 | func validateStackSequences(pushed []int, popped []int) bool {
7 | s := []int{}
8 | i := 0
9 | for _, num := range pushed {
10 | s = append(s, num)
11 | for len(s) > 0 && s[len(s) - 1] == popped[i] {
12 | s = s[:len(s)-1]
13 | i++
14 | }
15 | }
16 | return len(s) == 0
17 | }
18 |
--------------------------------------------------------------------------------
/LeetCode/0946. Validate Stack Sequences/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 151 / 151 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 15.1 MB
5 | """
6 | class Solution:
7 | def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool:
8 | s = []
9 | i = 0
10 | for num in pushed:
11 | s.append(num)
12 | while s and s[-1] == popped[i]:
13 | i += 1
14 | s.pop()
15 | return len(s) == 0
16 |
--------------------------------------------------------------------------------
/LeetCode/0954. Array of Doubled Pairs/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 60 / 60 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.7 MB
5 | */
6 | class Solution {
7 | public:
8 | bool canWinNim(int n) {
9 | return n % 4 != 0;
10 | }
11 | };
12 |
--------------------------------------------------------------------------------
/LeetCode/0954. Array of Doubled Pairs/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 60 / 60 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def canReorderDoubled(self, arr: List[int]) -> bool:
8 | cnt = Counter(arr)
9 | for x in sorted(cnt, key=abs):
10 | if cnt[x << 1] < cnt[x]:
11 | return False
12 | cnt[x << 1] -= cnt[x]
13 | return True
14 |
--------------------------------------------------------------------------------
/LeetCode/0961. N-Repeated Element in Size 2N Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 102 / 102 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 16.1 MB
5 | """
6 | class Solution:
7 | def repeatedNTimes(self, nums: List[int]) -> int:
8 | arr = [0] * 10010
9 | for num in nums:
10 | arr[num] += 1
11 | if arr[num] > 1:
12 | return num
13 | return -1
14 |
--------------------------------------------------------------------------------
/LeetCode/0989. Add to Array-Form of Integer/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 156 / 156 test cases passed.
3 | Status: Accepted
4 | Runtime: 416 ms
5 | """
6 | class Solution:
7 | def addToArrayForm(self, A: List[int], K: int) -> List[int]:
8 | a = reduce(lambda acc, n: acc * 10 + n, A)
9 | ans = a + K
10 | return list(str(ans))
11 |
--------------------------------------------------------------------------------
/LeetCode/0997. Find the Town Judge/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 92 / 92 test cases passed.
3 | * Runtime: 88 ms
4 | * Memory Usage: 7.7 MB
5 | */
6 | func findJudge(n int, trust [][]int) int {
7 | degree := make([]int, n + 1)
8 | for _, t := range trust {
9 | degree[t[0]] -= 1
10 | degree[t[1]] += 1
11 | }
12 | for i, d := range degree[1:] {
13 | if d == n - 1 {
14 | return i + 1
15 | }
16 | }
17 | return -1
18 | }
19 |
--------------------------------------------------------------------------------
/LeetCode/0997. Find the Town Judge/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 92 / 92 test cases passed.
3 | Status: Accepted
4 | Runtime: 760 ms
5 | """
6 | class Solution:
7 | def findJudge(self, N: int, trust: List[List[int]]) -> int:
8 | degree = [0] * (N + 1)
9 | for o, i in trust:
10 | degree[o] -= 1
11 | degree[i] += 1
12 | for i in range(1, N + 1):
13 | if degree[i] == N - 1:
14 | return i
15 | return -1
16 |
--------------------------------------------------------------------------------
/LeetCode/1004. Max Consecutive Ones III/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 48 / 48 test cases passed.
3 | Runtime: 632 ms
4 | Memory Usage: 15.3 MB
5 | """
6 | class Solution:
7 | def longestOnes(self, A: List[int], K: int) -> int:
8 | l, r = 0, 0
9 | while r < len(A):
10 | K -= A[r] ^ 1
11 | if K < 0:
12 | K += A[l] ^ 1
13 | l += 1
14 | r += 1
15 | return r - l
16 |
--------------------------------------------------------------------------------
/LeetCode/1006. Clumsy Factorial/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 84 / 84 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def clumsy(self, N: int) -> int:
8 | if N <= 4:
9 | return [1, 2, 6, 7][N - 1]
10 | else:
11 | return N + [1, 2, 2, -1][N % 4]
12 |
--------------------------------------------------------------------------------
/LeetCode/1009. Complement of Base 10 Integer/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 128 / 128 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 34.8 MB
5 | */
6 | class Solution {
7 | public int bitwiseComplement(int n) {
8 | if (n == 0) return 1;
9 |
10 | int mask = n;
11 | mask |= mask >> 1;
12 | mask |= mask >> 2;
13 | mask |= mask >> 4;
14 | mask |= mask >> 8;
15 | mask |= mask >> 16;
16 | return mask ^ n;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------
/LeetCode/1009. Complement of Base 10 Integer/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 128 / 128 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.7 MB
5 | */
6 | class Solution {
7 | public:
8 | int bitwiseComplement(int n) {
9 | if (n == 0) return 1;
10 | return ((1ul << (32 - __builtin_clz(n))) - 1) ^ n;
11 | }
12 | };
13 |
--------------------------------------------------------------------------------
/LeetCode/1009. Complement of Base 10 Integer/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 128 / 128 test cases passed.
3 | Runtime: 28 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def bitwiseComplement(self, n: int) -> int:
8 | if not n:
9 | return 1
10 |
11 | mask = n
12 | mask |= mask >> 1;
13 | mask |= mask >> 2;
14 | mask |= mask >> 4;
15 | mask |= mask >> 8;
16 | mask |= mask >> 16;
17 | return mask ^ n
18 |
--------------------------------------------------------------------------------
/LeetCode/1011. Capacity To Ship Packages Within D Days/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 56 / 56 test cases passed.
3 | Runtime: 100 ms
4 | Memory Usage: 15.1 MB
5 | """
6 |
7 |
--------------------------------------------------------------------------------
/LeetCode/1014. Best Sightseeing Pair/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 53 / 53 test cases passed.
3 | Runtime: 132 ms
4 | Memory Usage: 19.8 MB
5 | """
6 | class Solution:
7 | def maxScoreSightseeingPair(self, values: List[int]) -> int:
8 | dp, ans = float('-inf'), 0
9 | for i, val in enumerate(values):
10 | ans = max(ans, dp + val - i)
11 | dp = max(dp, val + i)
12 | return ans
13 |
--------------------------------------------------------------------------------
/LeetCode/1021. Remove Outermost Parentheses/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 59 / 59 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 6.5 MB
5 | */
6 | class Solution {
7 | public:
8 | string removeOuterParentheses(string s) {
9 | string ans = "";
10 | stack stk;
11 | for (auto &c : s) {
12 | if (c == ')') stk.pop();
13 | if (stk.size()) ans.push_back(c);
14 | if (c == '(') stk.push(c);
15 | }
16 | return ans;
17 | }
18 | };
19 |
--------------------------------------------------------------------------------
/LeetCode/1021. Remove Outermost Parentheses/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 59 / 59 test cases passed.
3 | Runtime: 44 ms
4 | Memory Usage: 15.2 MB
5 | """
6 | class Solution:
7 | def removeOuterParentheses(self, s: str) -> str:
8 | count = 0
9 | ans = ''
10 | for c in s:
11 | if c == ')':
12 | count -= 1
13 | if count != 0:
14 | ans += c
15 | if c == '(':
16 | count += 1
17 | return ans
18 |
--------------------------------------------------------------------------------
/LeetCode/1037. Valid Boomerang/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 206 / 206 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 10.1 MB
5 | */
6 | class Solution {
7 | public:
8 | bool isBoomerang(vector>& points) {
9 | int x1 = points[0][0], x2 = points[1][0], x3 = points[2][0];
10 | int y1 = points[0][1], y2 = points[1][1], y3 = points[2][1];
11 | return (x1 * y2 - x2 * y1) + (x2 * y3 - x3 * y2) + (x3 * y1 - x1 * y3) != 0;
12 | }
13 | };
14 |
--------------------------------------------------------------------------------
/LeetCode/1037. Valid Boomerang/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 206 / 206 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def canWinNim(self, n: int) -> bool:
8 | return n % 4 != 0
9 |
10 |
--------------------------------------------------------------------------------
/LeetCode/1047. Remove All Adjacent Duplicates In String/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 98 / 98 test cases passed.
3 | Runtime: 60 ms
4 | Memory Usage: 15.2 MB
5 | """
6 | class Solution:
7 | def removeDuplicates(self, S: str) -> str:
8 | stk = []
9 | for c in S:
10 | if stk and stk[-1] == c:
11 | stk.pop()
12 | else:
13 | stk.append(c)
14 | return ''.join(stk)
15 |
--------------------------------------------------------------------------------
/LeetCode/1051. Height Checker/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 81 / 81 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.7 MB
5 | """
6 | class Solution:
7 | def heightChecker(self, heights: List[int]) -> int:
8 | return sum(x != y for x, y in zip(heights, sorted(heights)))
9 |
--------------------------------------------------------------------------------
/LeetCode/1078. Occurrences After Bigram/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 30 / 30 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 2.1 MB
5 | */
6 | func findOcurrences(text string, first string, second string) []string {
7 | words := strings.Split(text, " ")
8 | ans := make([]string, 0, len(text))
9 | for i := 2; i < len(words); i++ {
10 | if words[i - 2] == first && words[i - 1] == second {
11 | ans = append(ans, words[i]);
12 | }
13 | }
14 | return ans
15 | }
16 |
--------------------------------------------------------------------------------
/LeetCode/1078. Occurrences After Bigram/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 30 / 30 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def findOcurrences(self, text: str, first: str, second: str) -> List[str]:
8 | words = text.split()
9 | ans = []
10 | for i in range(2, len(words)):
11 | if words[i - 2] == first and words[i - 1] == second:
12 | ans.append(words[i])
13 | return ans
14 |
--------------------------------------------------------------------------------
/LeetCode/1108. Defanging an IP Address/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 62 / 62 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.9 MB
5 | */
6 | class Solution {
7 | public:
8 | string defangIPaddr(string address) {
9 | string defanged = "";
10 | for (auto &c : address) {
11 | c == '.' ? defanged += "[.]" : defanged += c;
12 | }
13 | return defanged;
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/LeetCode/1108. Defanging an IP Address/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 62 / 62 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def defangIPaddr(self, address: str) -> str:
8 | return address.replace('.', '[.]')
9 |
--------------------------------------------------------------------------------
/LeetCode/1137. N-th Tribonacci Number/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 39 / 39 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 35.1 MB
5 | */
6 | class Solution {
7 | public int tribonacci(int n) {
8 | if (n < 2) return n;
9 | int a = 0, b = 1, c = 1, t1, t2;
10 | for (int i = 2; i < n; i ++) {
11 | t1 = c;
12 | t2 = b;
13 | c = a + b + c;
14 | b = t1;
15 | a = t2;
16 | }
17 | return c;
18 | }
19 | }
20 |
--------------------------------------------------------------------------------
/LeetCode/1137. N-th Tribonacci Number/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 39 / 39 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.9 MB
5 | */
6 | class Solution {
7 | public:
8 | int tribonacci(int n) {
9 | if (n < 2) return n;
10 | int a = 0, b = 1, c = 1, t1, t2;
11 | for (int i = 3; i <= n; ++ i) {
12 | t1 = c;
13 | t2 = b;
14 | c = a + b + c;
15 | b = t1;
16 | a = t2;
17 | }
18 | return c;
19 | }
20 | };
21 |
--------------------------------------------------------------------------------
/LeetCode/1137. N-th Tribonacci Number/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 39 / 39 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 1.9 MB
5 | */
6 | func tribonacci(n int) int {
7 | if n < 2 {
8 | return n
9 | }
10 | a, b, c := 0, 1, 1
11 | for i := 3; i <= n; i++ {
12 | a, b, c = b, c, a+b+c
13 | }
14 | return c
15 | }
16 |
--------------------------------------------------------------------------------
/LeetCode/1137. N-th Tribonacci Number/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 39 / 39 test cases passed.
3 | Runtime: 44 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def tribonacci(self, n: int) -> int:
8 | if n < 2:
9 | return n;
10 | a, b, c = 0, 1, 1
11 | for i in range(3, n + 1):
12 | a, b, c = b, c, a + b + c
13 | return c
14 |
15 |
--------------------------------------------------------------------------------
/LeetCode/1154. Day of the Year/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 10957 / 10957 test cases passed.
3 | Runtime: 64 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | days = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]
8 | def dayOfYear(self, date: str) -> int:
9 | y, m, d = list(map(int, date.split('-')))
10 | d += sum(self.days[:m])
11 | d += ((y % 4 == 0 and y % 100 != 0) or y % 400 == 0) if m > 2 else 0
12 | return d
13 |
--------------------------------------------------------------------------------
/LeetCode/1184. Distance Between Bus Stops/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 37 / 37 test cases passed.
3 | Runtime: 44 ms
4 | Memory Usage: 15.6 MB
5 | """
6 | class Solution:
7 | def distanceBetweenBusStops(self, distance: List[int], start: int, destination: int) -> int:
8 | if start > destination:
9 | start, destination = destination, start
10 | return min(sum(distance[start:destination]), sum(distance[:start]) + sum(distance[destination:]))
--------------------------------------------------------------------------------
/LeetCode/1185. Day of the Week/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 43 / 43 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 2.1 MB
5 | */
6 | func dayOfTheWeek(day int, month int, year int) string {
7 | return time.Date(year, time.Month(month), day, 0, 0, 0, 0, time.Local).Format("Monday")
8 | }
9 |
--------------------------------------------------------------------------------
/LeetCode/1217. Minimum Cost to Move Chips to The Same Position/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 51 / 51 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 7.1 MB
5 | */
6 | class Solution {
7 | public:
8 | int minCostToMoveChips(vector& position) {
9 | int counter[2] = {0, 0};
10 | for (auto &x : position) {
11 | ++ counter[x & 1];
12 | }
13 | return min(counter[0], counter[1]);
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/LeetCode/1217. Minimum Cost to Move Chips to The Same Position/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 51 / 51 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 2 MB
5 | */
6 | func minCostToMoveChips(position []int) int {
7 | cnt := [2]int{}
8 | for _, x := range position {
9 | cnt[x & 1] ++
10 | }
11 | return min(cnt[0], cnt[1])
12 | }
13 |
14 | func min(x, y int) int {
15 | if x > y { return y }
16 | return x
17 | }
18 |
--------------------------------------------------------------------------------
/LeetCode/1217. Minimum Cost to Move Chips to The Same Position/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 51 / 51 test cases passed.
3 | Runtime: 48 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def minCostToMoveChips(self, position: List[int]) -> int:
8 | cnt = Counter(x & 1 for x in position)
9 | return min(cnt[0], cnt[1])
10 |
--------------------------------------------------------------------------------
/LeetCode/1221. Split a String in Balanced Strings/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 40 / 40 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 35.9 MB
5 | */
6 | class Solution {
7 | public int balancedStringSplit(String s) {
8 | int ans = 0, total = 0;
9 | for (char c: s.toCharArray()) {
10 | total += (c == 'R') ? 1 : -1;
11 | ans += (total == 0) ? 1 : 0;
12 | }
13 | return ans;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/LeetCode/1221. Split a String in Balanced Strings/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 40 / 40 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 6 MB
5 | */
6 | class Solution {
7 | public:
8 | int balancedStringSplit(string s) {
9 | int ans = 0, total = 0;
10 | for (char& c: s) {
11 | total += (c == 'R') ? 1 : -1;
12 | ans += (total == 0) ? 1 : 0;
13 | }
14 | return ans;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/1221. Split a String in Balanced Strings/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 40 / 40 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 1.9 MB
5 | */
6 | func balancedStringSplit(s string) int {
7 | ans, total := 0, 0
8 | for _, c := range s {
9 | if c == 'R' { total += 1 } else { total -= 1 }
10 | if total == 0 { ans += 1 }
11 | }
12 | return ans
13 | }
14 |
--------------------------------------------------------------------------------
/LeetCode/1221. Split a String in Balanced Strings/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 40 / 40 test cases passed.
3 | Runtime: 0 ms
4 | Memory Usage: 35.9 MB
5 | """
6 | class Solution:
7 | def balancedStringSplit(self, s: str) -> int:
8 | ans, total = 0, 0
9 | for c in s:
10 | total += 1 if c == 'R' else -1
11 | ans += 1 if total == 0 else 0
12 | return ans
13 |
--------------------------------------------------------------------------------
/LeetCode/1281. Subtract the Product and Sum of Digits of an Integer/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 38.1 MB
4 | */
5 | class Solution {
6 | public int subtractProductAndSum(int n) {
7 | int p = 1, s = 0;
8 | while (n > 0) {
9 | p *= n % 10;
10 | s += n % 10;
11 | n /= 10;
12 | }
13 | return p - s;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/LeetCode/1281. Subtract the Product and Sum of Digits of an Integer/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 5.7 MB
4 | */
5 | class Solution {
6 | public:
7 | int subtractProductAndSum(int n) {
8 | int p = 1, s = 0;
9 | while (n > 0) {
10 | p *= n % 10;
11 | s += n % 10;
12 | n /= 10;
13 | }
14 | return p - s;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/1281. Subtract the Product and Sum of Digits of an Integer/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 1.8 MB
4 | */
5 | func subtractProductAndSum(n int) int {
6 | p, s := 1, 0
7 | for ; n > 0; n /= 10 {
8 | p *= n % 10
9 | s += n % 10
10 | }
11 | return p - s
12 | }
13 |
--------------------------------------------------------------------------------
/LeetCode/1281. Subtract the Product and Sum of Digits of an Integer/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 40 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def subtractProductAndSum(self, n: int) -> int:
7 | p, s = 1, 0
8 | while n > 0:
9 | p *= n % 10
10 | s += n % 10
11 | n //= 10
12 | return p - s
13 |
--------------------------------------------------------------------------------
/LeetCode/1290. Convert Binary Number in a Linked List to Integer/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * Definition for singly-linked list.
3 | * type ListNode struct {
4 | * Val int
5 | * Next *ListNode
6 | * }
7 | */
8 |
9 | /**
10 | * Runtime: 0 ms
11 | * Memory Usage: 1.9 MB
12 | */
13 | func getDecimalValue(head *ListNode) (ans int) {
14 | for p := head; p != nil; p = p.Next {
15 | ans = (ans << 1) | p.Val
16 | }
17 | return
18 | }
--------------------------------------------------------------------------------
/LeetCode/1295. Find Numbers with Even Number of Digits/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 8 ms
3 | * Memory Usage: 9.8 MB
4 | */
5 | class Solution {
6 | public:
7 | int findNumbers(vector& nums) {
8 | return accumulate(nums.begin(), nums.end(), 0, [](int ans, int num) {
9 | return ans + ((int)(log10(num) + 1) % 2 == 0) ;
10 | });
11 | }
12 | };
13 |
--------------------------------------------------------------------------------
/LeetCode/1295. Find Numbers with Even Number of Digits/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 40 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def findNumbers(self, nums: List[int]) -> int:
7 | return sum(1 for num in nums if int(math.log10(num) + 1) % 2 == 0)
8 |
--------------------------------------------------------------------------------
/LeetCode/1310. XOR Queries of a Subarray/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 42 / 42 test cases passed.
3 | Runtime: 416 ms
4 | Memory Usage: 29.2 MB
5 | """
6 | class Solution:
7 | def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]:
8 | xors = [0] + list(accumulate(arr, xor))
9 | return [xors[i] ^ xors[j + 1] for i, j in queries]
10 |
11 |
--------------------------------------------------------------------------------
/LeetCode/1313. Decompress Run-Length Encoded List/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 9.8 MB
4 | */
5 | class Solution {
6 | public:
7 | vector decompressRLElist(vector& nums) {
8 | vector ans;
9 | for (int i = 0; i < nums.size(); i += 2) {
10 | for (int j = 0; j < nums[i]; ++ j) {
11 | ans.push_back(nums[i + 1]);
12 | }
13 | }
14 | return ans;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/1313. Decompress Run-Length Encoded List/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 36 ms
3 | Memory Usage: 15.2 MB
4 | """
5 | class Solution:
6 | def decompressRLElist(self, nums: List[int]) -> List[int]:
7 | ans = []
8 | for i in range(0, len(nums), 2):
9 | ans.extend(nums[i] * [nums[i + 1]])
10 | return ans
11 |
--------------------------------------------------------------------------------
/LeetCode/1331. Rank Transform of an Array/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 38 / 38 test cases passed.
3 | * Runtime: 64 ms
4 | * Memory Usage: 12.3 MB
5 | */
6 | func arrayRankTransform(arr []int) []int {
7 | a := append([]int{}, arr...)
8 | sort.Ints(a)
9 | rec := map[int]int{}
10 | for _, v := range a{
11 | if _, ok := rec[v]; !ok {
12 | rec[v] = len(rec) + 1
13 | }
14 | }
15 | for i, v := range arr {
16 | arr[i] = rec[v]
17 | }
18 | return arr
19 | }
20 |
--------------------------------------------------------------------------------
/LeetCode/1331. Rank Transform of an Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 38 / 38 test cases passed.
3 | Runtime: 92 ms
4 | Memory Usage: 36.1 MB
5 | """
6 | class Solution:
7 | def arrayRankTransform(self, arr: List[int]) -> List[int]:
8 | rec = {v: i for i, v in enumerate(sorted(set(arr)))}
9 | return [rec[v] + 1 for v in arr]
10 |
--------------------------------------------------------------------------------
/LeetCode/1337. The K Weakest Rows in a Matrix/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 39 / 39 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.9 MB
5 | */
6 | func twoSum(nums []int, target int) []int {
7 | store := make(map[int]int)
8 | for idx, n := range nums {
9 | if diffIdx, ok := store[target - n]; ok {
10 | return []int{diffIdx, idx}
11 | } else {
12 | store[n] = idx
13 | }
14 | }
15 | return []int{}
16 | }
17 |
--------------------------------------------------------------------------------
/LeetCode/1337. The K Weakest Rows in a Matrix/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 229 / 229 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def twoSum(self, nums: List[int], target: int) -> List[int]:
8 | store = dict()
9 | for idx, elm in enumerate(nums):
10 | if (target - elm) in store:
11 | return [store[target - elm], idx]
12 | store[elm] = idx
13 |
14 |
--------------------------------------------------------------------------------
/LeetCode/1342. Number of Steps to Reduce a Number to Zero/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 204 / 204 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.9 MB
5 | */
6 | class Solution {
7 | public:
8 | int numberOfSteps(int num) {
9 | int step = 0;
10 | while (num > 0) {
11 | if (num & 1) --num;
12 | else num >>= 1;
13 | ++step;
14 | }
15 | return step;
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/LeetCode/1342. Number of Steps to Reduce a Number to Zero/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 204 / 204 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 1.9 MB
5 | */
6 | func numberOfSteps(num int) int {
7 | step := 0
8 | for num > 0 {
9 | if num & 1 == 1 {
10 | num--
11 | } else {
12 | num >>= 1
13 | }
14 | step++
15 | }
16 | return step
17 | }
18 |
--------------------------------------------------------------------------------
/LeetCode/1342. Number of Steps to Reduce a Number to Zero/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 204 / 204 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def numberOfSteps(self, num: int) -> int:
8 | step = 0
9 | while num > 0:
10 | num = num - 1 if num & 1 else num >> 1
11 | step += 1
12 | return step
13 |
--------------------------------------------------------------------------------
/LeetCode/1374. Generate a String With Characters That Have Odd Counts/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 103 / 103 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 6.3 MB
5 | */
6 | class Solution {
7 | public:
8 | string generateTheString(int n) {
9 | if (n & 1) return string(n, 'a');
10 | return string(n - 1, 'a') + 'b';
11 | }
12 | };
13 |
--------------------------------------------------------------------------------
/LeetCode/1374. Generate a String With Characters That Have Odd Counts/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 103 / 103 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 2 MB
5 | */
6 | func generateTheString(n int) string {
7 | if n % 2 == 1 {
8 | return strings.Repeat("a", n)
9 | }
10 | return strings.Repeat("a", n - 1) + "b"
11 | }
12 |
--------------------------------------------------------------------------------
/LeetCode/1374. Generate a String With Characters That Have Odd Counts/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 103 / 103 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def generateTheString(self, n: int) -> str:
8 | if n % 2 == 1:
9 | return 'a' * n
10 | return 'a' * (n - 1) + 'b'
11 |
--------------------------------------------------------------------------------
/LeetCode/1389. Create Target Array in the Given Order/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 8.2 MB
4 | */
5 | class Solution {
6 | public:
7 | vector createTargetArray(vector& nums, vector& index) {
8 | vector ans;
9 | for (int i = 0; i < nums.size(); ++ i) {
10 | ans.insert(ans.begin() + index[i], nums[i]);
11 | }
12 | return ans;
13 | }
14 | };
15 |
--------------------------------------------------------------------------------
/LeetCode/1389. Create Target Array in the Given Order/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 32 ms
3 | Memory Usage: 14.9 MB
4 | """
5 | class Solution:
6 | def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]:
7 | ans = []
8 | for i, num in enumerate(nums):
9 | ans.insert(index[i], num)
10 | return ans
11 |
--------------------------------------------------------------------------------
/LeetCode/1403. Minimum Subsequence in Non-Increasing Order/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 103 / 103 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def minSubsequence(self, nums: List[int]) -> List[int]:
8 | nums.sort(reverse=True)
9 | total, s = sum(nums), 0
10 | for i, num in enumerate(nums):
11 | s += num
12 | if s > total - s:
13 | return nums[:i+1]
14 |
--------------------------------------------------------------------------------
/LeetCode/1408. String Matching in an Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 67 / 67 test cases passed.
3 | Runtime: 44 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def stringMatching(self, words: List[str]) -> List[str]:
8 | ans = []
9 | for i, x in enumerate(words):
10 | for j, y in enumerate(words):
11 | if i != j and x in y:
12 | ans.append(x)
13 | break
14 | return ans
15 |
--------------------------------------------------------------------------------
/LeetCode/1413. Minimum Value to Get Positive Step by Step Sum/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 55 / 55 test cases passed.
3 | Runtime: 28 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def minStartValue(self, nums: List[int]) -> int:
8 | ans, acc = 1, 1
9 | for num in nums:
10 | if acc + num <= 0:
11 | diff = -(acc + num) + 1
12 | ans += diff
13 | acc += diff
14 | acc += num
15 | return ans
16 |
--------------------------------------------------------------------------------
/LeetCode/1422. Maximum Score After Splitting a String/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 104 / 104 test cases passed.
3 | Runtime: 48 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def maxScore(self, s: str) -> int:
8 | ans = score = (s[0] == '0') + s[1:].count('1')
9 | for c in s[1:-1]:
10 | score += 1 if c == '0' else -1
11 | ans = max(ans, score)
12 | return ans
13 |
--------------------------------------------------------------------------------
/LeetCode/1423. Maximum Points You Can Obtain from Cards/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 40 / 40 test cases passed.
3 | Status: Accepted
4 | Runtime: 84 ms
5 | """
6 | class Solution:
7 | def maxScore(self, cardPoints: List[int], k: int) -> int:
8 | range_sum = sum(cardPoints[:k])
9 | ans = range_sum
10 | for i, j in zip(cardPoints[k-1::-1], cardPoints[:-k-1:-1]):
11 | range_sum = range_sum -i + j
12 | ans = max(ans, range_sum)
13 | return ans
14 |
--------------------------------------------------------------------------------
/LeetCode/1431. Kids With the Greatest Number of Candies/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 41.8 MB
4 | */
5 | class Solution {
6 | public List kidsWithCandies(int[] candies, int extraCandies) {
7 | int bound = Arrays.stream(candies).max().getAsInt() - extraCandies;
8 | return Arrays.stream(candies).mapToObj(candy -> candy >= bound).collect(Collectors.toList());
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/LeetCode/1431. Kids With the Greatest Number of Candies/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 8.7 MB
4 | */
5 | class Solution {
6 | public:
7 | vector kidsWithCandies(vector& candies, int extraCandies) {
8 | vector ans;
9 | int bound = *max_element(candies.begin(), candies.end()) - extraCandies;
10 | for (auto &candy: candies) {
11 | ans.push_back(candy >= bound);
12 | }
13 | return ans;
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/LeetCode/1431. Kids With the Greatest Number of Candies/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 32 ms
3 | Memory Usage: 14.9 MB
4 | """
5 | class Solution:
6 | def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]:
7 | bound = max(candies) - extraCandies
8 | return [candy >= bound for candy in candies]
9 |
--------------------------------------------------------------------------------
/LeetCode/1441. Build an Array With Stack Operations/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 32 ms
3 | Memory Usage: 14.8 MB
4 | """
5 | class Solution:
6 | def buildArray(self, target: List[int], n: int) -> List[str]:
7 | ans = []
8 | prv = 0
9 | for num in target:
10 | for _ in range(num - prv - 1):
11 | ans += ['Push', 'Pop']
12 | ans += ['Push']
13 | prv = num
14 | return ans
15 |
--------------------------------------------------------------------------------
/LeetCode/1442. Count Triplets That Can Form Two Arrays of Equal XOR/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 56 / 56 test cases passed.
3 | Runtime: 52 ms
4 | Memory Usage: 15.1 MB
5 | """
6 | class Solution:
7 | def countTriplets(self, arr: List[int]) -> int:
8 | xors = [0] + list(accumulate(arr, xor))
9 | ans = 0
10 | for i in range(len(arr)):
11 | for k in range(i + 1, len(arr)):
12 | if xors[i] == xors[k + 1]:
13 | ans += k - i
14 | return ans
15 |
--------------------------------------------------------------------------------
/LeetCode/1450. Number of Students Doing Homework at a Given Time/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 111 / 111 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def busyStudent(self, startTime: List[int], endTime: List[int], queryTime: int) -> int:
8 | return sum(1 for st, et in zip(startTime, endTime) if st <= queryTime <= et)
9 |
--------------------------------------------------------------------------------
/LeetCode/1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 40 / 40 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def isPrefixOfWord(self, sentence: str, searchWord: str) -> int:
8 | for num, word in enumerate(sentence.split(), 1):
9 | if word.startswith(searchWord):
10 | return num
11 | return -1
12 |
--------------------------------------------------------------------------------
/LeetCode/1460. Make Two Arrays Equal by Reversing Sub-arrays/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 106 / 106 test cases passed.
3 | * Runtime: 12 ms
4 | * Memory Usage: 13.7 MB
5 | */
6 | class Solution {
7 | public:
8 | bool canBeEqual(vector& target, vector& arr) {
9 | sort(target.begin(), target.end());
10 | sort(arr.begin(), arr.end());
11 | return target == arr;
12 | }
13 | };
14 |
--------------------------------------------------------------------------------
/LeetCode/1460. Make Two Arrays Equal by Reversing Sub-arrays/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 106 / 106 test cases passed.
3 | Runtime: 48 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def canBeEqual(self, target: List[int], arr: List[int]) -> bool:
8 | return Counter(target) == Counter(arr)
9 |
--------------------------------------------------------------------------------
/LeetCode/1470. Shuffle the Array/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 53 / 53 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 41.5 MB
5 | */
6 | class Solution {
7 | public int[] shuffle(int[] nums, int n) {
8 | int[] ans = new int[nums.length];
9 | for (int i = 0, j = 0, half = nums.length >> 1; i < half; i++, j += 2) {
10 | ans[j] = nums[i];
11 | ans[j + 1] = nums[i + half];
12 | }
13 | return ans;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/LeetCode/1470. Shuffle the Array/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 53 / 53 test cases passed.
3 | * Runtime: 8 ms
4 | * Memory Usage: 3.6 MB
5 | */
6 | func shuffle(nums []int, n int) []int {
7 | ret := make([]int, 0, n + n)
8 | for i := 0; i < n; i ++ {
9 | ret = append(ret, nums[i], nums[i + n])
10 | }
11 | return ret
12 | }
13 |
--------------------------------------------------------------------------------
/LeetCode/1475. Final Prices With a Special Discount in a Shop/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 103 / 103 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def finalPrices(self, prices: List[int]) -> List[int]:
8 | for i, pricei in enumerate(prices):
9 | for j, pricej in enumerate(prices[i + 1:]):
10 | if pricej <= pricei:
11 | prices[i] = pricei - pricej
12 | break
13 | return prices
14 |
--------------------------------------------------------------------------------
/LeetCode/1486. XOR Operation in an Array/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 54 / 54 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.8 MB
5 | */
6 | class Solution {
7 | public:
8 | int xorOperation(int n, int start) {
9 | int ans = 0;
10 | for (int i = 0; i < n; ++ i ) {
11 | ans ^= start + 2 * i;
12 | }
13 | return ans;
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/LeetCode/1486. XOR Operation in an Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 54 / 54 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | from functools import reduce
7 | from operator import xor
8 | class Solution:
9 | def xorOperation(self, n: int, start: int) -> int:
10 | return reduce(xor, [start + 2 * i for i in range(n)])
11 |
--------------------------------------------------------------------------------
/LeetCode/1512. Number of Good Pairs/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 6.9 MB
4 | */
5 | class Solution {
6 | public:
7 | int numIdenticalPairs(vector& nums) {
8 | int ans = 0;
9 | for (int i = 0; i < nums.size(); ++ i) {
10 | for (int j = i + 1; j < nums.size(); ++ j) {
11 | if (nums[i] == nums[j]) {
12 | ++ans;
13 | }
14 | }
15 | }
16 | return ans;
17 | }
18 | };
19 |
--------------------------------------------------------------------------------
/LeetCode/1512. Number of Good Pairs/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 36 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def numIdenticalPairs(self, nums: List[int]) -> int:
7 | m = collections.Counter(nums)
8 | return sum(v * (v - 1) // 2 for k, v in m.items())
9 |
--------------------------------------------------------------------------------
/LeetCode/1528. Shuffle String/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 8 ms
3 | * Memory Usage: 14.7 MB
4 | */
5 | class Solution {
6 | public:
7 | string restoreString(string s, vector& indices) {
8 | for (int i = 0; i < s.size(); ++ i) {
9 | while (i != indices[i]) {
10 | swap(s[i], s[indices[i]]);
11 | swap(indices[i], indices[indices[i]]);
12 | }
13 | }
14 | return s;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/1528. Shuffle String/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 60 ms
3 | Memory Usage: 14.9 MB
4 | """
5 | class Solution:
6 | def restoreString(self, s: str, indices: List[int]) -> str:
7 | rec = [''] * len(s)
8 | for c, i in zip(s, indices):
9 | rec[i] = c
10 | return ''.join(rec)
11 |
--------------------------------------------------------------------------------
/LeetCode/1557. Minimum Number of Vertices to Reach All Nodes/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 66 / 66 test cases passed.
3 | Runtime: 140 ms
4 | Memory Usage: 36 MB
5 | """
6 | class Solution:
7 | def findSmallestSetOfVertices(self, n: int, edges: List[List[int]]) -> List[int]:
8 | indegree = [0] * n
9 | for edge in edges:
10 | indegree[edge[1]] = True
11 | ans = []
12 | for i, d in enumerate(indegree):
13 | if d == 0:
14 | ans.append(i)
15 | return ans
16 |
17 |
--------------------------------------------------------------------------------
/LeetCode/1572. Matrix Diagonal Sum/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 8 ms
3 | * Memory Usage: 10.9 MB
4 | */
5 | class Solution {
6 | public:
7 | int diagonalSum(vector>& mat) {
8 | int n = mat.size(), mid = n >> 1;
9 | int ans = -mat[mid][mid] * (n & 1);
10 | for (int i = 0; i < n; ++ i) {
11 | ans += mat[i][i] + mat[i][n - 1 - i];
12 | }
13 | return ans;
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/LeetCode/1572. Matrix Diagonal Sum/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 32 ms
3 | Memory Usage: 15.2 MB
4 | """
5 | class Solution:
6 | def diagonalSum(self, mat: List[List[int]]) -> int:
7 | n = len(mat)
8 | mid= n >> 1
9 | ans = -mat[mid][mid] * (n & 1)
10 | for i in range(n):
11 | ans += mat[i][i] + mat[i][n - 1 - i]
12 | return ans
13 |
--------------------------------------------------------------------------------
/LeetCode/1592. Rearrange Spaces Between Words/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 60 / 60 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def canWinNim(self, n: int) -> bool:
8 | return n % 4 != 0
9 |
10 |
--------------------------------------------------------------------------------
/LeetCode/1598. Crawler Log Folder/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 99 / 99 test cases passed.
3 | Runtime: 44 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def minOperations(self, logs: List[str]) -> int:
8 | s = []
9 | for log in logs:
10 | if log == '../':
11 | if s:
12 | s.pop()
13 | continue
14 | if log == './':
15 | continue
16 | s.append(log)
17 | return len(s)
18 |
--------------------------------------------------------------------------------
/LeetCode/1608. Special Array With X Elements Greater Than or Equal X/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 98 / 98 test cases passed.
3 | Runtime: 24 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def specialArray(self, nums: List[int]) -> int:
8 | nums.sort(reverse=True)
9 | n = len(nums)
10 | for i in range(1, n + 1):
11 | if i <= nums[i - 1] and (i == n or nums[i] < i):
12 | return i
13 | return -1
14 |
--------------------------------------------------------------------------------
/LeetCode/1614. Maximum Nesting Depth of the Parentheses/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 167 / 167 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 6.1 MB
5 | */
6 | class Solution {
7 | public:
8 | int maxDepth(string s) {
9 | int depth = 0;
10 | int max_depth = 0;
11 | for (auto &c : s) {
12 | if (c == '(') ++depth;
13 | else if (c == ')') --depth;
14 | max_depth = max(max_depth, depth);
15 | }
16 | return max_depth;
17 | }
18 | };
19 |
--------------------------------------------------------------------------------
/LeetCode/1614. Maximum Nesting Depth of the Parentheses/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 167 / 167 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 1.9 MB
5 | */
6 | func maxDepth(s string) int {
7 | max_depth, depth := 0, 0
8 | for _, c := range s {
9 | if c == '(' { depth++ }
10 | if c == ')' { depth-- }
11 | if depth > max_depth { max_depth = depth }
12 | }
13 | return max_depth
14 | }
15 |
--------------------------------------------------------------------------------
/LeetCode/1614. Maximum Nesting Depth of the Parentheses/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 167 / 67 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def maxDepth(self, s: str) -> int:
8 | max_depth, depth = 0, 0
9 | for c in s:
10 | if c == '(':
11 | depth += 1
12 | max_depth = max(max_depth, depth)
13 | elif c == ')':
14 | depth -= 1
15 | return max_depth
16 |
--------------------------------------------------------------------------------
/LeetCode/1619. Mean of Array After Removing Some Elements/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 50 / 50 test cases passed.
3 | * Runtime: 8 ms
4 | * Memory Usage: 9.1 MB
5 | */
6 | class Solution {
7 | public:
8 | double trimMean(vector& arr) {
9 | double ans = 0;
10 | sort(arr.begin(), arr.end());
11 | size_t len = arr.size();
12 | for (int i = len / 20, end = len * 19/20; i < end; ++i) {
13 | ans += arr[i];
14 | }
15 | return ans / (len * 0.9);
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/LeetCode/1619. Mean of Array After Removing Some Elements/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 50 / 50 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 15.1 MB
5 | """
6 | class Solution:
7 | def trimMean(self, arr: List[int]) -> float:
8 | arr.sort()
9 | n = len(arr)
10 | return sum(arr[n//20:-n//20]) / (n * 0.9)
11 |
--------------------------------------------------------------------------------
/LeetCode/1624. Largest Substring Between Two Equal Characters/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 54 / 54 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def maxLengthBetweenEqualCharacters(self, s: str) -> int:
8 | rec = {}
9 | ans = -1
10 | for i, c in enumerate(s):
11 | if c in rec:
12 | ans = max(ans, i - rec[c] - 1)
13 | else:
14 | rec[c] = i
15 | return ans
16 |
--------------------------------------------------------------------------------
/LeetCode/1636. Sort Array by Increasing Frequency/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 180 / 180 test cases passed.
3 | Runtime: 44 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def frequencySort(self, nums: List[int]) -> List[int]:
8 | cnt = Counter(nums)
9 | nums.sort(key=lambda x: (cnt[x], -x))
10 | return nums
11 |
--------------------------------------------------------------------------------
/LeetCode/1662. Check If Two String Arrays are Equivalent/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 1 ms
3 | * Memory Usage: 39.1 MB
4 | */
5 | class Solution {
6 | public boolean arrayStringsAreEqual(String[] word1, String[] word2) {
7 | return String.join("", word1).equals(String.join("", word2));
8 | }
9 | }
10 |
--------------------------------------------------------------------------------
/LeetCode/1662. Check If Two String Arrays are Equivalent/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 12.1 MB
4 | */
5 | class Solution {
6 | public:
7 | bool arrayStringsAreEqual(vector& word1, vector& word2) {
8 | return accumulate(word1.begin(), word1.end(), ""s) == accumulate(word2.begin(), word2.end(), ""s);
9 | }
10 | };
--------------------------------------------------------------------------------
/LeetCode/1662. Check If Two String Arrays are Equivalent/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 32 ms
3 | Memory Usage: 15.1 MB
4 | """
5 | class Solution:
6 | def arrayStringsAreEqual(self, word1: List[str], word2: List[str]) -> bool:
7 | return ''.join(word1) == ''.join(word2)
8 |
--------------------------------------------------------------------------------
/LeetCode/1672. Richest Customer Wealth/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 34 / 34 test cases passed.
3 | * Runtime: 4 ms
4 | * Memory Usage: 7.6 MB
5 | */
6 | class Solution {
7 | public:
8 | int maximumWealth(vector>& accounts) {
9 | int ans = 0;
10 | for (auto &account : accounts) {
11 | ans = max(ans, accumulate(account.begin(), account.end(), 0));
12 | }
13 | return ans;
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/LeetCode/1672. Richest Customer Wealth/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 34 / 34 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def maximumWealth(self, accounts: List[List[int]]) -> int:
8 | return max(sum(account) for account in accounts)
9 |
--------------------------------------------------------------------------------
/LeetCode/1672. Richest Customer Wealth/solution.rs:
--------------------------------------------------------------------------------
1 | /**
2 | * 34 / 34 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 2.2 MB
5 | */
6 | impl Solution {
7 | pub fn maximum_wealth(accounts: Vec>) -> i32 {
8 | accounts.iter().fold(0i32, |ans, account| ans.max(account.iter().sum()))
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/LeetCode/1678. Goal Parser Interpretation/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 32 ms
3 | Memory Usage: 14.9 MB
4 | """
5 | class Solution:
6 | def interpret(self, command: str) -> str:
7 | ans = []
8 | for i, c in enumerate(command):
9 | if c == 'G':
10 | ans.append(c)
11 | elif c == '(':
12 | ans.append('o' if command[i + 1] == ')' else 'al')
13 | return ''.join(ans)
14 |
--------------------------------------------------------------------------------
/LeetCode/1688. Count of Matches in Tournament/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 200 / 200 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 1.8 MB
5 | */
6 | func numberOfMatches(n int) int {
7 | return n - 1
8 | }
--------------------------------------------------------------------------------
/LeetCode/1688. Count of Matches in Tournament/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 200 / 200 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def numberOfMatches(self, n: int) -> int:
8 | return n - 1
9 |
--------------------------------------------------------------------------------
/LeetCode/1689. Partitioning Into Minimum Number Of Deci-Binary Numbers/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 28 ms
3 | * Memory Usage: 13.2 MB
4 | */
5 | class Solution {
6 | public:
7 | int minPartitions(string n) {
8 | char mx = '0';
9 | for (auto &c: n) {
10 | mx = max(mx, c);
11 | }
12 | return mx - '0';
13 | }
14 | };
15 |
--------------------------------------------------------------------------------
/LeetCode/1689. Partitioning Into Minimum Number Of Deci-Binary Numbers/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 76 ms
3 | Memory Usage: 16.2 MB
4 | """
5 | class Solution:
6 | def minPartitions(self, n: str) -> int:
7 | return int(max(list(n)))
8 |
--------------------------------------------------------------------------------
/LeetCode/1704. Determine if String Halves Are Alike/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 36 ms
3 | Memory Usage: 15.1 MB
4 | """
5 | class Solution:
6 | def halvesAreAlike(self, s: str) -> bool:
7 | vowels = 'aeiouAEIOU'
8 | h = len(s) >> 1
9 | return sum(c in vowels for c in s[:h]) == sum(c in vowels for c in s[h:])
10 |
--------------------------------------------------------------------------------
/LeetCode/1720. Decode XORed Array/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 76 / 76 test cases passed.
3 | * Runtime: 28 ms
4 | * Memory Usage: 24.3 MB
5 | */
6 | class Solution {
7 | public:
8 | vector decode(vector& encoded, int first) {
9 | vector ans(encoded.size() + 1);
10 | ans[0] = first;
11 | for (int i = 0; i < encoded.size(); ++ i) {
12 | ans[i + 1] = ans[i] ^ encoded[i];
13 | }
14 | return ans;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/1732. Find the Highest Altitude/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 7.7 MB
4 | */
5 | class Solution {
6 | public:
7 | int largestAltitude(vector& gain) {
8 | int ans = 0, pre = 0;
9 | for (int i = 0; i < gain.size(); ++ i) {
10 | ans = max(ans, pre + gain[i]);
11 | pre += gain[i];
12 | }
13 | return ans;
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/LeetCode/1732. Find the Highest Altitude/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 2 MB
4 | */
5 | func largestAltitude(gain []int) int {
6 | ans, pre := 0, 0
7 | for _, g := range gain {
8 | if pre + g > ans {
9 | ans = pre + g
10 | }
11 | pre += g
12 | }
13 | return ans
14 | }
15 |
--------------------------------------------------------------------------------
/LeetCode/1732. Find the Highest Altitude/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 32 ms
3 | Memory Usage: 14.9 MB
4 | """
5 | class Solution:
6 | def largestAltitude(self, gain: List[int]) -> int:
7 | ans = pre = 0
8 | for g in gain:
9 | ans = max(ans, pre + g)
10 | pre += g
11 | return ans
12 |
--------------------------------------------------------------------------------
/LeetCode/1734. Decode XORed Permutation/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 63 / 63 test cases passed.
3 | Runtime: 168 ms
4 | Memory Usage: 30.8 MB
5 | """
6 | class Solution:
7 | def decode(self, encoded: List[int]) -> List[int]:
8 | first = reduce(xor, encoded[1::2] + list(range(1, len(encoded) + 2)))
9 | return list(accumulate(encoded, xor, initial=first))
10 |
11 |
--------------------------------------------------------------------------------
/LeetCode/1768. Merge Strings Alternately/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 44 ms
3 | Memory Usage: 15 MB
4 | """
5 | from itertools import zip_longest
6 |
7 | class Solution:
8 | def mergeAlternately(self, word1: str, word2: str) -> str:
9 | return ''.join(a + b for a, b in zip_longest(word1, word2, fillvalue=''))
10 |
--------------------------------------------------------------------------------
/LeetCode/1773. Count Items Matching a Rule/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 48 ms
3 | Memory Usage: 19.2 MB
4 | """
5 | class Solution:
6 | def countMatches(self, items: List[List[str]], ruleKey: str, ruleValue: str) -> int:
7 | key2idx = {'type': 0, 'color': 1, 'name':2 }
8 | ans = 0
9 | for item in items:
10 | i = key2idx[ruleKey]
11 | if item[i] == ruleValue:
12 | ans += 1
13 | return ans
14 |
--------------------------------------------------------------------------------
/LeetCode/1791. Find Center of Star Graph/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 60 / 60 test cases passed.
3 | * Runtime: 164 ms
4 | * Memory Usage: 65.6 MB
5 | */
6 | class Solution {
7 | public:
8 | int findCenter(vector>& edges) {
9 | int u = edges[0][0], v = edges[0][1];
10 | if (u == edges[1][0] || u == edges[1][1]) return u;
11 | return v;
12 | }
13 | };
14 |
--------------------------------------------------------------------------------
/LeetCode/1800. Maximum Ascending Subarray Sum/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 104 / 104 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def maxAscendingSum(self, nums: List[int]) -> int:
8 | ans = total = 0
9 | for i, num in enumerate(nums):
10 | if i == 0 or num > nums[i - 1]:
11 | total += num
12 | else:
13 | total = num
14 | ans = max(ans, total)
15 | return ans
16 |
--------------------------------------------------------------------------------
/LeetCode/1812. Determine Color of a Chessboard Square/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 5.9 MB
4 | */
5 | class Solution {
6 | public:
7 | bool squareIsWhite(string c) {
8 | return ((c[0] - 'a') & 1) ^ ((c[1] - '1') & 1);
9 | }
10 | };
11 |
--------------------------------------------------------------------------------
/LeetCode/1812. Determine Color of a Chessboard Square/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 24 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def squareIsWhite(self, c: str) -> bool:
7 | return bool(ord(c[0]) & 1 ^ ord(c[1]) & 1)
8 |
--------------------------------------------------------------------------------
/LeetCode/1816. Truncate Sentence/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 71 / 71 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 6.1 MB
5 | */
6 | class Solution {
7 | public:
8 | string truncateSentence(string s, int k) {
9 | for (int i = 0; i < s.size(); ++i) {
10 | if (s[i] == ' ' && --k == 0) {
11 | return s.substr(0, i);
12 | }
13 | }
14 | return s;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/1822. Sign of the Product of an Array/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 40.9 MB
4 | */
5 | class Solution {
6 | public int arraySign(int[] nums) {
7 | int sign = 1;
8 | for (int num: nums) {
9 | if (num == 0) return 0;
10 | if (num < 0) sign *= -1;
11 | }
12 | return sign;
13 | }
14 | }
15 |
--------------------------------------------------------------------------------
/LeetCode/1822. Sign of the Product of an Array/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 10 MB
4 | */
5 | class Solution {
6 | public:
7 | int arraySign(vector& nums) {
8 | int sign = 1;
9 | for (auto &num : nums) {
10 | if (num == 0) return 0;
11 | if (num < 0) sign *= -1;
12 | }
13 | return sign;
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/LeetCode/1822. Sign of the Product of an Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 36 ms
3 | Memory Usage: 15.1 MB
4 | """
5 | class Solution:
6 | def arraySign(self, nums: List[int]) -> int:
7 | neg = 0
8 | for num in nums:
9 | if num == 0:
10 | return 0
11 | if num < 0:
12 | neg += 1
13 | return 1 if neg & 1 == 0 else -1
14 |
--------------------------------------------------------------------------------
/LeetCode/1823. Find the Winner of the Circular Game/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 95 / 95 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.8 MB
5 | */
6 | class Solution {
7 | public:
8 | int findTheWinner(int n, int k) {
9 | int p = 0;
10 | for (int i = 2; i <= n; ++ i) {
11 | p = (p + k) % i;
12 | }
13 | return p + 1;
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/LeetCode/1823. Find the Winner of the Circular Game/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 95 / 95 test cases passed.
3 | Runtime: 260 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def findTheWinner(self, n: int, k: int) -> int:
8 | que = collections.deque(range(1, n + 1))
9 | for _ in range(n - 1):
10 | for i in range(k - 1):
11 | que.append(que.popleft())
12 | que.popleft()
13 | return que.popleft()
14 |
--------------------------------------------------------------------------------
/LeetCode/1827. Minimum Operations to Make the Array Increasing/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 8 ms
3 | * Memory Usage: 15.3 MB
4 | */
5 | class Solution {
6 | public:
7 | int minOperations(vector& nums) {
8 | int ans = 0;
9 | for (int i = 1; i < nums.size(); ++ i) {
10 | if (nums[i] <= nums[i - 1]) {
11 | ans += nums[i - 1] - nums[i] + 1;
12 | nums[i] = nums[i - 1] + 1;
13 | }
14 | }
15 | return ans;
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/LeetCode/1828. Queries on Number of Points Inside a Circle/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 860 ms
3 | Memory Usage: 15.2 MB
4 | """
5 | class Solution:
6 | def countPoints(self, points: List[List[int]], queries: List[List[int]]) -> List[int]:
7 | calc = lambda x, y, r, i, j: 1 if (x - i) * (x - i) + (y - j) * (y - j) <= r * r else 0
8 | return [sum(calc(x, y, r, i, j) for i, j in points) for x, y, r in queries]
9 |
--------------------------------------------------------------------------------
/LeetCode/1832. Check if the Sentence Is Pangram/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 6.2 MB
4 | */
5 | class Solution {
6 | public:
7 | bool checkIfPangram(string sentence) {
8 | if (sentence.size() < 26) return false;
9 | int mask = 0;
10 | for (auto &c: sentence) {
11 | mask |= 1 << (c - 'a');
12 | }
13 | return mask == 0x3FFFFFF;
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/LeetCode/1832. Check if the Sentence Is Pangram/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 36 ms
3 | Memory Usage: 14.9 MB
4 | """
5 | class Solution:
6 | def checkIfPangram(self, sentence: str) -> bool:
7 | if len(sentence) < 26:
8 | return False;
9 | mask, a, alphabet = 0, ord('a'), 0x3FFFFFF
10 | for c in sentence:
11 | mask |= 1 << (ord(c) - a)
12 | return mask == alphabet
13 |
--------------------------------------------------------------------------------
/LeetCode/1844. Replace All Digits with Characters/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 5.9 MB
4 | */
5 | class Solution {
6 | public:
7 | string replaceDigits(string s) {
8 | for (int i = 1; i < s.size(); i += 2) {
9 | s[i] = s[i - 1] + (s[i] - '0');
10 | }
11 | return s;
12 | }
13 | };
14 |
--------------------------------------------------------------------------------
/LeetCode/1880. Check if Word Equals Summation of Two Words/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 28 ms
3 | Memory Usage: 14.8 MB
4 | """
5 | class Solution:
6 | def isSumEqual(self, firstWord: str, secondWord: str, targetWord: str) -> bool:
7 | def numerical(word):
8 | ret = 0
9 | for c in word:
10 | ret *= 10
11 | ret += ord(c) - ord('a')
12 | return ret
13 | return numerical(firstWord) + numerical(secondWord) == numerical(targetWord)
14 |
--------------------------------------------------------------------------------
/LeetCode/1920. Build Array from Permutation/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 140 / 140 test cases passed.
3 | * Runtime: 12 ms
4 | * Memory Usage: 15.8 MB
5 | */
6 | class Solution {
7 | public:
8 | vector buildArray(vector& nums) {
9 | for (auto &num : nums) {
10 | num += 1000 * (nums[num] % 1000);
11 | }
12 | for (auto &num : nums) {
13 | num /= 1000;
14 | }
15 | return nums;
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/LeetCode/1920. Build Array from Permutation/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 140 / 140 test cases passed.
3 | Runtime: 40 ms
4 | Memory Usage: 15 MB
5 | """
6 | class Solution:
7 | def buildArray(self, nums: List[int]) -> List[int]:
8 | return [nums[num] for num in nums]
9 |
--------------------------------------------------------------------------------
/LeetCode/1929. Concatenation of Array/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 12.5 MB
4 | */
5 | class Solution {
6 | public:
7 | vector getConcatenation(vector& nums) {
8 | nums.insert(nums.end(), nums.begin(), nums.end());
9 | return nums;
10 | }
11 | };
12 |
--------------------------------------------------------------------------------
/LeetCode/1929. Concatenation of Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 32 ms
3 | Memory Usage: 15.1 MB
4 | """
5 | class Solution:
6 | def getConcatenation(self, nums: List[int]) -> List[int]:
7 | return [*nums, *nums]
8 |
--------------------------------------------------------------------------------
/LeetCode/1941. Check if All Characters Have Equal Number of Occurrences/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 28 ms
3 | Memory Usage: 15.1 MB
4 | """
5 | class Solution:
6 | def areOccurrencesEqual(self, s: str) -> bool:
7 | cnt = Counter(s)
8 | p = len(s) // len(cnt)
9 | return all(p == q for q in cnt.values())
10 |
--------------------------------------------------------------------------------
/LeetCode/1967. Number of Strings That Appear as Substrings in Word/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 8.5 MB
4 | */
5 | class Solution {
6 | public:
7 | int numOfStrings(vector& patterns, string word) {
8 | int ans = 0;
9 | for (auto &pattern: patterns) {
10 | ans += word.find(pattern) != string::npos;
11 | }
12 | return ans;
13 | }
14 | };
15 |
--------------------------------------------------------------------------------
/LeetCode/1967. Number of Strings That Appear as Substrings in Word/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 28 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def numOfStrings(self, patterns: List[str], word: str) -> int:
7 | return sum(p in word for p in patterns)
8 |
--------------------------------------------------------------------------------
/LeetCode/2000. Reverse Prefix of Word/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 112 / 112 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 6 MB
5 | */
6 | class Solution {
7 | public:
8 | string reversePrefix(string word, char ch) {
9 | int idx = word.find(ch);
10 | if (idx != string::npos) {
11 | reverse(word.begin(), word.begin() +idx + 1);
12 | }
13 | return word;
14 | }
15 | };
--------------------------------------------------------------------------------
/LeetCode/2000. Reverse Prefix of Word/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 112 / 112 test cases passed.
3 | Runtime: 36 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | class Solution:
7 | def reversePrefix(self, word: str, ch: str) -> str:
8 | i = word.find(ch) + 1
9 | return word[:i][::-1] + word[i:]
10 |
--------------------------------------------------------------------------------
/LeetCode/2011. Final Value of Variable After Performing Operations/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 13.5 MB
4 | */
5 | class Solution {
6 | public:
7 | int finalValueAfterOperations(vector& operations) {
8 | int x = 0;
9 | for (const auto& op: operations) {
10 | x += op[1] == '+' ? 1 : -1;
11 | }
12 | return x;
13 | }
14 | };
15 |
--------------------------------------------------------------------------------
/LeetCode/2011. Final Value of Variable After Performing Operations/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 44 ms
3 | Memory Usage: 15.2 MB
4 | """
5 | class Solution:
6 | def finalValueAfterOperations(self, operations: List[str]) -> int:
7 | x = 0
8 | for op in operations:
9 | x += 1 if op[1] == '+' else -1
10 | return x
11 |
--------------------------------------------------------------------------------
/LeetCode/2022. Convert 1D Array Into 2D Array/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 107 / 107 test cases passed.
3 | * Runtime: 120 ms
4 | * Memory Usage: 8.7 MB
5 | */
6 | func construct2DArray(original []int, m int, n int) [][]int {
7 | if len(original) != m * n {
8 | return [][]int{}
9 | }
10 | mat := make([][]int, 0, m)
11 | for i := 0; i < len(original); i += n {
12 | mat = append(mat, original[i:i+n])
13 | }
14 | return mat
15 | }
16 |
--------------------------------------------------------------------------------
/LeetCode/2027. Minimum Moves to Convert String/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 6.3 MB
4 | */
5 | class Solution {
6 | public:
7 | int minimumMoves(string s) {
8 | int move = 0, cover = -1;
9 | for (int i = 0; i < s.size(); ++ i) {
10 | if (s[i] == 'X' && i > cover) {
11 | ++ move;
12 | cover = i + 2;
13 | }
14 | }
15 | return move;
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/LeetCode/2027. Minimum Moves to Convert String/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 40 ms
3 | Memory Usage: 14.9 MB
4 | """
5 | class Solution:
6 | def minimumMoves(self, s: str) -> int:
7 | move = 0
8 | cover = -1
9 | for i, c in enumerate(s):
10 | if c == 'X' and i > cover:
11 | move += 1
12 | cover = i + 2
13 | return move
14 |
--------------------------------------------------------------------------------
/LeetCode/2032. Two Out of Three/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 40 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def twoOutOfThree(self, nums1: List[int], nums2: List[int], nums3: List[int]) -> List[int]:
7 | mask = defaultdict(int)
8 | for i, nums in enumerate((nums1, nums2, nums3)):
9 | for n in nums:
10 | mask[n] |= 1 << i
11 | return [n for n, c in mask.items() if c & (c - 1)]
12 |
--------------------------------------------------------------------------------
/LeetCode/2037. Minimum Number of Moves to Seat Everyone/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 17.6 MB
4 | */
5 | class Solution {
6 | public:
7 | int minMovesToSeat(vector& seats, vector& students) {
8 | sort(seats.begin(), seats.end());
9 | sort(students.begin(), students.end());
10 | int ans = 0;
11 | for (int i = 0; i < seats.size(); ++ i) {
12 | ans += abs(students[i] - seats[i]);
13 | }
14 | return ans;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/2037. Minimum Number of Moves to Seat Everyone/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 44 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def minMovesToSeat(self, seats: List[int], students: List[int]) -> int:
7 | seats.sort()
8 | students.sort()
9 | ans = 0
10 | for seat, stu in zip(seats, students):
11 | ans += abs(seat - stu)
12 | return ans
13 |
--------------------------------------------------------------------------------
/LeetCode/2042. Check if Numbers Are Ascending in a Sentence/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 44 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def areNumbersAscending(self, s: str) -> bool:
7 | pre = 0
8 | for t in s.split():
9 | if t[0].isdigit():
10 | if (cur := int(t)) <= pre:
11 | return False
12 | pre = cur
13 | return True
14 |
--------------------------------------------------------------------------------
/LeetCode/2053. Kth Distinct String in an Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 44 ms
3 | Memory Usage: 15.2 MB
4 | """
5 | class Solution:
6 | def kthDistinct(self, arr: List[str], k: int) -> str:
7 | cnt = Counter(arr)
8 | for num in arr:
9 | if cnt[num] == 1:
10 | k -= 1
11 | if k == 0:
12 | return num
13 | return ''
14 |
--------------------------------------------------------------------------------
/LeetCode/2089. Find Target Indices After Sorting Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 40 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def targetIndices(self, nums: List[int], target: int) -> List[int]:
7 | nums.sort()
8 | return [i for i, num in enumerate(nums) if num == target]
9 |
--------------------------------------------------------------------------------
/LeetCode/2114. Maximum Number of Words Found in Sentences/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 36 ms
3 | Memory Usage: 15.1 MB
4 | """
5 | class Solution:
6 | def mostWordsFound(self, sentences: List[str]) -> int:
7 | ans = 0
8 | for sentence in sentences:
9 | w = sentence.count(' ') + 1
10 | if not ans or w > ans:
11 | ans = w
12 | return ans
13 |
--------------------------------------------------------------------------------
/LeetCode/2160. Minimum Sum of Four Digit Number After Splitting Digits/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 5.8 MB
4 | */
5 | class Solution {
6 | public:
7 | int c2i(char x) {
8 | return x - '0';
9 | }
10 |
11 | int minimumSum(int num) {
12 | string s = to_string(num);
13 | sort(s.begin(), s.end());
14 | return (c2i(s[0]) + c2i(s[1])) * 10 + c2i(s[2]) + c2i(s[3]);
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/2160. Minimum Sum of Four Digit Number After Splitting Digits/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 44 ms
3 | Memory Usage: 14.8 MB
4 | """
5 | class Solution:
6 | def minimumSum(self, num: int) -> int:
7 | s = sorted(str(num))
8 | ans = 0
9 | for i in range(0, len(s), 2):
10 | ans *= 10
11 | ans += int(s[i]) + int(s[i + 1])
12 | return ans
13 |
--------------------------------------------------------------------------------
/LeetCode/2169. Count Operations to Obtain Zero/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 161 / 161 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.7 MB
5 | */
6 | class Solution {
7 | public:
8 | int countOperations(int num1, int num2) {
9 | int op = 0;
10 | while (num1 != 0 && num2 != 0) {
11 | if (num1 >= num2) num1 -= num2;
12 | else num2 -= num1;
13 | op++;
14 | }
15 | return op;
16 | }
17 | };
18 |
--------------------------------------------------------------------------------
/LeetCode/2185. Counting Words With a Given Prefix/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 8 ms
3 | * Memory Usage: 9.6 MB
4 | */
5 | class Solution {
6 | public:
7 | int prefixCount(vector& words, string pref) {
8 | return count_if(words.begin(), words.end(), [&](const string& word) { return word.find(pref) == 0; });
9 | }
10 | };
11 |
--------------------------------------------------------------------------------
/LeetCode/2185. Counting Words With a Given Prefix/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 40 ms
3 | Memory Usage: 15.1 MB
4 | """
5 | class Solution:
6 | def prefixCount(self, words: List[str], pref: str) -> int:
7 | return sum(word.startswith(pref) for word in words)
8 |
--------------------------------------------------------------------------------
/LeetCode/2220. Minimum Bit Flips to Convert Number/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 5.8 MB
4 | */
5 | class Solution {
6 | public:
7 | int minBitFlips(int start, int goal) {
8 | return __builtin_popcount(start ^ goal);
9 | }
10 | };
11 |
--------------------------------------------------------------------------------
/LeetCode/2220. Minimum Bit Flips to Convert Number/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 36 ms
3 | Memory Usage: 14.8 MB
4 | """
5 | class Solution:
6 | def minBitFlips(self, start: int, goal: int) -> int:
7 | return bin(start ^ goal).count('1')
8 |
9 |
--------------------------------------------------------------------------------
/LeetCode/2235. Add Two Integers/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * 262 / 262 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 38.4 MB
5 | */
6 | class Solution {
7 | public int sum(int num1, int num2) {
8 | return num1 + num2;
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/LeetCode/2235. Add Two Integers/Solution.kt:
--------------------------------------------------------------------------------
1 | /**
2 | * 262 / 262 test cases passed.
3 | * Runtime: 160 ms
4 | * Memory Usage: 32.2 MB
5 | */
6 | class Solution {
7 | fun sum(num1: Int, num2: Int): Int {
8 | return num1 + num2
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/LeetCode/2235. Add Two Integers/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * 262 / 262 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 5.8 MB
5 | */
6 | class Solution {
7 | public:
8 | int sum(int num1, int num2) {
9 | return num1 + num2;
10 | }
11 | };
12 |
--------------------------------------------------------------------------------
/LeetCode/2235. Add Two Integers/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * 262 / 262 test cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 1.9 MB
5 | */
6 | func sum(num1 int, num2 int) int {
7 | return num1 + num2
8 | }
9 |
--------------------------------------------------------------------------------
/LeetCode/2235. Add Two Integers/solution.js:
--------------------------------------------------------------------------------
1 | /**
2 | * 262 / 262 test cases passed.
3 | * Runtime: 64 ms
4 | * Memory Usage: 41.1 MB
5 | *
6 | * @param {number} num1
7 | * @param {number} num2
8 | * @return {number}
9 | */
10 | var sum = function(num1, num2) {
11 | return num1 + num2;
12 | };
13 |
--------------------------------------------------------------------------------
/LeetCode/2235. Add Two Integers/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 262 / 262 test cases passed.
3 | Runtime: 28 ms
4 | Memory Usage: 14.8 MB
5 | """
6 | class Solution:
7 | def sum(self, num1: int, num2: int) -> int:
8 | return num1 + num2
9 |
--------------------------------------------------------------------------------
/LeetCode/2235. Add Two Integers/solution.rs:
--------------------------------------------------------------------------------
1 | /**
2 | * 262 / 262 cases passed.
3 | * Runtime: 0 ms
4 | * Memory Usage: 2 MB
5 | */
6 | impl Solution {
7 | pub fn sum(num1: i32, num2: i32) -> i32 {
8 | num1 + num2
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/LeetCode/2236. Root Equals Sum of Children/solution.py:
--------------------------------------------------------------------------------
1 | # Definition for a binary tree node.
2 | # class TreeNode:
3 | # def __init__(self, val=0, left=None, right=None):
4 | # self.val = val
5 | # self.left = left
6 | # self.right = right
7 |
8 | """
9 | Runtime: 40 ms
10 | Memory Usage: 14.8 MB
11 | """
12 | class Solution:
13 | def checkTree(self, root: Optional[TreeNode]) -> bool:
14 | return root.val == root.right.val + root.left.val
15 |
--------------------------------------------------------------------------------
/LeetCode/2278. Percentage of Letter in String/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 6 MB
4 | */
5 | class Solution {
6 | public:
7 | int percentageLetter(string s, char letter) {
8 | int cnt = 0;
9 | for (auto& c: s) {
10 | cnt += c == letter ? 1 : 0;
11 | }
12 | return 100 * cnt / s.size();
13 | }
14 | };
15 |
--------------------------------------------------------------------------------
/LeetCode/2278. Percentage of Letter in String/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 28 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def percentageLetter(self, s: str, letter: str) -> int:
7 | return 100 * sum(1 if c == letter else 0 for c in s) // len(s)
8 |
--------------------------------------------------------------------------------
/LeetCode/2283. Check if Number Has Equal Digit Count and Digit Value/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 6.1 MB
4 | */
5 | class Solution {
6 | public:
7 | bool digitCount(string num) {
8 | unordered_map cnt;
9 | for (auto &n: num) cnt[n - '0']++;
10 | for (int i = 0; i < num.size(); ++ i) {
11 | if (cnt[i] != num[i] - '0')
12 | return false;
13 | }
14 | return true;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/2283. Check if Number Has Equal Digit Count and Digit Value/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 44 ms
3 | Memory Usage: 14.8 MB
4 | """
5 | class Solution:
6 | def digitCount(self, num: str) -> bool:
7 | cnt = Counter(num)
8 | return all(cnt[str(i)] == int(n) for i, n in enumerate(num))
9 |
10 |
--------------------------------------------------------------------------------
/LeetCode/2287. Rearrange Characters to Make Target String/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | 115 / 115 test cases passed.
3 | Runtime: 32 ms
4 | Memory Usage: 14.9 MB
5 | """
6 | from collections import Counter
7 | class Solution:
8 | def rearrangeCharacters(self, s: str, target: str) -> int:
9 | scnt = Counter(list(s))
10 | tcnt = Counter(list(target))
11 | ans = 105
12 | for t in target:
13 | ans = min(ans, scnt[t] // tcnt[t])
14 | return ans
15 |
--------------------------------------------------------------------------------
/LeetCode/2309. Greatest English Letter in Upper and Lower Case/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 7.7 MB
4 | */
5 | class Solution {
6 | public:
7 | string greatestLetter(string s) {
8 | unordered_set rec(s.begin(), s.end());
9 | for (int i = 25; i >= 0; -- i) {
10 | if (rec.count('a' + i) && rec.count('A' + i)) {
11 | return string(1, 'A' + i);
12 | }
13 | }
14 | return "";
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/2309. Greatest English Letter in Upper and Lower Case/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 28 ms
3 | Memory Usage: 15.1 MB
4 | """
5 | class Solution:
6 | def greatestLetter(self, s: str) -> str:
7 | rec = set(s)
8 | for l, u in zip(reversed(ascii_lowercase), reversed(ascii_uppercase)):
9 | if l in rec and u in rec:
10 | return u
11 | return ""
12 |
--------------------------------------------------------------------------------
/LeetCode/2315. Count Asterisks/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 1 ms
3 | * Memory Usage: 39.5 MB
4 | */
5 | class Solution {
6 | public int countAsterisks(String s) {
7 | boolean open = true;
8 | int ans = 0;
9 | for (char c: s.toCharArray()) {
10 | if (c == '|') open = !open;
11 | else if (open && c == '*') ++ ans;
12 | }
13 | return ans;
14 | }
15 | }
16 |
--------------------------------------------------------------------------------
/LeetCode/2315. Count Asterisks/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 6.3 MB
4 | */
5 | class Solution {
6 | public:
7 | int countAsterisks(string s) {
8 | bool open = true;
9 | int ans = 0;
10 | for (auto &c: s) {
11 | if (c == '|') {
12 | open = !open;
13 | }
14 | if (open && c == '*') {
15 | ++ ans;
16 | }
17 | }
18 | return ans;
19 | }
20 | };
21 |
--------------------------------------------------------------------------------
/LeetCode/2315. Count Asterisks/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 1.9 MB
4 | */
5 | func countAsterisks(s string) (ans int) {
6 | open:= true
7 | for _, c := range s {
8 | if c == '|' {
9 | open = !open
10 | } else if open && c == '*' {
11 | ans++
12 | }
13 | }
14 | return
15 | }
16 |
--------------------------------------------------------------------------------
/LeetCode/2315. Count Asterisks/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 44 ms
3 | Memory Usage: 15.1 MB
4 | """
5 | class Solution:
6 | def countAsterisks(self, s: str) -> int:
7 | open = True
8 | ans = 0
9 | for c in s:
10 | if c == '|':
11 | open = not open
12 | if open and c == '*':
13 | ans += 1
14 | return ans
15 |
--------------------------------------------------------------------------------
/LeetCode/2325. Decode the Message/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 24 ms
3 | Memory Usage: 12.3 MB
4 | """
5 | class Solution:
6 | def decodeMessage(self, key: str, message: str) -> str:
7 | mp, it = {}, iter(ascii_lowercase)
8 | for c in key:
9 | if c != ' ' and c not in mp:
10 | mp[c] = next(it)
11 | return ''.join(mp.get(c, c) for c in message)
12 |
--------------------------------------------------------------------------------
/LeetCode/2335. Minimum Amount of Time to Fill Cups/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 11.4 MB
4 | */
5 | class Solution {
6 | public:
7 | int fillCups(vector& amount) {
8 | sort(amount.begin(), amount.end());
9 | int x = amount[0], y = amount[1], z = amount[2];
10 | if (x + y <= z) return z;
11 | return (x + y + z + 1) / 2;
12 | }
13 | };
14 |
--------------------------------------------------------------------------------
/LeetCode/2335. Minimum Amount of Time to Fill Cups/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 1.8 MB
4 | */
5 | func fillCups(amount []int) int {
6 | sort.Ints(amount)
7 | x, y, z := amount[0], amount[1], amount[2]
8 | if x + y <= z { return z }
9 | return (x + y + z + 1) / 2
10 | }
--------------------------------------------------------------------------------
/LeetCode/2335. Minimum Amount of Time to Fill Cups/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 28 ms
3 | Memory Usage: 14.8 MB
4 | """
5 | class Solution:
6 | def fillCups(self, amount: List[int]) -> int:
7 | amount.sort()
8 | x, y, z = amount
9 | if x + y <= z:
10 | return z
11 | return (x + y + z + 1) // 2
12 |
--------------------------------------------------------------------------------
/LeetCode/2341. Maximum Number of Pairs in Array/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 8.9 MB
4 | */
5 | class Solution {
6 | public:
7 | vector numberOfPairs(vector& nums) {
8 | unordered_map cnt;
9 | for (auto &num: nums) {
10 | cnt[num] += 1;
11 | }
12 | int pair = 0;
13 | for (auto &[_, c]: cnt) {
14 | pair += c / 2;
15 | }
16 | return {pair, int(nums.size()) - pair * 2};
17 | }
18 | };
19 |
--------------------------------------------------------------------------------
/LeetCode/2341. Maximum Number of Pairs in Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 40 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def numberOfPairs(self, nums: List[int]) -> List[int]:
7 | pair = sum(c // 2 for c in Counter(nums).values())
8 | return [pair, len(nums) - pair * 2]
9 |
--------------------------------------------------------------------------------
/LeetCode/2347. Best Poker Hand/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 44 ms
3 | Memory Usage: 14.9 MB
4 | """
5 | class Solution:
6 | def bestHand(self, ranks: List[int], suits: List[str]) -> str:
7 | if len(set(suits)) == 1:
8 | return "Flush"
9 | cnt = Counter(ranks)
10 | if len(cnt) == 5:
11 | return "High Card"
12 | for rank, c in cnt.items():
13 | if c > 2:
14 | return "Three of a Kind"
15 | return "Pair"
16 |
--------------------------------------------------------------------------------
/LeetCode/2351. First Letter to Appear Twice/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 6 MB
4 | */
5 | class Solution {
6 | public:
7 | char repeatedCharacter(string s) {
8 | unordered_map cnt;
9 | for (auto &c: s) {
10 | cnt[c]++;
11 | if (cnt[c] == 2) {
12 | return c;
13 | }
14 | }
15 | return {};
16 | }
17 | };
--------------------------------------------------------------------------------
/LeetCode/2351. First Letter to Appear Twice/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 36 ms
3 | Memory Usage: 14.9 MB
4 | """
5 | class Solution:
6 | def repeatedCharacter(self, s: str) -> str:
7 | a = ord('a')
8 | alphabet = [0] * 26
9 | for c in s:
10 | alphabet[ord(c) - a] += 1
11 | if alphabet[ord(c) - a] == 2:
12 | return c
13 | return ''
14 |
--------------------------------------------------------------------------------
/LeetCode/2357. Make Array Zero by Subtracting Equal Amounts/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 1 ms
3 | * Memory Usage: 39 MB
4 | */
5 | class Solution {
6 | public int minimumOperations(int[] nums) {
7 | HashSet rec = new HashSet<>();
8 | for (int num: nums) rec.add(num);
9 | return rec.contains(0) ? rec.size() - 1 : rec.size();
10 | }
11 | }
12 |
--------------------------------------------------------------------------------
/LeetCode/2357. Make Array Zero by Subtracting Equal Amounts/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 8.3 MB
4 | */
5 | class Solution {
6 | public:
7 | int minimumOperations(vector& nums) {
8 | unordered_set rec;
9 | for (auto &num: nums) rec.emplace(num);
10 | return rec.size() - rec.count(0);
11 | }
12 | };
13 |
--------------------------------------------------------------------------------
/LeetCode/2357. Make Array Zero by Subtracting Equal Amounts/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 2 MB
4 | */
5 | func minimumOperations(nums []int) int {
6 | rec := make(map[int]struct{})
7 | for _, num := range nums {
8 | if num > 0 {
9 | rec[num] = struct{}{}
10 | }
11 | }
12 | return len(rec)
13 | }
14 |
--------------------------------------------------------------------------------
/LeetCode/2357. Make Array Zero by Subtracting Equal Amounts/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 40 ms
3 | Memory Usage: 14.8 MB
4 | """
5 | class Solution:
6 | def minimumOperations(self, nums: List[int]) -> int:
7 | return len(set(nums) - {0})
8 |
--------------------------------------------------------------------------------
/LeetCode/2363. Merge Similar Items/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 44 ms
3 | Memory Usage: 15.6 MB
4 | """
5 | class Solution:
6 | def mergeSimilarItems(self, items1: List[List[int]], items2: List[List[int]]) -> List[List[int]]:
7 | cnt = Counter()
8 | for v, w in items1:
9 | cnt[v] += w
10 | for v, w in items2:
11 | cnt[v] += w
12 | return sorted([v, w] for v, w in cnt.items())
13 |
--------------------------------------------------------------------------------
/LeetCode/2367. Number of Arithmetic Triplets/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 32 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def arithmeticTriplets(self, nums: List[int], diff: int) -> int:
7 | ans, rec = 0, set()
8 | for num in nums:
9 | if (num - 2 * diff) in rec and (num - diff) in rec:
10 | ans += 1
11 | rec.add(num)
12 | return ans
13 |
14 |
--------------------------------------------------------------------------------
/LeetCode/2396. Strictly Palindromic Number/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 5.7 MB
4 | */
5 | class Solution {
6 | public:
7 | bool isStrictlyPalindromic(int n) {
8 | return false;
9 | }
10 | };
11 |
--------------------------------------------------------------------------------
/LeetCode/2396. Strictly Palindromic Number/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 36 ms
3 | Memory Usage: 14.8 MB
4 | """
5 | class Solution:
6 | def isStrictlyPalindromic(self, n: int) -> bool:
7 | # the number n in base (n - 2) is always 12, which is not palindromic
8 | return False
9 |
--------------------------------------------------------------------------------
/LeetCode/2413. Smallest Even Multiple/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 5.8 MB
4 | */
5 | class Solution {
6 | public:
7 | int smallestEvenMultiple(int n) {
8 | return n & 1 ? n << 1 : n;
9 | }
10 | };
11 |
--------------------------------------------------------------------------------
/LeetCode/2413. Smallest Even Multiple/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 40 ms
3 | Memory Usage: 14.8 MB
4 | """
5 | class Solution:
6 | def smallestEvenMultiple(self, n: int) -> int:
7 | return n << 1 if n & 1 == 1 else n
8 |
--------------------------------------------------------------------------------
/LeetCode/2418. Sort the People/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 44 ms
3 | Memory Usage: 15.3 MB
4 | """
5 | class Solution:
6 | def sortPeople(self, names: List[str], heights: List[int]) -> List[str]:
7 | return [name for _, name in sorted(zip(heights, names), reverse=True)]
8 |
--------------------------------------------------------------------------------
/LeetCode/2469. Convert the Temperature/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 5.9 MB
4 | */
5 | class Solution {
6 | public:
7 | vector convertTemperature(double celsius) {
8 | return {celsius + 273.15, celsius * 1.8 + 32};
9 | }
10 | };
11 |
--------------------------------------------------------------------------------
/LeetCode/2469. Convert the Temperature/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 32 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def convertTemperature(self, celsius: float) -> List[float]:
7 | return [celsius + 273.15, celsius * 1.8 + 32]
8 |
--------------------------------------------------------------------------------
/LeetCode/2485. Find the Pivot Integer/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 5.8 MB
4 | */
5 | class Solution {
6 | public:
7 | int pivotInteger(int n) {
8 | double x = sqrt(n * (n + 1) / 2.0);
9 | return x == int(x) ? int(x) : -1;
10 | }
11 | };
12 |
--------------------------------------------------------------------------------
/LeetCode/2485. Find the Pivot Integer/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 32 ms
3 | Memory Usage: 14.7 MB
4 | """
5 | class Solution {
6 | public:
7 | int pivotInteger(int n) {
8 | double x = sqrt(n * (n + 1) / 2.0);
9 | return x == int(x) ? int(x) : -1;
10 | }
11 | };
12 |
--------------------------------------------------------------------------------
/LeetCode/2490. Circular Sentence/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 6.5 MB
4 | */
5 | class Solution {
6 | public:
7 | bool isCircularSentence(string sentence) {
8 | if (sentence.front() != sentence.back()) return false;
9 | for (int i = 0; i < sentence.size(); ++ i) {
10 | if (sentence[i] == ' ' && sentence[i - 1] != sentence[i + 1]) {
11 | return false;
12 | }
13 | }
14 | return true;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/2490. Circular Sentence/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 48 ms
3 | Memory Usage: 15.1 MB
4 | """
5 | class Solution:
6 | def isCircularSentence(self, sentence: str) -> bool:
7 | return sentence[0] == sentence[-1] and all(sentence[i - 1] == sentence[i + 1] for i, c in enumerate(sentence) if c == ' ')
8 |
--------------------------------------------------------------------------------
/LeetCode/2496. Maximum Value of a String in an Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 40 ms
3 | Memory Usage: 14.7 MB
4 | """
5 | class Solution:
6 | def maximumValue(self, strs: List[str], f=[len, int]) -> int:
7 | return max(f[s.isdigit()](s) for s in strs)
8 |
--------------------------------------------------------------------------------
/LeetCode/2500. Delete Greatest Value in Each Row/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 36 ms
3 | Memory Usage: 15.1 MB
4 | """
5 | class Solution:
6 | def deleteGreatestValue(self, grid: List[List[int]]) -> int:
7 | for row in grid:
8 | row.sort()
9 | return sum(max(col) for col in zip(*grid))
10 |
--------------------------------------------------------------------------------
/LeetCode/2520. Count the Digits That Divide a Number/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 4 ms
3 | * Memory Usage: 5.8 MB
4 | */
5 | class Solution {
6 | public:
7 | int countDigits(int num) {
8 | int ans = 0, tmp = num;
9 | while (tmp > 0) {
10 | ans += num % (tmp % 10) == 0;
11 | tmp /= 10;
12 | }
13 | return ans;
14 | }
15 | };
16 |
--------------------------------------------------------------------------------
/LeetCode/2520. Count the Digits That Divide a Number/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 36 ms
3 | Memory Usage: 14.8 MB
4 | """
5 | class Solution:
6 | def countDigits(self, num: int) -> int:
7 | return sum(num % i == 0 for i in map(int, str(num)))
8 |
--------------------------------------------------------------------------------
/LeetCode/2529. Maximum Count of Positive Integer and Negative Integer/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 8 ms
3 | * Memory Usage: 17.3 MB
4 | */
5 | class Solution {
6 | public:
7 | int maximumCount(vector& nums) {
8 | int neg = lower_bound(nums.begin(), nums.end(), 0) - nums.begin();
9 | int pos = nums.end() - upper_bound(nums.begin(), nums.end(), 0);
10 | return neg > pos ? neg : pos;
11 | }
12 | };
13 |
--------------------------------------------------------------------------------
/LeetCode/2529. Maximum Count of Positive Integer and Negative Integer/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 44 ms
3 | Memory Usage: 15.2 MB
4 | """
5 | import bisect
6 |
7 | class Solution:
8 | def maximumCount(self, nums: List[int]) -> int:
9 | neg = bisect.bisect_left(nums, 0)
10 | pos = len(nums) - bisect.bisect_right(nums, 0)
11 | return neg if neg > pos else pos
12 |
--------------------------------------------------------------------------------
/LeetCode/2535. Difference Between Element Sum and Digit Sum of an Array/Solution.java:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 2 ms
3 | * Memory Usage: 41.6 MB
4 | */
5 | class Solution {
6 | public int differenceOfSum(int[] nums) {
7 | int a = 0, b = 0;
8 | for (int num: nums) {
9 | a += num;
10 | while (num > 0) {
11 | b += num % 10;
12 | num /= 10;
13 | }
14 | }
15 | return Math.abs(a - b);
16 | }
17 | }
18 |
--------------------------------------------------------------------------------
/LeetCode/2535. Difference Between Element Sum and Digit Sum of an Array/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 8 ms
3 | * Memory Usage: 15.2 MB
4 | */
5 | class Solution {
6 | public:
7 | int differenceOfSum(vector& nums) {
8 | int a = 0, b = 0;
9 | for (auto num: nums) {
10 | a += num;
11 | while (num > 0) {
12 | b += num % 10;
13 | num /= 10;
14 | }
15 | }
16 | return abs(a - b);
17 | }
18 | };
19 |
--------------------------------------------------------------------------------
/LeetCode/2535. Difference Between Element Sum and Digit Sum of an Array/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 12 ms
3 | * Memory Usage: 5.2 MB
4 | */
5 | func differenceOfSum(nums []int) int {
6 | a, b := 0, 0
7 | for _, num := range nums {
8 | a += num
9 | for x := num; x > 0; x /= 10 {
10 | b += x % 10
11 | }
12 | }
13 | if a > b {
14 | return a - b
15 | }
16 | return b - a
17 | }
18 |
--------------------------------------------------------------------------------
/LeetCode/2535. Difference Between Element Sum and Digit Sum of an Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 56 ms
3 | Memory Usage: 15.2 MB
4 | """
5 | class Solution:
6 | def differenceOfSum(self, nums: List[int]) -> int:
7 | a = b = 0
8 | for num in nums:
9 | a += num
10 | while num > 0:
11 | b += num % 10
12 | num //= 10
13 | return abs(a - b)
14 |
--------------------------------------------------------------------------------
/LeetCode/2544. Alternating Digit Sum/solution.cpp:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 5.9 MB
4 | */
5 | class Solution {
6 | public:
7 | int alternateDigitSum(int n) {
8 | int sum = 0, sign = -1;
9 | while (n > 0) {
10 | sum += (n % 10) * sign;
11 | sign = -sign;
12 | n /= 10;
13 | }
14 | return sum * -sign;
15 | }
16 | };
17 |
--------------------------------------------------------------------------------
/LeetCode/2544. Alternating Digit Sum/solution.go:
--------------------------------------------------------------------------------
1 | /**
2 | * Runtime: 0 ms
3 | * Memory Usage: 1.8 MB
4 | */
5 | func alternateDigitSum(n int) int {
6 | total, sign := 0, 1
7 | for ; n > 0; n /= 10 {
8 | total += (n % 10) * sign
9 | sign = -sign
10 | }
11 | return total * -sign
12 | }
--------------------------------------------------------------------------------
/LeetCode/2544. Alternating Digit Sum/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 36 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def alternateDigitSum(self, n: int) -> int:
7 | total, sign = 0, 1
8 | while n:
9 | total += (n % 10) * sign
10 | sign = -sign
11 | n //= 10
12 | return total * -sign
13 |
--------------------------------------------------------------------------------
/LeetCode/2553. Separate the Digits in an Array/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 36 ms
3 | Memory Usage: 15.2 MB
4 | """
5 | class Solution:
6 | def separateDigits(self, nums: List[int]) -> List[int]:
7 | ans = []
8 | for num in nums[::-1]:
9 | while num > 0:
10 | ans.append(num % 10)
11 | num //= 10
12 | return ans[::-1]
13 |
--------------------------------------------------------------------------------
/LeetCode/2574. Left and Right Sum Differences/solution.py:
--------------------------------------------------------------------------------
1 | """
2 | Runtime: 40 ms
3 | Memory Usage: 15 MB
4 | """
5 | class Solution:
6 | def leftRigthDifference(self, nums: List[int]) -> List[int]:
7 | left_sum, right_sum = 0, sum(nums)
8 | for i, num in enumerate(nums):
9 | right_sum -= num
10 | nums[i] = abs(left_sum - right_sum)
11 | left_sum += num
12 | return nums
13 |
--------------------------------------------------------------------------------
/template/language/Cpp Template.md:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/InnoFang/algo-set/65ed98dfc84802db627868e8ad648e2b6444ded9/template/language/Cpp Template.md
--------------------------------------------------------------------------------