├── .eslintrc.json ├── .gitignore ├── Cargo.lock ├── Cargo.toml ├── Leetcode.csproj ├── Properties └── AssemblyInfo.cs ├── README.md ├── algorithms ├── 0001.Two Sum │ ├── Cargo.toml │ ├── README.md │ ├── Solution.java │ ├── index.js │ ├── solution.go │ ├── solution.rb │ ├── solution.rs │ └── test.js ├── 0002.Add Two Numbers │ ├── Cargo.toml │ ├── README.md │ ├── Solution.cs │ ├── Test.cs │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0003.Longest Substring Without Repeating Characters │ ├── README.md │ ├── Solution.cs │ ├── Test.cs │ ├── index.js │ └── test.js ├── 0004.Median of Two Sorted Arrays │ ├── README.md │ ├── index.js │ └── test.js ├── 0005.Longest Palindromic Substring │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0006.ZigZag Conversion │ ├── README.md │ ├── index.js │ └── test.js ├── 0007.Reverse Integer │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0008.String to Integer (atoi) │ ├── README.md │ ├── index.js │ └── test.js ├── 0009.Palindrome Number │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0011.Container With Most Water │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rb │ ├── solution.rs │ └── test.js ├── 0012.Integer to Roman │ ├── README.md │ ├── index.js │ └── test.js ├── 0013.Roman to Integer │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0014.Longest Common Prefix │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0015.3Sum │ ├── README.md │ ├── index.js │ └── test.js ├── 0016.3Sum Closest │ ├── README.md │ ├── index.js │ └── test.js ├── 0017.Letter Combinations of a Phone Number │ ├── README.md │ ├── index.js │ └── test.js ├── 0018.4Sum │ ├── README.md │ ├── index.js │ └── test.js ├── 0019.Remove Nth Node From End of List │ ├── README.md │ ├── index.js │ └── test.js ├── 0020.Valid Parentheses │ ├── Cargo.toml │ ├── README.md │ ├── Solution.cs │ ├── Solution.java │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0021.Merge Two Sorted Lists │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0022.Generate Parentheses │ ├── README.md │ ├── Solution.cs │ ├── Test.cs │ ├── index.js │ └── test.js ├── 0023.Merge k Sorted Lists │ ├── README.md │ ├── index.js │ └── test.js ├── 0024.Swap Nodes in Pairs │ ├── README.md │ ├── index.js │ └── test.js ├── 0026.Remove Duplicates from Sorted Array │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0027.Remove Element │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0028.Implement strStr │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0031.Next Permutation │ ├── README.md │ ├── index.js │ └── test.js ├── 0032.Longest Valid Parentheses │ ├── README.md │ ├── index.js │ └── test.js ├── 0033.Search in Rotated Sorted Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0034.Find First and Last Position of Element in Sorted Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0035.Search Insert Position │ ├── Cargo.toml │ ├── README.md │ ├── Solution.java │ ├── index.js │ ├── solution.go │ ├── solution.rs │ └── test.js ├── 0036.Valid Sudoku │ ├── README.md │ ├── Solution.cs │ ├── Test.cs │ ├── index.js │ └── test.js ├── 0038.Count and Say │ ├── README.md │ ├── index.js │ └── test.js ├── 0039.Combination Sum │ ├── README.md │ ├── index.js │ └── test.js ├── 0042.Trapping Rain Water │ ├── README.md │ ├── index.js │ └── test.js ├── 0045.Jump Game II │ ├── README.md │ ├── index.js │ └── test.js ├── 0046.Permutations │ ├── README.md │ ├── index.js │ └── test.js ├── 0049.Group Anagrams │ ├── README.md │ ├── index.js │ ├── solution.go │ ├── solution.py │ └── test.js ├── 0050.Pow(x, n) │ ├── README.md │ ├── index.js │ └── test.js ├── 0051.N-Queens │ ├── README.md │ ├── index.js │ └── test.js ├── 0053.Maximum Subarray │ ├── README.md │ ├── Solution.java │ ├── index.js │ ├── solution.go │ └── test.js ├── 0055.Jump Game │ ├── README.md │ ├── index.js │ ├── solution.go │ └── test.js ├── 0058.Length of Last Word │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0060.Permutation Sequence │ ├── README.md │ ├── index.js │ └── test.js ├── 0062.Unique Paths │ ├── README.md │ ├── index.js │ └── test.js ├── 0063.Unique Paths II │ ├── README.md │ ├── index.js │ └── test.js ├── 0064.Minimum Path Sum │ ├── README.md │ ├── index.js │ └── test.js ├── 0066.Plus One │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0067.Add Binary │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0069.Sqrt(x) │ ├── README.md │ ├── index.js │ └── test.js ├── 0070.Climbing Stairs │ ├── Cargo.toml │ ├── README.md │ ├── Solution.cs │ ├── Solution.java │ ├── Test.cs │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0071.Simplify Path │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 0072.Edit Distance │ ├── README.md │ ├── index.js │ └── test.js ├── 0075.Sort Colors │ ├── README.md │ ├── index.js │ └── test.js ├── 0076.Minimum Window Substring │ ├── README.md │ ├── index.js │ └── test.js ├── 0078.Subsets │ ├── README.md │ ├── index.js │ └── test.js ├── 0079.Word Search │ ├── README.md │ ├── index.js │ └── test.js ├── 0082.Remove Duplicates from Sorted List II │ ├── README.md │ ├── index.js │ └── test.js ├── 0083.Remove Duplicates from Sorted List │ ├── README.md │ ├── index.js │ └── test.js ├── 0088.Merge Sorted Array │ ├── Cargo.toml │ ├── README.md │ ├── Solution.java │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0089.Gray Code │ ├── README.md │ ├── index.js │ └── test.js ├── 0094.Binary Tree Inorder Traversal │ ├── README.md │ ├── index.js │ └── test.js ├── 0096.Unique Binary Search Trees │ ├── README.md │ ├── index.js │ └── test.js ├── 0097.Interleaving String │ ├── README.md │ ├── index.js │ └── test.js ├── 0098.Validate Binary Search Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0100.Same Tree │ ├── README.md │ ├── index.js │ ├── solution.go │ └── test.js ├── 0101.Symmetric Tree │ ├── README.md │ ├── Solution.cs │ ├── index.js │ └── test.js ├── 0102.Binary Tree Level Order Traversal │ ├── README.md │ ├── index.js │ └── test.js ├── 0103.Binary Tree Zigzag Level Order Traversal │ ├── README.md │ ├── index.js │ └── test.js ├── 0104.Maximum Depth of Binary Tree │ ├── README.md │ ├── Solution.cs │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0105.Construct Binary Tree from Preorder and Inorder Traversal │ ├── README.md │ ├── index.js │ └── test.js ├── 0106.Construct Binary Tree from Inorder and Postorder Traversal │ ├── README.md │ ├── index.js │ └── test.js ├── 0107.Binary Tree Level Order Traversal II │ ├── README.md │ ├── index.js │ └── test.js ├── 0108.Convert Sorted Array to Binary Search Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0109.Convert Sorted List to Binary Search Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0110.Balanced Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0111.Minimum Depth of Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0112.Path Sum │ ├── README.md │ ├── index.js │ └── test.js ├── 0113.Path Sum II │ ├── README.md │ ├── index.js │ └── test.js ├── 0114.Flatten Binary Tree to Linked List │ ├── README.md │ ├── index.js │ └── test.js ├── 0115.Distinct Subsequences │ ├── README.md │ ├── index.js │ └── test.js ├── 0116.Populating Next Right Pointers in Each Node │ ├── README.md │ └── index.js ├── 0117.Populating Next Right Pointers in Each Node II │ ├── README.md │ └── index.js ├── 0118.Pascal's Triangle │ ├── README.md │ ├── Solution.cs │ ├── index.js │ └── test.js ├── 0119.Pascal's Triangle II │ ├── README.md │ ├── Solution.cs │ ├── index.js │ └── test.js ├── 0120.Triangle │ ├── README.md │ ├── index.js │ └── test.js ├── 0121.Best Time to Buy and Sell Stock │ ├── Cargo.toml │ ├── README.md │ ├── Solution.java │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0122.Best Time to Buy and Sell Stock II │ ├── README.md │ ├── index.js │ ├── solution.go │ ├── solution.py │ └── test.js ├── 0123.Best Time to Buy and Sell Stock III │ ├── README.md │ ├── index.js │ └── test.js ├── 0124.Binary Tree Maximum Path Sum │ ├── README.md │ ├── index.js │ └── test.js ├── 0125.Valid Palindrome │ ├── README.md │ ├── index.js │ └── test.js ├── 0127.Word Ladder │ ├── README.md │ ├── index.js │ └── test.js ├── 0128.Longest Consecutive Sequence │ ├── README.md │ ├── index.js │ └── test.js ├── 0129.Sum Root to Leaf Numbers │ ├── README.md │ ├── index.js │ └── test.js ├── 0130.Surrounded Regions │ ├── README.md │ ├── index.js │ └── test.js ├── 0131.Palindrome Partitioning │ ├── README.md │ ├── index.js │ └── test.js ├── 0133.Clone Graph │ ├── README.md │ ├── index.js │ └── test.js ├── 0134.Gas Station │ ├── README.md │ ├── index.js │ └── test.js ├── 0136.Single Number │ ├── README.md │ ├── index.js │ ├── solution.go │ └── test.js ├── 0137.Single Number II │ ├── README.md │ ├── index.js │ └── test.js ├── 0138.Copy List with Random Pointer │ ├── README.md │ └── index.js ├── 0139.Word Break │ ├── README.md │ ├── index.js │ └── test.js ├── 0140.Word Break II │ ├── README.md │ ├── index.js │ └── test.js ├── 0141.Linked List Cycle │ ├── README.md │ ├── index.js │ └── test.js ├── 0142.Linked List Cycle II │ ├── README.md │ ├── index.js │ └── test.js ├── 0144.Binary Tree Preorder Traversal │ ├── README.md │ ├── index.js │ └── test.js ├── 0145.Binary Tree Postorder Traversal │ ├── README.md │ ├── index.js │ └── test.js ├── 0146.LRU Cache │ ├── README.md │ ├── index.js │ └── test.js ├── 0148.Sort List │ ├── README.md │ ├── index.js │ └── test.js ├── 0149.Max Points on a Line │ ├── README.md │ ├── index.js │ └── test.js ├── 0151.Reverse Words in a String │ ├── README.md │ ├── index.js │ └── test.js ├── 0152.Maximum Product Subarray │ ├── README.md │ ├── index.js │ └── test.js ├── 0153.Find Minimum in Rotated Sorted Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0154.Find Minimum in Rotated Sorted Array II │ ├── README.md │ ├── index.js │ └── test.js ├── 0155.Min Stack │ ├── README.md │ ├── index.js │ └── test.js ├── 0160.Intersection of Two Linked Lists │ ├── README.md │ ├── index.js │ └── test.js ├── 0162.Find Peak Element │ ├── README.md │ ├── index.js │ └── test.js ├── 0167.Two Sum II - Input array is sorted │ ├── README.md │ ├── Solution.java │ ├── index.js │ ├── solution.go │ ├── solution.py │ └── test.js ├── 0168.Excel Sheet Column Title │ ├── README.md │ ├── index.js │ ├── solution.go │ └── test.js ├── 0169.Majority Element │ ├── README.md │ ├── index.js │ └── test.js ├── 0171.Excel Sheet Column Number │ ├── README.md │ ├── index.js │ ├── solution.rb │ └── test.js ├── 0172.Factorial Trailing Zeroes │ ├── README.md │ ├── index.js │ ├── solution.rb │ └── test.js ├── 0173.Binary Search Tree Iterator │ ├── README.md │ └── index.js ├── 0174.Dungeon Game │ ├── README.md │ ├── index.js │ └── test.js ├── 0175.Combine Two Tables │ ├── README.md │ └── query.sql ├── 0176.Second Highest Salary │ ├── README.md │ └── query.sql ├── 0179.Largest Number │ ├── README.md │ ├── index.js │ └── test.js ├── 0181.Employees Earning More Than Their Managers │ ├── README.md │ └── query.sql ├── 0182.Duplicate Emails │ ├── README.md │ └── query.sql ├── 0183.Customers Who Never Order │ ├── README.md │ └── query.sql ├── 0189.Rotate Array │ ├── README.md │ ├── index.js │ ├── solution.go │ └── test.js ├── 0190.Reverse Bits │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0191.Number of 1 Bits │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0193.Valid Phone Numbers │ ├── README.md │ ├── file.txt │ └── solution.bash ├── 0195.Tenth Line │ ├── README.md │ ├── file.txt │ └── solution.bash ├── 0196.Delete Duplicate Emails │ ├── README.md │ └── query.sql ├── 0197.Rising Temperature │ ├── README.md │ └── query.sql ├── 0198.House Robber │ ├── README.md │ ├── index.js │ └── test.js ├── 0199.Binary Tree Right Side View │ ├── README.md │ ├── index.js │ └── test.js ├── 0200.Number of Islands │ ├── README.md │ ├── index.js │ └── test.js ├── 0201.Bitwise AND of Numbers Range │ ├── README.md │ ├── index.js │ └── test.js ├── 0202.Happy Number │ ├── README.md │ ├── index.js │ ├── solution.go │ └── test.js ├── 0203.Remove Linked List Elements │ ├── README.md │ ├── index.js │ └── test.js ├── 0204.Count Primes │ ├── README.md │ ├── index.js │ └── test.js ├── 0205.Isomorphic Strings │ ├── README.md │ ├── index.js │ └── test.js ├── 0206.Reverse Linked List │ ├── README.md │ ├── index.js │ ├── solution.rb │ └── test.js ├── 0207.Course Schedule │ ├── README.md │ ├── Solution.cs │ ├── Test.cs │ ├── index.js │ └── test.js ├── 0208.Implement Trie (Prefix Tree) │ ├── README.md │ └── index.js ├── 0209.Minimum Size Subarray Sum │ ├── README.md │ ├── index.js │ └── test.js ├── 0210.Course Schedule II │ ├── README.md │ ├── index.js │ └── test.js ├── 0211.Design Add and Search Words Data Structure │ ├── README.md │ └── index.js ├── 0212.Word Search II │ ├── README.md │ ├── index.js │ └── test.js ├── 0213.House Robber II │ ├── README.md │ ├── index.js │ └── test.js ├── 0215.Kth Largest Element in an Array │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0217.Contains Duplicate │ ├── README.md │ ├── index.js │ ├── solution.rb │ └── test.js ├── 0219.Contains Duplicate II │ ├── README.md │ ├── index.js │ ├── solution.rb │ └── test.js ├── 0221.Maximal Square │ ├── README.md │ ├── index.js │ └── test.js ├── 0222.Count Complete Tree Nodes │ ├── README.md │ ├── index.js │ └── test.js ├── 0225.Implement Stack using Queues │ ├── README.md │ ├── index.js │ ├── solution.rb │ └── test.js ├── 0226.Invert Binary Tree │ ├── README.md │ ├── index.js │ ├── solution.rb │ └── test.js ├── 0227.Basic Calculator II │ ├── README.md │ ├── index.js │ └── test.js ├── 0228.Summary Ranges │ ├── README.md │ ├── index.js │ └── test.js ├── 0230.Kth Smallest Element in a BST │ ├── README.md │ ├── index.js │ └── test.js ├── 0231.Power of Two │ ├── README.md │ ├── index.js │ ├── solution.rb │ └── test.js ├── 0232.Implement Queue using Stacks │ ├── README.md │ ├── index.js │ └── test.js ├── 0234.Palindrome Linked List │ ├── README.md │ ├── index.js │ └── test.js ├── 0235.Lowest Common Ancestor of a Binary Search Tree │ ├── README.md │ ├── index.js │ ├── solution.go │ └── test.js ├── 0236.Lowest Common Ancestor of a Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0237.Delete Node in a Linked List │ ├── README.md │ └── index.js ├── 0238.Product of Array Except Self │ ├── README.md │ ├── index.js │ ├── solution.go │ └── test.js ├── 0239.Sliding Window Maximum │ ├── README.md │ ├── index.js │ └── test.js ├── 0240.Search a 2D Matrix II │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 0242.Valid Anagram │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rb │ ├── solution.rs │ └── test.js ├── 0257.Binary Tree Paths │ ├── README.md │ ├── index.js │ └── test.js ├── 0258.Add Digits │ ├── README.md │ ├── index.js │ └── test.js ├── 0260.Single Number III │ ├── README.md │ ├── index.js │ └── test.js ├── 0263.Ugly Number │ ├── README.md │ ├── index.js │ └── test.js ├── 0264.Ugly Number II │ ├── README.md │ ├── index.js │ └── test.js ├── 0268.Missing Number │ ├── README.md │ ├── index.js │ └── test.js ├── 0274.H-Index │ ├── README.md │ ├── index.js │ └── test.js ├── 0275.H-Index II │ ├── README.md │ ├── index.js │ └── test.js ├── 0278.First Bad Version │ ├── README.md │ ├── index.js │ ├── solution.go │ └── test.js ├── 0279.Perfect Squares │ ├── README.md │ ├── index.js │ └── test.js ├── 0283.Move Zeroes │ ├── README.md │ ├── index.js │ ├── solution.go │ ├── solution.py │ └── test.js ├── 0284.Peeking Iterator │ ├── README.md │ └── index.js ├── 0287.Find the Duplicate Number │ ├── README.md │ ├── index.js │ └── test.js ├── 0289.Game of Life │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0290.Word Pattern │ ├── README.md │ ├── index.js │ └── test.js ├── 0292.Nim Game │ ├── README.md │ ├── index.js │ └── test.js ├── 0297.Serialize and Deserialize Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0299.Bulls and Cows │ ├── README.md │ ├── index.js │ └── test.js ├── 0300.Longest Increasing Subsequence │ ├── README.md │ ├── index.js │ └── test.js ├── 0303.Range Sum Query - Immutable │ ├── README.md │ ├── index.js │ └── test.js ├── 0314.Vertical Order Traversal of a Binary Tree │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ └── solution.rs ├── 0322.Coin Change │ ├── README.md │ ├── index.js │ └── test.js ├── 0326.Power of Three │ ├── README.md │ ├── index.js │ └── test.js ├── 0328.Odd Even Linked List │ ├── README.md │ ├── index.js │ └── test.js ├── 0329.Longest Increasing Path in a Matrix │ ├── README.md │ ├── index.js │ └── test.js ├── 0331.Verify Preorder Serialization of a Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0332.Reconstruct Itinerary │ ├── README.md │ ├── index.js │ └── test.js ├── 0337.House Robber III │ ├── README.md │ ├── index.js │ └── test.js ├── 0338.Counting Bits │ ├── README.md │ ├── index.js │ └── test.js ├── 0341.Flatten Nested List Iterator │ ├── README.md │ └── index.js ├── 0342.Power of Four │ ├── README.md │ ├── index.js │ └── test.js ├── 0344.Reverse String │ ├── README.md │ ├── index.js │ ├── index.ts │ └── test.js ├── 0345.Reverse Vowels of a String │ ├── README.md │ ├── index.js │ └── test.js ├── 0347.Top K Frequent Elements │ ├── README.md │ ├── index.js │ └── test.js ├── 0349.Intersection of Two Arrays │ ├── README.md │ ├── index.js │ └── test.js ├── 0350.Intersection of Two Arrays II │ ├── README.md │ ├── index.js │ └── test.js ├── 0354.Russian Doll Envelopes │ ├── README.md │ ├── index.js │ └── test.js ├── 0367.Valid Perfect Square │ ├── README.md │ ├── index.js │ └── test.js ├── 0368.Largest Divisible Subset │ ├── README.md │ ├── index.js │ └── test.js ├── 0371.Sum of Two Integers │ ├── README.md │ ├── index.js │ └── test.js ├── 0373.Find K Pairs with Smallest Sums │ ├── README.md │ ├── index.js │ └── test.js ├── 0374.Guess Number Higher or Lower │ ├── README.md │ └── Solution.java ├── 0376.Wiggle Subsequence │ ├── README.md │ ├── index.js │ └── test.js ├── 0378.Kth Smallest Element in a Sorted Matrix │ ├── README.md │ ├── index.js │ └── test.js ├── 0380.Insert Delete GetRandom O(1) │ ├── README.md │ └── index.js ├── 0382.Linked List Random Node │ ├── README.md │ └── index.js ├── 0383.Ransom Note │ ├── README.md │ ├── index.js │ ├── solution.go │ └── test.js ├── 0387.First Unique Character in a String │ ├── README.md │ ├── index.js │ └── test.js ├── 0389.Find the Difference │ ├── README.md │ ├── index.js │ └── test.js ├── 0392.Is Subsequence │ ├── README.md │ ├── index.js │ └── test.js ├── 0400.Nth Digit │ ├── README.md │ ├── index.js │ └── test.js ├── 0401.Binary Watch │ ├── README.md │ ├── index.js │ └── test.js ├── 0402.Remove K Digits │ ├── README.md │ ├── index.js │ └── test.js ├── 0404.Sum of Left Leaves │ ├── README.md │ ├── index.js │ └── test.js ├── 0405.Convert a Number to Hexadecimal │ ├── README.md │ ├── index.js │ └── test.js ├── 0406.Queue Reconstruction by Height │ ├── README.md │ ├── index.js │ └── test.js ├── 0409.Longest Palindrome │ ├── README.md │ ├── index.js │ └── test.js ├── 0412.Fizz Buzz │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0413.Arithmetic Slices │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 0414.Third Maximum Number │ ├── README.md │ ├── index.js │ └── test.js ├── 0415.Add Strings │ ├── README.md │ ├── index.js │ └── test.js ├── 0417.Pacific Atlantic Water Flow │ ├── README.md │ ├── index.js │ └── test.js ├── 0423.Reconstruct Original Digits from English │ ├── README.md │ ├── index.js │ └── test.js ├── 0430.Flatten a Multilevel Doubly Linked List │ ├── README.md │ ├── index.js │ └── test.js ├── 0434.Number of Segments in a String │ ├── README.md │ ├── index.js │ └── test.js ├── 0435.Non-overlapping Intervals │ ├── README.md │ ├── index.js │ └── test.js ├── 0437.Path Sum III │ ├── README.md │ ├── index.js │ └── test.js ├── 0438.Find All Anagrams in a String │ ├── README.md │ ├── index.js │ └── test.js ├── 0441.Arranging Coins │ ├── README.md │ ├── index.js │ └── test.js ├── 0442.Find All Duplicates in an Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0443.String Compression │ ├── README.md │ ├── index.js │ └── test.js ├── 0448.Find All Numbers Disappeared in an Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0450.Delete Node in a BST │ ├── README.md │ ├── index.js │ └── test.js ├── 0451.Sort Characters By Frequency │ ├── README.md │ ├── index.js │ └── test.js ├── 0452.Minimum Number of Arrows to Burst Balloons │ ├── README.md │ ├── index.js │ └── test.js ├── 0453.Minimum Moves to Equal Array Elements │ ├── README.md │ ├── index.js │ └── test.js ├── 0454.4Sum II │ ├── README.md │ ├── index.js │ └── test.js ├── 0455.Assign Cookies │ ├── README.md │ ├── index.js │ └── test.js ├── 0459.Repeated Substring Pattern │ ├── README.md │ ├── index.js │ └── test.js ├── 0461.Hamming Distance │ ├── README.md │ ├── index.js │ └── test.js ├── 0463.Island Perimeter │ ├── README.md │ ├── index.js │ └── test.js ├── 0468.Validate IP Address │ ├── README.md │ ├── index.js │ └── test.js ├── 0474.Ones and Zeroes │ ├── README.md │ ├── index.js │ └── test.js ├── 0476.Number Complement │ ├── README.md │ ├── index.js │ ├── solution.go │ └── test.js ├── 0479.Largest Palindrome Product │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0482.License Key Formatting │ ├── README.md │ ├── index.js │ └── test.js ├── 0485.Max Consecutive Ones │ ├── README.md │ ├── index.js │ └── test.js ├── 0486.Predict the Winner │ ├── README.md │ ├── index.js │ └── test.js ├── 0487.Max Consecutive Ones II │ ├── README.md │ ├── index.js │ └── test.js ├── 0488.Zuma Game │ ├── README.md │ ├── index.js │ └── test.js ├── 0492.Construct the rectangle │ ├── README.md │ ├── index.js │ └── test.js ├── 0500.Keyboard Row │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0501.Find Mode in Binary Search Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0504.Base 7 │ ├── README.md │ ├── index.js │ └── test.js ├── 0507.Perfect Number │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0509.Fibonacci Number │ ├── README.md │ ├── index.js │ └── test.js ├── 0513.Find Bottom Left Tree Value │ ├── README.md │ ├── index.js │ └── test.js ├── 0518.Coin Change 2 │ ├── README.md │ ├── index.js │ └── test.js ├── 0520.Detect Capital │ ├── README.md │ ├── index.js │ └── test.js ├── 0524.Longest Word in Dictionary through Deleting │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 0525.Contiguous Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0528.Random Pick with Weight │ ├── README.md │ └── index.js ├── 0530.Minimum Absolute Difference in BST │ ├── README.md │ ├── index.js │ └── test.js ├── 0532.K-diff Pairs in an Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0535.Encode and Decode TinyURL │ ├── README.md │ ├── index.js │ └── test.js ├── 0538.Convert BST to Greater Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0540.Single Element in a Sorted Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0543.Diameter of Binary Tree │ ├── README.md │ ├── index.js │ ├── solution.py │ └── test.js ├── 0557.Reverse Words in a String III │ ├── README.md │ ├── index.js │ └── test.js ├── 0559.Maximum Depth of N-ary Tree │ ├── README.md │ └── index.js ├── 0560.Subarray Sum Equals K │ ├── README.md │ ├── index.js │ └── test.js ├── 0563.Binary Tree Tilt │ ├── README.md │ ├── index.js │ └── test.js ├── 0566.Reshape the Matrix │ ├── README.md │ ├── Solution.java │ ├── index.js │ └── test.js ├── 0567.Permutation in String │ ├── README.md │ ├── index.js │ └── test.js ├── 0572.Subtree of Another Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0575.Distribute Candies │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 0581.Shortest Unsorted Continuous Subarray │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0590.N-ary Tree Postorder Traversal │ ├── README.md │ └── index.js ├── 0594.Longest Harmonious Subsequence │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0599.Minimum Index Sum of Two Lists │ ├── README.md │ ├── index.js │ ├── solution.go │ └── test.js ├── 0605.Can Place Flowers │ ├── README.md │ ├── index.js │ └── test.js ├── 0606.Construct String from Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0617.Merge Two Binary Trees │ ├── README.md │ ├── index.js │ └── test.js ├── 0621.Task Scheduler │ ├── README.md │ ├── index.js │ └── test.js ├── 0623.Add One Row to Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0627.Swap Salary │ ├── README.md │ └── query.sql ├── 0628.Maximum Product of Three Numbers │ ├── README.md │ ├── index.js │ └── test.js ├── 0633.Sum of Square Numbers │ ├── README.md │ ├── index.js │ └── test.js ├── 0637.Average of Levels in Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0643.Maximum Average Subarray I │ ├── README.md │ ├── index.js │ └── test.js ├── 0645.Set Mismatch │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0646.Maximum Length of Pair Chain │ ├── README.md │ ├── index.js │ └── test.js ├── 0649.Dota2 Senate │ ├── README.md │ ├── index.js │ └── test.js ├── 0650.2 Keys Keyboard │ ├── README.md │ ├── index.js │ └── test.js ├── 0651.4 Keys Keyboard │ ├── README.md │ ├── index.js │ └── test.js ├── 0652.Find Duplicate Subtrees │ ├── README.md │ ├── index.js │ └── test.js ├── 0653.Two Sum IV - Input is a BST │ ├── README.md │ ├── index.js │ └── test.js ├── 0654.Maximum Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0655.Print Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0656.Coin Path │ ├── README.md │ ├── index.js │ └── test.js ├── 0657.Judge Route Circle │ ├── README.md │ ├── index.js │ ├── solution.rb │ └── test.js ├── 0658.Find K Closest Elements │ ├── README.md │ ├── index.js │ ├── solution.rb │ └── test.js ├── 0659.Split Array into Consecutive Subsequences │ ├── README.md │ ├── index.js │ └── test.js ├── 0660.Remove 9 │ ├── README.md │ ├── index.js │ └── test.js ├── 0661.Image Smoother │ ├── README.md │ ├── index.js │ └── test.js ├── 0662.Maximum Width of Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0663.Equal Tree Partition │ ├── README.md │ ├── index.js │ └── test.js ├── 0665.Non-decreasing Array │ ├── README.md │ ├── index.js │ ├── solution.rb │ └── test.js ├── 0669.Trim a Binary Search Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0674.Longest Continuous Increasing Subsequence │ ├── README.md │ ├── index.js │ └── test.js ├── 0677.Map Sum Pairs │ ├── README.md │ ├── index.js │ └── test.js ├── 0678.Valid Parenthesis String │ ├── README.md │ ├── index.js │ └── test.js ├── 0679.24 Game │ ├── README.md │ ├── index.js │ └── test.js ├── 0680.Valid Palindrome II │ ├── README.md │ ├── index.js │ └── test.js ├── 0681.Next Closest Time │ ├── README.md │ ├── index.js │ └── test.js ├── 0682.Baseball Game │ ├── README.md │ ├── index.js │ └── test.js ├── 0686.Repeated String Match │ ├── README.md │ ├── index.js │ └── test.js ├── 0687.Longest Univalue Path │ ├── README.md │ ├── index.js │ └── test.js ├── 0693.Binary Number with Alternating Bits │ ├── README.md │ ├── index.js │ └── test.js ├── 0695.Max Area of Island │ ├── README.md │ ├── index.js │ └── test.js ├── 0700.Search in a Binary Search Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0701.Insert into a Binary Search Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0703.Kth Largest Element in a Stream │ ├── README.md │ ├── index.js │ └── test.js ├── 0704.Binary Search │ ├── README.md │ ├── index.js │ └── test.js ├── 0705.Design HashSet │ ├── README.md │ ├── index.js │ └── test.js ├── 0706.Design HashMap │ ├── README.md │ └── index.js ├── 0707.Design Linked List │ ├── README.md │ ├── index.js │ └── test.js ├── 0714.Best Time to Buy and Sell Stock with Transaction Fee │ ├── README.md │ ├── index.js │ └── test.js ├── 0720.Longest Word in Dictionary │ ├── README.md │ ├── index.js │ └── test.js ├── 0721.Accounts Merge │ ├── README.md │ ├── index.js │ └── test.js ├── 0729.My Calendar I │ ├── README.md │ ├── index.js │ └── test.js ├── 0731.My Calendar II │ ├── README.md │ ├── index.js │ └── test.js ├── 0733.Flood Fill │ ├── README.md │ ├── index.js │ └── test.js ├── 0744.Find Smallest Letter Greater Than Target │ ├── README.md │ ├── index.js │ └── test.js ├── 0746.Min Cost Climbing Stairs │ ├── README.md │ ├── index.js │ └── test.js ├── 0748.Shortest Completing Word │ ├── README.md │ ├── index.js │ └── test.js ├── 0750.Number Of Corner Rectangles │ ├── README.md │ ├── index.js │ └── test.js ├── 0758.Bold Words in String │ ├── README.md │ ├── index.js │ └── test.js ├── 0759.Employee Free Time │ ├── README.md │ ├── index.js │ └── test.js ├── 0760.Find Anagram Mappings │ ├── README.md │ ├── index.js │ └── test.js ├── 0763.Partition Labels │ ├── README.md │ ├── index.js │ └── test.js ├── 0766.Toeplitz Matrix │ ├── README.md │ ├── index.js │ └── test.js ├── 0771.Jewels and Stones │ ├── README.md │ ├── index.js │ └── test.js ├── 0779.K-th Symbol in Grammar │ ├── README.md │ ├── index.js │ └── test.js ├── 0783.Minimum Distance Between BST Nodes │ ├── README.md │ ├── index.js │ └── test.js ├── 0784.Letter Case Permutation │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0785.Is Graph Bipartite │ └── README.md ├── 0785.Is Graph Bipartite? │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0787.Cheapest Flights Within K Stops │ ├── README.md │ ├── index.js │ └── test.js ├── 0788.Rotated Digits │ ├── README.md │ ├── index.js │ └── test.js ├── 0789.Escape The Ghosts │ ├── README.md │ ├── index.js │ └── test.js ├── 0791.Custom Sort String │ ├── README.md │ ├── index.js │ └── test.js ├── 0796.Rotate String │ ├── README.md │ ├── index.js │ └── test.js ├── 0797.All Paths From Source to Target │ ├── README.md │ ├── index.js │ └── test.js ├── 0799.Champagne Tower │ ├── README.md │ ├── index.js │ └── test.js ├── 0800.Similar RGB Color │ ├── README.md │ ├── index.js │ └── test.js ├── 0801.Minimum Swaps To Make Sequences Increasing │ ├── README.md │ ├── index.js │ └── test.js ├── 0802.Find Eventual Safe States │ ├── README.md │ ├── index.js │ └── test.js ├── 0804.Unique Morse Code Words │ ├── README.md │ ├── index.js │ └── test.js ├── 0805.Split Array With Same Average │ ├── README.md │ ├── index.js │ └── test.js ├── 0806.Number of Lines To Write String │ ├── README.md │ ├── index.js │ └── test.js ├── 0807.Max Increase to Keep City Skyline │ ├── README.md │ ├── index.js │ └── test.js ├── 0811.Subdomain Visit Count │ ├── README.md │ ├── index.js │ └── test.js ├── 0814.Binary Tree Pruning │ ├── README.md │ ├── index.js │ └── test.js ├── 0816.Ambiguous Coordinates │ ├── README.md │ ├── index.js │ └── test.js ├── 0817.Linked List Components │ ├── README.md │ ├── index.js │ └── test.js ├── 0819.Most Common Word │ ├── README.md │ ├── index.js │ └── test.js ├── 0820.Short Encoding of Words │ ├── README.md │ ├── index.js │ └── test.js ├── 0821.Shortest Distance to a Character │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 0822.Card Flipping Game │ ├── README.md │ ├── index.js │ └── test.js ├── 0823.Binary Trees With Factors │ ├── README.md │ ├── index.js │ └── test.js ├── 0824.Goat Latin │ ├── README.md │ ├── index.js │ └── test.js ├── 0825.Friends Of Appropriate Ages │ ├── README.md │ ├── index.js │ └── test.js ├── 0826.Most Profit Assigning Work │ ├── README.md │ ├── index.js │ └── test.js ├── 0828.Unique Letter String │ ├── README.md │ ├── index.js │ └── test.js ├── 0829.Consecutive Numbers Sum │ ├── README.md │ ├── index.js │ └── test.js ├── 0830.Positions of Large Groups │ ├── README.md │ ├── index.js │ └── test.js ├── 0831.Masking Personal Information │ ├── README.md │ ├── index.js │ └── test.js ├── 0832.Flipping an Image │ ├── README.md │ ├── index.js │ └── test.js ├── 0835.Image Overlap │ ├── README.md │ ├── index.js │ └── test.js ├── 0836.Rectangle Overlap │ ├── README.md │ ├── index.js │ └── test.js ├── 0840.Magic Squares In Grid │ ├── README.md │ ├── index.js │ └── test.js ├── 0841.Keys and Rooms │ ├── README.md │ ├── index.js │ └── test.js ├── 0844.Backspace String Compare │ ├── README.md │ ├── index.js │ ├── solution.go │ ├── solution.py │ └── test.js ├── 0845.Longest Mountain in Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0846.Hand of Straights │ ├── README.md │ ├── index.js │ └── test.js ├── 0848.Shifting Letters │ ├── README.md │ ├── index.js │ └── test.js ├── 0849.Maximize Distance to Closest Person │ ├── README.md │ ├── index.js │ └── test.js ├── 0852.Peak Index in a Mountain Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0856.Score of Parentheses │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 0859.Buddy Strings │ ├── README.md │ ├── index.js │ └── test.js ├── 0860.Lemonade Change │ ├── README.md │ ├── index.js │ └── test.js ├── 0863.All Nodes Distance K in Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0870.Advantage Shuffle │ ├── README.md │ ├── index.js │ └── test.js ├── 0872.Leaf-Similar Trees │ ├── README.md │ ├── index.js │ └── test.js ├── 0875.Koko Eating Bananas │ ├── README.md │ ├── index.js │ └── test.js ├── 0876.Middle of the Linked List │ ├── README.md │ ├── index.js │ ├── solution.cpp │ ├── solution.go │ ├── solution.py │ └── test.js ├── 0881.Boats to Save People │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 0884.Uncommon Words from Two Sentences │ ├── README.md │ ├── index.js │ └── test.js ├── 0886.Possible Bipartition │ ├── README.md │ ├── index.js │ └── test.js ├── 0889.Construct Binary Tree from Preorder and Postorder Traversal │ ├── README.md │ ├── index.js │ └── test.js ├── 0890.Find and Replace Pattern │ ├── README.md │ ├── index.js │ └── test.js ├── 0895.Maximum Frequency Stack │ ├── README.md │ └── index.js ├── 0896.Monotonic Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0897.Increasing Order Search Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0901.Online Stock Span │ ├── README.md │ ├── index.js │ └── test.js ├── 0905.Sort Array By Parity │ ├── README.md │ ├── index.js │ └── test.js ├── 0916.Word Subsets │ ├── README.md │ ├── index.js │ └── test.js ├── 0918.Maximum Sum Circular Subarray │ ├── README.md │ ├── index.js │ └── test.js ├── 0919.Complete Binary Tree Inserter │ ├── README.md │ └── index.js ├── 0929.Unique Email Addresses │ ├── README.md │ ├── index.js │ └── test.js ├── 0933.Number of Recent Calls │ ├── README.md │ ├── index.js │ └── test.js ├── 0938.Range Sum of BST │ ├── README.md │ ├── index.js │ └── test.js ├── 0941.Valid Mountain Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0942.DI String Match │ ├── README.md │ ├── index.js │ └── test.js ├── 0943.Find the Shortest Superstring │ ├── README.md │ ├── index.js │ └── test.js ├── 0944.Delete Columns to Make Sorted │ ├── README.md │ ├── index.js │ ├── solution.go │ ├── solution.py │ └── test.js ├── 0946.Validate Stack Sequences │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 0949.Largest Time for Given Digits │ ├── README.md │ ├── index.js │ └── test.js ├── 0950.Reveal Cards In Increasing Order │ ├── README.md │ ├── index.js │ └── test.js ├── 0951.Flip Equivalent Binary Trees │ ├── README.md │ ├── index.js │ └── test.js ├── 0953.Verifying an Alien Dictionary │ ├── README.md │ ├── index.js │ └── test.js ├── 0957.Prison Cells After N Days │ ├── README.md │ ├── index.js │ └── test.js ├── 0958.Check Completeness of a Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0961.N-Repeated Element in Size 2N Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0965.Univalued Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0967.Numbers With Same Consecutive Differences │ ├── README.md │ ├── index.js │ └── test.js ├── 0970.Powerful Integers │ ├── README.md │ ├── index.js │ └── test.js ├── 0973.K Closest Points to Origin │ ├── README.md │ ├── index.js │ └── test.js ├── 0977.Squares of a Sorted Array │ ├── README.md │ ├── index.js │ └── test.js ├── 0983.Minimum Cost For Tickets │ ├── README.md │ ├── index.js │ └── test.js ├── 0986.Interval List Intersections │ ├── README.md │ ├── index.js │ └── test.js ├── 0987.Vertical Order Traversal of a Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0991.Broken Calculator │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 0993.Cousins in Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 0994.Rotting Oranges │ ├── README.md │ ├── index.js │ └── test.js ├── 0997.Find the Town Judge │ ├── README.md │ ├── index.js │ └── test.js ├── 0999.Available Captures for Rook │ ├── README.md │ ├── index.js │ └── test.js ├── 1005.Maximize Sum Of Array After K Negations │ ├── README.md │ ├── index.js │ └── test.js ├── 1008.Construct Binary Search Tree from Preorder Traversal │ ├── README.md │ ├── index.js │ └── test.js ├── 1009.Complement of Base 10 Integer │ ├── README.md │ ├── index.js │ └── test.js ├── 1010.Pairs of Songs With Total Durations Divisible by 60 │ ├── README.md │ ├── index.js │ └── test.js ├── 1021.Remove Outermost Parentheses │ ├── README.md │ ├── index.js │ └── test.js ├── 1022.Sum of Root To Leaf Binary Numbers │ ├── README.md │ ├── index.js │ └── test.js ├── 1026.Maximum Difference Between Node and Ancestor │ ├── README.md │ ├── index.js │ └── test.js ├── 1029.Two City Scheduling │ ├── README.md │ ├── index.js │ └── test.js ├── 1030.Matrix Cells in Distance Order │ ├── README.md │ ├── index.js │ └── test.js ├── 1032.Stream of Characters │ ├── README.md │ ├── index.js │ └── test.js ├── 1035.Uncrossed Lines │ ├── README.md │ ├── index.js │ └── test.js ├── 1037.Valid Boomerang │ ├── README.md │ ├── index.js │ └── test.js ├── 1038.Binary Search Tree to Greater Sum Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 1041.Robot Bounded In Circle │ ├── README.md │ ├── index.js │ └── test.js ├── 1042.Flower Planting With No Adjacent │ ├── README.md │ ├── index.js │ └── test.js ├── 1044.Longest Duplicate Substring │ ├── README.md │ ├── index.js │ └── test.js ├── 1046.Last Stone Weight │ ├── README.md │ ├── index.js │ ├── solution.py │ └── test.js ├── 1051.Height Checker │ ├── README.md │ ├── index.js │ └── test.js ├── 1052.Grumpy Bookstore Owner │ ├── README.md │ ├── index.js │ └── test.js ├── 1053.Previous Permutation With One Swap │ ├── README.md │ ├── index.js │ └── test.js ├── 1054.Distant Barcodes │ ├── README.md │ ├── index.js │ └── test.js ├── 1071.Greatest Common Divisor of Strings │ ├── README.md │ ├── index.js │ └── test.js ├── 1072.Flip Columns For Maximum Number of Equal Rows │ ├── README.md │ ├── index.js │ └── test.js ├── 1078.Occurrences After Bigram │ ├── README.md │ ├── index.js │ └── test.js ├── 1079.Letter Tile Possibilities │ ├── README.md │ ├── index.js │ └── test.js ├── 1080.Insufficient Nodes in Root to Leaf Paths │ ├── README.md │ ├── index.js │ └── test.js ├── 1081.Smallest Subsequence of Distinct Characters │ ├── README.md │ ├── index.js │ └── test.js ├── 1089.Duplicate Zeros │ ├── README.md │ ├── index.js │ └── test.js ├── 1090.Largest Values From Labels │ ├── README.md │ ├── index.js │ └── test.js ├── 1091.Shortest Path in Binary Matrix │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 1093.Statistics from a Large Sample │ ├── README.md │ ├── index.js │ └── test.js ├── 1094.Car Pooling │ ├── README.md │ ├── index.js │ └── test.js ├── 1095.Find in Mountain Array │ ├── README.md │ ├── index.js │ └── test.js ├── 1103.Distribute Candies to People │ ├── README.md │ ├── index.js │ └── test.js ├── 1105.Filling Bookcase Shelves │ ├── README.md │ ├── index.js │ └── test.js ├── 1108.Defanging an IP Address │ ├── README.md │ ├── index.js │ └── test.js ├── 1109.Corporate Flight Bookings │ ├── README.md │ ├── index.js │ └── test.js ├── 1110.Delete Nodes And Return Forest │ ├── README.md │ ├── index.js │ └── test.js ├── 1111.Maximum Nesting Depth of Two Valid Parentheses Strings │ ├── README.md │ ├── index.js │ └── test.js ├── 1122.Relative Sort Array │ ├── README.md │ ├── index.js │ └── test.js ├── 1123.Lowest Common Ancestor of Deepest Leaves │ ├── README.md │ ├── index.js │ └── test.js ├── 1137.N-th Tribonacci Number │ ├── README.md │ ├── index.js │ └── test.js ├── 1143.Longest Common Subsequence │ ├── README.md │ ├── index.js │ └── test.js ├── 1154.Day of the Year │ ├── README.md │ ├── index.js │ └── test.js ├── 1155.Number of Dice Rolls With Target Sum │ ├── README.md │ ├── index.js │ └── test.js ├── 1156.Swap For Longest Repeated Character Substring │ ├── README.md │ ├── index.js │ └── test.js ├── 1160.Find Words That Can Be Formed by Characters │ ├── README.md │ ├── index.js │ └── test.js ├── 1161.Maximum Level Sum of a Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 1162.As Far from Land as Possible │ ├── README.md │ ├── index.js │ └── test.js ├── 1163.Last Substring in Lexicographical Order │ ├── README.md │ ├── index.js │ └── test.js ├── 1171.Remove Zero Sum Consecutive Nodes from Linked List │ ├── README.md │ ├── index.js │ └── test.js ├── 1175.Prime Arrangements │ ├── README.md │ ├── index.js │ └── test.js ├── 1184.Distance Between Bus Stops │ ├── README.md │ ├── index.js │ └── test.js ├── 1185.Day of the Week │ ├── README.md │ ├── index.js │ └── test.js ├── 1186.Maximum Subarray Sum with One Deletion │ ├── README.md │ ├── index.js │ └── test.js ├── 1190.Reverse Substrings Between Each Pair of Parentheses │ ├── README.md │ ├── index.js │ └── test.js ├── 1200.Minimum Absolute Difference │ ├── README.md │ ├── index.js │ └── test.js ├── 1201.Ugly Number III │ ├── README.md │ ├── index.js │ └── test.js ├── 1202.Smallest String With Swaps │ ├── README.md │ ├── index.js │ └── test.js ├── 1208.Get Equal Substrings Within Budget │ ├── README.md │ ├── index.js │ └── test.js ├── 1209.Remove All Adjacent Duplicates in String II │ ├── README.md │ ├── index.js │ └── test.js ├── 1217.Play with Chips │ ├── README.md │ ├── index.js │ └── test.js ├── 1218.Longest Arithmetic Subsequence of Given Difference │ ├── README.md │ ├── index.js │ └── test.js ├── 1219.Path with Maximum Gold │ ├── README.md │ ├── index.js │ └── test.js ├── 1220.Count Vowels Permutation │ ├── README.md │ ├── index.js │ └── test.js ├── 1221.Split a String in Balanced Strings │ ├── README.md │ ├── index.js │ └── test.js ├── 1222.Queens That Can Attack the King │ ├── README.md │ ├── index.js │ └── test.js ├── 1232.Check If It Is a Straight Line │ ├── README.md │ ├── index.js │ └── test.js ├── 1233.Remove Sub-Folders from the Filesystem │ ├── README.md │ ├── index.js │ └── test.js ├── 1237.Find Positive Integer Solution for a Given Equation │ ├── README.md │ └── index.js ├── 1238.Circular Permutation in Binary Representation │ ├── README.md │ ├── index.js │ └── test.js ├── 1239.Maximum Length of a Concatenated String with Unique Characters │ ├── README.md │ ├── index.js │ └── test.js ├── 1247.Minimum Swaps to Make Strings Equal │ ├── README.md │ ├── index.js │ └── test.js ├── 1248.Count Number of Nice Subarrays │ ├── README.md │ ├── index.js │ └── test.js ├── 1249.Minimum Remove to Make Valid Parentheses │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 1252.Cells with Odd Values in a Matrix │ ├── README.md │ ├── index.js │ └── test.js ├── 1253.Reconstruct a 2-Row Binary Matrix │ ├── README.md │ ├── index.js │ └── test.js ├── 1254.Number of Closed Islands │ ├── README.md │ ├── index.js │ └── test.js ├── 1255.Maximum Score Words Formed by Letters │ ├── README.md │ ├── index.js │ └── test.js ├── 1260.Shift 2D Grid │ ├── README.md │ ├── index.js │ └── test.js ├── 1261.Find Elements in a Contaminated Binary Tree │ ├── README.md │ └── index.js ├── 1262.Greatest Sum Divisible by Three │ ├── README.md │ ├── index.js │ └── test.js ├── 1263.Minimum Moves to Move a Box to Their Target Location │ ├── README.md │ ├── index.js │ └── test.js ├── 1266.Minimum Time Visiting All Points │ ├── README.md │ ├── index.js │ └── test.js ├── 1267.Count Servers that Communicate │ ├── README.md │ ├── index.js │ └── test.js ├── 1268.Search Suggestions System │ ├── README.md │ ├── index.js │ └── test.js ├── 1269.Number of Ways to Stay in the Same Place After Some Steps │ ├── README.md │ ├── index.js │ └── test.js ├── 1275.Find Winner on a Tic Tac Toe Game │ ├── README.md │ ├── index.js │ └── test.js ├── 1276.Number of Burgers with No Waste of Ingredients │ ├── README.md │ ├── index.js │ └── test.js ├── 1277.Count Square Submatrices with All Ones │ ├── README.md │ ├── index.js │ └── test.js ├── 1281.Subtract the Product and Sum of Digits of an Integer │ ├── README.md │ ├── index.js │ └── test.js ├── 1282.Group the People Given the Group Size They Belong To │ ├── README.md │ ├── index.js │ └── test.js ├── 1284.Minimum Number of Flips to Convert Binary Matrix to Zero Matrix │ ├── README.md │ ├── index.js │ └── test.js ├── 1286.Iterator for Combination │ ├── README.md │ ├── index.js │ └── test.js ├── 1287.Element Appearing More Than 25% In Sorted Array │ ├── README.md │ ├── index.js │ └── test.js ├── 1289.Minimum Falling Path Sum II │ ├── README.md │ ├── index.js │ └── test.js ├── 1290.Convert Binary Number in a Linked List to Integer │ ├── README.md │ ├── index.js │ └── test.js ├── 1291.Sequential Digits │ ├── README.md │ ├── index.js │ └── test.js ├── 1292.Maximum Side Length of a Square with Sum Less than or Equal to Threshold │ ├── README.md │ ├── index.js │ └── test.js ├── 1293.Shortest Path in a Grid with Obstacles Elimination │ ├── README.md │ ├── index.js │ └── test.js ├── 1304.Find N Unique Integers Sum up to Zero │ ├── README.md │ ├── index.js │ └── test.js ├── 1305.All Elements in Two Binary Search Trees │ ├── README.md │ ├── index.js │ └── test.js ├── 1306.Jump Game III │ ├── README.md │ ├── index.js │ └── test.js ├── 1315.Sum of Nodes with Even-Valued Grandparent │ ├── README.md │ ├── index.js │ └── test.js ├── 1317.Convert Integer to the Sum of Two No-Zero Integers │ ├── README.md │ ├── index.js │ └── test.js ├── 1318.Minimum Flips to Make a OR b Equal to c │ ├── README.md │ ├── index.js │ └── test.js ├── 1319.Number of Operations to Make Network Connected │ ├── README.md │ ├── index.js │ └── test.js ├── 1323.Maximum 69 Number │ ├── README.md │ ├── index.js │ └── test.js ├── 1324.Print Words Vertically │ ├── README.md │ ├── index.js │ └── test.js ├── 1325.Delete Leaves With a Given Value │ ├── README.md │ ├── index.js │ └── test.js ├── 1326.Minimum Number of Taps to Open to Water a Garden │ ├── README.md │ ├── index.js │ └── test.js ├── 1328.Break a Palindrome │ ├── README.md │ ├── index.js │ └── test.js ├── 1329.Sort the Matrix Diagonally │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 1331.Rank Transform of an Array │ ├── README.md │ ├── index.js │ └── test.js ├── 1332.Remove Palindromic Subsequences │ ├── README.md │ ├── index.js │ └── test.js ├── 1333.Filter Restaurants by Vegan-Friendly, Price and Distance │ ├── README.md │ ├── index.js │ └── test.js ├── 1334.Find the City With the Smallest Number of Neighbors at a Threshold Distance │ ├── README.md │ ├── index.js │ └── test.js ├── 1337.The K Weakest Rows in a Matrix │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 1338.Reduce Array Size to The Half │ ├── README.md │ ├── index.js │ └── test.js ├── 1339.Maximum Product of Splitted Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 1340.Jump Game V │ ├── README.md │ ├── index.js │ └── test.js ├── 1342.Number of Steps to Reduce a Number to Zero │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 1344.Angle Between Hands of a Clock │ ├── README.md │ ├── index.js │ └── test.js ├── 1345.Jump Game IV │ ├── README.md │ ├── index.js │ └── test.js ├── 1346.Check If N and Its Double Exist │ ├── README.md │ ├── index.js │ └── test.js ├── 1347.Minimum Number of Steps to Make Two Strings Anagram │ ├── README.md │ ├── index.js │ └── test.js ├── 1348.Tweet Counts Per Frequency │ ├── README.md │ ├── index.js │ └── test.js ├── 1351.Count Negative Numbers in a Sorted Matrix │ ├── README.md │ ├── index.js │ └── test.js ├── 1352.Product of the Last K Numbers │ ├── README.md │ ├── index.js │ └── test.js ├── 1360.Number of Days Between Two Dates │ ├── README.md │ ├── index.js │ └── test.js ├── 1361.Validate Binary Tree Nodes │ ├── README.md │ ├── index.js │ └── test.js ├── 1362.Closest Divisors │ ├── README.md │ ├── index.js │ └── test.js ├── 1363.Largest Multiple of Three │ ├── README.md │ ├── index.js │ └── test.js ├── 1365.How Many Numbers Are Smaller Than the Current Number │ ├── README.md │ ├── index.js │ └── test.js ├── 1366.Rank Teams by Votes │ ├── README.md │ ├── index.js │ └── test.js ├── 1367.Linked List in Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 1368.Minimum Cost to Make at Least One Valid Path in a Grid │ ├── README.md │ ├── index.js │ └── test.js ├── 1370.Increasing Decreasing String │ ├── README.md │ ├── index.js │ └── test.js ├── 1379.Find a Corresponding Node of a Binary Tree in a Clone of That Tree │ ├── README.md │ └── index.js ├── 1380.Lucky Numbers in a Matrix │ ├── README.md │ ├── index.js │ └── test.js ├── 1381.Design a Stack With Increment Operation │ ├── README.md │ └── index.js ├── 1383.Maximum Performance of a Team │ ├── README.md │ ├── index.js │ └── test.js ├── 1385.Find the Distance Value Between Two Arrays │ ├── README.md │ ├── index.js │ └── test.js ├── 1386.Cinema Seat Allocation │ ├── README.md │ ├── index.js │ └── test.js ├── 1387.Sort Integers by The Power Value │ ├── README.md │ ├── index.js │ └── test.js ├── 1389.Create Target Array in the Given Order │ ├── README.md │ ├── index.js │ └── test.js ├── 1390.Four Divisors │ ├── README.md │ ├── index.js │ └── test.js ├── 1391.Check if There is a Valid Path in a Grid │ ├── README.md │ ├── index.js │ └── test.js ├── 1392.Longest Happy Prefix │ ├── README.md │ ├── index.js │ └── test.js ├── 1394.Find Lucky Integer in an Array │ ├── README.md │ ├── index.js │ └── test.js ├── 1395.Count Number of Teams │ ├── README.md │ ├── index.js │ └── test.js ├── 1396.Design Underground System │ ├── README.md │ └── index.js ├── 1413.Minimum Value to Get Positive Step by Step Sum │ ├── README.md │ ├── index.js │ └── test.js ├── 1414.Find the Minimum Number of Fibonacci Numbers Whose Sum Is K │ ├── README.md │ ├── index.js │ └── test.js ├── 1417.Reformat The String │ ├── README.md │ ├── index.js │ └── test.js ├── 1426.Counting Elements │ ├── README.md │ ├── index.js │ ├── solution.go │ └── solution.py ├── 1427.Perform String Shifts │ ├── README.md │ ├── index.js │ └── test.js ├── 1428.Leftmost Column with at Least a One │ ├── README.md │ ├── index.js │ └── test.js ├── 1429.First Unique Number │ ├── README.md │ └── index.js ├── 1430.Check If a String Is a Valid Sequence from Root to Leaves Path in a Binary Tree │ ├── README.md │ └── index.js ├── 1437.Check If All 1's Are at Least Length K Places Away │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 1448.Count Good Nodes in Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 1455.Check If a Word Occurs As a Prefix of Any Word in a Sentence │ ├── README.md │ ├── index.js │ └── test.js ├── 1456.Maximum Number of Vowels in a Substring of Given Length │ ├── README.md │ ├── index.js │ └── test.js ├── 1457.Pseudo-Palindromic Paths in a Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 1458.Max Dot Product of Two Subsequences │ ├── README.md │ ├── index.js │ └── test.js ├── 1461.Check If a String Contains All Binary Codes of Size K │ ├── README.md │ ├── index.js │ └── test.js ├── 1486.XOR Operation in an Array │ ├── README.md │ ├── index.js │ └── test.js ├── 1487.Making File Names Unique │ ├── README.md │ ├── index.js │ └── test.js ├── 1497.Check If Array Pairs Are Divisible by k │ ├── README.md │ ├── index.js │ └── test.js ├── 1502.Can Make Arithmetic Progression From Sequence │ ├── README.md │ ├── index.js │ └── test.js ├── 1503.Last Moment Before All Ants Fall Out of a Plank │ ├── README.md │ ├── index.js │ └── test.js ├── 1504.Count Submatrices With All Ones │ ├── README.md │ ├── index.js │ └── test.js ├── 1512.Number of Good Pairs │ ├── README.md │ ├── index.js │ └── test.js ├── 1513.Number of Substrings With Only 1s │ ├── README.md │ ├── index.js │ └── test.js ├── 1514.Path with Maximum Probability │ ├── README.md │ ├── index.js │ └── test.js ├── 1518.Water Bottles │ ├── README.md │ ├── index.js │ └── test.js ├── 1519.Number of Nodes in the Sub-Tree With the Same Label │ ├── README.md │ ├── index.js │ └── test.js ├── 1520.Maximum Number of Non-Overlapping Substrings │ ├── README.md │ ├── index.js │ └── test.js ├── 1539.Kth Missing Positive Number │ ├── README.md │ ├── index.js │ └── test.js ├── 1557.Minimum Number of Vertices to Reach All Nodes │ ├── README.md │ ├── index.js │ └── test.js ├── 1609.Even Odd Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 1631.Path With Minimum Effort │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 1641.Count Sorted Vowel Strings │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 1642.Furthest Building You Can Reach │ ├── README.md │ ├── index.js │ └── test.js ├── 1646.Get Maximum in Generated Array │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 1649.Create Sorted Array through Instructions │ ├── Cargo.toml │ ├── README.md │ ├── index.js │ ├── solution.rs │ └── test.js ├── 1657.Determine if Two Strings Are Close │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 1658.Minimum Operations to Reduce X to Zero │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 1662.Check If Two String Arrays are Equivalent │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 1663.Smallest String With A Given Numeric Value │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 1672.Richest Customer Wealth │ ├── README.md │ ├── index.js │ └── test.js ├── 1673.Find the Most Competitive Subsequence │ ├── README.md │ ├── index.js │ └── test.js ├── 1675.Minimize Deviation in Array │ ├── README.md │ ├── index.js │ └── test.js ├── 1679.Max Number of K-Sum Pairs │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 1680.Concatenation of Consecutive Binary Numbers │ ├── Cargo.toml │ ├── README.md │ └── solution.rs ├── 1704.Determine if String Halves Are Alike │ ├── README.md │ ├── index.js │ └── test.js ├── 1710.Maximum Units on a Truck │ ├── README.md │ ├── index.js │ └── test.js ├── 1711.Count Good Meals │ ├── README.md │ ├── index.js │ └── test.js ├── 1712.Ways to Split Array Into Three Subarrays │ ├── README.md │ ├── index.js │ └── test.js ├── 1721.Swapping Nodes in a Linked List │ ├── README.md │ ├── index.js │ └── test.js ├── 1791.Find Center of Star Graph │ ├── README.md │ ├── index.js │ └── test.js ├── 1822.Sign of the Product of an Array │ ├── README.md │ └── index.js ├── 1971.Find if Path Exists in Graph │ ├── README.md │ ├── index.js │ └── test.js ├── 2160.Minimum Sum of Four Digit Number After Splitting Digits │ ├── README.md │ ├── index.js │ └── test.js ├── 2236.Root Equals Sum of Children │ ├── README.md │ ├── index.js │ └── test.js ├── 2265.Count Nodes Equal to Average of Subtree │ ├── README.md │ ├── index.js │ └── test.js ├── 2331.Evaluate Boolean Binary Tree │ ├── README.md │ ├── index.js │ └── test.js ├── 2375.Construct Smallest Number From DI String │ ├── README.md │ ├── index.js │ └── test.js ├── 2619.Array Prototype Last Easy │ ├── README.md │ └── index.js ├── 2678.Number of Senior Citizens │ ├── README.md │ ├── index.js │ └── test.js ├── 2685.Count the Number of Complete Components │ ├── README.md │ ├── index.js │ └── test.js ├── 3304.Find the K-th Character in String Game I │ ├── README.md │ ├── index.js │ └── test.js ├── 3305.Count of Substrings Containing Every Vowel and K Consonants I │ ├── README.md │ ├── index.js │ └── test.js ├── 547.Number of Provinces │ ├── README.md │ ├── index.js │ └── test.js ├── util.js └── utils │ ├── Cargo.toml │ ├── ListNode.cs │ ├── TreeNode.cs │ ├── binarySearch.js │ ├── combination.js │ ├── euclidean.js │ ├── index.js │ ├── larssonSadakane.js │ ├── lib.rs │ ├── list.rs │ ├── prime.js │ ├── priorityQueue.js │ ├── quickSort.js │ ├── tree.js │ └── tree.rs ├── benchmark.ts ├── gulp ├── tasks │ ├── build.ts │ ├── index.ts │ └── init.ts └── template │ ├── js │ ├── index.js.tpl │ └── test.js.tpl │ └── rust │ ├── Cargo.toml.tpl │ └── solution.rs.tpl ├── gulpfile.ts ├── lib └── test.js ├── package.json ├── packages.config ├── template ├── README.md ├── Template.java └── template.rb ├── test.js ├── tsconfig.json └── yarn.lock /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules 2 | npm-debug.log 3 | yarn-error.log 4 | 5 | .idea 6 | 7 | contest 8 | 9 | 10 | #Added by cargo 11 | # 12 | #already existing elements are commented out 13 | 14 | /target 15 | **/*.rs.bk 16 | 17 | .DS_Store 18 | Folder* 19 | bin 20 | obj 21 | packages 22 | -------------------------------------------------------------------------------- /algorithms/0001.Two Sum/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "two-sum" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "two_sum" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0001.Two Sum/Solution.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | 3 | public class Solution { 4 | public int[] twoSum(int[] nums, int target) { 5 | int l = nums.length; 6 | HashMap map = new HashMap(); 7 | for (int i = 0; i < l; i++) { 8 | int n = nums[i]; 9 | int diff = target - n; 10 | if (map.containsKey(diff)) { 11 | return new int[]{map.get(diff), i}; 12 | } 13 | map.put(n, i); 14 | } 15 | return new int[]{0, 0}; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0001.Two Sum/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func twoSum(nums []int, target int) []int { 4 | var indexMap map[int]int = make(map[int]int) 5 | var result []int 6 | for index, num := range nums { 7 | var diff = target - num 8 | if indexMap[diff] != 0 { 9 | result = []int{indexMap[diff] - 1, index} 10 | break 11 | } 12 | indexMap[num] = index + 1 13 | } 14 | return result 15 | } 16 | -------------------------------------------------------------------------------- /algorithms/0001.Two Sum/solution.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @param {Integer} target 3 | # @return {Integer[]} 4 | def two_sum(nums, target) 5 | hash = {} 6 | for i in 0..nums.length 7 | n = nums[i] 8 | diff = target - n 9 | t = hash[diff] 10 | if t != nil 11 | return [t, i] 12 | end 13 | hash[n] = i 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /algorithms/0002.Add Two Numbers/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "add-two-numbers" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | utils = { path = "../utils" } 9 | 10 | [lib] 11 | name = "add_two_numbers" 12 | path = "solution.rs" 13 | -------------------------------------------------------------------------------- /algorithms/0002.Add Two Numbers/README.md: -------------------------------------------------------------------------------- 1 | ## 2. Add Two Numbers QuestionEditorial Solution My Submissions 2 | 3 | https://leetcode.com/problems/add-two-numbers/ 4 | 5 | You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list. 6 | 7 | Input: (2 -> 4 -> 3) + (5 -> 6 -> 4) 8 | Output: 7 -> 0 -> 8 9 | 10 | Subscribe to see which companies asked this question 11 | 12 | Show Tags 13 | Show Similar Problems 14 | -------------------------------------------------------------------------------- /algorithms/0002.Add Two Numbers/Test.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace AddTwoNumbers 4 | { 5 | public class Test 6 | { 7 | private readonly Solution solution = new Solution(); 8 | 9 | [Test] 10 | public void Test1() 11 | { 12 | Assert.AreEqual( 13 | solution.AddTwoNumbers(ListNode.Create(new[] { 2, 4, 3 }), ListNode.Create(new[] { 5, 6, 4 })) 14 | .ToArray(), 15 | new[] { 7, 0, 8 } 16 | ); 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0005.Longest Palindromic Substring/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "longest-palindromic-substring" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "longest_palindromic_substring" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0005.Longest Palindromic Substring/README.md: -------------------------------------------------------------------------------- 1 | ## 5. Longest Palindromic Substring 2 | 3 | https://leetcode.com/problems/longest-palindromic-substring/ 4 | 5 | Given a string s, find the longest palindromic substring in s. You may assume that the maximum length of s is 1000. 6 | 7 | ``` 8 | Example: 9 | 10 | Input: "babad" 11 | 12 | Output: "bab" 13 | ``` 14 | 15 | Note: "aba" is also a valid answer. 16 | 17 | ``` 18 | Example: 19 | 20 | Input: "cbbd" 21 | 22 | Output: "bb" 23 | ``` 24 | -------------------------------------------------------------------------------- /algorithms/0007.Reverse Integer/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | final int MAX = 2147483647; 4 | final int MIN = -2147483648; 5 | 6 | public int reverse(int x) { 7 | long result = 0; 8 | while (x != 0) { 9 | result = result * 10 + x % 10; 10 | x /= 10; 11 | } 12 | if (result < MIN || result > MAX) { 13 | return 0; 14 | } 15 | return (int)result; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0007.Reverse Integer/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = reverse; 4 | 5 | const MAX = 2147483647; 6 | const MIN = -2147483648; 7 | 8 | /** 9 | * @param {number} x 10 | * @return {number} 11 | */ 12 | function reverse(x) { 13 | let result = 0; 14 | while (x !== 0) { 15 | result = result * 10 + (x % 10 | 0); 16 | x = (x / 10) | 0; 17 | } 18 | if (result > MAX || result < MIN) { 19 | return 0; 20 | } 21 | return result; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0009.Palindrome Number/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean isPalindrome(int x) { 3 | if (x < 0) { 4 | return false; 5 | } 6 | int n = x; 7 | long result = 0; 8 | while (n != 0) { 9 | result = result * 10 + n % 10; 10 | n /= 10; 11 | } 12 | return x == (int)result; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /algorithms/0011.Container With Most Water/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "container-with-most-water" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "container_with_most_water" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0011.Container With Most Water/README.md: -------------------------------------------------------------------------------- 1 | ## 11. Container With Most Water 2 | 3 | https://leetcode.com/problems/container-with-most-water/description/ 4 | 5 | Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). n vertical lines are drawn such that the two endpoints of line i is at (i, ai) and (i, 0). Find two lines, which together with x-axis forms a container, such that the container contains the most water. 6 | 7 | Note: You may not slant the container and n is at least 2. 8 | -------------------------------------------------------------------------------- /algorithms/0011.Container With Most Water/solution.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} height 2 | # @return {Integer} 3 | def max_area(height) 4 | max = 0 5 | left = 0 6 | right = height.length - 1 7 | while left < right do 8 | n1 = height[left] 9 | n2 = height[right] 10 | if n1 < n2 then 11 | max = [max, n1 * (right - left)].max 12 | left += 1 13 | else 14 | max = [max, n2 * (right - left)].max 15 | right -= 1 16 | end 17 | end 18 | return max 19 | end 20 | -------------------------------------------------------------------------------- /algorithms/0012.Integer to Roman/README.md: -------------------------------------------------------------------------------- 1 | ## 12. Integer to Roman 2 | 3 | https://leetcode.com/problems/integer-to-roman/description/ 4 | 5 | Given an integer, convert it to a roman numeral. 6 | 7 | Input is guaranteed to be within the range from 1 to 3999. 8 | -------------------------------------------------------------------------------- /algorithms/0013.Roman to Integer/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "roman-to-integer" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "roman_to_integer" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0013.Roman to Integer/README.md: -------------------------------------------------------------------------------- 1 | # 13. Roman to Integer 2 | 3 | https://leetcode.com/problems/roman-to-integer/ 4 | 5 | Given a roman numeral, convert it to an integer. 6 | 7 | Input is guaranteed to be within the range from 1 to 3999. 8 | -------------------------------------------------------------------------------- /algorithms/0014.Longest Common Prefix/README.md: -------------------------------------------------------------------------------- 1 | # 14. Longest Common Prefix 2 | 3 | https://leetcode.com/problems/longest-common-prefix/ 4 | 5 | Write a function to find the longest common prefix string amongst an array of strings. 6 | -------------------------------------------------------------------------------- /algorithms/0015.3Sum/README.md: -------------------------------------------------------------------------------- 1 | ## 015. 3Sum 2 | 3 | https://leetcode.com/problems/3sum 4 | 5 | Given an array nums of n integers, are there elements a, b, c in nums such that a + b + c = 0? Find all unique triplets in the array which gives the sum of zero. 6 | 7 | Note: 8 | 9 | The solution set must not contain duplicate triplets. 10 | 11 | Example: 12 | 13 | Given array nums = [-1, 0, 1, 2, -1, -4], 14 | 15 | A solution set is: 16 | [ 17 | [-1, 0, 1], 18 | [-1, -1, 2] 19 | ] 20 | -------------------------------------------------------------------------------- /algorithms/0016.3Sum Closest/README.md: -------------------------------------------------------------------------------- 1 | ## 016. 3Sum Closest 2 | 3 | https://leetcode.com/problems/3sum-closest 4 | 5 | Given an array nums of n integers and an integer target, find three integers in nums such that the sum is closest to target. Return the sum of the three integers. You may assume that each input would have exactly one solution. 6 | 7 | Example: 8 | 9 | Given array nums = [-1, 2, 1, -4], and target = 1. 10 | 11 | The sum that is closest to the target is 2. (-1 + 2 + 1 = 2). 12 | -------------------------------------------------------------------------------- /algorithms/0019.Remove Nth Node From End of List/README.md: -------------------------------------------------------------------------------- 1 | # 19. Remove Nth Node From End of List 2 | 3 | https://leetcode.com/problems/remove-nth-node-from-end-of-list/ 4 | 5 | Given a linked list, remove the nth node from the end of list and return its head. 6 | 7 | For example, 8 | 9 | Given linked list: 1->2->3->4->5, and n = 2. 10 | 11 | After removing the second node from the end, the linked list becomes 1->2->3->5. 12 | Note: 13 | Given n will always be valid. 14 | Try to do this in one pass. 15 | -------------------------------------------------------------------------------- /algorithms/0020.Valid Parentheses/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "valid-parentheses" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "valid_parentheses" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0020.Valid Parentheses/README.md: -------------------------------------------------------------------------------- 1 | # 20. Valid Parentheses 2 | 3 | https://leetcode.com/problems/valid-parentheses/ 4 | 5 | Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if the input string is valid. 6 | 7 | The brackets must close in the correct order, "()" and "()[]{}" are all valid but "(]" and "([)]" are not. 8 | -------------------------------------------------------------------------------- /algorithms/0021.Merge Two Sorted Lists/README.md: -------------------------------------------------------------------------------- 1 | # 21. Merge Two Sorted Lists 2 | 3 | https://leetcode.com/problems/merge-two-sorted-lists/ 4 | 5 | Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. 6 | -------------------------------------------------------------------------------- /algorithms/0022.Generate Parentheses/README.md: -------------------------------------------------------------------------------- 1 | ## 022. Generate Parentheses 2 | 3 | https://leetcode.com/problems/generate-parentheses 4 | 5 | Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. 6 | 7 | For example, given n = 3, a solution set is: 8 | 9 | [ 10 | "((()))", 11 | "(()())", 12 | "(())()", 13 | "()(())", 14 | "()()()" 15 | ] 16 | -------------------------------------------------------------------------------- /algorithms/0023.Merge k Sorted Lists/README.md: -------------------------------------------------------------------------------- 1 | ## 0023. Merge k Sorted Lists 2 | 3 | https://leetcode.com/problems/merge-k-sorted-lists 4 | 5 | Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 6 | 7 | Example: 8 | 9 | Input: 10 | [ 11 |   1->4->5, 12 |   1->3->4, 13 |   2->6 14 | ] 15 | Output: 1->1->2->3->4->4->5->6 16 | -------------------------------------------------------------------------------- /algorithms/0024.Swap Nodes in Pairs/README.md: -------------------------------------------------------------------------------- 1 | # 24. Swap Nodes in Pairs 2 | 3 | https://leetcode.com/problems/swap-nodes-in-pairs/ 4 | 5 | Given a linked list, swap every two adjacent nodes and return its head. 6 | 7 | For example, 8 | Given 1->2->3->4, you should return the list as 2->1->4->3. 9 | 10 | Your algorithm should use only constant space. You may not modify the values in the list, only nodes itself can be changed. 11 | -------------------------------------------------------------------------------- /algorithms/0026.Remove Duplicates from Sorted Array/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int removeDuplicates(int[] nums) { 3 | int l = nums.length; 4 | if (l <= 1) { 5 | return l; 6 | } 7 | int length = 1; 8 | int prev = nums[0]; 9 | for (int i = 1; i < l; i++) { 10 | int n = nums[i]; 11 | if (prev == n) { 12 | continue; 13 | } 14 | prev = n; 15 | nums[length++] = n; 16 | } 17 | return length; 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0027.Remove Element/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int removeElement(int[] nums, int val) { 3 | int length = 0; 4 | for (int i = 0; i < nums.length; i++) { 5 | int n = nums[i]; 6 | if (n == val) { 7 | continue; 8 | } 9 | nums[length++] = n; 10 | } 11 | return length; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /algorithms/0028.Implement strStr/README.md: -------------------------------------------------------------------------------- 1 | ## 28. Implement strStr() 2 | 3 | https://leetcode.com/problems/implement-strstr/ 4 | 5 | Implement strStr(). 6 | 7 | Returns the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack. 8 | -------------------------------------------------------------------------------- /algorithms/0032.Longest Valid Parentheses/README.md: -------------------------------------------------------------------------------- 1 | ## 032. Longest Valid Parentheses 2 | 3 | https://leetcode.com/problems/longest-valid-parentheses 4 | 5 | Given a string containing just the characters '(' and ')', find the length of the longest valid (well-formed) parentheses substring. 6 | 7 | Example 1: 8 | 9 | Input: "(()" 10 | Output: 2 11 | Explanation: The longest valid parentheses substring is "()" 12 | 13 | Example 2: 14 | 15 | Input: ")()())" 16 | Output: 4 17 | Explanation: The longest valid parentheses substring is "()()" 18 | -------------------------------------------------------------------------------- /algorithms/0035.Search Insert Position/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "search-insert-position" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "search_insert_position" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0035.Search Insert Position/README.md: -------------------------------------------------------------------------------- 1 | ## 035. Search Insert Position 2 | 3 | https://leetcode.com/problems/search-insert-position/#/description 4 | 5 | Given a sorted array and a target value, return the index if the target is found. If not, return the index where it would be if it were inserted in order. 6 | 7 | You may assume no duplicates in the array. 8 | 9 | Here are few examples. 10 | 11 | ``` 12 | [1,3,5,6], 5 → 2 13 | [1,3,5,6], 2 → 1 14 | [1,3,5,6], 7 → 4 15 | [1,3,5,6], 0 → 0 16 | ``` 17 | -------------------------------------------------------------------------------- /algorithms/0035.Search Insert Position/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int searchInsert(int[] nums, int target) { 3 | int l = nums.length; 4 | for (int i = 0; i < l; i++) { 5 | if (nums[i] >= target) { 6 | return i; 7 | } 8 | } 9 | return l; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /algorithms/0035.Search Insert Position/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "sort" 4 | 5 | func searchInsert(nums []int, target int) int { 6 | return sort.Search(len(nums), func(i int) bool { 7 | return nums[i] >= target 8 | }) 9 | } 10 | -------------------------------------------------------------------------------- /algorithms/0038.Count and Say/README.md: -------------------------------------------------------------------------------- 1 | ## 38. Count and Say 2 | 3 | https://leetcode.com/problems/count-and-say/ 4 | 5 | The count-and-say sequence is the sequence of integers beginning as follows: 6 | 1, 11, 21, 1211, 111221, ... 7 | 8 | 1 is read off as "one 1" or 11. 9 | 11 is read off as "two 1s" or 21. 10 | 21 is read off as "one 2, then one 1" or 1211. 11 | Given an integer n, generate the nth sequence. 12 | 13 | Note: The sequence of integers will be represented as a string. 14 | -------------------------------------------------------------------------------- /algorithms/0045.Jump Game II/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { jump }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {number} 8 | */ 9 | function jump(nums) { 10 | const last = nums.length - 1; 11 | const dp = Array.from(nums).fill(Infinity); 12 | dp[0] = 0; 13 | for (let i = 0; i < last; i++) { 14 | const n = nums[i]; 15 | const l = Math.min(i + n, last); 16 | for (let j = i + 1; j <= l; j++) { 17 | dp[j] = Math.min(dp[j], dp[i] + 1); 18 | } 19 | } 20 | return dp[last]; 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/0045.Jump Game II/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | const { jump } = require('./'); 6 | 7 | describe('#jump', () => { 8 | const tests = [ 9 | { 10 | nums: [2, 3, 1, 1, 4], 11 | result: 2, 12 | }, 13 | { 14 | nums: [2, 3, 0, 1, 4], 15 | result: 2, 16 | }, 17 | ]; 18 | 19 | for (const { nums, result } of tests) { 20 | it(`${nums} -> ${result}`, () => { 21 | assert.deepStrictEqual(jump(nums), result); 22 | }); 23 | } 24 | }); 25 | -------------------------------------------------------------------------------- /algorithms/0046.Permutations/README.md: -------------------------------------------------------------------------------- 1 | ## 0046. Permutations 2 | 3 | https://leetcode.com/problems/permutations 4 | 5 | Given a collection of distinct integers, return all possible permutations. 6 | 7 | Example: 8 | 9 | Input: [1,2,3] 10 | Output: 11 | [ 12 | [1,2,3], 13 | [1,3,2], 14 | [2,1,3], 15 | [2,3,1], 16 | [3,1,2], 17 | [3,2,1] 18 | ] 19 | -------------------------------------------------------------------------------- /algorithms/0046.Permutations/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { permute } = require('./'); 6 | 7 | describe('#permute', () => { 8 | const tests = [ 9 | { 10 | nums: [1, 2, 3], 11 | result: [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]], 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ nums, result }) => { 16 | it(`${nums} -> ${result}`, () => { 17 | assert.deepStrictEqual(permute(nums), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/0049.Group Anagrams/README.md: -------------------------------------------------------------------------------- 1 | ## 0049. Group Anagrams 2 | 3 | https://leetcode.com/problems/group-anagrams 4 | 5 | Given an array of strings, group anagrams together. 6 | 7 | Example: 8 | 9 | Input: ["eat", "tea", "tan", "ate", "nat", "bat"], 10 | Output: 11 | [ 12 | ["ate","eat","tea"], 13 | ["nat","tan"], 14 | ["bat"] 15 | ] 16 | 17 | Note: 18 | 19 | All inputs will be in lowercase. 20 | The order of your output does not matter. 21 | -------------------------------------------------------------------------------- /algorithms/0049.Group Anagrams/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func groupAnagrams(strs []string) [][]string { 4 | groupMap := make(map[[26]int][]string) 5 | for _, str := range strs { 6 | var counts [26]int 7 | for _, char := range str { 8 | counts[char-'a']++ 9 | } 10 | groupMap[counts] = append(groupMap[counts], str) 11 | } 12 | var result [][]string 13 | for _, group := range groupMap { 14 | result = append(result, group) 15 | } 16 | return result 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0049.Group Anagrams/solution.py: -------------------------------------------------------------------------------- 1 | from typing import List, Dict 2 | 3 | class Solution: 4 | def groupAnagrams(self, strs: List[str]) -> List[List[str]]: 5 | groupMap: Dict[str, List[str]] = {} 6 | for s in strs: 7 | counts = [0] * 26 8 | for c in s: 9 | counts[ord(c) - 97] += 1 10 | key = ','.join(map(str, counts)) 11 | groupMap[key] = groupMap.get(key, []) + [s] 12 | return list(groupMap.values()) 13 | -------------------------------------------------------------------------------- /algorithms/0050.Pow(x, n)/README.md: -------------------------------------------------------------------------------- 1 | ## 0050. Pow(x, n) 2 | 3 | https://leetcode.com/problems/powx-n 4 | 5 | Implement pow(x, n), which calculates x raised to the power n (xn). 6 | 7 | Example 1: 8 | 9 | Input: 2.00000, 10 10 | Output: 1024.00000 11 | 12 | Example 2: 13 | 14 | Input: 2.10000, 3 15 | Output: 9.26100 16 | 17 | Example 3: 18 | 19 | Input: 2.00000, -2 20 | Output: 0.25000 21 | Explanation: 2-2 = 1/22 = 1/4 = 0.25 22 | 23 | Note: 24 | 25 | -100.0 < x < 100.0 26 | n is a 32-bit signed integer, within the range [−231, 231 − 1] 27 | -------------------------------------------------------------------------------- /algorithms/0050.Pow(x, n)/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { myPow }; 4 | 5 | /** 6 | * @param {number} x 7 | * @param {number} n 8 | * @return {number} 9 | */ 10 | function myPow(x, n) { 11 | const plus = n >= 0; 12 | n = Math.abs(n); 13 | let result = 1; 14 | while (n > 0) { 15 | if ((n & 1) === 1) { 16 | result *= x; 17 | } 18 | x *= x; 19 | n = Math.floor(n / 2); 20 | } 21 | return plus ? result : 1 / result; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0053.Maximum Subarray/README.md: -------------------------------------------------------------------------------- 1 | ## 053. Maximum Subarray 2 | 3 | https://leetcode.com/problems/maximum-subarray/#/description 4 | 5 | Find the contiguous subarray within an array (containing at least one number) which has the largest sum. 6 | 7 | For example, given the array [-2,1,-3,4,-1,2,1,-5,4], 8 | the contiguous subarray [4,-1,2,1] has the largest sum = 6. 9 | -------------------------------------------------------------------------------- /algorithms/0053.Maximum Subarray/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | 3 | public int maxSubArray(int[] nums) { 4 | int sum = 0; 5 | int max = Integer.MIN_VALUE; 6 | for (int i = 0; i < nums.length; i++) { 7 | int n = nums[i]; 8 | sum += n; 9 | max = Math.max(max, sum); 10 | if (sum < 0) { 11 | sum = 0; 12 | } 13 | } 14 | return max; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /algorithms/0053.Maximum Subarray/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func maxSubArray(nums []int) int { 4 | cur, res := nums[0], nums[0] 5 | for _, n := range nums[1:] { 6 | cur = max(n, cur+n) 7 | res = max(res, cur) 8 | } 9 | return res 10 | } 11 | 12 | func max(n1, n2 int) int { 13 | if n2 > n1 { 14 | return n2 15 | } 16 | return n1 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0055.Jump Game/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func canJump(nums []int) bool { 4 | var max int 5 | for index, num := range nums { 6 | if index > max { 7 | return false 8 | } 9 | next := index + num 10 | if next > max { 11 | max = next 12 | } 13 | } 14 | return true 15 | } 16 | -------------------------------------------------------------------------------- /algorithms/0058.Length of Last Word/README.md: -------------------------------------------------------------------------------- 1 | ## 58. Length of Last Word 2 | 3 | https://leetcode.com/problems/length-of-last-word/ 4 | 5 | Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. 6 | 7 | If the last word does not exist, return 0. 8 | 9 | Note: A word is defined as a character sequence consists of non-space characters only. 10 | 11 | For example, 12 | Given s = "Hello World", 13 | return 5. 14 | -------------------------------------------------------------------------------- /algorithms/0058.Length of Last Word/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int lengthOfLastWord(String s) { 3 | int l = s.length(); 4 | int result = 0; 5 | while (l-- != 0) { 6 | char c = s.charAt(l); 7 | if (c == ' ') { 8 | if (result == 0) { 9 | continue; 10 | } 11 | break; 12 | } 13 | result++; 14 | } 15 | return result; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0064.Minimum Path Sum/README.md: -------------------------------------------------------------------------------- 1 | ## 064. Minimum Path Sum 2 | 3 | https://leetcode.com/problems/minimum-path-sum 4 | 5 | Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers along its path. 6 | 7 | Note: You can only move either down or right at any point in time. 8 | 9 | Example: 10 | 11 | Input: 12 | [ 13 |   [1,3,1], 14 | [1,5,1], 15 | [4,2,1] 16 | ] 17 | Output: 7 18 | Explanation: Because the path 1→3→1→1→1 minimizes the sum. 19 | -------------------------------------------------------------------------------- /algorithms/0064.Minimum Path Sum/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { minPathSum } = require('./'); 7 | 8 | describe('#minPathSum', () => { 9 | const tests = [ 10 | { 11 | grid: [[1, 3, 1], [1, 5, 1], [4, 2, 1]], 12 | result: 7, 13 | }, 14 | ]; 15 | 16 | _.forEach(tests, ({ grid, result }) => { 17 | it(`${grid} -> ${result}`, () => { 18 | assert.deepStrictEqual(minPathSum(grid), result); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /algorithms/0066.Plus One/README.md: -------------------------------------------------------------------------------- 1 | ## 66. Plus One 2 | 3 | https://leetcode.com/problems/plus-one/ 4 | 5 | Given a non-negative integer represented as a non-empty array of digits, plus one to the integer. 6 | 7 | You may assume the integer do not contain any leading zero, except the number 0 itself. 8 | 9 | The digits are stored such that the most significant digit is at the head of the list. 10 | -------------------------------------------------------------------------------- /algorithms/0067.Add Binary/README.md: -------------------------------------------------------------------------------- 1 | ## 67. Add Binary 2 | 3 | https://leetcode.com/problems/add-binary/ 4 | 5 | Given two binary strings, return their sum (also a binary string). 6 | 7 | For example, 8 | a = "11" 9 | b = "1" 10 | Return "100". 11 | -------------------------------------------------------------------------------- /algorithms/0069.Sqrt(x)/README.md: -------------------------------------------------------------------------------- 1 | ## 69. Sqrt(x) 2 | 3 | https://leetcode.com/problems/sqrtx/#/description 4 | 5 | Implement int sqrt(int x). 6 | 7 | Compute and return the square root of x. 8 | -------------------------------------------------------------------------------- /algorithms/0070.Climbing Stairs/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "climbing-stairs" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "climbing_stairs" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0070.Climbing Stairs/README.md: -------------------------------------------------------------------------------- 1 | ## 70. Climbing Stairs 2 | 3 | https://leetcode.com/problems/climbing-stairs/#/description 4 | 5 | You are climbing a stair case. It takes n steps to reach to the top. 6 | 7 | Each time you can either climb 1 or 2 steps. In how many distinct ways can you climb to the top? 8 | 9 | Note: Given n will be a positive integer. 10 | -------------------------------------------------------------------------------- /algorithms/0070.Climbing Stairs/Test.cs: -------------------------------------------------------------------------------- 1 | using NUnit.Framework; 2 | 3 | namespace ClimbingStairs 4 | { 5 | public class Test 6 | { 7 | private readonly Solution solution = new Solution(); 8 | 9 | [Test] 10 | public void Test1() 11 | { 12 | Assert.AreEqual(solution.ClimbStairs(2), 2); 13 | } 14 | 15 | [Test] 16 | public void Test2() 17 | { 18 | Assert.AreEqual(solution.ClimbStairs(3), 3); 19 | } 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/0071.Simplify Path/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "simplify-path" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "simplify_path" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0075.Sort Colors/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { sortColors }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {void} Do not return anything, modify nums in-place instead. 8 | */ 9 | function sortColors(nums) { 10 | const counts = [0, 0, 0]; 11 | for (const n of nums) { 12 | counts[n]++; 13 | } 14 | let i = 0; 15 | for (const [n, count] of counts.entries()) { 16 | for (let j = 0; j < count; j++) { 17 | nums[i++] = n; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /algorithms/0075.Sort Colors/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { sortColors } = require('./'); 7 | 8 | describe('#sortColors', () => { 9 | const tests = [ 10 | { 11 | nums: [2, 0, 2, 1, 1, 0], 12 | result: [0, 0, 1, 1, 2, 2], 13 | }, 14 | ]; 15 | 16 | _.forEach(tests, ({ nums, result }) => { 17 | it(`${nums} -> ${result}`, () => { 18 | sortColors(nums); 19 | assert.deepStrictEqual(nums, result); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /algorithms/0078.Subsets/README.md: -------------------------------------------------------------------------------- 1 | ## 078. Subsets 2 | 3 | https://leetcode.com/problems/subsets 4 | 5 | Given a set of distinct integers, nums, return all possible subsets (the power set). 6 | 7 | Note: The solution set must not contain duplicate subsets. 8 | 9 | Example: 10 | 11 | Input: nums = [1,2,3] 12 | Output: 13 | [ 14 | [3], 15 |   [1], 16 |   [2], 17 |   [1,2,3], 18 |   [1,3], 19 |   [2,3], 20 |   [1,2], 21 |   [] 22 | ] 23 | -------------------------------------------------------------------------------- /algorithms/0078.Subsets/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { subsets } = require('./'); 6 | 7 | describe('#subsets', () => { 8 | const tests = [ 9 | { 10 | nums: [1, 2, 3], 11 | result: [[], [1], [1, 2], [1, 2, 3], [1, 3], [2], [2, 3], [3]], 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ nums, result }) => { 16 | it(`${nums} -> ${result}`, () => { 17 | assert.deepStrictEqual(subsets(nums), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/0082.Remove Duplicates from Sorted List II/README.md: -------------------------------------------------------------------------------- 1 | ## 082. Remove Duplicates from Sorted List II 2 | 3 | https://leetcode.com/problems/remove-duplicates-from-sorted-list-ii 4 | 5 | Given a sorted linked list, delete all nodes that have duplicate numbers, leaving only distinct numbers from the original list. 6 | 7 | Example 1: 8 | 9 | Input: 1->2->3->3->4->4->5 10 | Output: 1->2->5 11 | 12 | Example 2: 13 | 14 | Input: 1->1->1->2->3 15 | Output: 2->3 16 | -------------------------------------------------------------------------------- /algorithms/0083.Remove Duplicates from Sorted List/README.md: -------------------------------------------------------------------------------- 1 | ## 83. Remove Duplicates from Sorted List 2 | 3 | https://leetcode.com/problems/remove-duplicates-from-sorted-list/#/description 4 | 5 | Given a sorted linked list, delete all duplicates such that each element appear only once. 6 | 7 | For example, 8 | 9 | ``` 10 | Given 1->1->2, return 1->2. 11 | Given 1->1->2->3->3, return 1->2->3. 12 | ``` 13 | -------------------------------------------------------------------------------- /algorithms/0088.Merge Sorted Array/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "merge-sorted-array" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "merge_sorted_array" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0088.Merge Sorted Array/README.md: -------------------------------------------------------------------------------- 1 | ## 088. Merge Sorted Array 2 | 3 | https://leetcode.com/problems/merge-sorted-array/#/description 4 | 5 | Given two sorted integer arrays nums1 and nums2, merge nums2 into nums1 as one sorted array. 6 | 7 | Note: 8 | You may assume that nums1 has enough space (size that is greater or equal to m + n) to hold additional elements from nums2. The number of elements initialized in nums1 and nums2 are m and n respectively. 9 | -------------------------------------------------------------------------------- /algorithms/0089.Gray Code/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { grayCode }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {number[]} 8 | */ 9 | function grayCode(n) { 10 | const max = 1 << n; 11 | const result = Array(max); 12 | for (let i = 0; i < max; i++) { 13 | result[i] = i ^ (i >> 1); 14 | } 15 | return result; 16 | } 17 | -------------------------------------------------------------------------------- /algorithms/0089.Gray Code/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { grayCode } = require('./'); 6 | 7 | describe('#grayCode', () => { 8 | const tests = [ 9 | { 10 | n: 2, 11 | result: [0, 1, 3, 2], 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ n, result }) => { 16 | it(`${n} -> ${result}`, () => { 17 | assert.deepStrictEqual(grayCode(n), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/0094.Binary Tree Inorder Traversal/README.md: -------------------------------------------------------------------------------- 1 | ## 094. Binary Tree Inorder Traversal 2 | 3 | https://leetcode.com/problems/binary-tree-inorder-traversal 4 | 5 | Given a binary tree, return the inorder traversal of its nodes' values. 6 | 7 | Example: 8 | 9 | Input: [1,null,2,3] 10 | 1 11 | \ 12 | 2 13 | / 14 | 3 15 | 16 | Output: [1,3,2] 17 | 18 | Follow up: Recursive solution is trivial, could you do it iteratively? 19 | -------------------------------------------------------------------------------- /algorithms/0096.Unique Binary Search Trees/README.md: -------------------------------------------------------------------------------- 1 | ## 0096. Unique Binary Search Trees 2 | 3 | https://leetcode.com/problems/unique-binary-search-trees 4 | 5 | Given n, how many structurally unique BST's (binary search trees) that store values 1 ... n? 6 | 7 | Example: 8 | 9 | Input: 3 10 | Output: 5 11 | Explanation: 12 | Given n = 3, there are a total of 5 unique BST's: 13 | 14 | 1 3 3 2 1 15 | \ / / / \ \ 16 | 3 2 1 1 3 2 17 | / / \ \ 18 | 2 1 2 3 19 | -------------------------------------------------------------------------------- /algorithms/0096.Unique Binary Search Trees/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { numTrees }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {number} 8 | */ 9 | function numTrees(n) { 10 | const dp = [1, 1, ...Array(n - 1).fill(0)]; 11 | for (let i = 2; i <= n; i++) { 12 | for (let j = 1; j <= i; j++) { 13 | dp[i] += dp[j - 1] * dp[i - j]; 14 | } 15 | } 16 | return dp[n]; 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0096.Unique Binary Search Trees/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { numTrees } = require('./'); 7 | 8 | describe('#numTrees', () => { 9 | const tests = [ 10 | { 11 | n: 3, 12 | result: 5, 13 | }, 14 | ]; 15 | 16 | _.forEach(tests, ({ n, result }) => { 17 | it(`${n} -> ${result}`, () => { 18 | assert.deepStrictEqual(numTrees(n), result); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /algorithms/0100.Same Tree/README.md: -------------------------------------------------------------------------------- 1 | ## 100. Same Tree 2 | 3 | https://leetcode.com/problems/same-tree/#/description 4 | 5 | Given two binary trees, write a function to check if they are equal or not. 6 | 7 | Two binary trees are considered equal if they are structurally identical and the nodes have the same value. 8 | -------------------------------------------------------------------------------- /algorithms/0100.Same Tree/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | /** 4 | * Definition for a binary tree node. 5 | * type TreeNode struct { 6 | * Val int 7 | * Left *TreeNode 8 | * Right *TreeNode 9 | * } 10 | */ 11 | func isSameTree(p *TreeNode, q *TreeNode) bool { 12 | if p == nil || q == nil { 13 | return p == q 14 | } 15 | if p.Val != q.Val { 16 | return false 17 | } 18 | return isSameTree(p.Left, q.Left) && isSameTree(p.Right, q.Right) 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0102.Binary Tree Level Order Traversal/README.md: -------------------------------------------------------------------------------- 1 | ## 102. Binary Tree Level Order Traversal 2 | 3 | https://leetcode.com/problems/binary-tree-level-order-traversal 4 | 5 | Given a binary tree, return the level order traversal of its nodes' values. (ie, from left to right, level by level). 6 | 7 | For example: 8 | Given binary tree [3,9,20,null,null,15,7], 9 | 3 10 | / \ 11 | 9 20 12 | / \ 13 | 15 7 14 | 15 | return its level order traversal as: 16 | [ 17 | [3], 18 | [9,20], 19 | [15,7] 20 | ] 21 | -------------------------------------------------------------------------------- /algorithms/0104.Maximum Depth of Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | ## 104. Maximum Depth of Binary Tree 2 | 3 | https://leetcode.com/problems/maximum-depth-of-binary-tree/#/description 4 | 5 | Given a binary tree, find its maximum depth. 6 | 7 | The maximum depth is the number of nodes along the longest path from the root node down to the farthest leaf node. 8 | -------------------------------------------------------------------------------- /algorithms/0104.Maximum Depth of Binary Tree/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for a binary tree node. 3 | * public class TreeNode { 4 | * int val; 5 | * TreeNode left; 6 | * TreeNode right; 7 | * TreeNode(int x) { val = x; } 8 | * } 9 | */ 10 | public class Solution { 11 | public int maxDepth(TreeNode root) { 12 | return root == null ? 0 : 1 + Math.max(this.getDepth(root.left), this.getDepth(root.righ)); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /algorithms/0104.Maximum Depth of Binary Tree/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { maxDepth }; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * function TreeNode(val) { 8 | * this.val = val; 9 | * this.left = this.right = null; 10 | * } 11 | */ 12 | /** 13 | * @param {TreeNode} root 14 | * @return {number} 15 | */ 16 | function maxDepth(root) { 17 | return !root ? 0 : 1 + Math.max(maxDepth(root.left), maxDepth(root.right)); 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/0108.Convert Sorted Array to Binary Search Tree/README.md: -------------------------------------------------------------------------------- 1 | ## 108. Convert Sorted Array to Binary Search Tree 2 | 3 | https://leetcode.com/problems/convert-sorted-array-to-binary-search-tree/#/description 4 | 5 | Given an array where elements are sorted in ascending order, convert it to a height balanced BST. 6 | -------------------------------------------------------------------------------- /algorithms/0110.Balanced Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | ## 110. Balanced Binary Tree 2 | 3 | https://leetcode.com/problems/balanced-binary-tree/#/description 4 | 5 | Given a binary tree, determine if it is height-balanced. 6 | 7 | For this problem, a height-balanced binary tree is defined as a binary tree in which the depth of the two subtrees of every node never differ by more than 1. 8 | -------------------------------------------------------------------------------- /algorithms/0111.Minimum Depth of Binary Tree/README.md: -------------------------------------------------------------------------------- 1 | ## 111. Minimum Depth of Binary Tree 2 | 3 | https://leetcode.com/problems/minimum-depth-of-binary-tree/#/description 4 | 5 | Given a binary tree, find its minimum depth. 6 | 7 | The minimum depth is the number of nodes along the shortest path from the root node down to the nearest leaf node. 8 | -------------------------------------------------------------------------------- /algorithms/0118.Pascal's Triangle/README.md: -------------------------------------------------------------------------------- 1 | ## 118. Pascal's Triangle 2 | 3 | https://leetcode.com/problems/pascals-triangle/#/description 4 | 5 | Given numRows, generate the first numRows of Pascal's triangle. 6 | 7 | For example, given numRows = 5, 8 | Return 9 | 10 | ``` 11 | [ 12 | [1], 13 | [1,1], 14 | [1,2,1], 15 | [1,3,3,1], 16 | [1,4,6,4,1] 17 | ] 18 | ``` 19 | -------------------------------------------------------------------------------- /algorithms/0119.Pascal's Triangle II/README.md: -------------------------------------------------------------------------------- 1 | ## 119. Pascal's Triangle II 2 | 3 | https://leetcode.com/problems/pascals-triangle-ii/#/description 4 | 5 | Given an index k, return the kth row of the Pascal's triangle. 6 | 7 | For example, given k = 3, 8 | 9 | ``` 10 | Return [1,3,3,1]. 11 | ``` 12 | 13 | Note: 14 | Could you optimize your algorithm to use only O(k) extra space? 15 | -------------------------------------------------------------------------------- /algorithms/0119.Pascal's Triangle II/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { getRow }; 4 | 5 | /** 6 | * @param {number} rowIndex 7 | * @return {number[]} 8 | */ 9 | function getRow(rowIndex) { 10 | const result = Array(rowIndex); 11 | result[0] = 1; 12 | for (let i = 1; i <= rowIndex; i++) { 13 | result[i] = 0; 14 | for (let j = i; j >= 1; j--) { 15 | result[j] += result[j - 1]; 16 | } 17 | } 18 | return result; 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0120.Triangle/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { minimumTotal }; 4 | 5 | /** 6 | * @param {number[][]} triangle 7 | * @return {number} 8 | */ 9 | function minimumTotal(triangle) { 10 | for (let i = 1; i < triangle.length; i++) { 11 | const prev = triangle[i - 1]; 12 | const row = triangle[i]; 13 | for (let j = 0; j < row.length; j++) { 14 | row[j] += Math.min(prev[j] ?? Infinity, prev[j - 1] ?? Infinity); 15 | } 16 | } 17 | return Math.min(...triangle[triangle.length - 1]); 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/0121.Best Time to Buy and Sell Stock/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "best-time-to-buy-and-sell-stock" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [dependencies] 10 | 11 | [lib] 12 | name = "best_time_to_buy_and_sell_stock" 13 | path = "solution.rs" 14 | -------------------------------------------------------------------------------- /algorithms/0121.Best Time to Buy and Sell Stock/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxProfit(int[] prices) { 3 | int min = Integer.MAX_VALUE; 4 | int max = 0; 5 | for (int price : prices) { 6 | int diff = price - min; 7 | if (diff > 0) { 8 | max = Math.max(max, diff); 9 | } else { 10 | min = price; 11 | } 12 | } 13 | return max; 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /algorithms/0122.Best Time to Buy and Sell Stock II/solution.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | class Solution: 4 | def maxProfit(self, prices: List[int]) -> int: 5 | profit: int = 0 6 | for i in range(1, len(prices)): 7 | profit += max(0, prices[i] - prices[i - 1]) 8 | return profit 9 | 10 | -------------------------------------------------------------------------------- /algorithms/0128.Longest Consecutive Sequence/README.md: -------------------------------------------------------------------------------- 1 | ## 0128. Longest Consecutive Sequence 2 | 3 | https://leetcode.com/problems/longest-consecutive-sequence 4 | 5 | Given an unsorted array of integers, find the length of the longest consecutive elements sequence. 6 | 7 | Your algorithm should run in O(n) complexity. 8 | 9 | Example: 10 | 11 | Input: [100, 4, 200, 1, 3, 2] 12 | Output: 4 13 | Explanation: The longest consecutive elements sequence is [1, 2, 3, 4]. Therefore its length is 4. 14 | -------------------------------------------------------------------------------- /algorithms/0136.Single Number/README.md: -------------------------------------------------------------------------------- 1 | ## 136. Single Number 2 | 3 | https://leetcode.com/problems/single-number/#/description 4 | 5 | Given an array of integers, every element appears twice except for one. Find that single one. 6 | 7 | Note: 8 | Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 9 | -------------------------------------------------------------------------------- /algorithms/0136.Single Number/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func singleNumber(nums []int) int { 4 | var bit int 5 | for _, n := range nums { 6 | bit ^= n 7 | } 8 | return bit 9 | } 10 | 11 | func singleNumber2(nums []int) int { 12 | countMap := map[int]int{} 13 | for _, n := range nums { 14 | countMap[n]++ 15 | } 16 | for n, count := range countMap { 17 | if count == 1 { 18 | return n 19 | } 20 | } 21 | return -1 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0137.Single Number II/README.md: -------------------------------------------------------------------------------- 1 | ## 0137. Single Number II 2 | 3 | https://leetcode.com/problems/single-number-ii 4 | 5 | Given a non-empty array of integers, every element appears three times except for one, which appears exactly once. Find that single one. 6 | 7 | Note: 8 | 9 | Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 10 | 11 | Example 1: 12 | 13 | Input: [2,2,3,2] 14 | Output: 3 15 | 16 | Example 2: 17 | 18 | Input: [0,1,0,1,0,1,99] 19 | Output: 99 20 | -------------------------------------------------------------------------------- /algorithms/0137.Single Number II/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { singleNumber }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {number} 8 | */ 9 | function singleNumber(nums) { 10 | const map = new Map(); 11 | for (const num of nums) { 12 | map.set(num, ~~map.get(num) + 1); 13 | } 14 | const [result] = Array.from(map).find(([num, count]) => count === 1); 15 | return result; 16 | } 17 | -------------------------------------------------------------------------------- /algorithms/0141.Linked List Cycle/README.md: -------------------------------------------------------------------------------- 1 | ## 141. Linked List Cycle 2 | 3 | https://leetcode.com/problems/linked-list-cycle/#/description 4 | 5 | Given a linked list, determine if it has a cycle in it. 6 | 7 | Follow up: 8 | Can you solve it without using extra space? 9 | -------------------------------------------------------------------------------- /algorithms/0148.Sort List/README.md: -------------------------------------------------------------------------------- 1 | ## 0148. Sort List 2 | 3 | https://leetcode.com/problems/sort-list 4 | 5 | Sort a linked list in O(n log n) time using constant space complexity. 6 | 7 | Example 1: 8 | 9 | Input: 4->2->1->3 10 | Output: 1->2->3->4 11 | 12 | Example 2: 13 | 14 | Input: -1->5->3->4->0 15 | Output: -1->0->3->4->5 16 | -------------------------------------------------------------------------------- /algorithms/0151.Reverse Words in a String/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { reverseWords }; 4 | 5 | /** 6 | * @param {string} s 7 | * @return {string} 8 | */ 9 | function reverseWords(s) { 10 | return s 11 | .split(' ') 12 | .filter(str => str) 13 | .reverse() 14 | .join(' '); 15 | } 16 | -------------------------------------------------------------------------------- /algorithms/0151.Reverse Words in a String/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { reverseWords } = require('./'); 7 | 8 | describe('#reverseWords', () => { 9 | const tests = [ 10 | { 11 | s: 'the sky is blue', 12 | result: 'blue is sky the', 13 | }, 14 | ]; 15 | 16 | _.forEach(tests, ({ s, result }) => { 17 | it(`${s} -> ${result}`, () => { 18 | assert.deepStrictEqual(reverseWords(s), result); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /algorithms/0152.Maximum Product Subarray/README.md: -------------------------------------------------------------------------------- 1 | ## 152. Maximum Product Subarray 2 | 3 | https://leetcode.com/problems/maximum-product-subarray 4 | 5 | Given an integer array nums, find the contiguous subarray within an array (containing at least one number) which has the largest product. 6 | 7 | Example 1: 8 | 9 | Input: [2,3,-2,4] 10 | Output: 6 11 | Explanation: [2,3] has the largest product 6. 12 | 13 | Example 2: 14 | 15 | Input: [-2,0,-1] 16 | Output: 0 17 | Explanation: The result cannot be 2, because [-2,-1] is not a subarray. 18 | -------------------------------------------------------------------------------- /algorithms/0153.Find Minimum in Rotated Sorted Array/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { findMin }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {number} 8 | */ 9 | function findMin(nums) { 10 | let l = 0; 11 | let r = nums.length; 12 | const last = nums[r - 1]; 13 | while (l < r) { 14 | const m = (l + (r - l) / 2) | 0; 15 | if (nums[m] > last) { 16 | l = m + 1; 17 | } else { 18 | r = m; 19 | } 20 | } 21 | return nums[l]; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0154.Find Minimum in Rotated Sorted Array II/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { findMin }; 4 | 5 | /** 6 | * 7 | * @param {number[]} nums 8 | * @return {number} 9 | */ 10 | function findMin(nums) { 11 | let l = 0; 12 | let r = nums.length; 13 | while (l < r) { 14 | const m = (l + (r - l) / 2) | 0; 15 | if (nums[m] > nums[r]) { 16 | l = m + 1; 17 | } else if (nums[m] < nums[r]) { 18 | r = m; 19 | } else { 20 | r--; 21 | } 22 | } 23 | return nums[l]; 24 | } 25 | -------------------------------------------------------------------------------- /algorithms/0162.Find Peak Element/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { findPeakElement }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {number} 8 | */ 9 | function findPeakElement(nums) { 10 | let l = 0; 11 | let r = nums.length; 12 | while (l < r) { 13 | const m = (l + (r - l) / 2) | 0; 14 | if (nums[m] <= nums[m + 1]) { 15 | l = m + 1; 16 | } else { 17 | r = m; 18 | } 19 | } 20 | return l; 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/0167.Two Sum II - Input array is sorted/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func twoSum(numbers []int, target int) []int { 4 | l, r := 0, len(numbers)-1 5 | for { 6 | if l == r { 7 | return []int{0, 0} 8 | } 9 | sum := numbers[l] + numbers[r] 10 | if sum == target { 11 | return []int{l + 1, r + 1} 12 | } 13 | if sum < target { 14 | l++ 15 | } else { 16 | r-- 17 | } 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0167.Two Sum II - Input array is sorted/solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def twoSum(self, numbers: List[int], target: int) -> List[int]: 3 | left, right = 0, len(numbers) - 1 4 | while left < right: 5 | sum = numbers[left] + numbers[right] 6 | if sum == target: 7 | return [left + 1, right + 1] 8 | if sum < target: 9 | left += 1 10 | else: 11 | right -= 1 12 | return [0, 0] 13 | -------------------------------------------------------------------------------- /algorithms/0168.Excel Sheet Column Title/README.md: -------------------------------------------------------------------------------- 1 | ## 168. Excel Sheet Column Title 2 | 3 | https://leetcode.com/problems/excel-sheet-column-title/description/ 4 | 5 | Given a positive integer, return its corresponding column title as appear in an Excel sheet. 6 | 7 | For example: 8 | 9 | ``` 10 | 1 -> A 11 | 2 -> B 12 | 3 -> C 13 | ... 14 | 26 -> Z 15 | 27 -> AA 16 | 28 -> AB 17 | ``` 18 | -------------------------------------------------------------------------------- /algorithms/0168.Excel Sheet Column Title/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { convertToTitle }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {string} 8 | */ 9 | function convertToTitle(n) { 10 | let str = ''; 11 | while (n > 0) { 12 | const charCode = n % 26 || 26; 13 | str = String.fromCharCode(64 + charCode) + str; 14 | n -= charCode; 15 | n /= 26; 16 | } 17 | return str; 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/0168.Excel Sheet Column Title/solution.go: -------------------------------------------------------------------------------- 1 | func convertToTitle(n int) string { 2 | chars := []byte{} 3 | for n > 0 { 4 | charCode := n % 26 5 | if charCode == 0 { 6 | charCode = 26 7 | } 8 | char := byte('@'+charCode) 9 | if len(chars) == 0 { 10 | chars = append(chars, char) 11 | } else { 12 | chars, chars[0] = append(chars[:1], chars[0:]...), char 13 | } 14 | n -= charCode 15 | n /= 26 16 | } 17 | return string(chars) 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/0169.Majority Element/README.md: -------------------------------------------------------------------------------- 1 | ## 169. Majority Element 2 | 3 | https://leetcode.com/problems/majority-element/description/ 4 | 5 | Given an array of size n, find the majority element. The majority element is the element that appears more than ⌊ n/2 ⌋ times. 6 | 7 | You may assume that the array is non-empty and the majority element always exist in the array. 8 | -------------------------------------------------------------------------------- /algorithms/0171.Excel Sheet Column Number/README.md: -------------------------------------------------------------------------------- 1 | ## 171. Excel Sheet Column Number 2 | 3 | https://leetcode.com/problems/excel-sheet-column-number/description/ 4 | 5 | Related to question Excel Sheet Column Title 6 | 7 | Given a column title as appear in an Excel sheet, return its corresponding column number. 8 | 9 | For example: 10 | 11 | ``` 12 | A -> 1 13 | B -> 2 14 | C -> 3 15 | ... 16 | Z -> 26 17 | AA -> 27 18 | AB -> 28 19 | ``` 20 | -------------------------------------------------------------------------------- /algorithms/0171.Excel Sheet Column Number/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { titleToNumber }; 4 | 5 | /** 6 | * @param {string} s 7 | * @return {number} 8 | */ 9 | function titleToNumber(s) { 10 | let i = -1; 11 | let result = 0; 12 | const l = s.length; 13 | while (++i < l) { 14 | const n = s[i].charCodeAt() - 64; 15 | result += n * 26 ** (l - i - 1); 16 | } 17 | return result; 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/0171.Excel Sheet Column Number/solution.rb: -------------------------------------------------------------------------------- 1 | # @param {String} s 2 | # @return {Integer} 3 | def title_to_number(s) 4 | index = s.length 5 | result = 0 6 | s.each_byte do |c| 7 | index -= 1 8 | val = c - 64 9 | if index != 0 then 10 | val *= 26 ** index 11 | end 12 | result += val 13 | end 14 | return result 15 | end 16 | -------------------------------------------------------------------------------- /algorithms/0172.Factorial Trailing Zeroes/README.md: -------------------------------------------------------------------------------- 1 | ## 172. Factorial Trailing Zeroes 2 | 3 | https://leetcode.com/problems/factorial-trailing-zeroes/description/ 4 | 5 | Given an integer n, return the number of trailing zeroes in n!. 6 | 7 | Note: Your solution should be in logarithmic time complexity. 8 | -------------------------------------------------------------------------------- /algorithms/0172.Factorial Trailing Zeroes/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { trailingZeroes, trailingZeroes2 }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {number} 8 | */ 9 | function trailingZeroes(n) { 10 | let result = 0; 11 | while (n > 0) { 12 | n = (n / 5) | 0; 13 | result += n; 14 | } 15 | return result; 16 | } 17 | 18 | /** 19 | * @param {number} n 20 | * @return {number} 21 | */ 22 | function trailingZeroes2(n) { 23 | n = (n / 5) | 0; 24 | return n === 0 ? 0 : n + trailingZeroes2(n); 25 | } 26 | -------------------------------------------------------------------------------- /algorithms/0172.Factorial Trailing Zeroes/solution.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer} n 2 | # @return {Integer} 3 | def trailing_zeroes(n) 4 | result = 0 5 | while n > 0 do 6 | n = n / 5 | 0 7 | result += n 8 | end 9 | return result 10 | end 11 | -------------------------------------------------------------------------------- /algorithms/0175.Combine Two Tables/query.sql: -------------------------------------------------------------------------------- 1 | -- Write your MySQL query statement below 2 | SELECT p.FirstName, p.LastName, a.City, a.State FROM Person p LEFT JOIN Address a ON a.PersonId=p.PersonId; 3 | -------------------------------------------------------------------------------- /algorithms/0176.Second Highest Salary/query.sql: -------------------------------------------------------------------------------- 1 | -- Write your MySQL query statement below 2 | SELECT MAX(Salary) SecondHighestSalary FROM Employee WHERE Salary != (SELECT MAX(Salary) FROM Employee) ORDER BY Salary DESC; 3 | -- SELECT DISTINCT Salary SecondHighestSalary FROM Employee ORDER BY Salary DESC LIMIT 1 OFFSET 1 4 | -------------------------------------------------------------------------------- /algorithms/0179.Largest Number/README.md: -------------------------------------------------------------------------------- 1 | ## 0179. Largest Number 2 | 3 | https://leetcode.com/problems/largest-number 4 | 5 | Given a list of non negative integers, arrange them such that they form the largest number. 6 | 7 | Example 1: 8 | 9 | Input: [10,2] 10 | Output: "210" 11 | 12 | Example 2: 13 | 14 | Input: [3,30,34,5,9] 15 | Output: "9534330" 16 | 17 | Note: The result may be very large, so you need to return a string instead of an integer. 18 | -------------------------------------------------------------------------------- /algorithms/0181.Employees Earning More Than Their Managers/query.sql: -------------------------------------------------------------------------------- 1 | -- Write your MySQL query statement below 2 | SELECT e1.Name Employee FROM Employee e1 INNER JOIN Employee e2 ON e2.Id = e1.ManagerId AND e1.Salary > e2.Salary; 3 | -------------------------------------------------------------------------------- /algorithms/0182.Duplicate Emails/query.sql: -------------------------------------------------------------------------------- 1 | -- Write your MySQL query statement below 2 | SELECT Email FROM Person GROUP BY Email HAVING COUNT(Email) > 1; 3 | -------------------------------------------------------------------------------- /algorithms/0183.Customers Who Never Order/query.sql: -------------------------------------------------------------------------------- 1 | -- Write your MySQL query statement below 2 | SELECT c.Name Customers FROM Customers c LEFT JOIN Orders o ON c.Id=o.CustomerId WHERE o.Id IS NULL; 3 | SELECT Name Customers FROM Customers c WHERE Id NOT IN (SELECT CustomerId FROM Orders); 4 | -------------------------------------------------------------------------------- /algorithms/0189.Rotate Array/README.md: -------------------------------------------------------------------------------- 1 | ## 189. Rotate Array 2 | 3 | https://leetcode.com/problems/rotate-array/description/ 4 | 5 | Rotate an array of n elements to the right by k steps. 6 | 7 | For example, with n = 7 and k = 3, the array [1,2,3,4,5,6,7] is rotated to [5,6,7,1,2,3,4]. 8 | 9 | Note: 10 | Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. 11 | 12 | [show hint] 13 | 14 | Related problem: Reverse Words in a String II 15 | -------------------------------------------------------------------------------- /algorithms/0189.Rotate Array/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func rotate(nums []int, k int) { 4 | var count, start int 5 | size := len(nums) 6 | for count < size { 7 | from := start 8 | prev := nums[from] 9 | for { 10 | to := (from + k) % size 11 | nums[to], prev, from = prev, nums[to], to 12 | count++ 13 | if count >= size || start == from { 14 | break 15 | } 16 | } 17 | start++ 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0190.Reverse Bits/README.md: -------------------------------------------------------------------------------- 1 | ## 190. Reverse Bits 2 | 3 | https://leetcode.com/problems/reverse-bits/description/ 4 | 5 | Reverse bits of a given 32 bits unsigned integer. 6 | 7 | For example, given input 43261596 (represented in binary as 00000010100101000001111010011100), return 964176192 (represented in binary as 00111001011110000010100101000000). 8 | 9 | Follow up: 10 | If this function is called many times, how would you optimize it? 11 | 12 | Related problem: Reverse Integer 13 | -------------------------------------------------------------------------------- /algorithms/0190.Reverse Bits/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need treat n as an unsigned value 3 | public int reverseBits(int n) { 4 | int result = 0; 5 | for (int i = 0; i < 32; i++) { 6 | result <<= 1; 7 | result |= n & 1; 8 | n >>= 1; 9 | } 10 | return result; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /algorithms/0191.Number of 1 Bits/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "number-of-1-bits" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "number_of_1_bits" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0191.Number of 1 Bits/README.md: -------------------------------------------------------------------------------- 1 | ## 191. Number of 1 Bits 2 | 3 | https://leetcode.com/problems/number-of-1-bits/description/ 4 | 5 | Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also known as the Hamming weight). 6 | 7 | For example, the 32-bit integer ’11' has binary representation 00000000000000000000000000001011, so the function should return 3. 8 | -------------------------------------------------------------------------------- /algorithms/0191.Number of 1 Bits/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { hammingWeight }; 4 | 5 | /** 6 | * @param {number} n - a positive integer 7 | * @return {number} 8 | */ 9 | function hammingWeight(n) { 10 | let count = 0; 11 | while (n > 0) { 12 | n & 1 && count++; 13 | n >>>= 1; 14 | } 15 | return count; 16 | } 17 | -------------------------------------------------------------------------------- /algorithms/0193.Valid Phone Numbers/file.txt: -------------------------------------------------------------------------------- 1 | 987-123-4567 2 | 123 456 7890 3 | (123) 456-7890 4 | (001) 345-00001 5 | -------------------------------------------------------------------------------- /algorithms/0193.Valid Phone Numbers/solution.bash: -------------------------------------------------------------------------------- 1 | grep -E '^[0-9]{3}-[0-9]{3}-[0-9]{4}$|^\([0-9]{3}\) [0-9]{3}-[0-9]{4}$' ${1} 2 | -------------------------------------------------------------------------------- /algorithms/0195.Tenth Line/README.md: -------------------------------------------------------------------------------- 1 | ## 195. Tenth Line 2 | 3 | https://leetcode.com/problems/tenth-line/description/ 4 | 5 | How would you print just the 10th line of a file? 6 | 7 | For example, assume that file.txt has the following content: 8 | 9 | ``` 10 | Line 1 11 | Line 2 12 | Line 3 13 | Line 4 14 | Line 5 15 | Line 6 16 | Line 7 17 | Line 8 18 | Line 9 19 | Line 10 20 | ``` 21 | 22 | Your script should output the tenth line, which is: 23 | 24 | ``` 25 | Line 10 26 | ``` 27 | -------------------------------------------------------------------------------- /algorithms/0195.Tenth Line/file.txt: -------------------------------------------------------------------------------- 1 | Line 1 2 | Line 2 3 | Line 3 4 | Line 4 5 | Line 5 6 | Line 6 7 | Line 7 8 | Line 8 9 | Line 9 10 | Line 10 11 | -------------------------------------------------------------------------------- /algorithms/0195.Tenth Line/solution.bash: -------------------------------------------------------------------------------- 1 | awk 'NR==10' ${1} 2 | -------------------------------------------------------------------------------- /algorithms/0196.Delete Duplicate Emails/query.sql: -------------------------------------------------------------------------------- 1 | DELETE p1 FROM Person p1, Person p2 WHERE p1.Email=p2.Email AND p1.Id > p2.Id; 2 | -------------------------------------------------------------------------------- /algorithms/0197.Rising Temperature/query.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | SELECT w1.Id FROM Weather w1, Weather w2 WHERE w1.Temperature > w2.Temperature AND DATEDIFF(w1.RecordDate, w2.RecordDate)=1; 3 | -------------------------------------------------------------------------------- /algorithms/0199.Binary Tree Right Side View/README.md: -------------------------------------------------------------------------------- 1 | ## 0199. Binary Tree Right Side View 2 | 3 | https://leetcode.com/problems/binary-tree-right-side-view 4 | 5 | Given a binary tree, imagine yourself standing on the right side of it, return the values of the nodes you can see ordered from top to bottom. 6 | 7 | Example: 8 | 9 | Input:[1,2,3,null,5,null,4] 10 | Output:[1, 3, 4] 11 | Explanation: 12 | 13 | 1 <--- 14 | / \ 15 | 2 3 <--- 16 | \ \ 17 | 5 4 <--- 18 | -------------------------------------------------------------------------------- /algorithms/0201.Bitwise AND of Numbers Range/README.md: -------------------------------------------------------------------------------- 1 | ## 0201. Bitwise AND of Numbers Range 2 | 3 | https://leetcode.com/problems/bitwise-and-of-numbers-range 4 | 5 | Given a range [m, n] where 0 <= m <= n <= 2147483647, return the bitwise AND of all numbers in this range, inclusive. 6 | 7 | Example 1: 8 | 9 | Input: [5,7] 10 | Output: 4 11 | 12 | Example 2: 13 | 14 | Input: [0,1] 15 | Output: 0 16 | -------------------------------------------------------------------------------- /algorithms/0201.Bitwise AND of Numbers Range/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { rangeBitwiseAnd }; 4 | 5 | /** 6 | * @param {number} m 7 | * @param {number} n 8 | * @return {number} 9 | */ 10 | function rangeBitwiseAnd(m, n) { 11 | const bit = ~(m ^ n); 12 | let digit = 1 << (Math.log2(n) | 0); 13 | let result = 0; 14 | while (digit > 0 && (bit & digit) === digit) { 15 | result |= digit & n; 16 | digit >>= 1; 17 | } 18 | return result; 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0202.Happy Number/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func isHappy(n int) bool { 4 | set := map[int]bool{} 5 | for !set[n] { 6 | set[n] = true 7 | var next int 8 | for ; n > 0; n /= 10 { 9 | rem := n % 10 10 | next += rem * rem 11 | } 12 | n = next 13 | } 14 | return n == 1 15 | } 16 | -------------------------------------------------------------------------------- /algorithms/0202.Happy Number/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { isHappy } = require('./'); 6 | 7 | describe('#isHappy', () => { 8 | const tests = [ 9 | { 10 | n: 19, 11 | result: true, 12 | }, 13 | { 14 | n: 321321, 15 | result: true, 16 | }, 17 | ]; 18 | 19 | _.forEach(tests, ({ n, result }) => { 20 | it(`${n} -> ${result}`, () => { 21 | assert.strictEqual(isHappy(n), result); 22 | }); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /algorithms/0203.Remove Linked List Elements/README.md: -------------------------------------------------------------------------------- 1 | ## 203. Remove Linked List Elements 2 | 3 | https://leetcode.com/problems/remove-linked-list-elements/description/ 4 | 5 | Remove all elements from a linked list of integers that have value val. 6 | 7 | Example 8 | Given: 1 --> 2 --> 6 --> 3 --> 4 --> 5 --> 6, val = 6 9 | Return: 1 --> 2 --> 3 --> 4 --> 5 10 | -------------------------------------------------------------------------------- /algorithms/0204.Count Primes/README.md: -------------------------------------------------------------------------------- 1 | ## 204. Count Primes 2 | 3 | https://leetcode.com/problems/count-primes/ 4 | 5 | Description: 6 | 7 | Count the number of prime numbers less than a non-negative number, n. 8 | 9 | Credits: 10 | Special thanks to @mithmatt for adding this problem and creating all test cases. 11 | -------------------------------------------------------------------------------- /algorithms/0204.Count Primes/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { countPrimes }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {number} 8 | */ 9 | function countPrimes(n) { 10 | const nums = Array(n); 11 | let count = n < 3 ? 0 : 1; 12 | for (let i = 3; i < n; i += 2) { 13 | if (nums[i]) { 14 | continue; 15 | } 16 | count++; 17 | for (let j = i * i; j < n; j += i) { 18 | nums[j] = true; 19 | } 20 | } 21 | return count; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0205.Isomorphic Strings/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { isIsomorphic }; 4 | 5 | /** 6 | * @param {string} s 7 | * @param {string} t 8 | * @return {boolean} 9 | */ 10 | function isIsomorphic(s, t) { 11 | const ms = {}; 12 | const mt = {}; 13 | for (let i = 0; i < s.length; i++) { 14 | const cs = s[i]; 15 | const ct = t[i]; 16 | if (ms[cs] !== mt[ct]) { 17 | return false; 18 | } 19 | ms[cs] = mt[ct] = i; 20 | } 21 | return true; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0206.Reverse Linked List/README.md: -------------------------------------------------------------------------------- 1 | ## 206. Reverse Linked List 2 | 3 | https://leetcode.com/problems/reverse-linked-list/description/ 4 | 5 | Reverse a singly linked list. 6 | -------------------------------------------------------------------------------- /algorithms/0206.Reverse Linked List/solution.rb: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode 3 | # attr_accessor :val, :next 4 | # def initialize(val) 5 | # @val = val 6 | # @next = nil 7 | # end 8 | # end 9 | 10 | # @param {ListNode} head 11 | # @return {ListNode} 12 | def reverse_list(head) 13 | prev = nil 14 | current = head 15 | while current do 16 | node = current.next 17 | current.next = prev 18 | prev = current 19 | current = node 20 | end 21 | return prev 22 | end 23 | -------------------------------------------------------------------------------- /algorithms/0215.Kth Largest Element in an Array/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "kth-largest-element-in-an-array" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "kth_largest_element_in_an_array" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0215.Kth Largest Element in an Array/README.md: -------------------------------------------------------------------------------- 1 | ## 0215. Kth Largest Element in an Array 2 | 3 | https://leetcode.com/problems/kth-largest-element-in-an-array 4 | 5 | Find the kth largest element in an unsorted array. Note that it is the kth largest element in the sorted order, not the kth distinct element. 6 | 7 | Example 1: 8 | 9 | Input: [3,2,1,5,6,4] and k = 2 10 | Output: 5 11 | 12 | Example 2: 13 | 14 | Input: [3,2,3,1,2,4,5,5,6] and k = 4 15 | Output: 4 16 | 17 | Note: 18 | You may assume k is always valid, 1 ≤ k ≤ array's length. 19 | -------------------------------------------------------------------------------- /algorithms/0217.Contains Duplicate/README.md: -------------------------------------------------------------------------------- 1 | ## 217. Contains Duplicate 2 | 3 | https://leetcode.com/problems/contains-duplicate/description/ 4 | 5 | Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct. 6 | -------------------------------------------------------------------------------- /algorithms/0217.Contains Duplicate/solution.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @return {Boolean} 3 | def contains_duplicate(nums) 4 | map = {} 5 | nums.each do |n| 6 | if map[n] then 7 | return true 8 | end 9 | map[n] = true 10 | end 11 | return false 12 | end 13 | -------------------------------------------------------------------------------- /algorithms/0217.Contains Duplicate/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { containsDuplicate } = require('./'); 6 | 7 | describe('#containsDuplicate', () => { 8 | const tests = [ 9 | { 10 | nums: [1, 2, 2, 3], 11 | result: true, 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ nums, result }) => { 16 | it(`[${nums}] -> ${result}`, () => { 17 | assert.strictEqual(containsDuplicate(nums), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/0219.Contains Duplicate II/README.md: -------------------------------------------------------------------------------- 1 | ## 219. Contains Duplicate II 2 | 3 | https://leetcode.com/problems/contains-duplicate-ii/description/ 4 | 5 | Given an array of integers and an integer k, find out whether there are two distinct indices i and j in the array such that nums[i] = nums[j] and the absolute difference between i and j is at most k. 6 | -------------------------------------------------------------------------------- /algorithms/0219.Contains Duplicate II/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { containsNearbyDuplicate }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @param {number} k 8 | * @return {boolean} 9 | */ 10 | function containsNearbyDuplicate(nums, k) { 11 | const map = {}; 12 | let i = nums.length; 13 | while (i--) { 14 | const n = nums[i]; 15 | const j = map[n]; 16 | if (j && j - i <= k) { 17 | return true; 18 | } 19 | map[n] = i; 20 | } 21 | return false; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0219.Contains Duplicate II/solution.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @param {Integer} k 3 | # @return {Boolean} 4 | def contains_nearby_duplicate(nums, k) 5 | map = {} 6 | nums.each_with_index do |n, i| 7 | t = map[n] 8 | if t && i - t <= k then 9 | return true 10 | end 11 | map[n] = i 12 | end 13 | return false 14 | end 15 | -------------------------------------------------------------------------------- /algorithms/0221.Maximal Square/README.md: -------------------------------------------------------------------------------- 1 | ## 0221. Maximal Square 2 | 3 | https://leetcode.com/problems/maximal-square 4 | 5 | Given a 2D binary matrix filled with 0's and 1's, find the largest square containing only 1's and return its area. 6 | 7 | Example: 8 | 9 | Input: 10 | 11 | 1 0 1 0 0 12 | 1 0 1 1 1 13 | 1 1 1 1 1 14 | 1 0 0 1 0 15 | 16 | Output: 4 17 | -------------------------------------------------------------------------------- /algorithms/0225.Implement Stack using Queues/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const { MyStack } = require('./'); 5 | 6 | describe('#MyStack', () => { 7 | it('should work', () => { 8 | const stack = new MyStack(); 9 | assert.strictEqual(stack.empty(), true); 10 | assert.strictEqual(stack.push(1), undefined); 11 | assert.strictEqual(stack.pop(), 1); 12 | assert.strictEqual(stack.empty(), true); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /algorithms/0226.Invert Binary Tree/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { invertTree }; 4 | 5 | /** 6 | * Definition for a binary tree node. 7 | * function TreeNode(val) { 8 | * this.val = val; 9 | * this.left = this.right = null; 10 | * } 11 | */ 12 | /** 13 | * @param {TreeNode} node 14 | * @return {TreeNode} 15 | */ 16 | function invertTree(node) { 17 | if (!node) { 18 | return null; 19 | } 20 | [node.right, node.left] = [invertTree(node.left), invertTree(node.right)]; 21 | return node; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0226.Invert Binary Tree/solution.rb: -------------------------------------------------------------------------------- 1 | # Definition for a binary tree node. 2 | # class TreeNode 3 | # attr_accessor :val, :left, :right 4 | # def initialize(val) 5 | # @val = val 6 | # @left, @right = nil, nil 7 | # end 8 | # end 9 | 10 | # @param {TreeNode} root 11 | # @return {TreeNode} 12 | def invert_tree(root) 13 | if !root then 14 | return 15 | end 16 | left = root.left 17 | right = root.right 18 | root.left = invert_tree(right) 19 | root.right = invert_tree(left) 20 | return root 21 | end 22 | -------------------------------------------------------------------------------- /algorithms/0228.Summary Ranges/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { summaryRanges }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {string[]} 8 | */ 9 | function summaryRanges(nums) { 10 | let left = 0; 11 | const result = []; 12 | for (let i = 1; i <= nums.length; i++) { 13 | const diff = i - left; 14 | if (nums[i] - nums[left] === diff) { 15 | continue; 16 | } 17 | result.push(diff === 1 ? `${nums[left]}` : `${nums[left]}->${nums[i - 1]}`); 18 | left = i; 19 | } 20 | return result; 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/0231.Power of Two/README.md: -------------------------------------------------------------------------------- 1 | ## 231. Power of Two 2 | 3 | https://leetcode.com/problems/power-of-two/description/ 4 | 5 | Given an integer, write a function to determine if it is a power of two. 6 | -------------------------------------------------------------------------------- /algorithms/0231.Power of Two/solution.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer} n 2 | # @return {Boolean} 3 | def is_power_of_two(n) 4 | return n > 0 && n & n - 1 === 0 5 | end 6 | -------------------------------------------------------------------------------- /algorithms/0232.Implement Queue using Stacks/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const { MyQueue } = require('./'); 5 | 6 | describe('#MyQueue', () => { 7 | it('should work', () => { 8 | const queue = new MyQueue(); 9 | assert.strictEqual(queue.empty(), true); 10 | assert.strictEqual(queue.push(1), undefined); 11 | assert.strictEqual(queue.push(2), undefined); 12 | assert.strictEqual(queue.peek(), 1); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /algorithms/0234.Palindrome Linked List/README.md: -------------------------------------------------------------------------------- 1 | ## 234. Palindrome Linked List 2 | 3 | https://leetcode.com/problems/palindrome-linked-list/description/ 4 | 5 | Given a singly linked list, determine if it is a palindrome. 6 | 7 | Follow up: 8 | Could you do it in O(n) time and O(1) space? 9 | -------------------------------------------------------------------------------- /algorithms/0237.Delete Node in a Linked List/README.md: -------------------------------------------------------------------------------- 1 | ## 237. Delete Node in a Linked List 2 | 3 | https://leetcode.com/problems/delete-node-in-a-linked-list/description/ 4 | 5 | Write a function to delete a node (except the tail) in a singly linked list, given only access to that node. 6 | 7 | Supposed the linked list is 1 -> 2 -> 3 -> 4 and you are given the third node with value 3, the linked list should become 1 -> 2 -> 4 after calling your function. 8 | -------------------------------------------------------------------------------- /algorithms/0238.Product of Array Except Self/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func productExceptSelf(nums []int) []int { 4 | size := len(nums) 5 | result := make([]int, size) 6 | for i := 0; i < size; i++ { 7 | result[i] = 1 8 | } 9 | left := 1 10 | right := 1 11 | for l := 0; l < size; l++ { 12 | r := size - l - 1 13 | result[l] *= left 14 | result[r] *= right 15 | left *= nums[l] 16 | right *= nums[r] 17 | } 18 | return result 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0239.Sliding Window Maximum/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { maxSlidingWindow }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @param {number} k 8 | * @return {number[]} 9 | */ 10 | function maxSlidingWindow(nums, k) { 11 | const max = [...nums]; 12 | for (let i = 1; i < nums.length; i++) { 13 | const m1 = max[i - 1] !== nums[i - k] ? max[i - 1] : Math.max(...nums.slice(i - k + 1, i)); 14 | max[i] = Math.max(max[i], m1); 15 | } 16 | return max.slice(k - 1); 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0240.Search a 2D Matrix II/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "search-a-2-d-matrix-ii" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "search_a_2_d_matrix_ii" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0242.Valid Anagram/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "valid-anagram" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "valid_anagram" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0242.Valid Anagram/README.md: -------------------------------------------------------------------------------- 1 | ## 242. Valid Anagram 2 | 3 | https://leetcode.com/problems/valid-anagram/description/ 4 | 5 | Given two strings s and t, write a function to determine if t is an anagram of s. 6 | 7 | For example, 8 | s = "anagram", t = "nagaram", return true. 9 | s = "rat", t = "car", return false. 10 | 11 | Note: 12 | You may assume the string contains only lowercase alphabets. 13 | 14 | Follow up: 15 | What if the inputs contain unicode characters? How would you adapt your solution to such case? 16 | -------------------------------------------------------------------------------- /algorithms/0242.Valid Anagram/solution.rb: -------------------------------------------------------------------------------- 1 | # @param {String} s 2 | # @param {String} t 3 | # @return {Boolean} 4 | def is_anagram(s, t) 5 | if s.length != t.length then 6 | return false 7 | end 8 | map = {} 9 | s.each_char do |c| 10 | map[c] = map[c] || 0 11 | map[c] += 1 12 | end 13 | t.each_char do |c| 14 | if !map[c] || map[c] == 0 then 15 | return false 16 | end 17 | map[c] -= 1 18 | end 19 | return true 20 | end 21 | -------------------------------------------------------------------------------- /algorithms/0257.Binary Tree Paths/README.md: -------------------------------------------------------------------------------- 1 | ## 257. Binary Tree Paths 2 | 3 | Given a binary tree, return all root-to-leaf paths. 4 | 5 | For example, given the following binary tree: 6 | 7 | 1 8 | / \ 9 | 2 3 10 | \ 11 | 5 12 | All root-to-leaf paths are: 13 | 14 | ["1->2->5", "1->3"] 15 | -------------------------------------------------------------------------------- /algorithms/0258.Add Digits/README.md: -------------------------------------------------------------------------------- 1 | ## 258. Add Digits 2 | 3 | https://leetcode.com/problems/add-digits/description/ 4 | 5 | Given a non-negative integer num, repeatedly add all its digits until the result has only one digit. 6 | 7 | For example: 8 | 9 | Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it. 10 | 11 | Follow up: 12 | Could you do it without any loop/recursion in O(1) runtime? 13 | -------------------------------------------------------------------------------- /algorithms/0258.Add Digits/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { addDigits } = require('./'); 6 | 7 | describe('#addDigits', () => { 8 | const tests = [ 9 | { 10 | nums: 38, 11 | result: 2, 12 | }, 13 | { 14 | nums: 121, 15 | result: 4, 16 | }, 17 | ]; 18 | 19 | _.forEach(tests, ({ nums, result }) => { 20 | it(`${nums} -> ${result}`, () => { 21 | assert.strictEqual(addDigits(nums), result); 22 | }); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /algorithms/0260.Single Number III/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { singleNumber }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {number[]} 8 | */ 9 | function singleNumber(nums) { 10 | let bit = 0; 11 | for (const num of nums) { 12 | bit ^= num; 13 | } 14 | bit &= -bit; 15 | 16 | let num1 = 0; 17 | let num2 = 0; 18 | for (const num of nums) { 19 | if ((num & bit) === 0) { 20 | num1 ^= num; 21 | } else { 22 | num2 ^= num; 23 | } 24 | } 25 | return [num1, num2]; 26 | } 27 | -------------------------------------------------------------------------------- /algorithms/0260.Single Number III/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { singleNumber } = require('./'); 7 | 8 | describe('#singleNumber', () => { 9 | const tests = [ 10 | { 11 | nums: [1, 2, 1, 3, 2, 5], 12 | result: [5, 3], 13 | }, 14 | ]; 15 | 16 | _.forEach(tests, ({ nums, result }) => { 17 | it(`${nums} -> ${result}`, () => { 18 | assert.deepStrictEqual(singleNumber(nums), result); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /algorithms/0263.Ugly Number/README.md: -------------------------------------------------------------------------------- 1 | ## 263. Ugly Number 2 | 3 | https://leetcode.com/problems/ugly-number/description/ 4 | 5 | Write a program to check whether a given number is an ugly number. 6 | 7 | Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. For example, 6, 8 are ugly while 14 is not ugly since it includes another prime factor 7. 8 | 9 | Note that 1 is typically treated as an ugly number. 10 | -------------------------------------------------------------------------------- /algorithms/0263.Ugly Number/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { isUgly }; 4 | 5 | /** 6 | * @param {number} num 7 | * @return {boolean} 8 | */ 9 | function isUgly(num) { 10 | if (num <= 0) { 11 | return false; 12 | } 13 | const factors = [2, 3, 5]; 14 | let l = factors.length; 15 | while (l--) { 16 | const f = factors[l]; 17 | while (num % f === 0) { 18 | num /= f; 19 | } 20 | } 21 | return num === 1; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0264.Ugly Number II/README.md: -------------------------------------------------------------------------------- 1 | ## 0264. Ugly Number II 2 | 3 | https://leetcode.com/problems/ugly-number-ii 4 | 5 | Write a program to find the n-th ugly number. 6 | 7 | Ugly numbers are positive numbers whose prime factors only include 2, 3, 5. 8 | 9 | Example: 10 | 11 | Input: n = 10 12 | Output: 12 13 | Explanation: 1, 2, 3, 4, 5, 6, 8, 9, 10, 12 is the sequence of the first 10 ugly numbers. 14 | 15 | Note: 16 | 17 | 1 is typically treated as an ugly number. 18 | n does not exceed 1690. 19 | -------------------------------------------------------------------------------- /algorithms/0264.Ugly Number II/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { nthUglyNumber } = require('./'); 7 | 8 | describe('#nthUglyNumber', () => { 9 | const tests = [ 10 | { 11 | n: 10, 12 | result: 12, 13 | }, 14 | ]; 15 | 16 | _.forEach(tests, ({ n, result }) => { 17 | it(`${n} -> ${result}`, () => { 18 | assert.deepStrictEqual(nthUglyNumber(n), result); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /algorithms/0268.Missing Number/README.md: -------------------------------------------------------------------------------- 1 | ## 268. Missing Number 2 | 3 | https://leetcode.com/problems/missing-number/description/ 4 | 5 | Given an array containing n distinct numbers taken from 0, 1, 2, ..., n, find the one that is missing from the array. 6 | 7 | For example, 8 | Given nums = [0, 1, 3] return 2. 9 | 10 | Note: 11 | Your algorithm should run in linear runtime complexity. Could you implement it using only constant extra space complexity? 12 | -------------------------------------------------------------------------------- /algorithms/0278.First Bad Version/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | const { solution } = require('./'); 6 | 7 | describe('#solution', () => { 8 | it('should work', () => { 9 | const isBadVersion = (n) => n > 10; 10 | assert.strictEqual(solution(isBadVersion)(20), 11); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /algorithms/0279.Perfect Squares/README.md: -------------------------------------------------------------------------------- 1 | ## 0279. Perfect Squares 2 | 3 | https://leetcode.com/problems/perfect-squares 4 | 5 | Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which sum to n. 6 | 7 | Example 1: 8 | 9 | Input: n = 12 10 | Output: 3 11 | Explanation: 12 = 4 + 4 + 4. 12 | 13 | Example 2: 14 | 15 | Input: n = 13 16 | Output: 2 17 | Explanation: 13 = 4 + 9. 18 | -------------------------------------------------------------------------------- /algorithms/0279.Perfect Squares/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { numSquares }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {number} 8 | */ 9 | function numSquares(n) { 10 | const dp = Array(n + 1).fill(Infinity); 11 | dp[0] = 0; 12 | for (let i = 1; i <= n; i++) { 13 | let j = 0; 14 | while (i - (++j) ** 2 >= 0) { 15 | dp[i] = Math.min(dp[i], dp[i - j ** 2] + 1); 16 | } 17 | } 18 | return dp[n]; 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0279.Perfect Squares/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { numSquares } = require('./'); 7 | 8 | describe('#numSquares', () => { 9 | const tests = [ 10 | { 11 | n: 12, 12 | result: 3, 13 | }, 14 | { 15 | n: 13, 16 | result: 2, 17 | }, 18 | ]; 19 | 20 | _.forEach(tests, ({ n, result }) => { 21 | it(`${n} -> ${result}`, () => { 22 | assert.deepStrictEqual(numSquares(n), result); 23 | }); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /algorithms/0283.Move Zeroes/README.md: -------------------------------------------------------------------------------- 1 | ## 283. Move Zeroes 2 | 3 | https://leetcode.com/problems/move-zeroes/description/ 4 | 5 | Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements. 6 | 7 | For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0]. 8 | 9 | Note: 10 | You must do this in-place without making a copy of the array. 11 | Minimize the total number of operations. 12 | -------------------------------------------------------------------------------- /algorithms/0283.Move Zeroes/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { moveZeroes }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {void} Do not return anything, modify nums in-place instead. 8 | */ 9 | function moveZeroes(nums) { 10 | let j = 0; 11 | for (const [i, n] of nums.entries()) { 12 | if (n === 0) { 13 | continue; 14 | } 15 | [nums[i], nums[j++]] = [nums[j], n]; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0283.Move Zeroes/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func moveZeroes(nums []int) { 4 | var j int 5 | for i, n := range nums { 6 | if n == 0 { 7 | continue 8 | } 9 | nums[i], nums[j] = nums[j], n 10 | j++ 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /algorithms/0283.Move Zeroes/solution.py: -------------------------------------------------------------------------------- 1 | from typing import List 2 | 3 | class Solution: 4 | def moveZeroes(self, nums: List[int]) -> None: 5 | j: int = 0 6 | for i, num in enumerate(nums): 7 | if num == 0: 8 | continue 9 | nums[i], nums[j] = nums[j], num 10 | j += 1 11 | -------------------------------------------------------------------------------- /algorithms/0289.Game of Life/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { gameOfLife }; 4 | 5 | /** 6 | * @param {number[][]} board 7 | * @return {void} Do not return anything, modify board in-place instead. 8 | */ 9 | function gameOfLife(board) {} 10 | -------------------------------------------------------------------------------- /algorithms/0289.Game of Life/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { gameOfLife } = require('./'); 6 | 7 | describe('#gameOfLife', () => {}); 8 | -------------------------------------------------------------------------------- /algorithms/0292.Nim Game/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { canWinNim }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {boolean} 8 | */ 9 | function canWinNim(n) { 10 | return n % 4 !== 0; 11 | } 12 | -------------------------------------------------------------------------------- /algorithms/0303.Range Sum Query - Immutable/README.md: -------------------------------------------------------------------------------- 1 | ## 303. Range Sum Query - Immutable 2 | 3 | https://leetcode.com/problems/range-sum-query-immutable/description/ 4 | 5 | Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. 6 | 7 | Example: 8 | Given nums = [-2, 0, 3, -5, 2, -1] 9 | 10 | ``` 11 | sumRange(0, 2) -> 1 12 | sumRange(2, 5) -> -1 13 | sumRange(0, 5) -> -3 14 | ``` 15 | 16 | Note: 17 | You may assume that the array does not change. 18 | There are many calls to sumRange function. 19 | -------------------------------------------------------------------------------- /algorithms/0303.Range Sum Query - Immutable/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | class NumArray { 4 | /** 5 | * @param {number[]} nums 6 | */ 7 | constructor(nums) { 8 | for (let i = 1; i < nums.length; i++) { 9 | nums[i] += nums[i - 1]; 10 | } 11 | this.nums = nums; 12 | } 13 | 14 | /** 15 | * @param {number} i 16 | * @param {number} j 17 | * @return {number} 18 | */ 19 | sumRange(i, j) { 20 | return this.nums[j] - (i ? this.nums[i - 1] : 0); 21 | } 22 | } 23 | 24 | module.exports = { NumArray }; 25 | -------------------------------------------------------------------------------- /algorithms/0314.Vertical Order Traversal of a Binary Tree/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "vertical-order-traversal-of-a-binary-tree" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | utils = { path = "../utils" } 9 | 10 | [lib] 11 | name = "vertical_order_traversal_of_a_binary_tree" 12 | path = "solution.rs" 13 | -------------------------------------------------------------------------------- /algorithms/0326.Power of Three/README.md: -------------------------------------------------------------------------------- 1 | ## 326. Power of Three 2 | 3 | https://leetcode.com/problems/power-of-three/description/ 4 | 5 | Given an integer, write a function to determine if it is a power of three. 6 | 7 | Follow up: 8 | Could you do it without using any loop / recursion? 9 | -------------------------------------------------------------------------------- /algorithms/0331.Verify Preorder Serialization of a Binary Tree/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { isValidSerialization }; 4 | 5 | /** 6 | * @param {string} preorder 7 | * @return {boolean} 8 | */ 9 | function isValidSerialization(preorder) { 10 | const list = preorder.split(','); 11 | return dfs() && list.length === 0; 12 | 13 | function dfs() { 14 | if (list.length === 0) { 15 | return false; 16 | } 17 | return list.shift() === '#' || (dfs() && dfs()); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0342.Power of Four/README.md: -------------------------------------------------------------------------------- 1 | ## 342. Power of Four 2 | 3 | Given an integer (signed 32 bits), write a function to check whether it is a power of 4. 4 | 5 | Example: 6 | Given num = 16, return true. Given num = 5, return false. 7 | 8 | Follow up: Could you solve it without loops/recursion? 9 | -------------------------------------------------------------------------------- /algorithms/0342.Power of Four/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { isPowerOfFour }; 4 | 5 | /** 6 | * @param {number} num 7 | * @return {boolean} 8 | */ 9 | function isPowerOfFour(num) { 10 | return num > 0 && (num & (num - 1)) === 0 && (num & 0x55555555) === num; 11 | } 12 | -------------------------------------------------------------------------------- /algorithms/0344.Reverse String/README.md: -------------------------------------------------------------------------------- 1 | ## 344. Reverse String 2 | 3 | https://leetcode.com/problems/reverse-string/ 4 | 5 | Write a function that takes a string as input and returns the string reversed. 6 | 7 | Example: 8 | Given s = "hello", return "olleh". 9 | -------------------------------------------------------------------------------- /algorithms/0344.Reverse String/index.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {character[]} s 3 | * @return {void} Do not return anything, modify s in-place instead. 4 | */ 5 | function reverseString(s: string[]): void { 6 | const l = s.length; 7 | for (let i = 0; i < l / 2; i++) { 8 | const j = l - i - 1; 9 | [s[i], s[j]] = [s[j], s[i]]; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /algorithms/0345.Reverse Vowels of a String/README.md: -------------------------------------------------------------------------------- 1 | ## 345. Reverse Vowels of a String 2 | 3 | Write a function that takes a string as input and reverse only the vowels of a string. 4 | 5 | Example 1: 6 | Given s = "hello", return "holle". 7 | 8 | Example 2: 9 | Given s = "leetcode", return "leotcede". 10 | 11 | Note: 12 | The vowels does not include the letter "y". 13 | -------------------------------------------------------------------------------- /algorithms/0349.Intersection of Two Arrays/README.md: -------------------------------------------------------------------------------- 1 | ## 349. Intersection of Two Arrays 2 | 3 | https://leetcode.com/problems/intersection-of-two-arrays/description/ 4 | 5 | Given two arrays, write a function to compute their intersection. 6 | 7 | Example: 8 | Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2]. 9 | 10 | Note: 11 | Each element in the result must be unique. 12 | The result can be in any order. 13 | -------------------------------------------------------------------------------- /algorithms/0350.Intersection of Two Arrays II/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | const { intersect } = require('./'); 6 | 7 | describe('#intersect', () => { 8 | const tests = [ 9 | { 10 | nums1: [1, 2, 2, 1], 11 | nums2: [2, 2], 12 | result: [2, 2], 13 | }, 14 | ]; 15 | 16 | for (const { nums1, nums2, result } of tests) { 17 | it(`${nums1}, ${nums2} -> ${result}`, () => { 18 | assert.deepStrictEqual(intersect(nums1, nums2), result); 19 | }); 20 | } 21 | }); 22 | -------------------------------------------------------------------------------- /algorithms/0367.Valid Perfect Square/README.md: -------------------------------------------------------------------------------- 1 | ## 367. Valid Perfect Square 2 | 3 | https://leetcode.com/problems/valid-perfect-square/description/ 4 | 5 | Given a positive integer num, write a function which returns True if num is a perfect square else False. 6 | 7 | Note: Do not use any built-in library function such as sqrt. 8 | 9 | Example 1: 10 | 11 | ``` 12 | Input: 16 13 | Returns: True 14 | ``` 15 | 16 | Example 2: 17 | 18 | ``` 19 | Input: 14 20 | Returns: False 21 | ``` 22 | -------------------------------------------------------------------------------- /algorithms/0367.Valid Perfect Square/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | const { isPerfectSquare } = require('./'); 6 | 7 | describe('#isPerfectSquare', () => { 8 | const tests = [ 9 | { 10 | num: 16, 11 | result: true, 12 | }, 13 | { 14 | num: 14, 15 | result: false, 16 | }, 17 | ]; 18 | 19 | for (const { num, result } of tests) { 20 | it(`${num} -> ${result}`, () => { 21 | assert.strictEqual(isPerfectSquare(num), result); 22 | }); 23 | } 24 | }); 25 | -------------------------------------------------------------------------------- /algorithms/0371.Sum of Two Integers/README.md: -------------------------------------------------------------------------------- 1 | ## 371. Sum of Two Integers 2 | 3 | https://leetcode.com/problems/sum-of-two-integers/description/ 4 | 5 | Calculate the sum of two integers a and b, but you are not allowed to use the operator + and -. 6 | 7 | Example: 8 | Given a = 1 and b = 2, return 3. 9 | -------------------------------------------------------------------------------- /algorithms/0371.Sum of Two Integers/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { getSum }; 4 | 5 | /** 6 | * @param {number} a 7 | * @param {number} b 8 | * @return {number} 9 | */ 10 | function getSum(a, b) { 11 | let r = a; 12 | let n = b; 13 | while (n !== 0) { 14 | a = r; 15 | r ^= n; 16 | n = (a & n) << 1; 17 | } 18 | return r; 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0383.Ransom Note/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { canConstruct }; 4 | 5 | /** 6 | * @param {string} ransomNote 7 | * @param {string} magazine 8 | * @return {boolean} 9 | */ 10 | function canConstruct(ransomNote, magazine) { 11 | const map = new Map(); 12 | for (const c of magazine) { 13 | map.set(c, ~~map.get(c) + 1); 14 | } 15 | for (const c of ransomNote) { 16 | if (!map.get(c)) { 17 | return false; 18 | } 19 | map.set(c, map.get(c) - 1); 20 | } 21 | return true; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0383.Ransom Note/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func canConstruct(ransomNote string, magazine string) bool { 4 | var counts [26]int 5 | for _, r := range magazine { 6 | counts[r-'a']++ 7 | } 8 | for _, r := range ransomNote { 9 | if counts[r-'a'] == 0 { 10 | return false 11 | } 12 | counts[r-'a']-- 13 | } 14 | return true 15 | } 16 | -------------------------------------------------------------------------------- /algorithms/0387.First Unique Character in a String/README.md: -------------------------------------------------------------------------------- 1 | ## 387. First Unique Character in a String 2 | 3 | https://leetcode.com/problems/first-unique-character-in-a-string/description/ 4 | 5 | Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. 6 | 7 | Examples: 8 | 9 | ``` 10 | s = "leetcode" 11 | return 0. 12 | ``` 13 | 14 | ``` 15 | s = "loveleetcode", 16 | return 2. 17 | ``` 18 | 19 | Note: You may assume the string contain only lowercase letters. 20 | -------------------------------------------------------------------------------- /algorithms/0387.First Unique Character in a String/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { firstUniqChar, firstUniqChar2 }; 4 | 5 | /** 6 | * @param {string} s 7 | * @return {number} 8 | */ 9 | function firstUniqChar(s) { 10 | const chars = Array(26).fill(s.length); 11 | for (let i = 0; i < s.length; i++) { 12 | const code = s.charCodeAt(i) - 97; 13 | chars[code] = chars[code] === s.length ? i : Infinity; 14 | } 15 | const min = Math.min(...chars); 16 | return min >= s.length ? -1 : min; 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0389.Find the Difference/README.md: -------------------------------------------------------------------------------- 1 | ## 389. Find the Difference 2 | 3 | https://leetcode.com/problems/find-the-difference/description/ 4 | 5 | Given two strings s and t which consist of only lowercase letters. 6 | 7 | String t is generated by random shuffling string s and then add one more letter at a random position. 8 | 9 | Find the letter that was added in t. 10 | 11 | Example: 12 | 13 | ``` 14 | Input: 15 | s = "abcd" 16 | t = "abcde" 17 | 18 | Output: 19 | e 20 | 21 | Explanation: 22 | 'e' is the letter that was added. 23 | ``` 24 | -------------------------------------------------------------------------------- /algorithms/0389.Find the Difference/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { findTheDifference } = require('./'); 6 | 7 | describe('#findTheDifference', () => { 8 | const tests = [ 9 | { 10 | s: 'abcd', 11 | t: 'abcde', 12 | result: 'e', 13 | }, 14 | ]; 15 | 16 | _.forEach(tests, ({ s, t, result }) => { 17 | it(`${s}, ${t} -> ${result}`, () => { 18 | assert.strictEqual(findTheDifference(s, t), result); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /algorithms/0400.Nth Digit/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { findNthDigit }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {number} 8 | */ 9 | function findNthDigit(n) { 10 | let sum = 1; 11 | let digit = 0; 12 | while (true) { 13 | const num = 9 * Math.pow(10, digit++) * digit + sum; 14 | if (n < num) { 15 | break; 16 | } 17 | sum = num; 18 | } 19 | n -= sum; 20 | const pos = n % digit; 21 | sum = Math.pow(10, digit - 1) + (n - pos) / digit; 22 | sum += ''; 23 | return +sum[pos]; 24 | } 25 | -------------------------------------------------------------------------------- /algorithms/0404.Sum of Left Leaves/README.md: -------------------------------------------------------------------------------- 1 | ## 404. Sum of Left Leaves 2 | 3 | https://leetcode.com/problems/sum-of-left-leaves 4 | 5 | Find the sum of all left leaves in a given binary tree. 6 | 7 | Example: 8 | 3 9 | / \ 10 | 9 20 11 | / \ 12 | 15 7 13 | 14 | There are two left leaves in the binary tree, with values 9 and 15 respectively. Return 24. 15 | -------------------------------------------------------------------------------- /algorithms/0405.Convert a Number to Hexadecimal/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { toHex } = require('./'); 6 | 7 | describe('#toHex', () => { 8 | const tests = [ 9 | { 10 | n: 26, 11 | result: '1a', 12 | }, 13 | { 14 | n: -1, 15 | result: 'ffffffff', 16 | }, 17 | ]; 18 | 19 | _.forEach(tests, ({ n, result }) => { 20 | it(`${n} -> ${result}`, () => { 21 | assert.strictEqual(toHex(n), result); 22 | }); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /algorithms/0406.Queue Reconstruction by Height/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { reconstructQueue }; 4 | 5 | /** 6 | * @param {number[][]} people 7 | * @return {number[][]} 8 | */ 9 | function reconstructQueue(people) { 10 | people.sort(([h1, k1], [h2, k2]) => h2 - h1 || k1 - k2); 11 | const result = []; 12 | const k = 1; 13 | for (const person of people) { 14 | result.splice(person[k], 0, person); 15 | } 16 | return result; 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0412.Fizz Buzz/README.md: -------------------------------------------------------------------------------- 1 | ## 412. Fizz Buzz 2 | 3 | https://leetcode.com/problems/fizz-buzz/#/description 4 | 5 | Write a program that outputs the string representation of numbers from 1 to n. 6 | 7 | But for multiples of three it should output “Fizz” instead of the number and for the multiples of five output “Buzz”. For numbers which are multiples of both three and five output “FizzBuzz”. 8 | -------------------------------------------------------------------------------- /algorithms/0413.Arithmetic Slices/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "arithmetic-slices" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "arithmetic_slices" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0415.Add Strings/README.md: -------------------------------------------------------------------------------- 1 | ## 415. Add Strings 2 | 3 | https://leetcode.com/problems/add-strings/description/ 4 | 5 | Given two non-negative integers num1 and num2 represented as string, return the sum of num1 and num2. 6 | 7 | Note: 8 | 9 | The length of both num1 and num2 is < 5100. 10 | Both num1 and num2 contains only digits 0-9. 11 | Both num1 and num2 does not contain any leading zero. 12 | You must not use any built-in BigInteger library or convert the inputs to integer directly. 13 | -------------------------------------------------------------------------------- /algorithms/0430.Flatten a Multilevel Doubly Linked List/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { flatten } = require('./'); 7 | 8 | describe('#hammingDistance', () => { 9 | const tests = []; 10 | 11 | _.forEach(tests, ({ head, _head, result, _result }) => { 12 | it(`${_head} -> ${_result}`, () => { 13 | assert.deepStrictEqual(flatten(head), result); 14 | }); 15 | }); 16 | }); 17 | -------------------------------------------------------------------------------- /algorithms/0434.Number of Segments in a String/README.md: -------------------------------------------------------------------------------- 1 | ## 434. Number of Segments in a String 2 | 3 | https://leetcode.com/problems/number-of-segments-in-a-string 4 | 5 | Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters. 6 | 7 | Please note that the string does not contain any non-printable characters. 8 | 9 | Example: 10 | Input: "Hello, my name is John" 11 | Output: 5 12 | -------------------------------------------------------------------------------- /algorithms/0435.Non-overlapping Intervals/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { eraseOverlapIntervals }; 4 | 5 | /** 6 | * @param {number[][]} intervals 7 | * @return {number} 8 | */ 9 | function eraseOverlapIntervals(intervals) { 10 | intervals.sort(([, e1], [, e2]) => e1 - e2); 11 | let count = 0; 12 | let current = -Infinity; 13 | for (const [start, end] of intervals) { 14 | if (start < current) { 15 | count++; 16 | continue; 17 | } 18 | current = end; 19 | } 20 | return count; 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/0441.Arranging Coins/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { arrangeCoins }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {number} 8 | */ 9 | function arrangeCoins(n) { 10 | let cur = 0; 11 | while (++cur <= n) { 12 | n -= cur; 13 | } 14 | return cur - 1; 15 | } 16 | -------------------------------------------------------------------------------- /algorithms/0441.Arranging Coins/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { arrangeCoins } = require('./'); 7 | 8 | describe('#arrangeCoins', () => { 9 | const tests = [ 10 | { 11 | n: 5, 12 | result: 2, 13 | }, 14 | { 15 | n: 8, 16 | result: 3, 17 | }, 18 | ]; 19 | 20 | _.forEach(tests, ({ n, result }) => { 21 | it(`${n} -> ${result}`, () => { 22 | assert.deepStrictEqual(arrangeCoins(n), result); 23 | }); 24 | }); 25 | }); 26 | -------------------------------------------------------------------------------- /algorithms/0442.Find All Duplicates in an Array/README.md: -------------------------------------------------------------------------------- 1 | ## 0442. Find All Duplicates in an Array 2 | 3 | https://leetcode.com/problems/find-all-duplicates-in-an-array 4 | 5 | Given an array of integers, 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once. 6 | 7 | Find all the elements that appear twice in this array. 8 | 9 | Could you do it without extra space and in O(n) runtime? 10 | 11 | Example: 12 | Input: 13 | [4,3,2,7,8,2,3,1] 14 | 15 | Output: 16 | [2,3] 17 | -------------------------------------------------------------------------------- /algorithms/0442.Find All Duplicates in an Array/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { findDuplicates }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {number[]} 8 | */ 9 | function findDuplicates(nums) { 10 | const result = []; 11 | for (const n of nums) { 12 | const num = Math.abs(n); 13 | const index = num - 1; 14 | if (nums[index] < 0) { 15 | result.push(num); 16 | } else { 17 | nums[index] *= -1; 18 | } 19 | } 20 | return result; 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/0442.Find All Duplicates in an Array/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { findDuplicates } = require('./'); 7 | 8 | describe('#findDuplicates', () => { 9 | const tests = [ 10 | { 11 | nums: [4, 3, 2, 7, 8, 2, 3, 1], 12 | result: [2, 3], 13 | }, 14 | ]; 15 | 16 | _.forEach(tests, ({ nums, result }) => { 17 | it(`${nums} -> ${result}`, () => { 18 | assert.deepStrictEqual(findDuplicates(nums), result); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /algorithms/0451.Sort Characters By Frequency/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { frequencySort }; 4 | 5 | /** 6 | * @param {string} s 7 | * @return {string} 8 | */ 9 | function frequencySort(s) { 10 | const map = new Map(); 11 | for (const c of s) { 12 | map.set(c, ~~map.get(c) + 1); 13 | } 14 | return Array.from(map) 15 | .sort(([, c1], [, c2]) => c2 - c1) 16 | .map(([char, count]) => char.repeat(count)) 17 | .join(''); 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/0451.Sort Characters By Frequency/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { frequencySort } = require('./'); 6 | 7 | describe('#frequencySort', () => { 8 | const tests = [ 9 | { 10 | s: 'tree', 11 | result: 'eetr', 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ s, result }) => { 16 | it(`${s} -> ${result}`, () => { 17 | assert.deepStrictEqual(frequencySort(s), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/0453.Minimum Moves to Equal Array Elements/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { minMoves }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {number} 8 | */ 9 | function minMoves(nums) { 10 | const min = Math.min(...nums); 11 | return nums.reduce((sum, n) => sum + n - min, 0); 12 | } 13 | -------------------------------------------------------------------------------- /algorithms/0453.Minimum Moves to Equal Array Elements/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { minMoves } = require('./'); 6 | 7 | describe('#minMoves', () => { 8 | const tests = [ 9 | { 10 | nums: [1, 2, 3], 11 | result: 3, 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ nums, result }) => { 16 | it(`${nums} -> ${result}`, () => { 17 | assert.deepStrictEqual(minMoves(nums), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/0476.Number Complement/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { findComplement }; 4 | 5 | /** 6 | * @param {number} num 7 | * @return {number} 8 | */ 9 | function findComplement(num) { 10 | return num ^ ((1 << (Math.log2(num) + 1)) - 1); 11 | } 12 | -------------------------------------------------------------------------------- /algorithms/0476.Number Complement/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import "math" 4 | 5 | func findComplement(num int) int { 6 | return num ^ (1<<(int(math.Log2(float64(num)))+1) - 1) 7 | } 8 | -------------------------------------------------------------------------------- /algorithms/0479.Largest Palindrome Product/README.md: -------------------------------------------------------------------------------- 1 | ## 479. Largest Palindrome Product 2 | 3 | https://leetcode.com/problems/largest-palindrome-product/description/ 4 | 5 | Find the largest palindrome made from the product of two n-digit numbers. 6 | 7 | Since the result could be very large, you should return the largest palindrome mod 1337. 8 | 9 | Example: 10 | 11 | Input: 2 12 | 13 | Output: 987 14 | 15 | Explanation: 99 x 91 = 9009, 9009 % 1337 = 987 16 | 17 | Note: 18 | 19 | The range of n is [1,8]. 20 | -------------------------------------------------------------------------------- /algorithms/0482.License Key Formatting/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { licenseKeyFormatting }; 4 | 5 | /** 6 | * @param {string} S 7 | * @param {number} K 8 | * @return {string} 9 | */ 10 | function licenseKeyFormatting(S, K) { 11 | S = S.toUpperCase(); 12 | let str = ''; 13 | let l = S.length; 14 | while (l--) { 15 | let c = S[l]; 16 | if (c === '-') { 17 | continue; 18 | } 19 | if (str.length % (K + 1) === K) { 20 | c += '-'; 21 | } 22 | str = c + str; 23 | } 24 | return str; 25 | } 26 | -------------------------------------------------------------------------------- /algorithms/0485.Max Consecutive Ones/README.md: -------------------------------------------------------------------------------- 1 | ## 485. Max Consecutive Ones 2 | 3 | https://leetcode.com/problems/max-consecutive-ones/ 4 | 5 | Given a binary array, find the maximum number of consecutive 1s in this array. 6 | 7 | Example 1: 8 | Input: [1,1,0,1,1,1] 9 | Output: 3 10 | Explanation: The first two digits or the last three digits are consecutive 1s. 11 | The maximum number of consecutive 1s is 3. 12 | Note: 13 | 14 | The input array will only contain 0 and 1. 15 | The length of input array is a positive integer and will not exceed 10,000 16 | -------------------------------------------------------------------------------- /algorithms/0486.Predict the Winner/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { PredictTheWinner }; 4 | 5 | function PredictTheWinner(nums) { 6 | const l = nums.length; 7 | const dp = Array(l); 8 | let i = l; 9 | while (i--) { 10 | dp[i] = Array(l); 11 | dp[i][i] = nums[i]; 12 | for (let j = i + 1; j < l; j++) { 13 | dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]); 14 | } 15 | } 16 | return dp[0][l - 1] >= 0; 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0500.Keyboard Row/README.md: -------------------------------------------------------------------------------- 1 | ## 500.Keyboard Row 2 | 3 | https://leetcode.com/problems/keyboard-row/#/description 4 | 5 | Given a List of words, return the words that can be typed using letters of alphabet on only one row's of American keyboard like the image below. 6 | 7 | American keyboard 8 | 9 | Example 1: 10 | Input: ["Hello", "Alaska", "Dad", "Peace"] 11 | Output: ["Alaska", "Dad"] 12 | Note: 13 | You may use one character in the keyboard more than once. 14 | -------------------------------------------------------------------------------- /algorithms/0504.Base 7/README.md: -------------------------------------------------------------------------------- 1 | ## 504. Base 7 2 | 3 | https://leetcode.com/problems/base-7/ 4 | 5 | Given an integer, return its base 7 string representation. 6 | 7 | Example 1: 8 | Input: 100 9 | Output: "202" 10 | Example 2: 11 | Input: -7 12 | Output: "-10" 13 | -------------------------------------------------------------------------------- /algorithms/0504.Base 7/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { convertToBase7, convertToBase72 }; 4 | 5 | /** 6 | * @param {number} num 7 | * @return {string} 8 | */ 9 | function convertToBase7(num) { 10 | let res = ''; 11 | let n = Math.abs(num); 12 | do { 13 | res = (n % 7) + res; 14 | n = (n / 7) | 0; 15 | } while (n !== 0); 16 | return num < 0 ? `-${res}` : res; 17 | } 18 | 19 | /** 20 | * @param {number} num 21 | * @return {string} 22 | */ 23 | function convertToBase72(num) { 24 | return num.toString(7); 25 | } 26 | -------------------------------------------------------------------------------- /algorithms/0507.Perfect Number/README.md: -------------------------------------------------------------------------------- 1 | ## 507. Perfect Number 2 | 3 | https://leetcode.com/problems/perfect-number/#/description 4 | 5 | We define the Perfect Number is a positive integer that is equal to the sum of all its positive divisors except itself. 6 | 7 | Now, given an integer n, write a function that returns true when it is a perfect number and false when it is not. 8 | Example: 9 | Input: 28 10 | Output: True 11 | Explanation: 28 = 1 + 2 + 4 + 7 + 14 12 | Note: The input number n will not exceed 100,000,000. (1e8) 13 | -------------------------------------------------------------------------------- /algorithms/0507.Perfect Number/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean checkPerfectNumber(int num) { 3 | if (num <= 1) { 4 | return false; 5 | } 6 | int n = num; 7 | while (n % 2 == 0) { 8 | n /= 2; 9 | } 10 | return num == n * (n + 1) / 2; 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /algorithms/0507.Perfect Number/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { checkPerfectNumber }; 4 | 5 | /** 6 | * @param {number} num 7 | * @return {boolean} 8 | */ 9 | function checkPerfectNumber(num) { 10 | if (num <= 1) { 11 | return false; 12 | } 13 | let n = num; 14 | while (n % 2 === 0) { 15 | n /= 2; 16 | } 17 | return num === n * ((n + 1) / 2); 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/0513.Find Bottom Left Tree Value/README.md: -------------------------------------------------------------------------------- 1 | ## 0513. Find Bottom Left Tree Value 2 | 3 | https://leetcode.com/problems/find-bottom-left-tree-value 4 | 5 | Given the root of a binary tree, return the leftmost value in the last row of the tree. 6 | 7 | Example 1: 8 | 9 | Input: root = [2,1,3] 10 | Output: 1 11 | 12 | Example 2: 13 | 14 | Input: root = [1,2,3,4,null,5,6,null,null,7] 15 | Output: 7 16 | 17 | Constraints: 18 | 19 | The number of nodes in the tree is in the range [1, 104]. 20 | -231 <= Node.val <= 231 - 1 21 | -------------------------------------------------------------------------------- /algorithms/0518.Coin Change 2/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { change }; 4 | 5 | /** 6 | * @param {number} amount 7 | * @param {number[]} coins 8 | * @return {number} 9 | */ 10 | function change(amount, coins) { 11 | if (amount === 0) { 12 | return 1; 13 | } 14 | const dp = Array(amount + 1).fill(0); 15 | for (const coin of coins) { 16 | dp[coin]++; 17 | for (let sum = coin; sum <= amount; sum++) { 18 | dp[sum] += dp[sum - coin]; 19 | } 20 | } 21 | return dp[amount]; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0520.Detect Capital/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { detectCapitalUse }; 4 | 5 | /** 6 | * @param {string} word 7 | * @return {boolean} 8 | */ 9 | function detectCapitalUse(word) { 10 | let count = 0; 11 | for (let i = 0; i < word.length; i++) { 12 | if (word.charAt(i) <= 'Z') { 13 | count++; 14 | } 15 | } 16 | return count === 0 || count === word.length || (count === 1 && word.charAt(0) <= 'Z'); 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0524.Longest Word in Dictionary through Deleting/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "longest-word-in-dictionary-through-deleting" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "longest_word_in_dictionary_through_deleting" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0535.Encode and Decode TinyURL/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { encode, decode } = require('./'); 6 | 7 | describe('#EncodeAndDecodeTinyURL', () => { 8 | it('should decode and encode', () => { 9 | const url = 'https://leetcode.com/problems/design-tinyurl'; 10 | assert.strictEqual(decode(encode(url)), url); 11 | }); 12 | }); 13 | -------------------------------------------------------------------------------- /algorithms/0540.Single Element in a Sorted Array/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { singleNonDuplicate }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {number} 8 | */ 9 | function singleNonDuplicate(nums) { 10 | let left = 0; 11 | let right = nums.length; 12 | while (left < right) { 13 | const mid = (left + (right - left) / 2) | 0; 14 | if ((mid % 2 === 1) === (nums[mid] === nums[mid + 1])) { 15 | right = mid; 16 | } else { 17 | left = mid + 1; 18 | } 19 | } 20 | return nums[left]; 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/0543.Diameter of Binary Tree/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { diameterOfBinaryTree } = require('./'); 7 | 8 | describe('#diameterOfBinaryTree', () => { 9 | const tests = [ 10 | { 11 | root: '', 12 | result: 0, 13 | }, 14 | ]; 15 | 16 | _.forEach(tests, ({ root, result }) => { 17 | it(`${root} -> ${result}`, () => { 18 | assert.deepStrictEqual(diameterOfBinaryTree(root), result); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /algorithms/0557.Reverse Words in a String III/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { reverseWords } = require('./'); 6 | 7 | describe('#reverseWords', () => { 8 | const tests = [ 9 | { 10 | s: "Let's take LeetCode contest", 11 | result: "s'teL ekat edoCteeL tsetnoc", 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ s, result }) => { 16 | it(`${s} -> ${result}`, () => { 17 | assert.deepStrictEqual(reverseWords(s), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/0560.Subarray Sum Equals K/README.md: -------------------------------------------------------------------------------- 1 | ## 0560. Subarray Sum Equals K 2 | 3 | https://leetcode.com/problems/subarray-sum-equals-k 4 | 5 | Given an array of integers and an integer k, you need to find the total number of continuous subarrays whose sum equals to k. 6 | 7 | Example 1: 8 | Input:nums = [1,1,1], k = 2 9 | Output: 2 10 | 11 | Note: 12 | 13 | The length of the array is in range [1, 20,000]. 14 | The range of numbers in the array is [-1000, 1000] and the range of the integer k is [-1e7, 1e7]. 15 | -------------------------------------------------------------------------------- /algorithms/0567.Permutation in String/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { checkInclusion } = require('./'); 7 | 8 | describe('#checkInclusion', () => { 9 | const tests = [ 10 | { 11 | s1: 'ab', 12 | s2: 'eidbaooo', 13 | result: true, 14 | }, 15 | ]; 16 | 17 | _.forEach(tests, ({ s1, s2, result }) => { 18 | it(`${s1}, ${s2} -> ${result}`, () => { 19 | assert.deepStrictEqual(checkInclusion(s1, s2), result); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /algorithms/0575.Distribute Candies/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "distribute-candies" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "distribute_candies" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0581.Shortest Unsorted Continuous Subarray/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "shortest-unsorted-continuous-subarray" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "shortest_unsorted_continuous_subarray" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0594.Longest Harmonious Subsequence/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "longest-harmonious-subsequence" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "longest_harmonious_subsequence" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0605.Can Place Flowers/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { canPlaceFlowers }; 4 | 5 | /** 6 | * @param {number[]} flowerbed 7 | * @param {number} n 8 | * @return {boolean} 9 | */ 10 | function canPlaceFlowers(flowerbed, n) { 11 | let i = -1; 12 | loop: while (n > 0 && ++i < flowerbed.length) { 13 | for (let j = -1; j <= 1; j++) { 14 | if (flowerbed[j + i]) { 15 | continue loop; 16 | } 17 | } 18 | i++; 19 | n--; 20 | } 21 | return n === 0; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0627.Swap Salary/query.sql: -------------------------------------------------------------------------------- 1 | # Write your MySQL query statement below 2 | UPDATE salary SET sex = CASE WHEN sex = "m" THEN "f" ELSE "m" END; 3 | -------------------------------------------------------------------------------- /algorithms/0628.Maximum Product of Three Numbers/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { maximumProduct }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {number} 8 | */ 9 | function maximumProduct(nums) { 10 | const l = nums.length; 11 | if (l < 3) { 12 | return 0; 13 | } 14 | nums.sort((n1, n2) => (n1 < n2 ? -1 : 1)); 15 | const max = nums[l - 1]; 16 | const r1 = max * nums[l - 2] * nums[l - 3]; 17 | const r2 = max * nums[0] * nums[1]; 18 | return Math.max(r1, r2); 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0633.Sum of Square Numbers/README.md: -------------------------------------------------------------------------------- 1 | ## 633. Sum of Square Numbers 2 | 3 | https://leetcode.com/problems/sum-of-square-numbers/description/ 4 | 5 | Given a non-negative integer c, your task is to decide whether there're two integers a and b such that a2 + b2 = c. 6 | 7 | ``` 8 | Example 1: 9 | Input: 5 10 | Output: True 11 | Explanation: 1 * 1 + 2 * 2 = 5 12 | ``` 13 | 14 | ``` 15 | Example 2: 16 | Input: 3 17 | Output: False 18 | ``` 19 | -------------------------------------------------------------------------------- /algorithms/0643.Maximum Average Subarray I/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const _ = require('lodash'); 4 | 5 | module.exports = { findMaxAverage }; 6 | 7 | /** 8 | * @param {number[]} nums 9 | * @param {number} k 10 | * @return {number} 11 | */ 12 | function findMaxAverage(nums, k) { 13 | let sum = _.sum(nums.slice(0, k)); 14 | let max = sum / k; 15 | for (let i = k; i < nums.length; i++) { 16 | sum += nums[i] - nums[i - k]; 17 | max = Math.max(max, sum / k); 18 | } 19 | return max; 20 | } 21 | -------------------------------------------------------------------------------- /algorithms/0645.Set Mismatch/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "set-mismatch" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "set_mismatch" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0657.Judge Route Circle/solution.rb: -------------------------------------------------------------------------------- 1 | # @param {String} moves 2 | # @return {Boolean} 3 | def judge_circle(moves) 4 | x = 0 5 | y = 0 6 | moves.each_char do |c| 7 | case c 8 | when "U" 9 | y += 1 10 | when "D" 11 | y -= 1 12 | when "R" 13 | x += 1 14 | when "L" 15 | x -= 1 16 | end 17 | end 18 | return x === 0 && y === 0 19 | end 20 | -------------------------------------------------------------------------------- /algorithms/0658.Find K Closest Elements/solution.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} arr 2 | # @param {Integer} k 3 | # @param {Integer} x 4 | # @return {Integer[]} 5 | def find_closest_elements(arr, k, x) 6 | arr.sort! do |n1, n2| 7 | a1 = (n1 - x).abs 8 | a2 = (n2 - x).abs 9 | a1 === a2 ? n1 <=> n2 : a1 <=> a2 10 | end 11 | return arr.slice(0, k).sort 12 | end 13 | -------------------------------------------------------------------------------- /algorithms/0660.Remove 9/README.md: -------------------------------------------------------------------------------- 1 | ## 660. Remove 9 2 | 3 | https://leetcode.com/problems/remove-9/description/ 4 | 5 | Start from integer 1, remove any integer that contains 9 such as 9, 19, 29... 6 | 7 | So now, you will have a new integer sequence: 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, ... 8 | 9 | Given a positive integer n, you need to return the n-th integer after removing. Note that 1 will be the first integer. 10 | 11 | Example 1: 12 | 13 | ``` 14 | Input: 9 15 | Output: 10 16 | ``` 17 | 18 | Hint: n will not exceed 9 x 10^8. 19 | -------------------------------------------------------------------------------- /algorithms/0660.Remove 9/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { newInteger }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {number} 8 | */ 9 | function newInteger(n) { 10 | let str = ''; 11 | while (n >= 9) { 12 | const d = n % 9; 13 | str = d + str; 14 | n = (n / 9) | 0; 15 | } 16 | return +(n + str); 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0665.Non-decreasing Array/solution.rb: -------------------------------------------------------------------------------- 1 | # @param {Integer[]} nums 2 | # @return {Boolean} 3 | def check_possibility(nums) 4 | prev = -Float::INFINITY 5 | changed = false 6 | nums.each_with_index do |n, i| 7 | if n >= prev 8 | prev = n 9 | next 10 | end 11 | if changed 12 | return false 13 | end 14 | changed = true 15 | if i === 1 || n >= nums[i - 2] 16 | prev = n 17 | end 18 | end 19 | return true 20 | end 21 | -------------------------------------------------------------------------------- /algorithms/0674.Longest Continuous Increasing Subsequence/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { findLengthOfLCIS }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {number} 8 | */ 9 | function findLengthOfLCIS(nums) { 10 | let max = 1; 11 | let left = 0; 12 | for (let right = 1; right < nums.length; right++) { 13 | if (nums[right] <= nums[right - 1]) { 14 | left = right; 15 | continue; 16 | } 17 | max = Math.max(max, right - left + 1); 18 | } 19 | return max; 20 | } 21 | -------------------------------------------------------------------------------- /algorithms/0677.Map Sum Pairs/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const { MapSum } = require('./'); 5 | 6 | describe('#MapSum', () => { 7 | it('should work', () => { 8 | const mapSum = new MapSum(); 9 | mapSum.insert('apple', 3); 10 | assert.strictEqual(mapSum.sum('ap'), 3); 11 | mapSum.insert('app', 2); 12 | assert.strictEqual(mapSum.sum('ap'), 5); 13 | assert.strictEqual(mapSum.sum('apple'), 3); 14 | }); 15 | }); 16 | -------------------------------------------------------------------------------- /algorithms/0680.Valid Palindrome II/README.md: -------------------------------------------------------------------------------- 1 | ## 680. Valid Palindrome II 2 | 3 | Given a non-empty string s, you may delete at most one character. Judge whether you can make it a palindrome. 4 | 5 | Example 1: 6 | 7 | ``` 8 | Input: "aba" 9 | Output: True 10 | ``` 11 | 12 | Example 2: 13 | 14 | ``` 15 | Input: "abca" 16 | Output: True 17 | ``` 18 | 19 | Explanation: You could delete the character 'c'. 20 | -------------------------------------------------------------------------------- /algorithms/0686.Repeated String Match/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { repeatedStringMatch }; 4 | 5 | /** 6 | * @param {string} A 7 | * @param {string} B 8 | * @return {number} 9 | */ 10 | function repeatedStringMatch(A, B) { 11 | let str = A; 12 | let count = 1; 13 | while (!str.includes(B)) { 14 | if (str.length - A.length > B.length) { 15 | return -1; 16 | } 17 | count++; 18 | str += A; 19 | } 20 | return count; 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/0746.Min Cost Climbing Stairs/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { minCostClimbingStairs }; 4 | 5 | /** 6 | * @param {number[]} cost 7 | * @return {number} 8 | */ 9 | function minCostClimbingStairs(cost) { 10 | let l = cost.length - 2; 11 | while (l--) { 12 | cost[l] += Math.min(cost[l + 1], cost[l + 2]); 13 | } 14 | return Math.min(cost[0], cost[1]); 15 | } 16 | -------------------------------------------------------------------------------- /algorithms/0763.Partition Labels/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { partitionLabels } = require('./'); 7 | 8 | describe('#partitionLabels', () => { 9 | const tests = [ 10 | { 11 | S: 'ababcbacadefegdehijhklij', 12 | result: [9, 7, 8], 13 | }, 14 | ]; 15 | 16 | _.forEach(tests, ({ S, result }) => { 17 | it(`${S} -> ${result}`, () => { 18 | assert.deepStrictEqual(partitionLabels(S), result); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /algorithms/0771.Jewels and Stones/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { numJewelsInStones }; 4 | 5 | /** 6 | * @param {string} J 7 | * @param {string} S 8 | * @return {number} 9 | */ 10 | function numJewelsInStones(J, S) { 11 | const set = new Set(J); 12 | return S.split('').reduce((sum, c) => sum + ~~set.has(c), 0); 13 | } 14 | -------------------------------------------------------------------------------- /algorithms/0771.Jewels and Stones/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { numJewelsInStones } = require('./'); 7 | 8 | describe('#numJewelsInStones', () => { 9 | const tests = [ 10 | { 11 | J: 'aA', 12 | S: 'aAAbbbb', 13 | result: 3, 14 | }, 15 | ]; 16 | 17 | _.forEach(tests, ({ J, S, result }) => { 18 | it(`${J}, ${S} -> ${result}`, () => { 19 | assert.deepStrictEqual(numJewelsInStones(J, S), result); 20 | }); 21 | }); 22 | }); 23 | -------------------------------------------------------------------------------- /algorithms/0784.Letter Case Permutation/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "letter-case-permutation" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "letter_case_permutation" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0784.Letter Case Permutation/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { letterCasePermutation }; 4 | 5 | /** 6 | * @param {string} S 7 | * @return {string[]} 8 | */ 9 | function letterCasePermutation(S) { 10 | if (S.length === 0) { 11 | return ['']; 12 | } 13 | const c = S.slice(-1); 14 | const num = /\d/.test(c); 15 | return letterCasePermutation(S.slice(0, -1)).flatMap((str) => 16 | num ? `${str}${c}` : [`${str}${c.toLowerCase()}`, `${str}${c.toUpperCase()}`], 17 | ); 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/0785.Is Graph Bipartite?/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "is-graph-bipartite" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "is_graph_bipartite" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0788.Rotated Digits/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { rotatedDigits } = require('./'); 6 | 7 | describe('#rotatedDigits', () => { 8 | const tests = [ 9 | { 10 | N: 10, 11 | result: 4, 12 | }, 13 | { 14 | N: 857, 15 | result: 247, 16 | }, 17 | ]; 18 | 19 | _.forEach(tests, ({ N, result }) => { 20 | it(`${N} -> ${result}`, () => { 21 | assert.deepStrictEqual(rotatedDigits(N), result); 22 | }); 23 | }); 24 | }); 25 | -------------------------------------------------------------------------------- /algorithms/0789.Escape The Ghosts/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { escapeGhosts }; 4 | 5 | /** 6 | * @param {number[][]} ghosts 7 | * @param {number[]} target 8 | * @return {boolean} 9 | */ 10 | function escapeGhosts(ghosts, [tx, ty]) { 11 | const distance = getDistance(tx, ty); 12 | return ghosts.every(([x, y]) => distance < getDistance(tx - x, ty - y)); 13 | } 14 | 15 | function getDistance(dx, dy) { 16 | return Math.abs(dx) + Math.abs(dy); 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0800.Similar RGB Color/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const similarRGB = require('./'); 6 | 7 | describe('#similarRGB', () => { 8 | const tests = [ 9 | { 10 | color: '#09f166', 11 | result: '#11ee66', 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ color, result }) => { 16 | it(`${color} -> ${result}`, () => { 17 | assert.strictEqual(similarRGB(color), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/0805.Split Array With Same Average/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { splitArraySameAverage }; 4 | 5 | /** 6 | * @param {number[]} A 7 | * @return {boolean} 8 | */ 9 | function splitArraySameAverage(A) { 10 | const l = A.length; 11 | return cal(0, 0, 0, 0, -1, true); 12 | 13 | function cal(B, C, bc, cc, i) { 14 | if (++i === l) { 15 | return B / bc === C / cc; 16 | } 17 | const n = A[i]; 18 | return cal(B + n, C, bc + 1, cc, i) || cal(B, C + n, bc, cc + 1, i); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /algorithms/0821.Shortest Distance to a Character/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "shortest-distance-to-a-character" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "shortest_distance_to_a_character" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0824.Goat Latin/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { toGoatLatin }; 4 | 5 | /** 6 | * @param {string} S 7 | * @return {string} 8 | */ 9 | function toGoatLatin(S) { 10 | const result = []; 11 | const words = S.split(/\s/); 12 | let prev = ''; 13 | for (const word of words) { 14 | const w = /^[aeiou]/i.test(word) ? `${word}ma` : `${word.slice(1)}${word[0]}ma`; 15 | prev += 'a'; 16 | result.push(`${w}${prev}`); 17 | } 18 | return result.join(' '); 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0830.Positions of Large Groups/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { largeGroupPositions }; 4 | 5 | /** 6 | * @param {string} S 7 | * @return {number[][]} 8 | */ 9 | function largeGroupPositions(S) { 10 | const result = []; 11 | let start = -1; 12 | for (let i = 0; i <= S.length; i++) { 13 | const c = S[i]; 14 | if (S[start] === c) { 15 | continue; 16 | } 17 | if (i - start >= 3) { 18 | result.push([start, i - 1]); 19 | } 20 | start = i; 21 | } 22 | return result; 23 | } 24 | -------------------------------------------------------------------------------- /algorithms/0836.Rectangle Overlap/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { isRectangleOverlap }; 4 | 5 | /** 6 | * @param {number[]} rec1 7 | * @param {number[]} rec2 8 | * @return {boolean} 9 | */ 10 | function isRectangleOverlap([x11, y11, x12, y12], [x21, y21, x22, y22]) { 11 | return x22 > x11 && y22 > y11 && x21 < x12 && y21 < y12; 12 | } 13 | -------------------------------------------------------------------------------- /algorithms/0841.Keys and Rooms/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { canVisitAllRooms }; 4 | 5 | /** 6 | * @param {number[][]} rooms 7 | * @return {boolean} 8 | */ 9 | function canVisitAllRooms(rooms) { 10 | const visited = new Set([0]); 11 | const [keys] = rooms; 12 | while (keys.length) { 13 | const key = keys.shift(); 14 | if (visited.has(key)) { 15 | continue; 16 | } 17 | visited.add(key); 18 | keys.push(...rooms[key]); 19 | } 20 | return visited.size === rooms.length; 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/0849.Maximize Distance to Closest Person/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { maxDistToClosest }; 4 | 5 | /** 6 | * @param {number[]} seats 7 | * @return {number} 8 | */ 9 | function maxDistToClosest(seats) { 10 | let l = seats.indexOf(1); 11 | let max = l; 12 | for (let r = l + 1; r < seats.length; r++) { 13 | if (seats[r] === 0) { 14 | continue; 15 | } 16 | [max, l] = [Math.max(max, ((r - l) / 2) | 0), r]; 17 | } 18 | return Math.max(max, seats.length - l - 1); 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/0856.Score of Parentheses/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "score-of-parentheses" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "score_of_parentheses" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0876.Middle of the Linked List/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { middleNode }; 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * function ListNode(val) { 8 | * this.val = val 9 | * this.next = null 10 | * } 11 | */ 12 | /** 13 | * @param {ListNode} head 14 | * @return {ListNode} 15 | */ 16 | function middleNode(head) { 17 | let fast = head; 18 | let slow = head; 19 | while (fast && fast.next) { 20 | slow = slow.next; 21 | fast = fast.next.next; 22 | } 23 | return slow; 24 | } 25 | -------------------------------------------------------------------------------- /algorithms/0876.Middle of the Linked List/solution.cpp: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * struct ListNode { 4 | * int val; 5 | * ListNode *next; 6 | * ListNode(int x) : val(x), next(NULL) {} 7 | * }; 8 | */ 9 | class Solution { 10 | public: 11 | ListNode* middleNode(ListNode* head) { 12 | ListNode *slow = head, *fast = head; 13 | while (fast && fast->next) { 14 | slow = slow->next, fast = fast->next->next; 15 | } 16 | return slow; 17 | } 18 | }; 19 | -------------------------------------------------------------------------------- /algorithms/0876.Middle of the Linked List/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | /** 4 | * Definition for singly-linked list. 5 | * type ListNode struct { 6 | * Val int 7 | * Next *ListNode 8 | * } 9 | */ 10 | func middleNode(head *ListNode) *ListNode { 11 | slow, fast := head, head 12 | for fast != nil && fast.Next != nil { 13 | slow = slow.Next 14 | fast = fast.Next.Next 15 | } 16 | return slow 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/0876.Middle of the Linked List/solution.py: -------------------------------------------------------------------------------- 1 | # Definition for singly-linked list. 2 | # class ListNode: 3 | # def __init__(self, x): 4 | # self.val = x 5 | # self.next = None 6 | 7 | class Solution: 8 | def middleNode(self, head: ListNode) -> ListNode: 9 | slow = fast = head 10 | while fast and fast.next: 11 | slow = slow.next 12 | fast = fast.next.next 13 | return slow 14 | -------------------------------------------------------------------------------- /algorithms/0881.Boats to Save People/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "boats-to-save-people" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "boats_to_save_people" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0905.Sort Array By Parity/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { sortArrayByParity }; 4 | 5 | /** 6 | * @param {number[]} A 7 | * @return {number[]} 8 | */ 9 | function sortArrayByParity(A) { 10 | let ie = 0; 11 | let io = A.length - 1; 12 | while (ie < io) { 13 | const n1 = A[ie]; 14 | if (n1 % 2 === 0) { 15 | ie++; 16 | } else { 17 | [A[ie], A[io]] = [A[io], n1]; 18 | io--; 19 | } 20 | } 21 | return A; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0905.Sort Array By Parity/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { sortArrayByParity } = require('./'); 6 | 7 | describe('#sortArrayByParity', () => { 8 | const tests = [ 9 | { 10 | A: [3, 1, 2, 4], 11 | result: [4, 2, 1, 3], 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ A, result }) => { 16 | it(`${A} -> ${result}`, () => { 17 | assert.deepStrictEqual(sortArrayByParity(A), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/0929.Unique Email Addresses/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { numUniqueEmails }; 4 | 5 | /** 6 | * @param {string[]} emails 7 | * @return {number} 8 | */ 9 | function numUniqueEmails(emails) { 10 | const set = new Set(); 11 | for (const email of emails) { 12 | const [name, domain] = email.split('@'); 13 | set.add(`${name.replace(/\./g, '').replace(/\+.+/, '')}@${domain}`); 14 | } 15 | return set.size; 16 | } 17 | -------------------------------------------------------------------------------- /algorithms/0933.Number of Recent Calls/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const { RecentCounter } = require('./'); 5 | 6 | describe('#RecentCounter', () => { 7 | it('should work', () => { 8 | const counter = new RecentCounter(); 9 | assert.strictEqual(counter.ping(1), 1); 10 | assert.strictEqual(counter.ping(100), 2); 11 | assert.strictEqual(counter.ping(3001), 3); 12 | assert.strictEqual(counter.ping(3002), 3); 13 | }); 14 | }); 15 | -------------------------------------------------------------------------------- /algorithms/0942.DI String Match/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { diStringMatch }; 4 | 5 | /** 6 | * @param {string} S 7 | * @return {number[]} 8 | */ 9 | function diStringMatch(S) { 10 | let i = -1; 11 | let l = 0; 12 | let h = S.length; 13 | const arr = Array(h + 1); 14 | while (l <= h) { 15 | arr[++i] = S[i] === 'I' ? l++ : h--; 16 | } 17 | return arr; 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/0944.Delete Columns to Make Sorted/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { minDeletionSize }; 4 | 5 | /** 6 | * @param {string[]} A 7 | * @return {number} 8 | */ 9 | function minDeletionSize(A) { 10 | const size = A[0].length; 11 | let result = 0; 12 | for (let c = 0; c < size; c++) { 13 | for (let r = 1; r < A.length; r++) { 14 | if (A[r][c] >= A[r - 1][c]) { 15 | continue; 16 | } 17 | result++; 18 | break; 19 | } 20 | } 21 | return result; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/0944.Delete Columns to Make Sorted/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func minDeletionSize(A []string) int { 4 | var count int 5 | for c := 0; c < len(A[0]); c++ { 6 | for r := 1; r < len(A); r++ { 7 | if A[r][c] >= A[r-1][c] { 8 | continue 9 | } 10 | count++ 11 | break 12 | } 13 | } 14 | return count 15 | } 16 | -------------------------------------------------------------------------------- /algorithms/0944.Delete Columns to Make Sorted/solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minDeletionSize(self, A: List[str]) -> int: 3 | return sum(any(a > b for a, b in zip(col, col[1:])) for col in zip(*A)) 4 | -------------------------------------------------------------------------------- /algorithms/0946.Validate Stack Sequences/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "validate-stack-sequences" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "validate_stack_sequences" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/0977.Squares of a Sorted Array/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { sortedSquares }; 4 | 5 | /** 6 | * @param {number[]} A 7 | * @return {number[]} 8 | */ 9 | function sortedSquares(A) { 10 | A = A.map((num) => num ** 2); 11 | let l = 0; 12 | let r = A.length - 1; 13 | return Array.from(A, () => (A[l] < A[r] ? A[r--] : A[l++])).reverse(); 14 | } 15 | -------------------------------------------------------------------------------- /algorithms/0991.Broken Calculator/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "broken-calculator" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "broken_calculator" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1009.Complement of Base 10 Integer/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { bitwiseComplement }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {number} 8 | */ 9 | function bitwiseComplement(n) { 10 | return n ^ ((1 << (Math.log2(n || 1) + 1)) - 1); 11 | } 12 | -------------------------------------------------------------------------------- /algorithms/1010.Pairs of Songs With Total Durations Divisible by 60/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { numPairsDivisibleBy60 }; 4 | 5 | /** 6 | * @param {number[]} time 7 | * @return {number} 8 | */ 9 | function numPairsDivisibleBy60(time) { 10 | const map = new Map(); 11 | let count = 0; 12 | for (const t of time) { 13 | const mod = t % 60; 14 | count += map.get(mod === 0 ? 0 : 60 - mod) || 0; 15 | map.set(mod, map.has(mod) ? map.get(mod) + 1 : 1); 16 | } 17 | return count; 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/1021.Remove Outermost Parentheses/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { removeOuterParentheses }; 4 | 5 | /** 6 | * @param {string} S 7 | * @return {string} 8 | */ 9 | function removeOuterParentheses(S) { 10 | let result = ''; 11 | let count = 0; 12 | for (const c of S) { 13 | if (c === '(') { 14 | if (count++ === 0) { 15 | continue; 16 | } 17 | } else if (--count === 0) { 18 | continue; 19 | } 20 | result += c; 21 | } 22 | return result; 23 | } 24 | -------------------------------------------------------------------------------- /algorithms/1037.Valid Boomerang/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { isBoomerang }; 4 | 5 | /** 6 | * @param {number[][]} points 7 | * @return {boolean} 8 | */ 9 | function isBoomerang(points) { 10 | const [[ax, ay], [bx, by], [cx, cy]] = points; 11 | return (by - ay) * (cx - bx) !== (bx - ax) * (cy - by); 12 | } 13 | -------------------------------------------------------------------------------- /algorithms/1046.Last Stone Weight/solution.py: -------------------------------------------------------------------------------- 1 | from heapq import heappush, heappop, heapify 2 | 3 | class Solution: 4 | def lastStoneWeight(self, stones: List[int]) -> int: 5 | heap = [-stone for stone in stones] 6 | heapify(heap) 7 | while len(heap) > 1 and heap[0] != 0: 8 | heappush(heap, heappop(heap) - heappop(heap)) 9 | return -heap[0] 10 | -------------------------------------------------------------------------------- /algorithms/1051.Height Checker/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { heightChecker }; 4 | 5 | /** 6 | * @param {number[]} heights 7 | * @return {number} 8 | */ 9 | function heightChecker(heights) { 10 | const sorted = [...heights].sort((n1, n2) => n1 - n2); 11 | return heights.filter((n, i) => n === sorted[i]).length; 12 | } 13 | -------------------------------------------------------------------------------- /algorithms/1051.Height Checker/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { heightChecker } = require('./'); 6 | 7 | describe('#heightChecker', () => { 8 | const tests = [ 9 | { 10 | heights: [1, 1, 4, 2, 1, 3], 11 | result: 3, 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ heights, result }) => { 16 | it(`${heights} -> ${result}`, () => { 17 | assert.deepStrictEqual(heightChecker(heights), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/1053.Previous Permutation With One Swap/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { prevPermOpt1 }; 4 | 5 | /** 6 | * @param {number[]} A 7 | * @return {number[]} 8 | */ 9 | function prevPermOpt1(A) { 10 | const n = A.length; 11 | let l = n - 2; 12 | while (A[l] <= A[l + 1] && l--) {} 13 | if (l < 0) { 14 | return A; 15 | } 16 | let r = n - 1; 17 | while (A[l] <= A[r] && --r) {} 18 | while (A[r - 1] === A[r] && --r) {} 19 | [A[r], A[l]] = [A[l], A[r]]; 20 | return A; 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/1072.Flip Columns For Maximum Number of Equal Rows/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { maxEqualRowsAfterFlips }; 4 | 5 | /** 6 | * @param {number[][]} matrix 7 | * @return {number} 8 | */ 9 | function maxEqualRowsAfterFlips(matrix) { 10 | let obj = {}; 11 | for (let r of matrix) { 12 | const p = r.reduce((s, n) => s + (n ^ r[0]), ''); 13 | obj[p] = ++obj[p] || 1; 14 | } 15 | return Math.max(...Object.values(obj)); 16 | } 17 | -------------------------------------------------------------------------------- /algorithms/1091.Shortest Path in Binary Matrix/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "shortest-path-in-binary-matrix" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "shortest_path_in_binary_matrix" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1094.Car Pooling/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { carPooling }; 4 | 5 | /** 6 | * @param {number[][]} trips 7 | * @param {number} capacity 8 | * @return {boolean} 9 | */ 10 | function carPooling(trips, capacity) { 11 | const nums = Array(1001).fill(0); 12 | for (const [num, from, to] of trips) { 13 | nums[from] += num; 14 | nums[to] -= num; 15 | } 16 | return nums.every((num) => { 17 | capacity -= num; 18 | return capacity >= 0; 19 | }); 20 | } 21 | -------------------------------------------------------------------------------- /algorithms/1103.Distribute Candies to People/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { distributeCandies }; 4 | 5 | /** 6 | * @param {number} candies 7 | * @param {number} n 8 | * @return {number[]} 9 | */ 10 | function distributeCandies(candies, n) { 11 | const result = Array(n).fill(0); 12 | let i = 0; 13 | while (candies > 0) { 14 | result[i++ % n] += Math.min(candies, i); 15 | candies -= i; 16 | } 17 | return result; 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/1108.Defanging an IP Address/README.md: -------------------------------------------------------------------------------- 1 | ## 1108. Defanging an IP Address 2 | 3 | https://leetcode.com/problems/defanging-an-ip-address 4 | 5 | Given a valid (IPv4) IP address, return a defanged version of that IP address. 6 | 7 | A defanged IP address replaces every period "." with "[.]". 8 | 9 | Example 1: 10 | Input: address = "1.1.1.1" 11 | Output: "1[.]1[.]1[.]1" 12 | Example 2: 13 | Input: address = "255.100.50.0" 14 | Output: "255[.]100[.]50[.]0" 15 | 16 | Constraints: 17 | 18 | The given address is a valid IPv4 address. 19 | -------------------------------------------------------------------------------- /algorithms/1108.Defanging an IP Address/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { defangIPaddr }; 4 | 5 | /** 6 | * @param {string} address 7 | * @return {string} 8 | */ 9 | function defangIPaddr(address) { 10 | return address.replace(/\./g, '[.]'); 11 | } 12 | -------------------------------------------------------------------------------- /algorithms/1108.Defanging an IP Address/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { defangIPaddr } = require('./'); 6 | 7 | describe('#defangIPaddr', () => { 8 | const tests = [ 9 | { 10 | address: '1.1.1.1', 11 | result: '1[.]1[.]1[.]1', 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ address, result }) => { 16 | it(`${address} -> ${result}`, () => { 17 | assert.deepStrictEqual(defangIPaddr(address), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/1111.Maximum Nesting Depth of Two Valid Parentheses Strings/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { maxDepthAfterSplit }; 4 | 5 | /** 6 | * @param {string} seq 7 | * @return {number[]} 8 | */ 9 | function maxDepthAfterSplit(seq) { 10 | let depth = 0; 11 | return seq.split('').map(c => (c === '(' ? depth++ % 2 : --depth % 2)); 12 | } 13 | -------------------------------------------------------------------------------- /algorithms/1154.Day of the Year/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { dayOfYear }; 4 | 5 | const days = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 6 | 7 | /** 8 | * @param {string} str 9 | * @return {number} 10 | */ 11 | function dayOfYear(str) { 12 | let [year, month, date] = str.split('-').map(n => Number(n)); 13 | if (month > 2 && year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0)) { 14 | date++; 15 | } 16 | while (--month) { 17 | date += days[month - 1]; 18 | } 19 | return date; 20 | } 21 | -------------------------------------------------------------------------------- /algorithms/1186.Maximum Subarray Sum with One Deletion/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { maximumSum }; 4 | 5 | /** 6 | * @param {number[]} arr 7 | * @return {number} 8 | */ 9 | function maximumSum(arr) { 10 | let max = -Infinity; 11 | let d0 = max; 12 | let d1 = max; 13 | for (const n of arr) { 14 | [d0, d1] = [Math.max(d0 + n, n), Math.max(d0, d1 + n)]; 15 | max = Math.max(max, d0, d1); 16 | } 17 | return max; 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/1217.Play with Chips/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { minCostToMoveChips }; 4 | 5 | /** 6 | * @param {number[]} chips 7 | * @return {number} 8 | */ 9 | function minCostToMoveChips(chips) { 10 | const counts = [0, 0]; 11 | for (const n of chips) { 12 | counts[n % 2]++; 13 | } 14 | return Math.min(...counts); 15 | } 16 | -------------------------------------------------------------------------------- /algorithms/1217.Play with Chips/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { minCostToMoveChips } = require('./'); 6 | 7 | describe('#minCostToMoveChips', () => { 8 | const tests = [ 9 | { 10 | chips: [1, 2, 3], 11 | result: 1, 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ chips, result }) => { 16 | it(`${chips} -> ${result}`, () => { 17 | assert.deepStrictEqual(minCostToMoveChips(chips), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/1218.Longest Arithmetic Subsequence of Given Difference/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { longestSubsequence }; 4 | 5 | /** 6 | * @param {number[]} arr 7 | * @param {number} difference 8 | * @return {number} 9 | */ 10 | function longestSubsequence(arr, difference) { 11 | const map = new Map(); 12 | let max = 0; 13 | for (const n of arr) { 14 | let sum = map.get(n - difference) || 0; 15 | max = Math.max(max, ++sum); 16 | map.set(n, sum); 17 | } 18 | return max; 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/1219.Path with Maximum Gold/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { getMaximumGold } = require('./'); 6 | 7 | describe('#getMaximumGold', () => { 8 | const tests = [ 9 | { 10 | grid: [], 11 | result: 0, 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ grid, result }) => { 16 | it(`${grid} -> ${result}`, () => { 17 | assert.deepStrictEqual(getMaximumGold(grid), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/1220.Count Vowels Permutation/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { countVowelPermutation }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {number} 8 | */ 9 | function countVowelPermutation(n) { 10 | let prev = [1, 1, 1, 1, 1]; 11 | const mod = 1e9 + 7; 12 | while (--n) { 13 | const [a, e, i, o, u] = prev; 14 | prev = [e, (a + i) % mod, (a + e + o + u) % mod, (i + u) % mod, a]; 15 | } 16 | return prev.reduce((sum, n) => (sum + n) % mod); 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/1221.Split a String in Balanced Strings/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { balancedStringSplit }; 4 | 5 | /** 6 | * @param {string} s 7 | * @return {number} 8 | */ 9 | function balancedStringSplit(s) { 10 | let cur = 0; 11 | let count = 0; 12 | for (const c of s) { 13 | cur += c === 'R' ? +1 : -1; 14 | count += cur === 0 ? 1 : 0; 15 | } 16 | return count; 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/1221.Split a String in Balanced Strings/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { balancedStringSplit } = require('./'); 6 | 7 | describe('#balancedStringSplit', () => { 8 | const tests = [ 9 | { 10 | s: 'RLRRLLRLRL', 11 | result: 4, 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ s, result }) => { 16 | it(`${s} -> ${result}`, () => { 17 | assert.deepStrictEqual(balancedStringSplit(s), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/1247.Minimum Swaps to Make Strings Equal/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { minimumSwap }; 4 | 5 | /** 6 | * @param {string} s1 7 | * @param {string} s2 8 | * @return {number} 9 | */ 10 | function minimumSwap(s1, s2) { 11 | const c = { x: 0, y: 0 }; 12 | for (let i = 0; i < s1.length; i++) { 13 | if (s1[i] !== s2[i]) { 14 | c[s1[i]]++; 15 | } 16 | } 17 | if ((c.x + c.y) % 2 === 1) { 18 | return -1; 19 | } 20 | return ((c.x / 2) | 0) + (c.x % 2) + ((c.y / 2) | 0) + (c.y % 2); 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/1249.Minimum Remove to Make Valid Parentheses/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "minimum-remove-to-make-valid-parentheses" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "minimum_remove_to_make_valid_parentheses" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1266.Minimum Time Visiting All Points/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { minTimeToVisitAllPoints }; 4 | 5 | /** 6 | * @param {number[][]} points 7 | * @return {number} 8 | */ 9 | function minTimeToVisitAllPoints(points) { 10 | let sec = 0; 11 | for (const [index, [x1, y1]] of points.slice(1).entries()) { 12 | const [x0, y0] = points[index]; 13 | sec += Math.max(Math.abs(x1 - x0), Math.abs(y0 - y1)); 14 | } 15 | return sec; 16 | } 17 | -------------------------------------------------------------------------------- /algorithms/1276.Number of Burgers with No Waste of Ingredients/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { numOfBurgers }; 4 | 5 | /** 6 | * @param {number} tomatoSlices 7 | * @param {number} cheeseSlices 8 | * @return {number[]} 9 | */ 10 | function numOfBurgers(tomatoSlices, cheeseSlices) { 11 | const jumbo = tomatoSlices / 2 - cheeseSlices; 12 | const small = cheeseSlices - jumbo; 13 | return jumbo % 1 !== 0 || jumbo < 0 || small < 0 ? [] : [jumbo, small]; 14 | } 15 | -------------------------------------------------------------------------------- /algorithms/1281.Subtract the Product and Sum of Digits of an Integer/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { subtractProductAndSum }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {number} 8 | */ 9 | function subtractProductAndSum(n) { 10 | let product = 1; 11 | let sum = 0; 12 | while (n) { 13 | const rem = n % 10; 14 | product *= rem; 15 | sum += rem; 16 | n = (n / 10) | 0; 17 | } 18 | return product - sum; 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/1287.Element Appearing More Than 25% In Sorted Array/README.md: -------------------------------------------------------------------------------- 1 | ## 1287. Element Appearing More Than 25% In Sorted Array 2 | 3 | https://leetcode.com/problems/element-appearing-more-than-25-in-sorted-array 4 | 5 | Given an integer array sorted in non-decreasing order, there is exactly one integer in the array that occurs more than 25% of the time. 6 | 7 | Return that integer. 8 | 9 | Example 1: 10 | Input: arr = [1,2,2,6,6,6,6,7,10] 11 | Output: 6 12 | 13 | Constraints: 14 | 15 | 1 <= arr.length <= 10^4 16 | 0 <= arr[i] <= 10^5 17 | -------------------------------------------------------------------------------- /algorithms/1287.Element Appearing More Than 25% In Sorted Array/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { findSpecialInteger }; 4 | 5 | /** 6 | * @param {number[]} arr 7 | * @return {number} 8 | */ 9 | function findSpecialInteger(arr) { 10 | let left = 0; 11 | for (let i = 0; i < arr.length; i++) { 12 | if (arr[left] !== arr[i]) { 13 | left = i; 14 | continue; 15 | } 16 | if (i - left >= ((arr.length / 4) | 0)) { 17 | return arr[left]; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /algorithms/1290.Convert Binary Number in a Linked List to Integer/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { getDecimalValue }; 4 | 5 | /** 6 | * Definition for singly-linked list. 7 | * function ListNode(val) { 8 | *     this.val = val 9 | *     this.next = null 10 | * } 11 | */ 12 | /** 13 | * @param {ListNode} head 14 | * @return {number} 15 | */ 16 | function getDecimalValue(head) { 17 | let bit = 0; 18 | while (head) { 19 | bit <<= 1; 20 | bit |= head.val; 21 | head = head.next; 22 | } 23 | return bit; 24 | } 25 | -------------------------------------------------------------------------------- /algorithms/1304.Find N Unique Integers Sum up to Zero/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { sumZero }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {number[]} 8 | */ 9 | function sumZero(n) { 10 | const result = []; 11 | if (n % 2 === 1) { 12 | result.push(0); 13 | } 14 | for (let i = 1; i <= n / 2; i++) { 15 | result.push(-i, i); 16 | } 17 | return result; 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/1304.Find N Unique Integers Sum up to Zero/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { sumZero } = require('./'); 6 | 7 | describe('#sumZero', () => { 8 | const tests = [ 9 | { 10 | n: 5, 11 | result: [0, -1, 1, -2, 2], 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ n, result }) => { 16 | it(`${n} -> ${result}`, () => { 17 | assert.deepStrictEqual(sumZero(n), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/1317.Convert Integer to the Sum of Two No-Zero Integers/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { getNoZeroIntegers }; 4 | 5 | /** 6 | * @param {number} n 7 | * @return {number[]} 8 | */ 9 | function getNoZeroIntegers(n) { 10 | for (let n1 = 1; n1 < n; n1++) { 11 | const n2 = n - n1; 12 | if (n1.toString().includes('0') || n2.toString().includes('0')) { 13 | continue; 14 | } 15 | return [n1, n2]; 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/1318.Minimum Flips to Make a OR b Equal to c/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { minFlips }; 4 | 5 | /** 6 | * @param {number} a 7 | * @param {number} b 8 | * @param {number} c 9 | * @return {number} 10 | */ 11 | function minFlips(a, b, c) { 12 | let count = 0; 13 | while (a || b || c) { 14 | count += c & 1 ? ((a | b) & 1) ^ 1 : (a & 1) + (b & 1); 15 | a >>= 1; 16 | b >>= 1; 17 | c >>= 1; 18 | } 19 | return count; 20 | } 21 | -------------------------------------------------------------------------------- /algorithms/1323.Maximum 69 Number/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { maximum69Number }; 4 | 5 | /** 6 | * @param {number} num 7 | * @return {number} 8 | */ 9 | function maximum69Number(num) { 10 | let digits = Math.pow(10, Math.log10(num) | 0); 11 | while (digits > 0) { 12 | const n = (num / digits) % 10 | 0; 13 | if (n === 6) { 14 | return num + digits * 3; 15 | } 16 | digits /= 10; 17 | } 18 | return num; 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/1323.Maximum 69 Number/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { maximum69Number } = require('./'); 6 | 7 | describe('#maximum69Number', () => { 8 | const tests = [ 9 | { 10 | num: 9669, 11 | result: 9969, 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ num, result }) => { 16 | it(`${num} -> ${result}`, () => { 17 | assert.deepStrictEqual(maximum69Number(num), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/1324.Print Words Vertically/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { printVertically } = require('./'); 6 | 7 | describe('#printVertically', () => { 8 | const tests = [ 9 | { 10 | s: 'HOW ARE YOU', 11 | result: ['HAY', 'ORO', 'WEU'], 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ s, result }) => { 16 | it(`${s} -> ${result}`, () => { 17 | assert.deepStrictEqual(printVertically(s), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/1328.Break a Palindrome/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { breakPalindrome }; 4 | 5 | /** 6 | * @param {string} palindrome 7 | * @return {string} 8 | */ 9 | function breakPalindrome(palindrome) { 10 | if (palindrome.length <= 1) { 11 | return ''; 12 | } 13 | for (let i = 0; i < ((palindrome.length / 2) | 0); i++) { 14 | if (palindrome[i] !== 'a') { 15 | return palindrome.slice(0, i) + 'a' + palindrome.slice(i + 1); 16 | } 17 | } 18 | return palindrome.slice(0, -1) + 'b'; 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/1329.Sort the Matrix Diagonally/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "sort-the-matrix-diagonally" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "sort_the_matrix_diagonally" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1332.Remove Palindromic Subsequences/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { removePalindromeSub } = require('./'); 6 | 7 | describe('#removePalindromeSub', () => { 8 | const tests = [ 9 | { 10 | s: 'ababa', 11 | result: 1, 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ s, result }) => { 16 | it(`${s} -> ${result}`, () => { 17 | assert.deepStrictEqual(removePalindromeSub(s), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/1337.The K Weakest Rows in a Matrix/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "the-k-weakest-rows-in-a-matrix" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "the_k_weakest_rows_in_a_matrix" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1337.The K Weakest Rows in a Matrix/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { kWeakestRows }; 4 | 5 | /** 6 | * @param {number[][]} mat 7 | * @param {number} k 8 | * @return {number[]} 9 | */ 10 | function kWeakestRows(mat, k) { 11 | return mat 12 | .map((row, index) => [index, row.filter(n => n === 1).length]) 13 | .sort(([i1, c1], [i2, c2]) => c1 - c2 || i1 - i2) 14 | .map(([index]) => index) 15 | .slice(0, k); 16 | } 17 | -------------------------------------------------------------------------------- /algorithms/1338.Reduce Array Size to The Half/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { minSetSize } = require('./'); 6 | 7 | describe('#minSetSize', () => { 8 | const tests = [ 9 | { 10 | arr: [3, 3, 3, 3, 5, 5, 5, 2, 2, 7], 11 | result: 2, 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ arr, result }) => { 16 | it(`${arr} -> ${result}`, () => { 17 | assert.deepStrictEqual(minSetSize(arr), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/1342.Number of Steps to Reduce a Number to Zero/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "number-of-steps-to-reduce-a-number-to-zero" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "number_of_steps_to_reduce_a_number_to_zero" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1344.Angle Between Hands of a Clock/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { angleClock }; 4 | 5 | /** 6 | * @param {number} hour 7 | * @param {number} minutes 8 | * @return {number} 9 | */ 10 | function angleClock(hour, minutes) { 11 | const angleM = minutes * (360 / 60); 12 | const angleH = ((hour % 12) + minutes / 60) * (360 / 12); 13 | const diff = Math.abs(angleH - angleM); 14 | return Math.min(diff, 360 - diff); 15 | } 16 | -------------------------------------------------------------------------------- /algorithms/1346.Check If N and Its Double Exist/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { checkIfExist }; 4 | 5 | /** 6 | * @param {number[]} arr 7 | * @return {boolean} 8 | */ 9 | function checkIfExist(arr) { 10 | const set = new Set(); 11 | for (const n of arr) { 12 | if (set.has(n * 2) || set.has(n / 2)) { 13 | return true; 14 | } 15 | set.add(n); 16 | } 17 | return false; 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/1347.Minimum Number of Steps to Make Two Strings Anagram/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { minSteps }; 4 | 5 | /** 6 | * @param {string} s 7 | * @param {string} t 8 | * @return {number} 9 | */ 10 | function minSteps(s, t) { 11 | const countMap = {}; 12 | for (const c of t) { 13 | countMap[c] = ++countMap[c] || 1; 14 | } 15 | let count = 0; 16 | for (const c of s) { 17 | if (countMap[c]) { 18 | countMap[c]--; 19 | continue; 20 | } 21 | count++; 22 | } 23 | return count; 24 | } 25 | -------------------------------------------------------------------------------- /algorithms/1347.Minimum Number of Steps to Make Two Strings Anagram/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { minSteps } = require('./'); 6 | 7 | describe('#minSteps', () => { 8 | const tests = [ 9 | { 10 | s: 'bab', 11 | t: 'aba', 12 | result: 1, 13 | }, 14 | ]; 15 | 16 | _.forEach(tests, ({ s, t, result }) => { 17 | it(`${s}, ${t} -> ${result}`, () => { 18 | assert.deepStrictEqual(minSteps(s, t), result); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /algorithms/1351.Count Negative Numbers in a Sorted Matrix/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { countNegatives }; 4 | 5 | /** 6 | * @param {number[][]} grid 7 | * @return {number} 8 | */ 9 | function countNegatives(grid) { 10 | const [row] = grid; 11 | let count = grid.length * row.length; 12 | let x = row.length - 1; 13 | let y = 0; 14 | while (y < grid.length && x >= 0) { 15 | if (grid[y][x] < 0) { 16 | x--; 17 | continue; 18 | } 19 | count -= x + 1; 20 | y++; 21 | } 22 | return count; 23 | } 24 | -------------------------------------------------------------------------------- /algorithms/1362.Closest Divisors/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { closestDivisors }; 4 | 5 | /** 6 | * @param {number} num 7 | * @return {number[]} 8 | */ 9 | function closestDivisors(num) { 10 | const n1 = num + 1; 11 | const n2 = num + 2; 12 | for (let n = Math.sqrt(n2) | 0; n >= 0; n--) { 13 | if ((n1 / n) % 1 === 0) { 14 | return [n, n1 / n]; 15 | } 16 | if ((n2 / n) % 1 === 0) { 17 | return [n, n2 / n]; 18 | } 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /algorithms/1366.Rank Teams by Votes/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | const { rankTeams } = require('./'); 6 | 7 | describe('#rankTeams', () => { 8 | const tests = [ 9 | { 10 | votes: ['ABC', 'ACB', 'ABC', 'ACB', 'ACB'], 11 | result: 'ACB', 12 | }, 13 | ]; 14 | 15 | _.forEach(tests, ({ votes, result }) => { 16 | it(`${votes} -> ${result}`, () => { 17 | assert.deepStrictEqual(rankTeams(votes), result); 18 | }); 19 | }); 20 | }); 21 | -------------------------------------------------------------------------------- /algorithms/1385.Find the Distance Value Between Two Arrays/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { findTheDistanceValue }; 4 | 5 | /** 6 | * @param {number[]} arr1 7 | * @param {number[]} arr2 8 | * @param {number} d 9 | * @return {number} 10 | */ 11 | function findTheDistanceValue(arr1, arr2, d) { 12 | let count = 0; 13 | loop: for (const n1 of arr1) { 14 | for (const n2 of arr2) { 15 | if (Math.abs(n1 - n2) <= d) { 16 | continue loop; 17 | } 18 | } 19 | count++; 20 | } 21 | return count; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/1413.Minimum Value to Get Positive Step by Step Sum/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { minStartValue }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {number} 8 | */ 9 | function minStartValue(nums) { 10 | let cur = 1; 11 | let result = 1; 12 | for (const n of nums) { 13 | cur += n; 14 | if (cur >= 1) { 15 | continue; 16 | } 17 | result -= cur - 1; 18 | cur = 1; 19 | } 20 | return result; 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/1417.Reformat The String/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { reformat }; 4 | 5 | /** 6 | * @param {string} s 7 | * @return {string} 8 | */ 9 | function reformat(s) { 10 | const queue = [[], []]; 11 | for (const c of s) { 12 | queue[~~isNaN(c)].push(c); 13 | } 14 | const [first, second] = queue.sort((r1, r2) => r2.length - r1.length); 15 | if (first.length - second.length > 1) { 16 | return ''; 17 | } 18 | return first.reduce((str, char, index) => str + char + (second[index] || ''), ''); 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/1426.Counting Elements/index.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} arr 3 | * @return {number} 4 | */ 5 | function countElements(arr) { 6 | const set = new Set(arr.map(n => n - 1)); 7 | return arr.reduce((sum, num) => sum + ~~set.has(num), 0); 8 | } 9 | -------------------------------------------------------------------------------- /algorithms/1426.Counting Elements/solution.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func countElements(arr []int) int { 4 | set := make(map[int]struct{}) 5 | for _, n := range arr { 6 | set[n-1] = struct{}{} 7 | } 8 | var cnt int 9 | for _, n := range arr { 10 | if _, ok := set[n]; ok { 11 | cnt++ 12 | } 13 | } 14 | return cnt 15 | } 16 | -------------------------------------------------------------------------------- /algorithms/1426.Counting Elements/solution.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countElements(self, arr: List[int]) -> int: 3 | s = set(arr) 4 | return sum(n + 1 in s for n in arr) 5 | -------------------------------------------------------------------------------- /algorithms/1427.Perform String Shifts/README.md: -------------------------------------------------------------------------------- 1 | ## perform String Shifts 2 | -------------------------------------------------------------------------------- /algorithms/1427.Perform String Shifts/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { stringShift }; 4 | 5 | /** 6 | * @param {string} s 7 | * @param {number[][]} shift 8 | * @return {string} 9 | */ 10 | function stringShift(s, shift) { 11 | const count = shift.reduce((sum, [direction, amount]) => sum + (direction === 0 ? amount : -amount), 0) % s.length; 12 | return s.slice(count) + s.slice(0, count); 13 | } 14 | -------------------------------------------------------------------------------- /algorithms/1428.Leftmost Column with at Least a One/README.md: -------------------------------------------------------------------------------- 1 | ## Leftmost Column with at Least a One 2 | -------------------------------------------------------------------------------- /algorithms/1437.Check If All 1's Are at Least Length K Places Away/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "check-if-all-1-s-are-at-least-length-k-places-away" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "check_if_all_1_s_are_at_least_length_k_places_away" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1456.Maximum Number of Vowels in a Substring of Given Length/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { maxVowels }; 4 | 5 | /** 6 | * @param {string} s 7 | * @param {number} k 8 | * @return {number} 9 | */ 10 | function maxVowels(s, k) { 11 | let max = 0; 12 | let count = 0; 13 | const set = new Set('aeiou'); 14 | for (let i = 0; i < s.length; i++) { 15 | count += ~~set.has(s[i]) - ~~set.has(s[i - k]); 16 | max = Math.max(max, count); 17 | } 18 | return max; 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/1461.Check If a String Contains All Binary Codes of Size K/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { hasAllCodes }; 4 | 5 | /** 6 | * @param {string} s 7 | * @param {number} k 8 | * @return {boolean} 9 | */ 10 | function hasAllCodes(s, k) { 11 | const set = new Set(); 12 | for (let right = k; right <= s.length; right++) { 13 | set.add(s.slice(right - k, right)); 14 | } 15 | return set.size === 2 ** k; 16 | } 17 | -------------------------------------------------------------------------------- /algorithms/1486.XOR Operation in an Array/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { xorOperation }; 4 | 5 | /** 6 | * @param {number} n 7 | * @param {number} start 8 | * @return {number} 9 | */ 10 | function xorOperation(n, start) { 11 | let res = 0; 12 | while (--n >= 0) { 13 | res ^= start + n * 2; 14 | } 15 | return res; 16 | } 17 | -------------------------------------------------------------------------------- /algorithms/1497.Check If Array Pairs Are Divisible by k/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { canArrange }; 4 | 5 | /** 6 | * @param {number[]} arr 7 | * @param {number} k 8 | * @return {boolean} 9 | */ 10 | function canArrange(arr, k) { 11 | const memo = Array(k).fill(0); 12 | for (const n of arr) { 13 | const pair = (k - (n % k)) % k; 14 | if (memo[pair] > 0) { 15 | memo[pair]--; 16 | continue; 17 | } 18 | 19 | memo[(k - pair) % k]++; 20 | } 21 | 22 | return memo.every((n) => n === 0); 23 | } 24 | -------------------------------------------------------------------------------- /algorithms/1503.Last Moment Before All Ants Fall Out of a Plank/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { getLastMoment }; 4 | 5 | /** 6 | * @param {number} n 7 | * @param {number[]} left 8 | * @param {number[]} right 9 | * @return {number} 10 | */ 11 | function getLastMoment(n, left, right) { 12 | return Math.max(Math.max(...left), n - Math.min(...right)); 13 | } 14 | -------------------------------------------------------------------------------- /algorithms/1512.Number of Good Pairs/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { numIdenticalPairs }; 4 | 5 | /** 6 | * @param {number[]} nums 7 | * @return {number} 8 | */ 9 | function numIdenticalPairs(nums) { 10 | const counts = Array(101).fill(0); 11 | let result = 0; 12 | for (const num of nums) { 13 | result += counts[num]++; 14 | } 15 | return result; 16 | } 17 | -------------------------------------------------------------------------------- /algorithms/1513.Number of Substrings With Only 1s/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { numSub }; 4 | 5 | /** 6 | * @param {string} s 7 | * @return {number} 8 | */ 9 | function numSub(s) { 10 | let cur = 0; 11 | let result = 0; 12 | const mod = 1e9 + 7; 13 | for (const c of s) { 14 | if (c === '0') { 15 | cur = 0; 16 | continue; 17 | } 18 | result = (result + ++cur) % mod; 19 | } 20 | return result; 21 | } 22 | -------------------------------------------------------------------------------- /algorithms/1513.Number of Substrings With Only 1s/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const _ = require('lodash'); 5 | 6 | const { numSub } = require('./'); 7 | 8 | describe('#numSub', () => { 9 | const tests = [ 10 | { 11 | s: '0110111', 12 | result: 9, 13 | }, 14 | ]; 15 | 16 | _.forEach(tests, ({ s, result }) => { 17 | it(`${s} -> ${result}`, () => { 18 | assert.deepStrictEqual(numSub(s), result); 19 | }); 20 | }); 21 | }); 22 | -------------------------------------------------------------------------------- /algorithms/1518.Water Bottles/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { numWaterBottles }; 4 | 5 | /** 6 | * @param {number} numBottles 7 | * @param {number} numExchange 8 | * @return {number} 9 | */ 10 | function numWaterBottles(numBottles, numExchange) { 11 | let ans = numBottles; 12 | while (numBottles >= numExchange) { 13 | const rem = numBottles % numExchange; 14 | numBottles = Math.floor(numBottles / numExchange); 15 | ans += numBottles; 16 | numBottles += rem; 17 | } 18 | return ans; 19 | } 20 | -------------------------------------------------------------------------------- /algorithms/1557.Minimum Number of Vertices to Reach All Nodes/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { findSmallestSetOfVertices }; 4 | 5 | /** 6 | * @param {number} n 7 | * @param {number[][]} edges 8 | * @return {number[]} 9 | */ 10 | function findSmallestSetOfVertices(n, edges) { 11 | const result = new Set(Array.from({ length: n }, (_, i) => i)); 12 | for (const [, dest] of edges) { 13 | result.delete(dest); 14 | } 15 | 16 | return Array.from(result); 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/1631.Path With Minimum Effort/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "path-with-minimum-effort" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "path_with_minimum_effort" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1641.Count Sorted Vowel Strings/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "count-sorted-vowel-strings" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "count_sorted_vowel_strings" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1646.Get Maximum in Generated Array/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "get-maximum-in-generated-array" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "get_maximum_in_generated_array" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1649.Create Sorted Array through Instructions/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "create-sorted-array-through-instructions" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "create_sorted_array_through_instructions" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1657.Determine if Two Strings Are Close/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "determine-if-two-strings-are-close" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "determine_if_two_strings_are_close" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1658.Minimum Operations to Reduce X to Zero/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "minimum-operations-to-reduce-x-to-zero" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "minimum_operations_to_reduce_x_to_zero" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1662.Check If Two String Arrays are Equivalent/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "check-if-two-string-arrays-are-equivalent" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "check_if_two_string_arrays_are_equivalent" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1663.Smallest String With A Given Numeric Value/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "smallest-string-with-a-given-numeric-value" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "smallest_string_with_a_given_numeric_value" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1672.Richest Customer Wealth/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { maximumWealth }; 4 | 5 | const _ = require('lodash'); 6 | 7 | /** 8 | * @param {number[][]} accounts 9 | * @return {number} 10 | */ 11 | function maximumWealth(accounts) { 12 | return _.chain(accounts).map(_.sum).max().value(); 13 | } 14 | -------------------------------------------------------------------------------- /algorithms/1679.Max Number of K-Sum Pairs/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "max-number-of-k-sum-pairs" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "max_number_of_k_sum_pairs" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1680.Concatenation of Consecutive Binary Numbers/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "concatenation-of-consecutive-binary-numbers" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "concatenation_of_consecutive_binary_numbers" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /algorithms/1791.Find Center of Star Graph/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { findCenter }; 4 | 5 | /** 6 | * @param {number[][]} edges 7 | * @return {number} 8 | */ 9 | function findCenter(edges) { 10 | const [[n1, n2], [n3, n4]] = edges; 11 | return n1 === n3 || n1 === n4 ? n1 : n2; 12 | } 13 | -------------------------------------------------------------------------------- /algorithms/1822.Sign of the Product of an Array/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { arraySign }; 4 | 5 | const mask = ~(-1 >>> 1); 6 | 7 | /** 8 | * @param {number[]} nums 9 | * @return {number} 10 | */ 11 | function arraySign(nums) { 12 | let sign = 1; 13 | for (const n of nums) { 14 | if (n === 0) { 15 | return 0; 16 | } 17 | 18 | sign ^= (n & mask) === mask; 19 | } 20 | 21 | return sign === 1 ? 1 : -1; 22 | } 23 | -------------------------------------------------------------------------------- /algorithms/2619.Array Prototype Last Easy/index.js: -------------------------------------------------------------------------------- 1 | Array.prototype.last = function () { 2 | return this[this.length - 1] ?? -1; 3 | }; 4 | 5 | Array.prototype.last = function () { 6 | return this.slice(-1).pop() ?? -1; 7 | }; 8 | 9 | Array.prototype.last = function () { 10 | return this.at(-1) ?? -1; 11 | }; 12 | 13 | /** 14 | * const arr = [1, 2, 3]; 15 | * arr.last(); // 3 16 | */ 17 | -------------------------------------------------------------------------------- /algorithms/2678.Number of Senior Citizens/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { countSeniors }; 4 | 5 | const start = 10 + 1; 6 | const end = start + 2; 7 | 8 | /** 9 | * @param {string[]} details 10 | * @return {number} 11 | */ 12 | function countSeniors(details) { 13 | return details.reduce((acc, cur) => acc + Number(Number(cur.slice(start, end)) > 60), 0); 14 | } 15 | -------------------------------------------------------------------------------- /algorithms/util.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | // TODO: delete 4 | module.exports = require('./utils'); 5 | -------------------------------------------------------------------------------- /algorithms/utils/Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "utils" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html 8 | 9 | [lib] 10 | name = 'utils' 11 | path = 'lib.rs' 12 | -------------------------------------------------------------------------------- /algorithms/utils/TreeNode.cs: -------------------------------------------------------------------------------- 1 | public class TreeNode 2 | { 3 | public int val; 4 | public TreeNode left; 5 | public TreeNode right; 6 | 7 | public TreeNode(int val = 0, TreeNode left = null, TreeNode right = null) 8 | { 9 | this.val = val; 10 | this.left = left; 11 | this.right = right; 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /algorithms/utils/combination.js: -------------------------------------------------------------------------------- 1 | function factorial(n) { 2 | let f = n; 3 | if (n <= 0) { 4 | return 1; 5 | } 6 | while (--n) { 7 | f *= n; 8 | } 9 | return f; 10 | } 11 | 12 | function combination(n, r) { 13 | if (r <= 0) { 14 | return 1; 15 | } 16 | return factorial(n) / (factorial(r) * factorial(n - r)); 17 | } 18 | -------------------------------------------------------------------------------- /algorithms/utils/euclidean.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * generate greatest common divisor (GCD) 5 | */ 6 | function euclidean(a, b) { 7 | if (a === 0 || b === 0) { 8 | return 1; 9 | } 10 | if (a < b) { 11 | [a, b] = [b, a]; 12 | } 13 | let r = a % b; 14 | while (r !== 0) { 15 | [a, b, r] = [b, r, b % r]; 16 | } 17 | return b; 18 | } 19 | -------------------------------------------------------------------------------- /algorithms/utils/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.assign(exports, ...['./prime', './priorityQueue', './tree', './quickSort', './larssonSadakane'].map(require)); 4 | -------------------------------------------------------------------------------- /algorithms/utils/lib.rs: -------------------------------------------------------------------------------- 1 | mod list; 2 | mod tree; 3 | pub use list::*; 4 | pub use tree::*; 5 | -------------------------------------------------------------------------------- /algorithms/utils/prime.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | Object.assign(exports, { getPrimes }); 4 | 5 | function getPrimes(max) { 6 | const sieve = Array(max); 7 | const primes = max < 2 ? [] : [2]; 8 | for (let i = 3; i <= max; i += 2) { 9 | if (sieve[i]) { 10 | continue; 11 | } 12 | primes.push(i); 13 | for (let j = i << 1; j <= max; j += i) { 14 | sieve[j] = true; 15 | } 16 | } 17 | return primes; 18 | } 19 | -------------------------------------------------------------------------------- /gulp/tasks/index.ts: -------------------------------------------------------------------------------- 1 | export * from './build'; 2 | export * from './init'; 3 | -------------------------------------------------------------------------------- /gulp/template/js/index.js.tpl: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { template }; 4 | 5 | -------------------------------------------------------------------------------- /gulp/template/js/test.js.tpl: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | 5 | const { template } = require('./'); 6 | 7 | describe('#template', () => { 8 | 9 | const tests = $1; 10 | 11 | for (const { $2 result } of tests) { 12 | it(`$3 -> ${result}`, () => { 13 | assert.deepStrictEqual(template($4), result); 14 | }); 15 | } 16 | }); 17 | -------------------------------------------------------------------------------- /gulp/template/rust/Cargo.toml.tpl: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "$1" 3 | version = "0.1.0" 4 | authors = ["Suguru Motegi "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | 9 | [lib] 10 | name = "$2" 11 | path = "solution.rs" 12 | -------------------------------------------------------------------------------- /gulp/template/rust/solution.rs.tpl: -------------------------------------------------------------------------------- 1 | pub struct Solution; 2 | 3 | $1 4 | 5 | #[test] 6 | fn test1() { 7 | assert_eq!($2); 8 | } 9 | 10 | -------------------------------------------------------------------------------- /gulpfile.ts: -------------------------------------------------------------------------------- 1 | export * from './gulp/tasks'; 2 | -------------------------------------------------------------------------------- /lib/test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const _ = require('lodash'); 4 | const { Agent } = require('vm-agent'); 5 | 6 | Object.assign(exports, { getTasks }); 7 | 8 | function getTasks(filepath) { 9 | let tasks; 10 | const describe = (name, func) => { 11 | const lodash = _.clone(_); 12 | lodash.forEach = _.noop; 13 | const result = new Agent(func, { _: lodash, console }).run().getInnerVariable(); 14 | tasks = result.tests; 15 | }; 16 | new Agent(filepath, { describe }).run(); 17 | return tasks; 18 | } 19 | -------------------------------------------------------------------------------- /packages.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | -------------------------------------------------------------------------------- /template/Template.java: -------------------------------------------------------------------------------- 1 | import java.util.*; 2 | public class Test { 3 | public static void main(String[] args) { 4 | <% tasks %> 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /template/template.rb: -------------------------------------------------------------------------------- 1 | require <% filepath %> 2 | 3 | p <% func %>(<% args %>) 4 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "lib": ["esnext", "dom"], 4 | "target": "esnext", 5 | "noEmit": true, 6 | "module": "commonjs", 7 | "moduleResolution": "node", 8 | "resolveJsonModule": true, 9 | "noImplicitReturns": true 10 | } 11 | } 12 | --------------------------------------------------------------------------------