├── README.md ├── Algorithms ├── 0001-1000 │ ├── 0292. Nim Game │ │ └── Solution.java │ ├── 0319. Bulb Switcher │ │ └── Solution.java │ ├── 0403. Frog Jump │ │ └── 0403. Frog Jump.md │ ├── 0488. Zuma Game │ │ └── 0488. Zuma Game.md │ ├── 0656. Coin Path │ │ └── 0656. Coin Path.md │ ├── 0565. Array Nesting │ │ └── 0565. Array Nesting.md │ ├── 0130. Surrounded Regions │ │ └── 0130. Surrounded Regions.md │ ├── 0226. Invert Binary Tree │ │ └── 0226. Invert Binary Tree.md │ ├── 0796. Rotate String │ │ └── Solution.java │ ├── 0092. Reverse Linked List II │ │ └── 0092. Reverse Linked List II.md │ ├── 0191. Number of 1 Bits │ │ └── Solution.java │ ├── 0492. Construct the Rectangle │ │ ├── 0492. Construct the Rectangle.md │ │ └── Solution.java │ ├── 0169. Majority Element │ │ ├── Solution.java │ │ └── 0169. Majority Element.md │ ├── 0258. Add Digits │ │ └── Solution.java │ ├── 0836. Rectangle Overlap │ │ └── Solution.java │ ├── 0231. Power of Two │ │ └── Solution.java │ ├── 0334. Increasing Triplet Subsequence │ │ ├── 0334. Increasing Triplet Subsequence.md │ │ └── Solution.java │ ├── 0405. Convert a Number to Hexadecimal │ │ └── 0405. Convert a Number to Hexadecimal.md │ ├── 0136. Single Number │ │ └── Solution.java │ ├── 0326. Power of Three │ │ └── Solution.java │ ├── 0357. Count Numbers with Unique Digits │ │ ├── 0357. Count Numbers with Unique Digits.md │ │ └── Solution.java │ ├── 0441. Arranging Coins │ │ └── Solution.java │ ├── 0058. Length of Last Word │ │ └── Solution.java │ ├── 0338. Counting Bits │ │ └── Solution.java │ ├── 0521. Longest Uncommon Subsequence I │ │ └── Solution.java │ ├── 0476. Number Complement │ │ └── Solution.java │ ├── 0371. Sum of Two Integers │ │ ├── Solution.java │ │ └── 0371. Sum of Two Integers.md │ ├── 0458. Poor Pigs │ │ └── Solution.java │ ├── 0810. Chalkboard XOR Game │ │ └── Solution.java │ ├── 0137. Single Number II │ │ └── Solution.java │ ├── 0172. Factorial Trailing Zeroes │ │ └── Solution.java │ ├── 0561. Array Partition I │ │ └── Solution.java │ ├── 0908. Smallest Range I │ │ └── Solution.java │ ├── 0217. Contains Duplicate │ │ └── Solution.java │ ├── 0268. Missing Number │ │ └── Solution.java │ ├── 0852. Peak Index in a Mountain Array │ │ └── Solution.java │ ├── 0961. N-Repeated Element in Size 2N Array │ │ └── Solution.java │ ├── 0055. Jump Game │ │ └── Solution.java │ ├── 0233. Number of Digit One │ │ └── Solution.java │ ├── 0190. Reverse Bits │ │ └── Solution.java │ ├── 0991. Broken Calculator │ │ └── Solution.java │ ├── 0344. Reverse String │ │ └── Solution.java │ ├── 0518. Coin Change 2 │ │ └── Solution.java │ ├── 0453. Minimum Moves to Equal Array Elements │ │ └── Solution.java │ ├── 0342. Power of Four │ │ └── Solution.java │ ├── 0660. Remove 9 │ │ └── Solution.java │ ├── 0651. 4 Keys Keyboard │ │ └── Solution.java │ ├── 0168. Excel Sheet Column Title │ │ └── Solution.java │ ├── 0575. Distribute Candies │ │ └── Solution.java │ ├── 0053. Maximum Subarray │ │ └── Solution.java │ ├── 0070. Climbing Stairs │ │ └── Solution.java │ ├── 0122. Best Time to Buy and Sell Stock II │ │ └── Solution.java │ ├── 0867. Transpose Matrix │ │ └── Solution.java │ ├── 0746. Min Cost Climbing Stairs │ │ └── Solution.java │ ├── 0754. Reach a Number │ │ └── Solution.java │ ├── 0237. Delete Node in a Linked List │ │ └── Solution.java │ ├── 0509. Fibonacci Number │ │ └── Solution.java │ ├── 0769. Max Chunks To Make Sorted │ │ └── Solution.java │ ├── 0976. Largest Perimeter Triangle │ │ └── Solution.java │ ├── 0075. Sort Colors │ │ └── Solution.java │ ├── 0462. Minimum Moves to Equal Array Elements II │ │ └── Solution.java │ ├── 0634. Find the Derangement of An Array │ │ └── Solution.java │ ├── 0837. New 21 Game │ │ └── Solution.java │ ├── 0461. Hamming Distance │ │ └── Solution.java │ ├── 0779. K-th Symbol in Grammar │ │ └── Solution.java │ ├── 0442. Find All Duplicates in an Array │ │ └── Solution.java │ ├── 0881. Boats to Save People │ │ └── Solution.java │ ├── 0276. Paint Fence │ │ └── Solution.java │ ├── 0283. Move Zeroes │ │ └── Solution.java │ ├── 0633. Sum of Square Numbers │ │ └── Solution.java │ ├── 0263. Ugly Number │ │ └── Solution.java │ ├── 0009. Palindrome Number │ │ └── Solution.java │ ├── 0096. Unique Binary Search Trees │ │ └── Solution.java │ ├── 0171. Excel Sheet Column Number │ │ └── Solution.java │ ├── 0045. Jump Game II │ │ └── Solution.java │ ├── 0062. Unique Paths │ │ └── Solution.java │ ├── 0397. Integer Replacement │ │ └── Solution.java │ ├── 0598. Range Addition II │ │ └── Solution.java │ ├── 0643. Maximum Average Subarray I │ │ └── Solution.java │ ├── 0717. 1-bit and 2-bit Characters │ │ └── Solution.java │ ├── 0829. Consecutive Numbers Sum │ │ └── Solution.java │ ├── 0007. Reverse Integer │ │ └── Solution.java │ ├── 0704. Binary Search │ │ └── Solution.java │ ├── 0771. Jewels and Stones │ │ └── Solution.java │ ├── 0121. Best Time to Buy and Sell Stock │ │ └── Solution.java │ ├── 0412. Fizz Buzz │ │ └── Solution.java │ ├── 0945. Minimum Increment to Make Array Unique │ │ └── Solution.java │ ├── 0035. Search Insert Position │ │ └── Solution.java │ ├── 0343. Integer Break │ │ └── Solution.java │ ├── 0693. Binary Number with Alternating Bits │ │ └── Solution.java │ ├── 0790. Domino and Tromino Tiling │ │ └── Solution.java │ ├── 0944. Delete Columns to Make Sorted │ │ └── Solution.java │ ├── 0324. Wiggle Sort II │ │ └── Solution.java │ ├── 0377. Combination Sum IV │ │ └── Solution.java │ ├── 0223. Rectangle Area │ │ └── Solution.java │ ├── 0275. H-Index II │ │ └── Solution.java │ ├── 0920. Number of Music Playlists │ │ └── Solution.java │ ├── 0219. Contains Duplicate II │ │ └── Solution.java │ ├── 0260. Single Number III │ │ └── Solution.java │ ├── 0278. First Bad Version │ │ └── Solution.java │ ├── 0470. Implement Rand10() Using Rand7() │ │ └── Solution.java │ ├── 0485. Max Consecutive Ones │ │ └── Solution.java │ ├── 0198. House Robber │ │ └── Solution.java │ ├── 0504. Base 7 │ │ └── Solution.java │ ├── 0788. Rotated Digits │ │ └── Solution.java │ ├── 0201. Bitwise AND of Numbers Range │ │ └── Solution.java │ ├── 0370. Range Addition │ │ └── Solution.java │ ├── 0686. Repeated String Match │ │ └── Solution.java │ ├── 0482. License Key Formatting │ │ └── Solution.java │ ├── 0997. Find the Town Judge │ │ └── Solution.java │ ├── 0214. Shortest Palindrome │ │ └── Solution.java │ ├── 0495. Teemo Attacking │ │ └── Solution.java │ ├── 0891. Sum of Subsequence Widths │ │ └── Solution.java │ ├── 0434. Number of Segments in a String │ │ ├── Solution.java │ │ └── 0434. Number of Segments in a String.md │ ├── 0274. H-Index │ │ └── Solution.java │ ├── 0483. Smallest Good Base │ │ └── Solution.java │ ├── 0760. Find Anagram Mappings │ │ └── Solution.java │ ├── 0896. Monotonic Array │ │ └── Solution.java │ ├── 0206. Reverse Linked List │ │ └── Solution.java │ ├── 0629. K Inverse Pairs Array │ │ └── Solution.java │ ├── 0848. Shifting Letters │ │ └── Solution.java │ ├── 0910. Smallest Range II │ │ └── Solution.java │ ├── 0220. Contains Duplicate III │ │ └── Solution.java │ ├── 0287. Find the Duplicate Number │ │ └── Solution.java │ ├── 0389. Find the Difference │ │ └── Solution.java │ ├── 0088. Merge Sorted Array │ │ └── Solution.java │ ├── 0112. Path Sum │ │ └── Solution.java │ ├── 0396. Rotate Function │ │ └── Solution.java │ ├── 0876. Middle of the Linked List │ │ └── Solution.java │ ├── 0877. Stone Game │ │ └── Solution.java │ ├── 0922. Sort Array By Parity II │ │ └── Solution.java │ ├── 0093. Restore IP Addresses │ │ └── 0093. Restore IP Addresses.md │ ├── 0255. Verify Preorder Sequence in Binary Search Tree │ │ └── Solution.java │ ├── 0494. Target Sum │ │ └── Solution.java │ ├── 0266. Palindrome Permutation │ │ └── Solution.java │ ├── 0365. Water and Jug Problem │ │ └── Solution.java │ ├── 0486. Predict the Winner │ │ └── Solution.java │ ├── 0242. Valid Anagram │ │ └── Solution.java │ ├── 0537. Complex Number Multiplication │ │ └── Solution.java │ ├── 0560. Subarray Sum Equals K │ │ └── Solution.java │ ├── 0789. Escape The Ghosts │ │ └── Solution.java │ ├── 0921. Minimum Add to Make Parentheses Valid │ │ └── Solution.java │ ├── 0151. Reverse Words in a String │ │ └── Solution.java │ ├── 0386. Lexicographical Numbers │ │ └── Solution.java │ ├── 0398. Random Pick Index │ │ └── Solution.java │ ├── 0718. Maximum Length of Repeated Subarray │ │ └── Solution.java │ ├── 0946. Validate Stack Sequences │ │ └── Solution.java │ ├── 0515. Find Largest Value in Each Tree Row │ │ └── 0515. Find Largest Value in Each Tree Row.md │ ├── 0780. Reaching Points │ │ └── Solution.java │ ├── 0257. Binary Tree Paths │ │ └── 0257. Binary Tree Paths.md │ ├── 0383. Ransom Note │ │ └── Solution.java │ ├── 0775. Global and Local Inversions │ │ └── Solution.java │ ├── 0392. Is Subsequence │ │ └── Solution.java │ ├── 0667. Beautiful Arrangement II │ │ └── Solution.java │ ├── 0915. Partition Array into Disjoint Intervals │ │ └── Solution.java │ ├── 0973. K Closest Points to Origin │ │ └── Solution.java │ ├── 0011. Container With Most Water │ │ └── Solution.java │ ├── 0022. Generate Parentheses │ │ └── Solution.java │ ├── 0026. Remove Duplicates from Sorted Array │ │ └── Solution.java │ ├── 0119. Pascal's Triangle II │ │ └── Solution.java │ ├── 0367. Valid Perfect Square │ │ └── Solution.java │ ├── 0724. Find Pivot Index │ │ └── Solution.java │ ├── 0806. Number of Lines To Write String │ │ └── Solution.java │ ├── 0888. Fair Candy Swap │ │ └── Solution.java │ ├── 0215. Kth Largest Element in an Array │ │ └── Solution.java │ ├── 0709. To Lower Case │ │ └── 0709. To Lower Case.md │ └── 0713. Subarray Product Less Than K │ │ └── Solution.java └── 1001-2000 │ ├── 1227. Airplane Seat Assignment Probability │ └── Solution.java │ ├── 1108. Defanging an IP Address │ └── Solution.java │ ├── 1908. Game of Nim │ └── Solution.java │ ├── 1419. Minimum Number of Frogs Croaking │ └── 1419. Minimum Number of Frogs Croaking.md │ ├── 1460. Make Two Arrays Equal by Reversing Sub-arrays │ └── Solution.java │ ├── 1551. Minimum Operations to Make Array Equal │ └── Solution.java │ ├── 1678. Goal Parser Interpretation │ └── Solution.java │ ├── 1523. Count Odd Numbers in an Interval Range │ └── Solution.java │ ├── 1486. XOR Operation in an Array │ └── Solution.java │ ├── 1913. Maximum Product Difference Between Two Pairs │ └── Solution.java │ ├── 1418. Display Table of Food Orders in a Restaurant │ └── 1418. Display Table of Food Orders in a Restaurant.md │ ├── 1688. Count of Matches in Tournament │ └── Solution.java │ ├── 1812. Determine Color of a Chessboard Square │ └── Solution.java │ ├── 1823. Find the Winner of the Circular Game │ └── Solution.java │ ├── 1556. Thousand Separator │ └── Solution.java │ ├── 1837. Sum of Digits in Base K │ └── Solution.java │ ├── 1920. Build Array from Permutation │ └── Solution.java │ ├── 1009. Complement of Base 10 Integer │ └── Solution.java │ ├── 1295. Find Numbers with Even Number of Digits │ └── Solution.java │ ├── 1816. Truncate Sentence │ └── Solution.java │ ├── 1835. Find XOR Sum of All Pairs Bitwise AND │ └── Solution.java │ ├── 1037. Valid Boomerang │ └── Solution.java │ ├── 1470. Shuffle the Array │ └── Solution.java │ ├── 1780. Check if Number is a Sum of Powers of Three │ └── Solution.java │ ├── 1480. Running Sum of 1d Array │ └── Solution.java │ ├── 1720. Decode XORed Array │ └── Solution.java │ ├── 1967. Number of Strings That Appear as Substrings in Word │ └── Solution.java │ ├── 1929. Concatenation of Array │ └── Solution.java │ ├── 1550. Three Consecutive Odds │ └── Solution.java │ ├── 1342. Number of Steps to Reduce a Number to Zero │ └── Solution.java │ ├── 1689. Partitioning Into Minimum Number Of Deci-Binary Numbers │ └── Solution.java │ ├── 1822. Sign of the Product of an Array │ └── Solution.java │ ├── 1903. Largest Odd Number in String │ └── Solution.java │ ├── 1874. Minimize Product Sum of Two Arrays │ └── Solution.java │ ├── 1910. Remove All Occurrences of a Substring │ └── Solution.java │ ├── 1134. Armstrong Number │ └── Solution.java │ ├── 1952. Three Divisors │ └── Solution.java │ ├── 1015. Smallest Integer Divisible by K │ └── Solution.java │ ├── 1344. Angle Between Hands of a Clock │ └── Solution.java │ ├── 1426. Counting Elements │ └── Solution.java │ ├── 1846. Maximum Element After Decreasing and Rearranging │ └── Solution.java │ ├── 1018. Binary Prefix Divisible By 5 │ └── Solution.java │ ├── 1572. Matrix Diagonal Sum │ └── Solution.java │ ├── 1732. Find the Highest Altitude │ └── Solution.java │ ├── 1512. Number of Good Pairs │ └── Solution.java │ ├── 1528. Shuffle String │ └── Solution.java │ ├── 1450. Number of Students Doing Homework at a Given Time │ └── Solution.java │ ├── 1529. Bulb Switcher IV │ └── Solution.java │ ├── 1680. Concatenation of Consecutive Binary Numbers │ └── Solution.java │ ├── 1025. Divisor Game │ └── Solution.java │ ├── 1413. Minimum Value to Get Positive Step by Step Sum │ └── Solution.java │ ├── 1323. Maximum 69 Number │ └── Solution.java │ ├── 1359. Count All Valid Pickup and Delivery Options │ └── Solution.java │ ├── 1844. Replace All Digits with Characters │ └── Solution.java │ ├── 1997. First Day Where You Have Been in All the Rooms │ └── Solution.java │ ├── 1641. Count Sorted Vowel Strings │ └── Solution.java │ ├── 1832. Check if the Sentence Is Pangram │ └── Solution.java │ ├── 1553. Minimum Number of Days to Eat N Oranges │ └── Solution.java │ ├── 1561. Maximum Number of Coins You Can Get │ └── Solution.java │ ├── 1877. Minimize Maximum Pair Sum in Array │ └── Solution.java │ ├── 1217. Play with Chips │ └── Solution.java │ ├── 1464. Maximum Product of Two Elements in an Array │ └── Solution.java │ ├── 1608. Special Array With X Elements Greater Than or Equal X │ └── Solution.java │ ├── 1619. Mean of Array After Removing Some Elements │ └── Solution.java │ ├── 1925. Count Square Sum Triples │ └── Solution.java │ ├── 1051. Height Checker │ └── Solution.java │ ├── 1936. Add Minimum Number of Rungs │ └── Solution.java │ ├── 1332. Remove Palindromic Subsequences │ └── Solution.java │ ├── 1800. Maximum Ascending Subarray Sum │ └── Solution.java │ ├── 1085. Sum of Digits in the Minimum Number │ └── Solution.java │ ├── 1165. Single-Row Keyboard │ └── Solution.java │ ├── 1374. Generate a String With Characters That Have Odd Counts │ └── Solution.java │ ├── 1389. Create Target Array in the Given Order │ └── Solution.java │ ├── 1672. Richest Customer Wealth │ └── Solution.java │ ├── 1491. Average Salary Excluding the Minimum and Maximum Salary │ └── Solution.java │ ├── 1668. Maximum Repeating Substring │ └── Solution.java │ ├── 1827. Minimum Operations to Make the Array Increasing │ └── Solution.java │ ├── 1118. Number of Days in a Month │ └── Solution.java │ ├── 1826. Faulty Sensor │ └── Solution.java │ ├── 1833. Maximum Ice Cream Bars │ └── Solution.java │ ├── 1502. Can Make Arithmetic Progression From Sequence │ └── Solution.java │ ├── 1518. Water Bottles │ └── Solution.java │ ├── 1984. Minimum Difference Between Highest and Lowest of K Scores │ └── Solution.java │ ├── 1614. Maximum Nesting Depth of the Parentheses │ └── Solution.java │ ├── 1692. Count Ways to Distribute Candies │ └── Solution.java │ ├── 1784. Check if Binary String Has at Most One Segment of Ones │ └── Solution.java │ ├── 1921. Eliminate Maximum Number of Monsters │ └── Solution.java │ ├── 1064. Fixed Point │ └── Solution.java │ ├── 1436. Destination City │ └── Solution.java │ ├── 1848. Minimum Distance to the Target Element │ └── Solution.java │ ├── 1893. Check if All the Integers in a Range Are Covered │ └── Solution.java │ ├── 1598. Crawler Log Folder │ └── Solution.java │ ├── 1137. N-th Tribonacci Number.md │ └── Solution.java │ ├── 1411. Number of Ways to Paint N × 3 Grid │ └── Solution.java │ ├── 1716. Calculate Money in Leetcode Bank │ └── Solution.java │ ├── 1979. Find Greatest Common Divisor of Array │ └── Solution.java │ ├── 1103. Distribute Candies to People │ └── Solution.java │ ├── 1221. Split a String in Balanced Strings │ └── Solution.java │ ├── 1266. Minimum Time Visiting All Points │ └── Solution.java │ ├── 1109. Corporate Flight Bookings │ └── Solution.java │ ├── 1526. Minimum Number of Increments on Subarrays to Form a Target Array │ └── Solution.java │ ├── 1557. Minimum Number of Vertices to Reach All Nodes │ └── Solution.java │ ├── 1985. Find the Kth Largest Integer in the Array │ └── Solution.java │ ├── 1437. Check If All 1's Are at Least Length K Places Away │ └── Solution.java │ ├── 1911. Maximum Alternating Subsequence Sum │ └── Solution.java │ ├── 1033. Moving Stones Until Consecutive │ └── Solution.java │ ├── 1287. Element Appearing More Than 25% In Sorted Array │ └── Solution.java │ ├── 1509. Minimum Difference Between Largest and Smallest Value in Three Moves │ └── Solution.java │ ├── 1798. Maximum Number of Consecutive Values You Can Make │ └── Solution.java │ ├── 1953. Maximum Number of Weeks for Which You Can Work │ └── Solution.java │ ├── 1974. Minimum Time to Type Word Using Special Typewriter │ └── Solution.java │ ├── 1119. Remove Vowels from a String │ └── Solution.java │ ├── 1375. Bulb Switcher III │ └── Solution.java │ ├── 1791. Find Center of Star Graph │ └── Solution.java │ ├── 1876. Substrings of Size Three with Distinct Characters │ └── Solution.java │ ├── 1346. Check If N and Its Double Exist │ └── Solution.java │ ├── 1431. Kids With the Greatest Number of Candies │ └── Solution.java │ ├── 1708. Largest Subarray Length K │ └── Solution.java │ ├── 1029. Two City Scheduling │ └── Solution.java │ ├── 1184. Distance Between Bus Stops │ └── Solution.java │ ├── 1281. Subtract the Product and Sum of Digits of an Integer │ └── Solution.java │ ├── 1646. Get Maximum in Generated Array │ └── Solution.java │ ├── 1748. Sum of Unique Elements │ └── Solution.java │ ├── 1894. Find the Student that Will Replace the Chalk │ └── Solution.java │ ├── 1884. Egg Drop With 2 Eggs and N Floors │ └── Solution.java │ ├── 1860. Incremental Memory Leak │ └── Solution.java │ ├── 1014. Best Sightseeing Pair │ └── Solution.java │ ├── 1199. Minimum Time to Build Blocks │ └── Solution.java │ ├── 1351. Count Negative Numbers in a Sorted Matrix │ └── Solution.java │ ├── 1290. Convert Binary Number in a Linked List to Integer │ └── Solution.java │ ├── 1347. Minimum Number of Steps to Make Two Strings Anagram │ └── Solution.java │ ├── 1230. Toss Strange Coins │ └── Solution.java │ ├── 1259. Handshakes That Don't Cross │ └── Solution.java │ ├── 1605. Find Valid Matrix Given Row and Column Sums │ └── Solution.java │ ├── 1679. Max Number of K-Sum Pairs │ └── Solution.java │ ├── 1725. Number Of Rectangles That Can Form The Largest Square │ └── Solution.java │ ├── 1785. Minimum Elements to Add to Form a Given Sum │ └── Solution.java │ ├── 1189. Maximum Number of Balloons │ └── Solution.java │ ├── 1196. How Many Apples Can You Put into the Basket │ └── Solution.java │ ├── 1167. Minimum Cost to Connect Sticks │ └── Solution.java │ ├── 1256. Encode Number │ └── Solution.java │ ├── 1328. Break a Palindrome │ └── Solution.java │ ├── 1899. Merge Triplets to Form Target Triplet │ └── Solution.java │ ├── 1228. Missing Number In Arithmetic Progression │ └── Solution.java │ ├── 1662. Check If Two String Arrays are Equivalent │ └── Solution.java │ ├── 1855. Maximum Distance Between a Pair of Values │ └── Solution.java │ ├── 1866. Number of Ways to Rearrange Sticks With K Sticks Visible │ └── Solution.java │ ├── 1060. Missing Element in Sorted Array │ └── Solution.java │ ├── 1013. Partition Array Into Three Parts With Equal Sum │ └── Solution.java │ ├── 1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence │ └── Solution.java │ ├── 1539. Kth Missing Positive Number │ └── Solution.java │ ├── 1991. Find the Middle Index in Array │ └── Solution.java │ ├── 1968. Array With Elements Not Equal to Average of Neighbors │ └── Solution.java │ ├── 1004. Max Consecutive Ones III │ └── Solution.java │ ├── 1104. Path In Zigzag Labelled Binary Tree │ └── Solution.java │ ├── 1111. Maximum Nesting Depth of Two Valid Parentheses Strings │ └── Solution.java │ ├── 1394. Find Lucky Integer in an Array │ └── Solution.java │ ├── 1962. Remove Stones to Minimize the Total │ └── Solution.java │ ├── 1475. Final Prices With a Special Discount in a Shop │ └── Solution.java │ ├── 1749. Maximum Absolute Sum of Any Subarray │ └── Solution.java │ ├── 1047. Remove All Adjacent Duplicates In String │ └── Solution.java │ ├── 1403. Minimum Subsequence in Non-Increasing Order │ └── Solution.java │ ├── 1427. Perform String Shifts │ └── Solution.java │ ├── 1441. Build an Array With Stack Operations │ └── Solution.java │ ├── 1624. Largest Substring Between Two Equal Characters │ └── Solution.java │ ├── 1663. Smallest String With A Given Numeric Value │ └── Solution.java │ ├── 1891. Cutting Ribbons │ └── Solution.java │ ├── 1897. Redistribute Characters to Make All Strings Equal │ └── Solution.java │ ├── 1043. Partition Array for Maximum Sum │ └── Solution.java │ ├── 1071. Greatest Common Divisor of Strings │ └── Solution.java │ ├── 1400. Construct K Palindrome Strings │ └── Solution.java │ ├── 1829. Maximum XOR for Each Query │ └── Solution.java │ ├── 1957. Delete Characters to Make Fancy String │ └── Solution.java │ ├── 1157. Online Majority Element In Subarray │ └── MajorityChecker.java │ ├── 1232. Check If It Is a Straight Line │ └── Solution.java │ ├── 1276. Number of Burgers with No Waste of Ingredients │ └── Solution.java │ ├── 1299. Replace Elements with Greatest Element on Right Side │ └── Solution.java │ ├── 1313. Decompress Run-Length Encoded List │ └── Solution.java │ ├── 1446. Consecutive Characters │ └── Solution.java │ └── 1099. Two Sum Less Than K │ └── Solution.java └── Database ├── 0620. Not Boring Movies └── 0620. Not Boring Movies.md └── 0597. Friend Requests I Overall Acceptance Rate └── 0597. Friend Requests I Overall Acceptance Rate.md /README.md: -------------------------------------------------------------------------------- 1 | # My-LeetCode-Solution 2 | My LeetCode solution to the problems. 3 | -------------------------------------------------------------------------------- /Algorithms/0001-1000/0292. Nim Game/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canWinNim(int n) { 3 | return n % 4 != 0; 4 | } 5 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0319. Bulb Switcher/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int bulbSwitch(int n) { 3 | return (int) Math.sqrt(n); 4 | } 5 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0403. Frog Jump/0403. Frog Jump.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Algorithms/0001-1000/0403. Frog Jump/0403. Frog Jump.md -------------------------------------------------------------------------------- /Algorithms/0001-1000/0488. Zuma Game/0488. Zuma Game.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Algorithms/0001-1000/0488. Zuma Game/0488. Zuma Game.md -------------------------------------------------------------------------------- /Algorithms/0001-1000/0656. Coin Path/0656. Coin Path.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Algorithms/0001-1000/0656. Coin Path/0656. Coin Path.md -------------------------------------------------------------------------------- /Database/0620. Not Boring Movies/0620. Not Boring Movies.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Database/0620. Not Boring Movies/0620. Not Boring Movies.md -------------------------------------------------------------------------------- /Algorithms/0001-1000/0565. Array Nesting/0565. Array Nesting.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Algorithms/0001-1000/0565. Array Nesting/0565. Array Nesting.md -------------------------------------------------------------------------------- /Algorithms/1001-2000/1227. Airplane Seat Assignment Probability/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double nthPersonGetsNthSeat(int n) { 3 | return n == 1 ? 1.0 : 0.5; 4 | } 5 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0130. Surrounded Regions/0130. Surrounded Regions.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Algorithms/0001-1000/0130. Surrounded Regions/0130. Surrounded Regions.md -------------------------------------------------------------------------------- /Algorithms/0001-1000/0226. Invert Binary Tree/0226. Invert Binary Tree.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Algorithms/0001-1000/0226. Invert Binary Tree/0226. Invert Binary Tree.md -------------------------------------------------------------------------------- /Algorithms/0001-1000/0796. Rotate String/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean rotateString(String A, String B) { 3 | return A.length() == B.length() && (B + B).indexOf(A) >= 0; 4 | } 5 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0092. Reverse Linked List II/0092. Reverse Linked List II.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Algorithms/0001-1000/0092. Reverse Linked List II/0092. Reverse Linked List II.md -------------------------------------------------------------------------------- /Algorithms/0001-1000/0191. Number of 1 Bits/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | // you need to treat n as an unsigned value 3 | public int hammingWeight(int n) { 4 | return Integer.bitCount(n); 5 | } 6 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0492. Construct the Rectangle/0492. Construct the Rectangle.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Algorithms/0001-1000/0492. Construct the Rectangle/0492. Construct the Rectangle.md -------------------------------------------------------------------------------- /Algorithms/1001-2000/1108. Defanging an IP Address/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String defangIPaddr(String address) { 3 | address = address.replaceAll("\\.", "[\\.]"); 4 | return address; 5 | } 6 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0169. Majority Element/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int majorityElement(int[] nums) { 3 | Arrays.sort(nums); 4 | int midIndex = nums.length / 2; 5 | return nums[midIndex]; 6 | } 7 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1908. Game of Nim/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean nimGame(int[] piles) { 3 | int xor = 0; 4 | for (int pile : piles) 5 | xor ^= pile; 6 | return xor != 0; 7 | } 8 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0258. Add Digits/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int addDigits(int num) { 3 | if (num == 0) 4 | return 0; 5 | int remainder = num % 9; 6 | return remainder == 0 ? 9 : remainder; 7 | } 8 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0836. Rectangle Overlap/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isRectangleOverlap(int[] rec1, int[] rec2) { 3 | return rec1[2] > rec2[0] && rec1[0] < rec2[2] && rec1[3] > rec2[1] && rec1[1] < rec2[3]; 4 | } 5 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0231. Power of Two/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPowerOfTwo(int n) { 3 | if (n <= 0) 4 | return false; 5 | while (n % 2 == 0) 6 | n /= 2; 7 | return n == 1; 8 | } 9 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0334. Increasing Triplet Subsequence/0334. Increasing Triplet Subsequence.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Algorithms/0001-1000/0334. Increasing Triplet Subsequence/0334. Increasing Triplet Subsequence.md -------------------------------------------------------------------------------- /Algorithms/0001-1000/0405. Convert a Number to Hexadecimal/0405. Convert a Number to Hexadecimal.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Algorithms/0001-1000/0405. Convert a Number to Hexadecimal/0405. Convert a Number to Hexadecimal.md -------------------------------------------------------------------------------- /Algorithms/0001-1000/0136. Single Number/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | int singleNumber = 0; 4 | for (int num : nums) 5 | singleNumber ^= num; 6 | return singleNumber; 7 | } 8 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0326. Power of Three/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPowerOfThree(int n) { 3 | if (n <= 0) 4 | return false; 5 | while (n % 3 == 0) 6 | n /= 3; 7 | return n == 1; 8 | } 9 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0357. Count Numbers with Unique Digits/0357. Count Numbers with Unique Digits.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Algorithms/0001-1000/0357. Count Numbers with Unique Digits/0357. Count Numbers with Unique Digits.md -------------------------------------------------------------------------------- /Algorithms/1001-2000/1419. Minimum Number of Frogs Croaking/1419. Minimum Number of Frogs Croaking.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Algorithms/1001-2000/1419. Minimum Number of Frogs Croaking/1419. Minimum Number of Frogs Croaking.md -------------------------------------------------------------------------------- /Algorithms/0001-1000/0441. Arranging Coins/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int arrangeCoins(int n) { 3 | int row = 0; 4 | while (n > row) { 5 | row++; 6 | n -= row; 7 | } 8 | return row; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0058. Length of Last Word/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int lengthOfLastWord(String s) { 3 | s = s.trim(); 4 | int length = s.length(); 5 | int index = s.lastIndexOf(" "); 6 | return length - (index + 1); 7 | } 8 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0338. Counting Bits/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] countBits(int num) { 3 | int[] bits = new int[num + 1]; 4 | for (int i = 0; i <= num; i++) 5 | bits[i] = Integer.bitCount(i); 6 | return bits; 7 | } 8 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0521. Longest Uncommon Subsequence I/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findLUSlength(String a, String b) { 3 | if (a.equals(b)) 4 | return -1; 5 | else 6 | return Math.max(a.length(), b.length()); 7 | } 8 | } -------------------------------------------------------------------------------- /Database/0597. Friend Requests I Overall Acceptance Rate/0597. Friend Requests I Overall Acceptance Rate.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Database/0597. Friend Requests I Overall Acceptance Rate/0597. Friend Requests I Overall Acceptance Rate.md -------------------------------------------------------------------------------- /Algorithms/0001-1000/0476. Number Complement/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findComplement(int num) { 3 | int power = (int) (Math.log(num) / Math.log(2)); 4 | long max = (long) Math.pow(2, power + 1) - 1; 5 | return (int) (max - num); 6 | } 7 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1460. Make Two Arrays Equal by Reversing Sub-arrays/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canBeEqual(int[] target, int[] arr) { 3 | Arrays.sort(target); 4 | Arrays.sort(arr); 5 | return Arrays.equals(target, arr); 6 | } 7 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1551. Minimum Operations to Make Array Equal/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minOperations(int n) { 3 | int operations = 0; 4 | for (int i = n - 1; i > 0; i -= 2) 5 | operations += i; 6 | return operations; 7 | } 8 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1678. Goal Parser Interpretation/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String interpret(String command) { 3 | command = command.replaceAll("\\(al\\)", "al"); 4 | command = command.replaceAll("\\(\\)", "o"); 5 | return command; 6 | } 7 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0371. Sum of Two Integers/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getSum(int a, int b) { 3 | while (a != 0) { 4 | int temp = a ^ b; 5 | a = (a & b) << 1; 6 | b = temp; 7 | } 8 | return b; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1523. Count Odd Numbers in an Interval Range/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countOdds(int low, int high) { 3 | if (low % 2 == 0) 4 | low++; 5 | if (high % 2 == 0) 6 | high--; 7 | return (high - low) / 2 + 1; 8 | } 9 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0458. Poor Pigs/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int poorPigs(int buckets, int minutesToDie, int minutesToTest) { 3 | int side = minutesToTest / minutesToDie + 1; 4 | int min = (int) Math.ceil(Math.log(buckets) / Math.log(side)); 5 | return min; 6 | } 7 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1486. XOR Operation in an Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int xorOperation(int n, int start) { 3 | int end = start + 2 * (n - 1); 4 | int xor = 0; 5 | for (int i = start; i <= end; i += 2) 6 | xor ^= i; 7 | return xor; 8 | } 9 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1913. Maximum Product Difference Between Two Pairs/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProductDifference(int[] nums) { 3 | Arrays.sort(nums); 4 | int length = nums.length; 5 | return nums[length - 2] * nums[length - 1] - nums[0] * nums[1]; 6 | } 7 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1418. Display Table of Food Orders in a Restaurant/1418. Display Table of Food Orders in a Restaurant.md: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormsunshine/My-LeetCode-Solution/HEAD/Algorithms/1001-2000/1418. Display Table of Food Orders in a Restaurant/1418. Display Table of Food Orders in a Restaurant.md -------------------------------------------------------------------------------- /Algorithms/1001-2000/1688. Count of Matches in Tournament/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfMatches(int n) { 3 | int matches = 0; 4 | while (n > 1) { 5 | matches += n / 2; 6 | n = (n + 1) / 2; 7 | } 8 | return matches; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1812. Determine Color of a Chessboard Square/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean squareIsWhite(String coordinates) { 3 | int column = coordinates.charAt(0) - 'a' + 1; 4 | int row = coordinates.charAt(1) - '0'; 5 | return column % 2 != row % 2; 6 | } 7 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1823. Find the Winner of the Circular Game/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findTheWinner(int n, int k) { 3 | int last = 0; 4 | for (int i = 2; i <= n; i++) 5 | last = (last + k) % i; 6 | last = last % n + 1; 7 | return last; 8 | } 9 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0810. Chalkboard XOR Game/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean xorGame(int[] nums) { 3 | if (nums.length % 2 == 0) 4 | return true; 5 | int xor = 0; 6 | for (int num : nums) 7 | xor ^= num; 8 | return xor == 0; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1556. Thousand Separator/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String thousandSeparator(int n) { 3 | StringBuffer sb = new StringBuffer(String.valueOf(n)); 4 | for (int i = sb.length() - 3; i > 0; i -= 3) 5 | sb.insert(i, '.'); 6 | return sb.toString(); 7 | } 8 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1837. Sum of Digits in Base K/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int sumBase(int n, int k) { 3 | int sum = 0; 4 | while (n > 0) { 5 | int remainder = n % k; 6 | sum += remainder; 7 | n /= k; 8 | } 9 | return sum; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0137. Single Number II/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int singleNumber(int[] nums) { 3 | int ones = 0, twos = 0; 4 | for (int num : nums) { 5 | ones = ones ^ num & ~twos; 6 | twos = twos ^ num & ~ones; 7 | } 8 | return ones; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0172. Factorial Trailing Zeroes/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int trailingZeroes(int n) { 3 | int zeroes = 0; 4 | int fives = 5; 5 | while (fives <= n) { 6 | zeroes += n / fives; 7 | n /= 5; 8 | } 9 | return zeroes; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1920. Build Array from Permutation/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] buildArray(int[] nums) { 3 | int length = nums.length; 4 | int[] array = new int[length]; 5 | for (int i = 0; i < length; i++) 6 | array[i] = nums[nums[i]]; 7 | return array; 8 | } 9 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0561. Array Partition I/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int arrayPairSum(int[] nums) { 3 | int sum = 0; 4 | Arrays.sort(nums); 5 | int length = nums.length; 6 | for (int i = 0; i < length; i += 2) 7 | sum += nums[i]; 8 | return sum; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0908. Smallest Range I/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int smallestRangeI(int[] A, int K) { 3 | Arrays.sort(A); 4 | int length = A.length; 5 | int min = A[0], max = A[length - 1]; 6 | int difference = max - min; 7 | return Math.max(0, difference - 2 * K); 8 | } 9 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1009. Complement of Base 10 Integer/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int bitwiseComplement(int N) { 3 | if (N <= 1) 4 | return 1 - N; 5 | int power = (int) (Math.log(N) / Math.log(2)); 6 | int max = (int) (Math.pow(2, power + 1) - 1); 7 | return max - N; 8 | } 9 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0217. Contains Duplicate/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean containsDuplicate(int[] nums) { 3 | Set set = new HashSet(); 4 | for (int num : nums) { 5 | if (!set.add(num)) 6 | return true; 7 | } 8 | return false; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0268. Missing Number/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int missingNumber(int[] nums) { 3 | int length = nums.length; 4 | long sum = 0; 5 | for (int i = 0; i < length; i++) 6 | sum += (int) nums[i]; 7 | return (int) ((long) length * ((long) length + 1) / 2 - sum); 8 | } 9 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0852. Peak Index in a Mountain Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int peakIndexInMountainArray(int[] A) { 3 | int length = A.length; 4 | for (int i = 1; i < length; i++) { 5 | if (A[i] < A[i - 1]) 6 | return i - 1; 7 | } 8 | return -1; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0961. N-Repeated Element in Size 2N Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int repeatedNTimes(int[] A) { 3 | Set set = new HashSet(); 4 | for (int num : A) { 5 | if (!set.add(num)) 6 | return num; 7 | } 8 | return -1; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1295. Find Numbers with Even Number of Digits/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findNumbers(int[] nums) { 3 | int count = 0; 4 | for (int num : nums) { 5 | if (String.valueOf(num).length() % 2 == 0) 6 | count++; 7 | } 8 | return count; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0055. Jump Game/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canJump(int[] nums) { 3 | int lastPos = nums.length - 1; 4 | for (int i = nums.length - 1; i >= 0; i--) { 5 | if (i + nums[i] >= lastPos) 6 | lastPos = i; 7 | } 8 | return lastPos == 0; 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1816. Truncate Sentence/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String truncateSentence(String s, int k) { 3 | String[] array = s.split(" "); 4 | StringBuffer sb = new StringBuffer(array[0]); 5 | for (int i = 1; i < k; i++) 6 | sb.append(" " + array[i]); 7 | return sb.toString(); 8 | } 9 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1835. Find XOR Sum of All Pairs Bitwise AND/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getXORSum(int[] arr1, int[] arr2) { 3 | int xor1 = 0, xor2 = 0; 4 | for (int num : arr1) 5 | xor1 ^= num; 6 | for (int num : arr2) 7 | xor2 ^= num; 8 | return xor1 & xor2; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1037. Valid Boomerang/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isBoomerang(int[][] points) { 3 | int difX1 = points[1][0] - points[0][0], difY1 = points[1][1] - points[0][1]; 4 | int difX2 = points[2][0] - points[1][0], difY2 = points[2][1] - points[1][1]; 5 | return difX1 * difY2 != difX2 * difY1; 6 | } 7 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0233. Number of Digit One/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countDigitOne(int n) { 3 | int ones = 0; 4 | for (long i = 1; i <= n; i *= 10) { 5 | long unit = i * 10; 6 | ones += n / unit * i + Math.min(i, Math.max(0, n % unit - i + 1)); 7 | } 8 | return ones; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1470. Shuffle the Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] shuffle(int[] nums, int n) { 3 | int[] shuffled = new int[n * 2]; 4 | for (int i = 0; i < n; i++) { 5 | shuffled[i * 2] = nums[i]; 6 | shuffled[i * 2 + 1] = nums[i + n]; 7 | } 8 | return shuffled; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1780. Check if Number is a Sum of Powers of Three/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean checkPowersOfThree(int n) { 3 | while (n > 0) { 4 | int remainder = n % 3; 5 | if (remainder == 2) 6 | return false; 7 | n /= 3; 8 | } 9 | return true; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/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 num = 0; 5 | for (int i = 0; i < 32; i++) { 6 | num <<= 1; 7 | num |= (n & 1); 8 | n >>= 1; 9 | } 10 | return num; 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0991. Broken Calculator/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int brokenCalc(int X, int Y) { 3 | int count = 0; 4 | while (X < Y) { 5 | if (Y % 2 == 0) 6 | Y /= 2; 7 | else 8 | Y++; 9 | count++; 10 | } 11 | return count + X - Y; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1480. Running Sum of 1d Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] runningSum(int[] nums) { 3 | int length = nums.length; 4 | int[] sums = new int[length]; 5 | sums[0] = nums[0]; 6 | for (int i = 1; i < length; i++) 7 | sums[i] = sums[i - 1] + nums[i]; 8 | return sums; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0344. Reverse String/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void reverseString(char[] s) { 3 | int low = 0, high = s.length - 1; 4 | while (low < high) { 5 | char temp = s[low]; 6 | s[low] = s[high]; 7 | s[high] = temp; 8 | low++; 9 | high--; 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1720. Decode XORed Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] decode(int[] encoded, int first) { 3 | int length = encoded.length; 4 | int[] arr = new int[length + 1]; 5 | arr[0] = first; 6 | for (int i = 0; i < length; i++) 7 | arr[i + 1] = arr[i] ^ encoded[i]; 8 | return arr; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1967. Number of Strings That Appear as Substrings in Word/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numOfStrings(String[] patterns, String word) { 3 | int count = 0; 4 | for (String pattern : patterns) { 5 | if (word.indexOf(pattern) >= 0) 6 | count++; 7 | } 8 | return count; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0518. Coin Change 2/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int change(int amount, int[] coins) { 3 | int[] dp = new int[amount + 1]; 4 | dp[0] = 1; 5 | for (int coin : coins) { 6 | for (int i = coin; i <= amount; i++) 7 | dp[i] += dp[i - coin]; 8 | } 9 | return dp[amount]; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1929. Concatenation of Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] getConcatenation(int[] nums) { 3 | int n = nums.length; 4 | int[] concat = new int[n * 2]; 5 | for (int i = 0; i < n; i++) { 6 | concat[i] = nums[i]; 7 | concat[i + n] = nums[i]; 8 | } 9 | return concat; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0453. Minimum Moves to Equal Array Elements/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minMoves(int[] nums) { 3 | Arrays.sort(nums); 4 | int min = nums[0]; 5 | int moves = 0; 6 | int length = nums.length; 7 | for (int i = 0; i < length; i++) 8 | moves += nums[i] - min; 9 | return moves; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1550. Three Consecutive Odds/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean threeConsecutiveOdds(int[] arr) { 3 | int length = arr.length; 4 | for (int i = 2; i < length; i++) { 5 | if (arr[i - 2] % 2 == 1 && arr[i - 1] % 2 == 1 && arr[i] % 2 == 1) 6 | return true; 7 | } 8 | return false; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0342. Power of Four/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPowerOfFour(int num) { 3 | if (num <= 0) 4 | return false; 5 | int remain = num; 6 | while (remain > 1) { 7 | if (remain % 4 != 0) 8 | return false; 9 | remain /= 4; 10 | } 11 | return true; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1342. Number of Steps to Reduce a Number to Zero/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfSteps (int num) { 3 | int count = 0; 4 | while (num > 0) { 5 | if (num % 2 == 0) 6 | num /= 2; 7 | else 8 | num--; 9 | count++; 10 | } 11 | return count; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0660. Remove 9/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int newInteger(int n) { 3 | String radix9Str = ""; 4 | while (n > 0) { 5 | int remainder = n % 9; 6 | radix9Str = remainder + radix9Str; 7 | n /= 9; 8 | } 9 | int radix9 = Integer.parseInt(radix9Str); 10 | return radix9; 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0651. 4 Keys Keyboard/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxA(int N) { 3 | int[] dp = new int[N + 1]; 4 | for (int i = 1; i <= N; i++) { 5 | dp[i] = dp[i - 1] + 1; 6 | for (int j = i - 3; j > 0; j--) 7 | dp[i] = Math.max(dp[i], dp[j] * (i - j - 1)); 8 | } 9 | return dp[N]; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0168. Excel Sheet Column Title/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String convertToTitle(int n) { 3 | StringBuilder stringBuilder = new StringBuilder(); 4 | while (n != 0) { 5 | n--; 6 | stringBuilder.append((char)(n % 26 + 'A')); 7 | n /= 26; 8 | } 9 | return stringBuilder.reverse().toString(); 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0575. Distribute Candies/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int distributeCandies(int[] candies) { 3 | int length = candies.length; 4 | int numOfCandies = length / 2; 5 | Set set = new HashSet(); 6 | for (int candy : candies) 7 | set.add(candy); 8 | return Math.min(numOfCandies, set.size()); 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1689. Partitioning Into Minimum Number Of Deci-Binary Numbers/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minPartitions(String n) { 3 | int max = 0; 4 | int length = n.length(); 5 | for (int i = 0; i < length; i++) { 6 | int digit = n.charAt(i) - '0'; 7 | max = Math.max(max, digit); 8 | } 9 | return max; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1822. Sign of the Product of an Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int arraySign(int[] nums) { 3 | int negativeCount = 0; 4 | for (int num : nums) { 5 | if (num < 0) 6 | negativeCount++; 7 | else if (num == 0) 8 | return 0; 9 | } 10 | return negativeCount % 2 == 0 ? 1 : -1; 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1903. Largest Odd Number in String/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String largestOddNumber(String num) { 3 | for (int i = num.length() - 1; i >= 0; i--) { 4 | char c = num.charAt(i); 5 | int digit = c - '0'; 6 | if (digit % 2 != 0) 7 | return num.substring(0, i + 1); 8 | } 9 | return ""; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0053. Maximum Subarray/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxSubArray(int[] nums) { 3 | int length = nums.length; 4 | int newSum = nums[0], max = nums[0]; 5 | for (int i = 1; i < length; i++) { 6 | newSum = Math.max(newSum + nums[i], nums[i]); 7 | max = Math.max(max, newSum); 8 | } 9 | return max; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0070. Climbing Stairs/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int climbStairs(int n) { 3 | if (n <= 2) 4 | return n; 5 | int prev2 = 1, prev1 = 2; 6 | int num = 0; 7 | for (int i = 3; i <= n; i++) { 8 | num = prev2 + prev1; 9 | prev2 = prev1; 10 | prev1 = num; 11 | } 12 | return num; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1874. Minimize Product Sum of Two Arrays/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minProductSum(int[] nums1, int[] nums2) { 3 | Arrays.sort(nums1); 4 | Arrays.sort(nums2); 5 | int sum = 0; 6 | int length = nums1.length; 7 | for (int i = 0; i < length; i++) 8 | sum += nums1[i] * nums2[length - 1 - i]; 9 | return sum; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1910. Remove All Occurrences of a Substring/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String removeOccurrences(String s, String part) { 3 | int partLength = part.length(); 4 | while (s.indexOf(part) >= 0) { 5 | int index = s.indexOf(part); 6 | s = s.substring(0, index) + s.substring(index + partLength); 7 | } 8 | return s; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0122. Best Time to Buy and Sell Stock II/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices) { 3 | int maxProfit = 0; 4 | int length = prices.length; 5 | for (int i = 1; i < length; i++) { 6 | if (prices[i] - prices[i - 1] > 0) 7 | maxProfit += prices[i] - prices[i - 1]; 8 | } 9 | return maxProfit; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1134. Armstrong Number/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isArmstrong(int N) { 3 | char[] array = String.valueOf(N).toCharArray(); 4 | int length = array.length; 5 | int sum = 0; 6 | for (char c : array) { 7 | int digit = c - '0'; 8 | sum += (int) Math.pow(digit, length); 9 | } 10 | return sum == N; 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1952. Three Divisors/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isThree(int n) { 3 | int sqrt = (int) Math.sqrt(n); 4 | if (sqrt * sqrt != n) 5 | return false; 6 | int divisors = 1; 7 | for (int i = 1; i < sqrt; i++) { 8 | if (n % i == 0) 9 | divisors += 2; 10 | } 11 | return divisors == 3; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1015. Smallest Integer Divisible by K/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int smallestRepunitDivByK(int K) { 3 | if (K % 2 == 0 || K % 5 == 0) 4 | return -1; 5 | int value = 1; 6 | int length = 1; 7 | while (value % K != 0) { 8 | value = value % K * 10 + 1; 9 | length++; 10 | } 11 | return length; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1344. Angle Between Hands of a Clock/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double angleClock(int hour, int minutes) { 3 | double hoursDegree = hour * 30 + minutes * 30.0 / 60; 4 | int minutesDegree = minutes * 6; 5 | double angle = Math.abs(minutesDegree - hoursDegree); 6 | if (angle > 180) 7 | angle = 360 - angle; 8 | return angle; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1426. Counting Elements/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countElements(int[] arr) { 3 | Set set = new HashSet(); 4 | for (int num : arr) 5 | set.add(num); 6 | int count = 0; 7 | for (int num : arr) { 8 | if (set.contains(num + 1)) 9 | count++; 10 | } 11 | return count; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1846. Maximum Element After Decreasing and Rearranging/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximumElementAfterDecrementingAndRearranging(int[] arr) { 3 | Arrays.sort(arr); 4 | int length = arr.length; 5 | arr[0] = 1; 6 | for (int i = 1; i < length; i++) 7 | arr[i] = Math.min(arr[i], arr[i - 1] + 1); 8 | return arr[length - 1]; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0867. Transpose Matrix/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] transpose(int[][] A) { 3 | int rows = A.length, columns = A[0].length; 4 | int[][] transpose = new int[columns][rows]; 5 | for (int i = 0; i < rows; i++) { 6 | for (int j = 0; j < columns; j++) 7 | transpose[j][i] = A[i][j]; 8 | } 9 | return transpose; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0746. Min Cost Climbing Stairs/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minCostClimbingStairs(int[] cost) { 3 | int length = cost.length; 4 | int[] dp = new int[length + 1]; 5 | dp[0] = 0; 6 | dp[1] = 0; 7 | for (int i = 2; i <= length; i++) 8 | dp[i] = Math.min(dp[i - 2] + cost[i - 2], dp[i - 1] + cost[i - 1]); 9 | return dp[length]; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1018. Binary Prefix Divisible By 5/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List prefixesDivBy5(int[] A) { 3 | List list = new ArrayList(); 4 | int length = A.length; 5 | int num = 0; 6 | for (int i = 0; i < length; i++) { 7 | num = (num * 2 + A[i]) % 5; 8 | list.add(num == 0); 9 | } 10 | return list; 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1572. Matrix Diagonal Sum/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int diagonalSum(int[][] mat) { 3 | int sum = 0; 4 | int side = mat.length; 5 | for (int i = 0; i < side; i++) { 6 | sum += mat[i][i]; 7 | sum += mat[i][side - 1 - i]; 8 | } 9 | if (side % 2 != 0) 10 | sum -= mat[side / 2][side / 2]; 11 | return sum; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1732. Find the Highest Altitude/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int largestAltitude(int[] gain) { 3 | int maxAltitude = 0; 4 | int altitude = 0; 5 | int length = gain.length; 6 | for (int i = 0; i < length; i++) { 7 | altitude += gain[i]; 8 | maxAltitude = Math.max(maxAltitude, altitude); 9 | } 10 | return maxAltitude; 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0754. Reach a Number/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reachNumber(int target) { 3 | target = Math.abs(target); 4 | if (target == 0) 5 | return 0; 6 | int position = 0; 7 | int step = 0; 8 | while (position < target || position % 2 != target % 2) { 9 | step++; 10 | position += step; 11 | } 12 | return step; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1512. Number of Good Pairs/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numIdenticalPairs(int[] nums) { 3 | int pairs = 0; 4 | int length = nums.length; 5 | for (int i = 0; i < length; i++) { 6 | for (int j = i + 1; j < length; j++) { 7 | if (nums[i] == nums[j]) 8 | pairs++; 9 | } 10 | } 11 | return pairs; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1528. Shuffle String/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String restoreString(String s, int[] indices) { 3 | int length = indices.length; 4 | char[] array = new char[length]; 5 | for (int i = 0; i < length; i++) { 6 | char c = s.charAt(i); 7 | int index = indices[i]; 8 | array[index] = c; 9 | } 10 | return new String(array); 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0237. Delete Node in a Linked List/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { val = x; } 7 | * } 8 | */ 9 | class Solution { 10 | public void deleteNode(ListNode node) { 11 | ListNode nextNode = node.next; 12 | node.val = nextNode.val; 13 | node.next = nextNode.next; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0509. Fibonacci Number/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int fib(int N) { 3 | if (N <= 1) 4 | return N; 5 | int prev2 = 0, prev1 = 1; 6 | int num = 1; 7 | int index = 2; 8 | while (index <= N) { 9 | num = prev2 + prev1; 10 | prev2 = prev1; 11 | prev1 = num; 12 | index++; 13 | } 14 | return num; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0769. Max Chunks To Make Sorted/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxChunksToSorted(int[] arr) { 3 | int curMax = 0; 4 | int chunksCount = 0; 5 | int length = arr.length; 6 | for (int i = 0; i < length; i++) { 7 | curMax = Math.max(curMax, arr[i]); 8 | if (curMax == i) 9 | chunksCount++; 10 | } 11 | return chunksCount; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0976. Largest Perimeter Triangle/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int largestPerimeter(int[] A) { 3 | Arrays.sort(A); 4 | int length = A.length; 5 | for (int i = length - 1; i >= 2; i--) { 6 | int side1 = A[i], side2 = A[i - 1], side3 = A[i - 2]; 7 | if (side2 + side3 > side1) 8 | return side1 + side2 + side3; 9 | } 10 | return 0; 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1450. Number of Students Doing Homework at a Given Time/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int busyStudent(int[] startTime, int[] endTime, int queryTime) { 3 | int count = 0; 4 | int length = startTime.length; 5 | for (int i = 0; i < length; i++) { 6 | if (startTime[i] <= queryTime && endTime[i] >= queryTime) 7 | count++; 8 | } 9 | return count; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1529. Bulb Switcher IV/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minFlips(String target) { 3 | int flips = 0; 4 | char prev = '0'; 5 | int length = target.length(); 6 | for (int i = 0; i < length; i++) { 7 | char curr = target.charAt(i); 8 | if (curr != prev) 9 | flips++; 10 | prev = curr; 11 | } 12 | return flips; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1680. Concatenation of Consecutive Binary Numbers/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int concatenatedBinary(int n) { 3 | final int MODULO = 1000000007; 4 | long num = 0; 5 | int bits = 0; 6 | for (int i = 1; i <= n; i++) { 7 | if ((i & (i - 1)) == 0) 8 | bits++; 9 | num = ((num << bits) + i) % MODULO; 10 | } 11 | return (int) num; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0075. Sort Colors/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void sortColors(int[] nums) { 3 | int length = nums.length; 4 | for (int i = 1; i < length; i++) { 5 | int num = nums[i]; 6 | int k = i - 1; 7 | while (k >= 0 && nums[k] > num) { 8 | nums[k + 1] = nums[k]; 9 | k--; 10 | } 11 | nums[k + 1] = num; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0462. Minimum Moves to Equal Array Elements II/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minMoves2(int[] nums) { 3 | Arrays.sort(nums); 4 | int length = nums.length; 5 | int median = length % 2 == 0 ? (nums[length / 2 - 1] + nums[length / 2]) / 2 : nums[length / 2]; 6 | int moves = 0; 7 | for (int num : nums) 8 | moves += Math.abs(num - median); 9 | return moves; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0634. Find the Derangement of An Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findDerangement(int n) { 3 | if (n <= 3) 4 | return n - 1; 5 | final int MODULO = 1000000007; 6 | long[] dp = new long[n]; 7 | dp[0] = 0; 8 | dp[1] = 1; 9 | for (int i = 2; i < n; i++) 10 | dp[i] = (dp[i - 2] + dp[i - 1]) * i % MODULO; 11 | return (int) dp[n - 1]; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0837. New 21 Game/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double new21Game(int N, int K, int W) { 3 | double[] dp = new double[N + W + 1]; 4 | for (int i = K; i <= N; i++) 5 | dp[i] = 1.0; 6 | double sum = Math.min(N - K + 1, W); 7 | for (int i = K - 1; i >= 0; i--) { 8 | dp[i] = sum / W; 9 | sum -= dp[i + W] - dp[i]; 10 | } 11 | return dp[0]; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1025. Divisor Game/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean divisorGame(int N) { 3 | boolean[] dp = new boolean[N + 1]; 4 | for (int i = 1; i <= N; i++) { 5 | for (int j = 1; j < i; j++) { 6 | if (i % (i - j) == 0 && !dp[j]) { 7 | dp[i] = true; 8 | break; 9 | } 10 | } 11 | } 12 | return dp[N]; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1413. Minimum Value to Get Positive Step by Step Sum/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minStartValue(int[] nums) { 3 | int min = 0; 4 | int sum = 0; 5 | int length = nums.length; 6 | for (int i = 0; i < length; i++) { 7 | sum += nums[i]; 8 | min = Math.min(min, sum); 9 | } 10 | int minStart = min >= 0 ? 1 : -min + 1; 11 | return minStart; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0461. Hamming Distance/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int hammingDistance(int x, int y) { 3 | if (x == y) 4 | return 0; 5 | int distance = 0; 6 | while (x > 0 || y > 0) { 7 | int xBit = x & 0x1, yBit = y & 0x1; 8 | if (xBit != yBit) 9 | distance++; 10 | x >>>= 1; 11 | y >>>= 1; 12 | } 13 | return distance; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1323. Maximum 69 Number/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximum69Number(int num) { 3 | char[] array = String.valueOf(num).toCharArray(); 4 | int length = array.length; 5 | for (int i = 0; i < length; i++) { 6 | if (array[i] == '6') { 7 | array[i] = '9'; 8 | break; 9 | } 10 | } 11 | return Integer.parseInt(new String(array)); 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0492. Construct the Rectangle/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] constructRectangle(int area) { 3 | int sqrt = (int) Math.sqrt(area); 4 | int width = sqrt; 5 | while (width > 1) { 6 | if (area % width == 0) 7 | break; 8 | width--; 9 | } 10 | int length = area / width; 11 | int[] rectangle = {length, width}; 12 | return rectangle; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1359. Count All Valid Pickup and Delivery Options/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countOrders(int n) { 3 | final int MODULO = 1000000007; 4 | long count = 1; 5 | for (int i = 2; i <= n; i++) { 6 | long twice = i * 2 % MODULO; 7 | long curCount = twice * (twice - 1) / 2 % MODULO; 8 | count = (count * curCount) % MODULO; 9 | } 10 | return (int) count; 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1844. Replace All Digits with Characters/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String replaceDigits(String s) { 3 | char[] array = s.toCharArray(); 4 | int length = array.length; 5 | for (int i = 1; i < length; i += 2) { 6 | char letter = array[i - 1]; 7 | int digit = array[i] - '0'; 8 | array[i] = (char) (letter + digit); 9 | } 10 | return new String(array); 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1997. First Day Where You Have Been in All the Rooms/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int firstDayBeenInAllRooms(int[] nextVisit) { 3 | final int MODULO = 1000000007; 4 | int length = nextVisit.length; 5 | int[] dp = new int[length]; 6 | for (int i = 1; i < length; i++) 7 | dp[i] = ((dp[i - 1] * 2 - dp[nextVisit[i - 1]] + 2) % MODULO + MODULO) % MODULO; 8 | return dp[length - 1]; 9 | } 10 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0779. K-th Symbol in Grammar/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int kthGrammar(int N, int K) { 3 | int reverseCount = 0; 4 | while (K > 1) { 5 | int log = (int) Math.ceil(Math.log(K) / Math.log(2)); 6 | int power2 = (int) Math.pow(2, log); 7 | K = power2 + 1 - K; 8 | if (log % 2 == 1) 9 | reverseCount++; 10 | } 11 | return reverseCount % 2; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1641. Count Sorted Vowel Strings/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countVowelStrings(int n) { 3 | int[][] dp = new int[n][5]; 4 | for (int i = 0; i < 5; i++) 5 | dp[0][i] = i + 1; 6 | for (int i = 1; i < n; i++) { 7 | dp[i][0] = 1; 8 | for (int j = 1; j < 5; j++) 9 | dp[i][j] = dp[i - 1][j] + dp[i][j - 1]; 10 | } 11 | return dp[n - 1][4]; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1832. Check if the Sentence Is Pangram/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean checkIfPangram(String sentence) { 3 | Set set = new HashSet(); 4 | int length = sentence.length(); 5 | for (int i = 0; i < length; i++) { 6 | char c = sentence.charAt(i); 7 | if (c >= 'a' && c <= 'z') 8 | set.add(c); 9 | } 10 | return set.size() == 26; 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0442. Find All Duplicates in an Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List findDuplicates(int[] nums) { 3 | List duplicates = new ArrayList(); 4 | for (int num : nums) { 5 | num = Math.abs(num); 6 | if (nums[num - 1] < 0) 7 | duplicates.add(num); 8 | else 9 | nums[num - 1] *= -1; 10 | } 11 | return duplicates; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0881. Boats to Save People/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numRescueBoats(int[] people, int limit) { 3 | int boatsCount = 0; 4 | Arrays.sort(people); 5 | int low = 0, high = people.length - 1; 6 | while (low <= high) { 7 | if (people[low] + people[high] <= limit) 8 | low++; 9 | high--; 10 | boatsCount++; 11 | } 12 | return boatsCount; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1553. Minimum Number of Days to Eat N Oranges/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | Map map = new HashMap(); 3 | 4 | public int minDays(int n) { 5 | if (n <= 1) 6 | return n; 7 | if (map.containsKey(n)) 8 | return map.get(n); 9 | int days = Math.min(minDays(n / 2) + n % 2 + 1, minDays(n / 3) + n % 3 + 1); 10 | map.put(n, days); 11 | return days; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1561. Maximum Number of Coins You Can Get/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxCoins(int[] piles) { 3 | Arrays.sort(piles); 4 | int length = piles.length; 5 | int rounds = length / 3; 6 | int maxCoins = 0; 7 | int index = length - 2; 8 | for (int i = 0; i < rounds; i++) { 9 | maxCoins += piles[index]; 10 | index -= 2; 11 | } 12 | return maxCoins; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1877. Minimize Maximum Pair Sum in Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minPairSum(int[] nums) { 3 | Arrays.sort(nums); 4 | int maxSum = 0; 5 | int left = 0, right = nums.length - 1; 6 | while (left < right) { 7 | int sum = nums[left] + nums[right]; 8 | maxSum = Math.max(maxSum, sum); 9 | left++; 10 | right--; 11 | } 12 | return maxSum; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0276. Paint Fence/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numWays(int n, int k) { 3 | if (n == 0 || k == 0) 4 | return 0; 5 | int[][] dp = new int[n][2]; 6 | dp[0][0] = 0; 7 | dp[0][1] = k; 8 | for (int i = 1; i < n; i++) { 9 | dp[i][0] = dp[i - 1][1]; 10 | dp[i][1] = (dp[i - 1][0] + dp[i - 1][1]) * (k - 1); 11 | } 12 | return dp[n - 1][0] + dp[n - 1][1]; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0283. Move Zeroes/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void moveZeroes(int[] nums) { 3 | int index = 0; 4 | int length = nums.length; 5 | for (int i = 0; i < length; i++) { 6 | if (nums[i] != 0) { 7 | nums[index] = nums[i]; 8 | index++; 9 | } 10 | } 11 | while (index < length) { 12 | nums[index] = 0; 13 | index++; 14 | } 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0633. Sum of Square Numbers/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean judgeSquareSum(int c) { 3 | int low = 0, high = (int) Math.sqrt(c); 4 | while (low <= high) { 5 | int sum = low * low + high * high; 6 | if (sum == c) 7 | return true; 8 | else if (sum > c) 9 | high--; 10 | else 11 | low++; 12 | } 13 | return false; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1217. Play with Chips/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minCostToMoveChips(int[] chips) { 3 | int evenCount = 0, oddCount = 0; 4 | int length = chips.length; 5 | for (int i = 0; i < length; i++) { 6 | int chip = chips[i]; 7 | if (chip % 2 == 0) 8 | evenCount++; 9 | else 10 | oddCount++; 11 | } 12 | return Math.min(evenCount, oddCount); 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1464. Maximum Product of Two Elements in an Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProduct(int[] nums) { 3 | int max1 = Integer.MIN_VALUE, max2 = Integer.MIN_VALUE; 4 | for (int num : nums) { 5 | if (num > max1) { 6 | max2 = max1; 7 | max1 = num; 8 | } else if (num > max2) 9 | max2 = num; 10 | } 11 | return (max1 - 1) * (max2 - 1); 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1608. Special Array With X Elements Greater Than or Equal X/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int specialArray(int[] nums) { 3 | Arrays.sort(nums); 4 | int length = nums.length; 5 | if (length <= nums[0]) 6 | return length; 7 | for (int x = 1; x < length; x++) { 8 | if (nums[length - x] >= x && nums[length - x - 1] < x) 9 | return x; 10 | } 11 | return -1; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1619. Mean of Array After Removing Some Elements/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double trimMean(int[] arr) { 3 | Arrays.sort(arr); 4 | int length = arr.length; 5 | int removeCount = length / 20; 6 | int start = removeCount, end = length - removeCount; 7 | double sum = 0; 8 | for (int i = start; i < end; i++) 9 | sum += arr[i]; 10 | return sum / (length - removeCount * 2); 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1925. Count Square Sum Triples/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countTriples(int n) { 3 | int count = 0; 4 | for (int a = 1; a <= n; a++) { 5 | for (int b = 1; b <= n; b++) { 6 | for (int c = Math.max(a, b) + 1; c <= n; c++) { 7 | if (a * a + b * b == c * c) 8 | count++; 9 | } 10 | } 11 | } 12 | return count; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0263. Ugly Number/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isUgly(int num) { 3 | if (num == 0) 4 | return false; 5 | int temp = num; 6 | int[] factors = {2, 3, 5}; 7 | int length = factors.length; 8 | for (int i = 0; i < length; i++) { 9 | int factor = factors[i]; 10 | while (temp % factor == 0) 11 | temp /= factor; 12 | } 13 | return temp == 1; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1051. Height Checker/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int heightChecker(int[] heights) { 3 | int length = heights.length; 4 | int[] sorted = new int[length]; 5 | System.arraycopy(heights, 0, sorted, 0, length); 6 | Arrays.sort(sorted); 7 | int count = 0; 8 | for (int i = 0; i < length; i++) { 9 | if (heights[i] != sorted[i]) 10 | count++; 11 | } 12 | return count; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1936. Add Minimum Number of Rungs/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int addRungs(int[] rungs, int dist) { 3 | int addCount = 0; 4 | int prev = 0; 5 | int length = rungs.length; 6 | for (int i = 0; i < length; i++) { 7 | int curr = rungs[i]; 8 | int difference = curr - prev; 9 | addCount += (difference - 1) / dist; 10 | prev = curr; 11 | } 12 | return addCount; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1332. Remove Palindromic Subsequences/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removePalindromeSub(String s) { 3 | if (s == null || s.length() == 0) 4 | return 0; 5 | int low = 0, high = s.length() - 1; 6 | while (low < high) { 7 | if (s.charAt(low) == s.charAt(high)) { 8 | low++; 9 | high--; 10 | } else 11 | return 2; 12 | } 13 | return 1; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1800. Maximum Ascending Subarray Sum/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxAscendingSum(int[] nums) { 3 | int maxSum = nums[0], sum = nums[0]; 4 | int length = nums.length; 5 | for (int i = 1; i < length; i++) { 6 | if (nums[i] > nums[i - 1]) 7 | sum += nums[i]; 8 | else 9 | sum = nums[i]; 10 | maxSum = Math.max(maxSum, sum); 11 | } 12 | return maxSum; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0009. Palindrome Number/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPalindrome(int x) { 3 | if (x < 0) 4 | return false; 5 | char[] array = String.valueOf(x).toCharArray(); 6 | int index1 = 0, index2 = array.length - 1; 7 | while (index1 < index2) { 8 | if (array[index1] != array[index2]) 9 | return false; 10 | index1++; 11 | index2--; 12 | } 13 | return true; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0096. Unique Binary Search Trees/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int numTrees(int n) { 3 | int[] counts = new int[n + 1]; 4 | if (n == 0) 5 | return 1; 6 | counts[0] = 1; 7 | counts[1] = 1; 8 | for (int i = 2; i <= n; i++) { 9 | counts[i] = 0; 10 | for (int j = 0; j < i; j++) 11 | counts[i] += counts[j] * counts[i - j - 1]; 12 | } 13 | return counts[n]; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1085. Sum of Digits in the Minimum Number/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int sumOfDigits(int[] A) { 3 | int min = Integer.MAX_VALUE; 4 | for (int num : A) 5 | min = Math.min(min, num); 6 | if (min == 0) 7 | return 1; 8 | int sum = 0; 9 | int temp = min; 10 | while (temp > 0) { 11 | sum += temp % 10; 12 | temp /= 10; 13 | } 14 | return sum % 2 == 0 ? 1 : 0; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1165. Single-Row Keyboard/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int calculateTime(String keyboard, String word) { 3 | int time = 0; 4 | int index = 0; 5 | int length = word.length(); 6 | for (int i = 0; i < length; i++) { 7 | char c = word.charAt(i); 8 | int curIndex = keyboard.indexOf(c); 9 | time += Math.abs(curIndex - index); 10 | index = curIndex; 11 | } 12 | return time; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1374. Generate a String With Characters That Have Odd Counts/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String generateTheString(int n) { 3 | StringBuffer sb = new StringBuffer(); 4 | if (n % 2 == 0) { 5 | sb.append('a'); 6 | for (int i = 1; i < n; i++) 7 | sb.append('b'); 8 | } else { 9 | for (int i = 0; i < n; i++) 10 | sb.append('a'); 11 | } 12 | return sb.toString(); 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1389. Create Target Array in the Given Order/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] createTargetArray(int[] nums, int[] index) { 3 | List list = new ArrayList(); 4 | int length = nums.length; 5 | for (int i = 0; i < length; i++) 6 | list.add(index[i], nums[i]); 7 | int[] target = new int[length]; 8 | for (int i = 0; i < length; i++) 9 | target[i] = list.get(i); 10 | return target; 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1672. Richest Customer Wealth/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximumWealth(int[][] accounts) { 3 | int maxWealth = 0; 4 | int rows = accounts.length; 5 | for (int i = 0; i < rows; i++) { 6 | int[] account = accounts[i]; 7 | int sum = 0; 8 | for (int amount : account) 9 | sum += amount; 10 | maxWealth = Math.max(maxWealth, sum); 11 | } 12 | return maxWealth; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0171. Excel Sheet Column Number/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int titleToNumber(String s) { 3 | int length = s.length(); 4 | int[] array = new int[length]; 5 | for (int i = 0; i < length; i++) 6 | array[i] = s.charAt(i) - 'A'; 7 | int number = 0; 8 | for (int i = 0; i < length; i++) { 9 | number *= 26; 10 | number += array[i]; 11 | number++; 12 | } 13 | return number; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1491. Average Salary Excluding the Minimum and Maximum Salary/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double average(int[] salary) { 3 | int length = salary.length; 4 | double sum = 0; 5 | int max = Integer.MIN_VALUE, min = Integer.MAX_VALUE; 6 | for (int num : salary) { 7 | sum += num; 8 | max = Math.max(max, num); 9 | min = Math.min(min, num); 10 | } 11 | return (sum - max - min) / (length - 2); 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1668. Maximum Repeating Substring/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxRepeating(String sequence, String word) { 3 | int maxRepeating = 0; 4 | StringBuffer sb = new StringBuffer(); 5 | int k = 0; 6 | while (sb.length() <= sequence.length()) { 7 | sb.append(word); 8 | k++; 9 | if (sequence.indexOf(sb.toString()) >= 0) 10 | maxRepeating = k; 11 | } 12 | return maxRepeating; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1827. Minimum Operations to Make the Array Increasing/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minOperations(int[] nums) { 3 | int operations = 0; 4 | int length = nums.length; 5 | for (int i = 1; i < length; i++) { 6 | int prev = nums[i - 1], curr = nums[i]; 7 | int minCurr = Math.max(prev + 1, curr); 8 | operations += minCurr - curr; 9 | nums[i] = minCurr; 10 | } 11 | return operations; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0045. Jump Game II/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int jump(int[] nums) { 3 | int length = nums.length; 4 | int end = 0; 5 | int maxPosition = 0; 6 | int steps = 0; 7 | for (int i = 0; i < length - 1; i++) { 8 | maxPosition = Math.max(maxPosition, i + nums[i]); 9 | if (i == end) { 10 | end = maxPosition; 11 | steps++; 12 | } 13 | } 14 | return steps; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1118. Number of Days in a Month/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfDays(int Y, int M) { 3 | if (M == 1 || M == 3 || M == 5 || M == 7 || M == 8 || M == 10 || M == 12) 4 | return 31; 5 | else if (M == 4 || M == 6 || M == 9 || M == 11) 6 | return 30; 7 | else { 8 | if (Y % 4 == 0 && Y % 100 != 0 || Y % 400 == 0) 9 | return 29; 10 | else 11 | return 28; 12 | } 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1826. Faulty Sensor/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int badSensor(int[] sensor1, int[] sensor2) { 3 | int length = sensor1.length; 4 | int index = 0; 5 | while (index < length && sensor1[index] == sensor2[index]) 6 | index++; 7 | if (index >= length - 1) 8 | return -1; 9 | while (index < length - 1 && sensor1[index] == sensor2[index + 1]) 10 | index++; 11 | return index == length - 1 ? 1 : 2; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1833. Maximum Ice Cream Bars/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxIceCream(int[] costs, int coins) { 3 | Arrays.sort(costs); 4 | int count = 0; 5 | int length = costs.length; 6 | for (int i = 0; i < length; i++) { 7 | int cost = costs[i]; 8 | if (coins >= cost) { 9 | coins -= cost; 10 | count++; 11 | } else 12 | break; 13 | } 14 | return count; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1502. Can Make Arithmetic Progression From Sequence/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canMakeArithmeticProgression(int[] arr) { 3 | if (arr.length == 2) 4 | return true; 5 | Arrays.sort(arr); 6 | int length = arr.length; 7 | int difference = arr[1] - arr[0]; 8 | for (int i = 2; i < length; i++) { 9 | if (arr[i] - arr[i - 1] != difference) 10 | return false; 11 | } 12 | return true; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1518. Water Bottles/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numWaterBottles(int numBottles, int numExchange) { 3 | int total = 0; 4 | int water = numBottles, empty = 0; 5 | while (water > 0) { 6 | total += water; 7 | empty += water; 8 | water = 0; 9 | int exchange = empty / numExchange; 10 | water += exchange; 11 | empty -= numExchange * exchange; 12 | } 13 | return total; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1984. Minimum Difference Between Highest and Lowest of K Scores/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minimumDifference(int[] nums, int k) { 3 | int minDifference = Integer.MAX_VALUE; 4 | Arrays.sort(nums); 5 | int length = nums.length; 6 | for (int i = k - 1; i < length; i++) { 7 | int difference = nums[i] - nums[i - k + 1]; 8 | minDifference = Math.min(minDifference, difference); 9 | } 10 | return minDifference; 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0062. Unique Paths/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int uniquePaths(int m, int n) { 3 | int[][] counts = new int[m][n]; 4 | for (int i = 0; i < m; i++) 5 | counts[i][0] = 1; 6 | for (int j = 1; j < n; j++) 7 | counts[0][j] = 1; 8 | for (int i = 1; i < m; i++) { 9 | for (int j = 1; j < n; j++) 10 | counts[i][j] = counts[i - 1][j] + counts[i][j - 1]; 11 | } 12 | return counts[m - 1][n - 1]; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0397. Integer Replacement/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int integerReplacement(int n) { 3 | long longN = (long) n; 4 | return integerReplacement(longN); 5 | } 6 | 7 | public int integerReplacement(long n) { 8 | if (n == 1) 9 | return 0; 10 | else if (n % 2 == 0) 11 | return integerReplacement(n / 2) + 1; 12 | else 13 | return Math.min(integerReplacement(n - 1), integerReplacement(n + 1)) + 1; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0598. Range Addition II/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxCount(int m, int n, int[][] ops) { 3 | if (m == 0 || n == 0) 4 | return 0; 5 | int maxRow = m, maxColumn = n; 6 | for (int[] op : ops) { 7 | maxRow = Math.min(maxRow, op[0]); 8 | maxColumn = Math.min(maxColumn, op[1]); 9 | } 10 | long maxArea = (long) maxRow * (long) maxColumn; 11 | int maxCount = (int) maxArea; 12 | return maxCount; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0643. Maximum Average Subarray I/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double findMaxAverage(int[] nums, int k) { 3 | int length = nums.length; 4 | int sum = 0; 5 | for (int i = 0; i < k; i++) 6 | sum += nums[i]; 7 | int maxSum = sum; 8 | for (int i = k; i < length; i++) { 9 | sum -= nums[i - k]; 10 | sum += nums[i]; 11 | maxSum = Math.max(maxSum, sum); 12 | } 13 | return 1.0 * maxSum / k; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0717. 1-bit and 2-bit Characters/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isOneBitCharacter(int[] bits) { 3 | int length = bits.length; 4 | int index = 0; 5 | while (index < length) { 6 | if (index == length - 1) 7 | return true; 8 | int bit = bits[index]; 9 | if (bit == 0) 10 | index++; 11 | else if (bit == 1) 12 | index += 2; 13 | } 14 | return false; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0829. Consecutive Numbers Sum/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int consecutiveNumbersSum(int N) { 3 | int max = (int) Math.sqrt(N * 2); 4 | int count = 0; 5 | for (int i = 1; i <= max; i++) { 6 | if (i % 2 == 1) { 7 | if (N % i == 0) 8 | count++; 9 | } else { 10 | if (N % i != 0 && N * 2 % i == 0) 11 | count++; 12 | } 13 | } 14 | return count; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1614. Maximum Nesting Depth of the Parentheses/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxDepth(String s) { 3 | int maxDepth = 0; 4 | int depth = 0; 5 | int length = s.length(); 6 | for (int i = 0; i < length; i++) { 7 | if (s.charAt(i) == '(') 8 | depth++; 9 | else if (s.charAt(i) == ')') 10 | depth--; 11 | maxDepth = Math.max(maxDepth, depth); 12 | } 13 | return maxDepth; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1692. Count Ways to Distribute Candies/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int waysToDistribute(int n, int k) { 3 | final int MODULO = 1000000007; 4 | long[][] dp = new long[k][n]; 5 | for (int j = 0; j < n; j++) 6 | dp[0][j] = 1; 7 | for (int i = 1; i < k; i++) { 8 | for (int j = i; j < n; j++) 9 | dp[i][j] = (dp[i][j - 1] * (i + 1) + dp[i - 1][j - 1]) % MODULO; 10 | } 11 | return (int) dp[k - 1][n - 1]; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1784. Check if Binary String Has at Most One Segment of Ones/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean checkOnesSegment(String s) { 3 | int onesSegments = 0; 4 | int length = s.length(); 5 | for (int i = 0; i < length; i++) { 6 | char c = s.charAt(i); 7 | if (c == '1') { 8 | if (i == 0 || s.charAt(i - 1) == '0') 9 | onesSegments++; 10 | } 11 | } 12 | return onesSegments <= 1; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1921. Eliminate Maximum Number of Monsters/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int eliminateMaximum(int[] dist, int[] speed) { 3 | int n = dist.length; 4 | int[] time = new int[n]; 5 | for (int i = 0; i < n; i++) 6 | time[i] = dist[i] / speed[i] + (dist[i] % speed[i] == 0 ? 0 : 1); 7 | Arrays.sort(time); 8 | for (int i = 1; i < n; i++) { 9 | if (time[i] <= i) 10 | return i; 11 | } 12 | return n; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0007. Reverse Integer/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int reverse(int x) { 3 | int sign = x >= 0 ? 1 : -1; 4 | int newX = Math.abs(x); 5 | long xLong = 0; 6 | while (newX > 0) { 7 | xLong = xLong * 10 + newX % 10; 8 | newX /= 10; 9 | } 10 | xLong *= sign; 11 | if (xLong > Integer.MAX_VALUE || xLong < Integer.MIN_VALUE) 12 | return 0; 13 | int reverse = (int) xLong; 14 | return reverse; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0357. Count Numbers with Unique Digits/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countNumbersWithUniqueDigits(int n) { 3 | if (n == 0) 4 | return 1; 5 | int totalCount = 10; 6 | int count = 9; 7 | int nextCount = 9; 8 | int upper = Math.min(n, 10); 9 | for (int i = 2; i <= upper; i++) { 10 | count *= nextCount; 11 | nextCount--; 12 | totalCount += count; 13 | } 14 | return totalCount; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1064. Fixed Point/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int fixedPoint(int[] A) { 3 | int low = 0, high = A.length - 1; 4 | while (low < high) { 5 | int mid = (high - low) / 2 + low; 6 | int num = A[mid]; 7 | if (num == mid) 8 | high = mid; 9 | else if (num > mid) 10 | high = mid - 1; 11 | else 12 | low = mid + 1; 13 | } 14 | return A[low] == low ? low : -1; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1436. Destination City/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String destCity(List> paths) { 3 | Set srcSet = new HashSet(); 4 | for (List path : paths) { 5 | String src = path.get(0); 6 | srcSet.add(src); 7 | } 8 | for (List path : paths) { 9 | String dst = path.get(1); 10 | if (!srcSet.contains(dst)) 11 | return dst; 12 | } 13 | return ""; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1848. Minimum Distance to the Target Element/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getMinDistance(int[] nums, int target, int start) { 3 | int minDistance = Integer.MAX_VALUE; 4 | int length = nums.length; 5 | for (int i = 0; i < length; i++) { 6 | if (nums[i] == target) { 7 | int distance = Math.abs(i - start); 8 | minDistance = Math.min(minDistance, distance); 9 | } 10 | } 11 | return minDistance; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1893. Check if All the Integers in a Range Are Covered/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isCovered(int[][] ranges, int left, int right) { 3 | Set set = new HashSet(); 4 | for (int[] range : ranges) { 5 | int start = Math.max(range[0], left); 6 | int end = Math.min(range[1], right); 7 | for (int i = start; i <= end; i++) 8 | set.add(i); 9 | } 10 | return set.size() == right - left + 1; 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0704. Binary Search/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int search(int[] nums, int target) { 3 | int low = 0, high = nums.length - 1; 4 | while (low <= high) { 5 | int mid = (high - low) / 2 + low; 6 | int num = nums[mid]; 7 | if (num == target) 8 | return mid; 9 | else if (num > target) 10 | high = mid - 1; 11 | else 12 | low = mid + 1; 13 | } 14 | return -1; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0771. Jewels and Stones/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numJewelsInStones(String J, String S) { 3 | if (J == null || S == null || J.length() == 0 || S.length() == 0) 4 | return 0; 5 | int jewels = 0; 6 | int length = S.length(); 7 | for (int i = 0; i < length; i++) { 8 | char c = S.charAt(i); 9 | int index = J.indexOf(c); 10 | if (index >= 0) 11 | jewels++; 12 | } 13 | return jewels; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1598. Crawler Log Folder/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minOperations(String[] logs) { 3 | int level = 0; 4 | int length = logs.length; 5 | for (int i = 0; i < length; i++) { 6 | String log = logs[i]; 7 | if (log.equals("../")) 8 | level = Math.max(level - 1, 0); 9 | else if (log.equals("./")) 10 | continue; 11 | else 12 | level++; 13 | } 14 | return level; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0121. Best Time to Buy and Sell Stock/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxProfit(int[] prices) { 3 | int minPrice = Integer.MAX_VALUE; 4 | int maxProfit = 0; 5 | int length = prices.length; 6 | for (int i = 0; i < length; i++) { 7 | if (prices[i] < minPrice) 8 | minPrice = prices[i]; 9 | else if (prices[i] - minPrice > maxProfit) 10 | maxProfit = prices[i] - minPrice; 11 | } 12 | return maxProfit; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0412. Fizz Buzz/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List fizzBuzz(int n) { 3 | List list = new ArrayList(); 4 | for (int i = 1; i <= n; i++) { 5 | String str = ""; 6 | if (i % 3 == 0) 7 | str += "Fizz"; 8 | if (i % 5 == 0) 9 | str += "Buzz"; 10 | if (str.length() == 0) 11 | str = String.valueOf(i); 12 | list.add(str); 13 | } 14 | return list; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1137. N-th Tribonacci Number.md/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int tribonacci(int n) { 3 | if (n == 0) 4 | return 0; 5 | if (n == 1 || n == 2) 6 | return 1; 7 | int[] tribonacci = new int[n + 1]; 8 | tribonacci[0] = 0; 9 | tribonacci[1] = 1; 10 | tribonacci[2] = 1; 11 | for (int i = 3; i <= n; i++) 12 | tribonacci[i] = tribonacci[i - 3] + tribonacci[i - 2] + tribonacci[i - 1]; 13 | return tribonacci[n]; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1411. Number of Ways to Paint N × 3 Grid/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numOfWays(int n) { 3 | final int MODULO = 1000000007; 4 | long prev2 = 6, prev3 = 6; 5 | for (int i = 2; i <= n; i++) { 6 | long curr2 = (prev2 * 3 + prev3 * 2) % MODULO; 7 | long curr3 = (prev2 * 2 + prev3 * 2) % MODULO; 8 | prev2 = curr2; 9 | prev3 = curr3; 10 | } 11 | int ways = (int) ((prev2 + prev3) % MODULO); 12 | return ways; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1716. Calculate Money in Leetcode Bank/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int totalMoney(int n) { 3 | int total = 0; 4 | int start = 0; 5 | int curr = 0; 6 | for (int i = 1; i <= n; i++) { 7 | if (i % 7 == 1) { 8 | start++; 9 | total += start; 10 | curr = start; 11 | } else { 12 | curr++; 13 | total += curr; 14 | } 15 | } 16 | return total; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1979. Find Greatest Common Divisor of Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findGCD(int[] nums) { 3 | Arrays.sort(nums); 4 | int minNum = nums[0], maxNum = nums[nums.length - 1]; 5 | return gcd(minNum, maxNum); 6 | } 7 | 8 | public int gcd(int a, int b) { 9 | if (a > b) { 10 | int temp = a; 11 | a = b; 12 | b = temp; 13 | } 14 | if (b % a == 0) 15 | return a; 16 | return gcd(a, b % a); 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0945. Minimum Increment to Make Array Unique/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minIncrementForUnique(int[] A) { 3 | if (A == null || A.length <= 1) 4 | return 0; 5 | int increments = 0; 6 | Arrays.sort(A); 7 | int curValue = A[0]; 8 | int length = A.length; 9 | for (int i = 1; i < length; i++) { 10 | curValue = Math.max(curValue + 1, A[i]); 11 | increments += curValue - A[i]; 12 | } 13 | return increments; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1103. Distribute Candies to People/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] distributeCandies(int candies, int num_people) { 3 | int[] array = new int[num_people]; 4 | int index = 0; 5 | int curCandies = 1; 6 | while (candies > 0) { 7 | int num = Math.min(curCandies, candies); 8 | array[index] += num; 9 | candies -= num; 10 | curCandies++; 11 | index = (index + 1) % num_people; 12 | } 13 | return array; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1221. Split a String in Balanced Strings/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int balancedStringSplit(String s) { 3 | int count = 0; 4 | int position = 0; 5 | int length = s.length(); 6 | for (int i = 0; i < length; i++) { 7 | char c = s.charAt(i); 8 | if (c == 'R') 9 | position++; 10 | else 11 | position--; 12 | if (position == 0) 13 | count++; 14 | } 15 | return count; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1266. Minimum Time Visiting All Points/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minTimeToVisitAllPoints(int[][] points) { 3 | int time = 0; 4 | int length = points.length; 5 | for (int i = 1; i < length; i++) { 6 | int[] prevPoint = points[i - 1]; 7 | int[] curPoint = points[i]; 8 | int curTime = Math.max(Math.abs(curPoint[0] - prevPoint[0]), Math.abs(curPoint[1] - prevPoint[1])); 9 | time += curTime; 10 | } 11 | return time; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0035. Search Insert Position/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int searchInsert(int[] nums, int target) { 3 | int low = 0, high = nums.length - 1; 4 | while (low <= high) { 5 | int mid = (high - low) / 2 + low; 6 | int num = nums[mid]; 7 | if (num == target) 8 | return mid; 9 | else if (num > target) 10 | high = mid - 1; 11 | else 12 | low = mid + 1; 13 | } 14 | return low; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0343. Integer Break/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int integerBreak(int n) { 3 | if (n == 2) 4 | return 1; 5 | if (n == 3) 6 | return 2; 7 | int quotient = n / 3; 8 | int remainder = n % 3; 9 | if (remainder == 1) { 10 | quotient--; 11 | remainder += 3; 12 | } 13 | int product = (int) Math.pow(3, quotient); 14 | if (remainder != 0) 15 | product *= remainder; 16 | return product; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0693. Binary Number with Alternating Bits/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean hasAlternatingBits(int n) { 3 | if (n <= 2) 4 | return true; 5 | else if (n <= 4) 6 | return false; 7 | int prevBit = n & 1; 8 | n >>= 1; 9 | while (n > 0) { 10 | int curBit = n & 1; 11 | if (prevBit == curBit) 12 | return false; 13 | prevBit = curBit; 14 | n >>= 1; 15 | } 16 | return true; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0790. Domino and Tromino Tiling/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numTilings(int N) { 3 | if (N == 0) 4 | return 1; 5 | if (N <= 2) 6 | return N; 7 | final int MODULO = 1000000007; 8 | int prev2 = 1, prev1 = 1, cur = 2; 9 | for (int i = 3; i <= N; i++) { 10 | int next = (cur * 2 % MODULO + prev2) % MODULO; 11 | prev2 = prev1; 12 | prev1 = cur; 13 | cur = next; 14 | } 15 | return cur; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0944. Delete Columns to Make Sorted/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minDeletionSize(String[] A) { 3 | int rows = A.length, columns = A[0].length(); 4 | int deletionSize = 0; 5 | for (int i = 0; i < columns; i++) { 6 | for (int j = 1; j < rows; j++) { 7 | if (A[j].charAt(i) < A[j - 1].charAt(i)) { 8 | deletionSize++; 9 | break; 10 | } 11 | } 12 | } 13 | return deletionSize; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1109. Corporate Flight Bookings/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] corpFlightBookings(int[][] bookings, int n) { 3 | int[] seats = new int[n]; 4 | for (int[] booking : bookings) { 5 | int start = booking[0], end = booking[1], count = booking[2]; 6 | seats[start - 1] += count; 7 | if (end < n) 8 | seats[end] -= count; 9 | } 10 | for (int i = 1; i < n; i++) 11 | seats[i] += seats[i - 1]; 12 | return seats; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1526. Minimum Number of Increments on Subarrays to Form a Target Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minNumberOperations(int[] target) { 3 | int operations = 0; 4 | int prevHeight = 0; 5 | int length = target.length; 6 | for (int i = 0; i < length; i++) { 7 | int height = target[i]; 8 | if (height > prevHeight) 9 | operations += height - prevHeight; 10 | prevHeight = height; 11 | } 12 | return operations; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0324. Wiggle Sort II/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public void wiggleSort(int[] nums) { 3 | Arrays.sort(nums); 4 | int length = nums.length; 5 | int[] temp = new int[length]; 6 | int mid = (length + 1) / 2; 7 | for (int i = 0; i < mid; i++) 8 | temp[2 * (mid - i - 1)] = nums[i]; 9 | for (int i = mid; i < length; i++) 10 | temp[2 * (length - i - 1) + 1] = nums[i]; 11 | for (int i = 0; i < length; i++) 12 | nums[i] = temp[i]; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0377. Combination Sum IV/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int combinationSum4(int[] nums, int target) { 3 | if (nums == null || nums.length == 0) 4 | return 0; 5 | int length = nums.length; 6 | int[] dp = new int[target + 1]; 7 | dp[0] = 1; 8 | for (int i = 1; i <= target; i++) { 9 | for (int num : nums) { 10 | if (num <= i) 11 | dp[i] += dp[i - num]; 12 | } 13 | } 14 | return dp[target]; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1557. Minimum Number of Vertices to Reach All Nodes/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List findSmallestSetOfVertices(int n, List> edges) { 3 | List list = new ArrayList(); 4 | Set endSet = new HashSet(); 5 | for (List edge : edges) 6 | endSet.add(edge.get(1)); 7 | for (int i = 0; i < n; i++) { 8 | if (!endSet.contains(i)) 9 | list.add(i); 10 | } 11 | return list; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1985. Find the Kth Largest Integer in the Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String kthLargestNumber(String[] nums, int k) { 3 | Arrays.sort(nums, new Comparator() { 4 | public int compare(String num1, String num2) { 5 | if (num1.length() != num2.length()) 6 | return num1.length() - num2.length(); 7 | else 8 | return num1.compareTo(num2); 9 | } 10 | }); 11 | return nums[nums.length - k]; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0223. Rectangle Area/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int computeArea(int A, int B, int C, int D, int E, int F, int G, int H) { 3 | long area1 = ((long) C - (long) A) * ((long) D - (long) B), area2 = ((long) G - (long) E) * ((long) H - (long) F); 4 | long overlapWidth = (long) Math.min(C, G) - (long) Math.max(A, E), overlapHeight = (long) Math.min(D, H) - (long) Math.max(B, F); 5 | long overlapArea = Math.max(overlapWidth, 0) * Math.max(overlapHeight, 0); 6 | return (int) (area1 + area2 - overlapArea); 7 | } 8 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0275. H-Index II/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int hIndex(int[] citations) { 3 | int length = citations.length; 4 | if (length == 0 || citations[length - 1] == 0) 5 | return 0; 6 | int low = 0, high = length - 1; 7 | while (low < high) { 8 | int mid = (high - low) / 2 + low; 9 | if (citations[mid] < length - mid) 10 | low = mid + 1; 11 | else 12 | high = mid; 13 | } 14 | return length - low; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0920. Number of Music Playlists/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numMusicPlaylists(int N, int L, int K) { 3 | final int MODULO = 1000000007; 4 | long[][] dp = new long[L + 1][N + 1]; 5 | dp[0][0] = 1; 6 | for (int i = 1; i <= L; i++) { 7 | for (int j = 1; j <= N; j++) { 8 | dp[i][j] += dp[i - 1][j - 1] * (N - j + 1) + dp[i - 1][j] * Math.max(j - K, 0); 9 | dp[i][j] %= MODULO; 10 | } 11 | } 12 | return (int) dp[L][N]; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0219. Contains Duplicate II/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean containsNearbyDuplicate(int[] nums, int k) { 3 | Map map = new HashMap(); 4 | int length = nums.length; 5 | for (int i = 0; i < length; i++) { 6 | int num = nums[i]; 7 | int prevIndex = map.getOrDefault(num, -1); 8 | if (prevIndex >= 0 && i - prevIndex <= k) 9 | return true; 10 | map.put(num, i); 11 | } 12 | return false; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0260. Single Number III/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int[] singleNumber(int[] nums) { 3 | int[] singles = new int[2]; 4 | int result = nums[0]; 5 | for (int i = 1; i < nums.length; i++) 6 | result = result ^ nums[i]; 7 | singles[0] = 0; 8 | singles[1] = 0; 9 | int n = result & (~(result - 1)); 10 | for (int i = 0; i < nums.length; i++) { 11 | if ((n & nums[i]) != 0) 12 | singles[0] = singles[0] ^ nums[i]; 13 | else 14 | singles[1] = singles[1] ^ nums[i]; 15 | } 16 | return singles; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0278. First Bad Version/Solution.java: -------------------------------------------------------------------------------- 1 | /* The isBadVersion API is defined in the parent class VersionControl. 2 | boolean isBadVersion(int version); */ 3 | 4 | public class Solution extends VersionControl { 5 | public int firstBadVersion(int n) { 6 | int low = 1, high = n; 7 | while (low < high) { 8 | int mid = (high - low) / 2 + low; 9 | if (isBadVersion(mid)) 10 | high = mid; 11 | else 12 | low = mid + 1; 13 | } 14 | return low; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0470. Implement Rand10() Using Rand7()/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * The rand7() API is already defined in the parent class SolBase. 3 | * public int rand7(); 4 | * @return a random integer in the range 1 to 7 5 | */ 6 | class Solution extends SolBase { 7 | public int rand10() { 8 | int num = 100; 9 | while (num >= 40) { 10 | int num1 = rand7() - 1; 11 | int num2 = rand7() - 1; 12 | num = num1 * 7 + num2; 13 | } 14 | int randNum = num / 4 + 1; 15 | return randNum; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0485. Max Consecutive Ones/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMaxConsecutiveOnes(int[] nums) { 3 | int max = 0; 4 | int curConsecutiveOnes = 0; 5 | int length = nums.length; 6 | for (int i = 0; i < length; i++) { 7 | int num = nums[i]; 8 | if (num == 1) { 9 | curConsecutiveOnes++; 10 | max = Math.max(max, curConsecutiveOnes); 11 | } else 12 | curConsecutiveOnes = 0; 13 | } 14 | return max; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1437. Check If All 1's Are at Least Length K Places Away/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean kLengthApart(int[] nums, int k) { 3 | if (k == 0) 4 | return true; 5 | int length = nums.length; 6 | int prevOneIndex = -k - 1; 7 | for (int i = 0; i < length; i++) { 8 | if (nums[i] == 1) { 9 | if (i - prevOneIndex <= k) 10 | return false; 11 | prevOneIndex = i; 12 | } 13 | } 14 | return true; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1911. Maximum Alternating Subsequence Sum/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long maxAlternatingSum(int[] nums) { 3 | int length = nums.length; 4 | long[][] dp = new long[length][2]; 5 | dp[0][0] = nums[0]; 6 | for (int i = 1; i < length; i++) { 7 | dp[i][0] = Math.max(Math.max(dp[i - 1][1], 0) + nums[i], dp[i - 1][0]); 8 | dp[i][1] = Math.max(Math.max(dp[i - 1][0] - nums[i], 0), dp[i - 1][1]); 9 | } 10 | return Math.max(dp[length - 1][0], dp[length - 1][1]); 11 | } 12 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0198. House Robber/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rob(int[] nums) { 3 | if (nums == null || nums.length == 0) 4 | return 0; 5 | int length = nums.length; 6 | int[][] dp = new int[length][2]; 7 | dp[0][0] = 0; 8 | dp[0][1] = nums[0]; 9 | for (int i = 1; i < length; i++) { 10 | dp[i][0] = Math.max(dp[i - 1][0], dp[i - 1][1]); 11 | dp[i][1] = dp[i - 1][0] + nums[i]; 12 | } 13 | return Math.max(dp[length - 1][0], dp[length - 1][1]); 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0504. Base 7/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String convertToBase7(int num) { 3 | if (num == 0) 4 | return "0"; 5 | int sign = num >= 0 ? 1 : -1; 6 | num = Math.abs(num); 7 | StringBuffer base7 = new StringBuffer(); 8 | while (num > 0) { 9 | int remainder = num % 7; 10 | base7.insert(0, (char) (remainder + '0')); 11 | num /= 7; 12 | } 13 | if (sign < 0) 14 | base7.insert(0, '-'); 15 | return base7.toString(); 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0788. Rotated Digits/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rotatedDigits(int N) { 3 | int count = 0; 4 | for (int i = 1; i <= N; i++) { 5 | String numStr = String.valueOf(i); 6 | if (numStr.indexOf('3') >= 0 || numStr.indexOf('4') >= 0 || numStr.indexOf('7') >= 0) 7 | continue; 8 | if (numStr.indexOf('2') >= 0 || numStr.indexOf('5') >= 0 || numStr.indexOf('6') >= 0 || numStr.indexOf('9') >= 0) 9 | count++; 10 | } 11 | return count; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1033. Moving Stones Until Consecutive/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] numMovesStones(int a, int b, int c) { 3 | int left = Math.min(Math.min(a, b), c); 4 | int right = Math.max(Math.max(a, b), c); 5 | int mid = a + b + c - left - right; 6 | int min = 2; 7 | if (mid - left == 1 && right - mid == 1) 8 | min = 0; 9 | else if (mid - left <= 2 || right - mid <= 2) 10 | min = 1; 11 | int max = right - left - 2; 12 | return new int[]{min, max}; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0201. Bitwise AND of Numbers Range/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rangeBitwiseAnd(int m, int n) { 3 | int res = 0; 4 | while (m > 0 && n > 0) { 5 | int log1 = (int) (Math.log(m) / Math.log(2)); 6 | int log2 = (int) (Math.log(n) / Math.log(2)); 7 | if (log1 != log2) 8 | return res; 9 | int curAndVal = 1 << log1; 10 | res += curAndVal; 11 | m -= curAndVal; 12 | n -= curAndVal; 13 | } 14 | return res; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0370. Range Addition/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] getModifiedArray(int length, int[][] updates) { 3 | int[] array = new int[length]; 4 | for (int[] update : updates) { 5 | int startIndex = update[0], endIndex = update[1], inc = update[2]; 6 | array[startIndex] += inc; 7 | if (endIndex < length - 1) 8 | array[endIndex + 1] -= inc; 9 | } 10 | for (int i = 1; i < length; i++) 11 | array[i] += array[i - 1]; 12 | return array; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0686. Repeated String Match/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int repeatedStringMatch(String A, String B) { 3 | int lengthA = A.length(), lengthB = B.length(); 4 | StringBuffer sb = new StringBuffer(); 5 | int maxLength = lengthA * 2 + lengthB; 6 | int repeatTimes = 0; 7 | while (sb.length() <= maxLength) { 8 | sb.append(A); 9 | repeatTimes++; 10 | if (sb.toString().indexOf(B) >= 0) 11 | return repeatTimes; 12 | } 13 | return -1; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1287. Element Appearing More Than 25% In Sorted Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findSpecialInteger(int[] arr) { 3 | int length = arr.length; 4 | Map map = new HashMap(); 5 | for (int i = 0; i < length; i++) { 6 | int num = arr[i]; 7 | int count = map.getOrDefault(num, 0); 8 | count++; 9 | if (count * 4 > length) 10 | return num; 11 | map.put(num, count); 12 | } 13 | return 0; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1509. Minimum Difference Between Largest and Smallest Value in Three Moves/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minDifference(int[] nums) { 3 | int length = nums.length; 4 | if (length <= 4) 5 | return 0; 6 | Arrays.sort(nums); 7 | int difference = length - 4; 8 | int minDifference = nums[length - 1] - nums[0]; 9 | for (int i = difference; i < length; i++) 10 | minDifference = Math.min(minDifference, nums[i] - nums[i - difference]); 11 | return minDifference; 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0482. License Key Formatting/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String licenseKeyFormatting(String S, int K) { 3 | S = S.toUpperCase(); 4 | S = S.replaceAll("-", ""); 5 | int length = S.length(); 6 | StringBuffer sb = new StringBuffer(S); 7 | int count = 0; 8 | for (int i = length - 1; i > 0; i--) { 9 | count++; 10 | if (count == K) { 11 | sb.insert(i, '-'); 12 | count = 0; 13 | } 14 | } 15 | return sb.toString(); 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0997. Find the Town Judge/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findJudge(int N, int[][] trust) { 3 | int[][] trustArray = new int[N][2]; 4 | for (int[] curTrust : trust) { 5 | int person1 = curTrust[0] - 1, person2 = curTrust[1] - 1; 6 | trustArray[person1][0]++; 7 | trustArray[person2][1]++; 8 | } 9 | for (int i = 0; i < N; i++) { 10 | if (trustArray[i][0] == 0 && trustArray[i][1] == N - 1) 11 | return i + 1; 12 | } 13 | return -1; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1798. Maximum Number of Consecutive Values You Can Make/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getMaximumConsecutive(int[] coins) { 3 | Arrays.sort(coins); 4 | if (coins[0] > 1) 5 | return 1; 6 | int curr = 1; 7 | int length = coins.length, index = 0; 8 | while (true) { 9 | if (index < length && coins[index] <= curr) { 10 | curr += coins[index]; 11 | index++; 12 | } else 13 | break; 14 | } 15 | return curr; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1953. Maximum Number of Weeks for Which You Can Work/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public long numberOfWeeks(int[] milestones) { 3 | Arrays.sort(milestones); 4 | int length = milestones.length; 5 | int maxMilestone = milestones[length - 1]; 6 | long sum = 0; 7 | for (int milestone : milestones) 8 | sum += milestone; 9 | long remaining = sum - maxMilestone; 10 | if (remaining >= maxMilestone) 11 | return sum; 12 | else 13 | return remaining * 2 + 1; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1974. Minimum Time to Type Word Using Special Typewriter/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minTimeToType(String word) { 3 | int time = 0; 4 | char prev = 'a'; 5 | int length = word.length(); 6 | for (int i = 0; i < length; i++) { 7 | char curr = word.charAt(i); 8 | int difference = Math.abs(curr - prev); 9 | difference = Math.min(difference, 26 - difference); 10 | time += difference + 1; 11 | prev = curr; 12 | } 13 | return time; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0214. Shortest Palindrome/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String shortestPalindrome(String s) { 3 | String reversed = new StringBuffer(s).reverse().toString(); 4 | int length = s.length(); 5 | int index = 0; 6 | for (int i = 0; i < length; i++) { 7 | if (s.substring(0, length - i).equals(reversed.substring(i))) { 8 | index = i; 9 | break; 10 | } 11 | } 12 | String palindrome = reversed.substring(0, index) + s; 13 | return palindrome; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0495. Teemo Attacking/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findPoisonedDuration(int[] timeSeries, int duration) { 3 | if (timeSeries == null || timeSeries.length == 0) 4 | return 0; 5 | int totalDuration = 0; 6 | int length = timeSeries.length; 7 | for (int i = 1; i < length; i++) { 8 | int prev = timeSeries[i - 1], cur = timeSeries[i]; 9 | totalDuration += Math.min(cur - prev, duration); 10 | } 11 | totalDuration += duration; 12 | return totalDuration; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0891. Sum of Subsequence Widths/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int sumSubseqWidths(int[] A) { 3 | final int MODULO = 1000000007; 4 | Arrays.sort(A); 5 | int length = A.length; 6 | long[] pow2 = new long[length]; 7 | pow2[0] = 1; 8 | for (int i = 1; i < length; i++) 9 | pow2[i] = pow2[i - 1] * 2 % MODULO; 10 | long sum = 0; 11 | for (int i = 0; i < length; i++) 12 | sum = (sum + (pow2[i] - pow2[length - 1 - i]) * A[i]) % MODULO; 13 | return (int) sum; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1119. Remove Vowels from a String/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String removeVowels(String S) { 3 | StringBuffer sb = new StringBuffer(); 4 | int length = S.length(); 5 | for (int i = 0; i < length; i++) { 6 | char c = S.charAt(i); 7 | if (!isVowel(c)) 8 | sb.append(c); 9 | } 10 | return sb.toString(); 11 | } 12 | 13 | public boolean isVowel(char letter) { 14 | return letter == 'a' || letter == 'e' || letter == 'i' || letter == 'o' || letter == 'u'; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1375. Bulb Switcher III/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numTimesAllBlue(int[] light) { 3 | int count = 1; 4 | int length = light.length; 5 | int maxConsecutive = length - 1; 6 | for (int i = length - 1; i > 0; i--) { 7 | int turnOn = light[i]; 8 | maxConsecutive = Math.min(maxConsecutive, turnOn - 1); 9 | if (maxConsecutive == i) 10 | count++; 11 | else if (maxConsecutive == 0) 12 | break; 13 | } 14 | return count; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1791. Find Center of Star Graph/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findCenter(int[][] edges) { 3 | int n = edges.length + 1; 4 | int[] connections = new int[n + 1]; 5 | for (int[] edge : edges) { 6 | int node1 = edge[0], node2 = edge[1]; 7 | connections[node1]++; 8 | connections[node2]++; 9 | if (connections[node1] > 1) 10 | return node1; 11 | else if (connections[node2] > 1) 12 | return node2; 13 | } 14 | return 0; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1876. Substrings of Size Three with Distinct Characters/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countGoodSubstrings(String s) { 3 | int count = 0; 4 | int length = s.length(); 5 | for (int i = 3; i <= length; i++) { 6 | String sub = s.substring(i - 3, i); 7 | Set set = new HashSet(); 8 | for (int j = 0; j < 3; j++) 9 | set.add(sub.charAt(j)); 10 | if (set.size() == 3) 11 | count++; 12 | } 13 | return count; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0434. Number of Segments in a String/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countSegments(String s) { 3 | if (s == null) 4 | return 0; 5 | s = s.trim(); 6 | while (s.indexOf(" ") >= 0) 7 | s = s.replaceAll(" ", " "); 8 | int length = s.length(); 9 | if (length == 0) 10 | return 0; 11 | int segments = 1; 12 | for (int i = 0; i < length; i++) { 13 | if (s.charAt(i) == ' ') 14 | segments++; 15 | } 16 | return segments; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1346. Check If N and Its Double Exist/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean checkIfExist(int[] arr) { 3 | Set set = new HashSet(); 4 | for (int num : arr) { 5 | if (num != 0) 6 | set.add(num); 7 | else { 8 | if (!set.add(num)) 9 | return true; 10 | } 11 | } 12 | for (int num : arr) { 13 | if (num != 0 && set.contains(num * 2)) 14 | return true; 15 | } 16 | return false; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1431. Kids With the Greatest Number of Candies/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List kidsWithCandies(int[] candies, int extraCandies) { 3 | int length = candies.length; 4 | int max = candies[0]; 5 | for (int i = 1; i < length; i++) 6 | max = Math.max(max, candies[i]); 7 | List list = new ArrayList(); 8 | for (int i = 0; i < length; i++) { 9 | int total = candies[i] + extraCandies; 10 | list.add(total >= max); 11 | } 12 | return list; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1708. Largest Subarray Length K/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] largestSubarray(int[] nums, int k) { 3 | int maxStart = nums.length - k; 4 | int maxNum = nums[0], maxIndex = 0; 5 | for (int i = 1; i <= maxStart; i++) { 6 | int num = nums[i]; 7 | if (num > maxNum) { 8 | maxNum = num; 9 | maxIndex = i; 10 | } 11 | } 12 | int[] subarray = new int[k]; 13 | System.arraycopy(nums, maxIndex, subarray, 0, k); 14 | return subarray; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0274. H-Index/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int hIndex(int[] citations) { 3 | Arrays.sort(citations); 4 | int length = citations.length; 5 | if (length == 0 || citations[length - 1] == 0) 6 | return 0; 7 | int low = 0, high = length - 1; 8 | while (low < high) { 9 | int mid = (high - low) / 2 + low; 10 | if (citations[mid] < length - mid) 11 | low = mid + 1; 12 | else 13 | high = mid; 14 | } 15 | return length - low; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0483. Smallest Good Base/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String smallestGoodBase(String n) { 3 | long num = Long.parseLong(n); 4 | long max = (long) (Math.log(num) / Math.log(2)); 5 | for (long i = max; i > 1; i--) { 6 | long base = (long) Math.pow(num, 1.0 / i); 7 | long sum = 0; 8 | for (long j = 0; j <= i; j++) 9 | sum = sum * base + 1; 10 | if (sum == num) 11 | return String.valueOf(base); 12 | } 13 | return String.valueOf(num - 1); 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1029. Two City Scheduling/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int twoCitySchedCost(int[][] costs) { 3 | int length = costs.length; 4 | int sum = 0; 5 | int[] differences = new int[length]; 6 | for (int i = 0; i < length; i++) { 7 | sum += costs[i][0]; 8 | differences[i] = costs[i][1] - costs[i][0]; 9 | } 10 | Arrays.sort(differences); 11 | int halfLength = length / 2; 12 | for (int i = 0; i < halfLength; i++) 13 | sum += differences[i]; 14 | return sum; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1184. Distance Between Bus Stops/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int distanceBetweenBusStops(int[] distance, int start, int destination) { 3 | int totalDistance = 0; 4 | for (int num : distance) 5 | totalDistance += num; 6 | int min = Math.min(start, destination), max = Math.max(start, destination); 7 | int distance1 = 0; 8 | for (int i = min; i < max; i++) 9 | distance1 += distance[i]; 10 | int distance2 = totalDistance - distance1; 11 | return Math.min(distance1, distance2); 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1281. Subtract the Product and Sum of Digits of an Integer/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subtractProductAndSum(int n) { 3 | if (n < 10) 4 | return 0; 5 | char[] array = String.valueOf(n).toCharArray(); 6 | int length = array.length; 7 | int product = 1, sum = 0; 8 | for (int i = 0; i < length; i++) { 9 | int digit = array[i] - '0'; 10 | product *= digit; 11 | sum += digit; 12 | } 13 | int difference = product - sum; 14 | return difference; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1646. Get Maximum in Generated Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getMaximumGenerated(int n) { 3 | if (n <= 1) 4 | return n; 5 | int max = 1; 6 | int[] nums = new int[n + 1]; 7 | nums[0] = 0; 8 | nums[1] = 1; 9 | for (int i = 2; i <= n; i++) { 10 | if (i % 2 == 0) 11 | nums[i] = nums[i / 2]; 12 | else 13 | nums[i] = nums[i / 2] + nums[i / 2 + 1]; 14 | max = Math.max(max, nums[i]); 15 | } 16 | return max; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1748. Sum of Unique Elements/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int sumOfUnique(int[] nums) { 3 | Map map = new HashMap(); 4 | for (int num : nums) { 5 | int count = map.getOrDefault(num, 0) + 1; 6 | map.put(num, count); 7 | } 8 | int sum = 0; 9 | Set set = map.keySet(); 10 | for (int num : set) { 11 | int count = map.get(num); 12 | if (count == 1) 13 | sum += num; 14 | } 15 | return sum; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1894. Find the Student that Will Replace the Chalk/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int chalkReplacer(int[] chalk, int k) { 3 | int n = chalk.length; 4 | long[] prefixSums = new long[n]; 5 | prefixSums[0] = chalk[0]; 6 | for (int i = 1; i < n; i++) 7 | prefixSums[i] = prefixSums[i - 1] + chalk[i]; 8 | long remainder = ((long) k) % prefixSums[n - 1]; 9 | for (int i = 0; i < n; i++) { 10 | if (prefixSums[i] > remainder) 11 | return i; 12 | } 13 | return -1; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0760. Find Anagram Mappings/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] anagramMappings(int[] A, int[] B) { 3 | int length = A.length; 4 | List listB = new ArrayList(); 5 | for (int i = 0; i < length; i++) 6 | listB.add(B[i]); 7 | int[] anagram = new int[length]; 8 | for (int i = 0; i < length; i++) { 9 | int num = A[i]; 10 | int index = listB.indexOf(num); 11 | anagram[i] = index; 12 | listB.set(index, -1); 13 | } 14 | return anagram; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1884. Egg Drop With 2 Eggs and N Floors/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int twoEggDrop(int n) { 3 | int[][] dp = new int[n + 1][2]; 4 | for (int i = 0; i <= n; i++) 5 | dp[i][0] = i; 6 | dp[1][1] = 1; 7 | for (int i = 2; i <= n; i++) { 8 | dp[i][1] = Integer.MAX_VALUE; 9 | for (int j = 1; j < i; j++) { 10 | int curr = Math.max(dp[j - 1][0], dp[i - j][1]) + 1; 11 | dp[i][1] = Math.min(dp[i][1], curr); 12 | } 13 | } 14 | return dp[n][1]; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0896. Monotonic Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isMonotonic(int[] A) { 3 | int length = A.length; 4 | if (length <= 2) 5 | return true; 6 | int prevDifference = 0; 7 | for (int i = 1; i < length; i++) { 8 | int difference = A[i] - A[i - 1]; 9 | if (prevDifference > 0 && difference < 0 || prevDifference < 0 && difference > 0) 10 | return false; 11 | if (difference != 0) 12 | prevDifference = difference; 13 | } 14 | return true; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1860. Incremental Memory Leak/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] memLeak(int memory1, int memory2) { 3 | int time = 0; 4 | boolean flag = true; 5 | while (flag) { 6 | time++; 7 | if (memory1 < time && memory2 < time) 8 | flag = false; 9 | else { 10 | if (memory1 >= memory2) 11 | memory1 -= time; 12 | else 13 | memory2 -= time; 14 | } 15 | } 16 | return new int[]{time, memory1, memory2}; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0206. Reverse Linked List/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { val = x; } 7 | * } 8 | */ 9 | class Solution { 10 | public ListNode reverseList(ListNode head) { 11 | ListNode prev = null; 12 | ListNode curr = head; 13 | while (curr != null) { 14 | ListNode nextTemp = curr.next; 15 | curr.next = prev; 16 | prev = curr; 17 | curr = nextTemp; 18 | } 19 | return prev; 20 | } 21 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0629. K Inverse Pairs Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int kInversePairs(int n, int k) { 3 | final int MODULO = 1000000007; 4 | int[][] dp = new int[n + 1][k + 1]; 5 | for (int i = 1; i <= n; i++) 6 | dp[i][0] = 1; 7 | for (int i = 2; i <= n; i++) { 8 | for (int j = 1; j <= k; j++) { 9 | int sum = (dp[i - 1][j] + dp[i][j - 1]) % MODULO; 10 | dp[i][j] = (sum - ((j - i < 0) ? 0 : dp[i - 1][j - i]) + MODULO) % MODULO; 11 | } 12 | } 13 | return dp[n][k]; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0848. Shifting Letters/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String shiftingLetters(String S, int[] shifts) { 3 | int length = shifts.length; 4 | shifts[length - 1] %= 26; 5 | for (int i = length - 2; i >= 0; i--) 6 | shifts[i] = (shifts[i] + shifts[i + 1]) % 26; 7 | char[] array = S.toCharArray(); 8 | for (int i = 0; i < length; i++) { 9 | int num = array[i] - 'a'; 10 | num = (num + shifts[i]) % 26; 11 | array[i] = (char) (num + 'a'); 12 | } 13 | return new String(array); 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0910. Smallest Range II/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int smallestRangeII(int[] A, int K) { 3 | Arrays.sort(A); 4 | int length = A.length; 5 | int min = A[0], max = A[length - 1]; 6 | int difference = max - min; 7 | for (int i = 1; i < length; i++) { 8 | int num1 = A[i - 1], num2 = A[i]; 9 | int high = Math.max(A[length - 1] - K, num1 + K); 10 | int low = Math.min(A[0] + K, num2 - K); 11 | difference = Math.min(difference, high - low); 12 | } 13 | return difference; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1014. Best Sightseeing Pair/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxScoreSightseeingPair(int[] A) { 3 | int maxLeftScore = A[0] + 0; 4 | int maxScore = 0; 5 | int length = A.length; 6 | for (int i = 1; i < length; i++) { 7 | int curLeftScore = A[i] + i; 8 | int curRightScore = A[i] - i; 9 | int curScore = maxLeftScore + curRightScore; 10 | maxScore = Math.max(maxScore, curScore); 11 | maxLeftScore = Math.max(maxLeftScore, curLeftScore); 12 | } 13 | return maxScore; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1199. Minimum Time to Build Blocks/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minBuildTime(int[] blocks, int split) { 3 | PriorityQueue priorityQueue = new PriorityQueue(); 4 | for (int block : blocks) 5 | priorityQueue.offer(block); 6 | while (priorityQueue.size() > 1) { 7 | priorityQueue.poll(); 8 | int secondSmallest = priorityQueue.poll(); 9 | int newTime = secondSmallest + split; 10 | priorityQueue.offer(newTime); 11 | } 12 | return priorityQueue.poll(); 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1351. Count Negative Numbers in a Sorted Matrix/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countNegatives(int[][] grid) { 3 | int count = 0; 4 | int rows = grid.length, columns = grid[0].length; 5 | for (int i = rows - 1; i >= 0; i--) { 6 | if (grid[i][columns - 1] >= 0) 7 | break; 8 | for (int j = columns - 1; j >= 0; j--) { 9 | if (grid[i][j] < 0) 10 | count++; 11 | else 12 | break; 13 | } 14 | } 15 | return count; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0220. Contains Duplicate III/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean containsNearbyAlmostDuplicate(int[] nums, int k, int t) { 3 | int length = nums.length; 4 | for (int i = 0; i < length; i++) { 5 | int num = nums[i]; 6 | int maxIndex = Math.min(i + k, length - 1); 7 | for (int j = i + 1; j <= maxIndex; j++) { 8 | long difference = Math.abs((long) num - (long) nums[j]); 9 | if (difference <= t) 10 | return true; 11 | } 12 | } 13 | return false; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1290. Convert Binary Number in a Linked List to Integer/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { val = x; } 7 | * } 8 | */ 9 | class Solution { 10 | public int getDecimalValue(ListNode head) { 11 | int decimalValue = 0; 12 | ListNode temp = head; 13 | while (temp != null) { 14 | decimalValue <<= 1; 15 | decimalValue += temp.val; 16 | temp = temp.next; 17 | } 18 | return decimalValue; 19 | } 20 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1347. Minimum Number of Steps to Make Two Strings Anagram/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minSteps(String s, String t) { 3 | int length = s.length(); 4 | int[] counts = new int[26]; 5 | for (int i = 0; i < length; i++) { 6 | char c1 = s.charAt(i), c2 = t.charAt(i); 7 | counts[c1 - 'a']++; 8 | counts[c2 - 'a']--; 9 | } 10 | int steps = 0; 11 | for (int i = 0; i < 26; i++) { 12 | if (counts[i] > 0) 13 | steps += counts[i]; 14 | } 15 | return steps; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0287. Find the Duplicate Number/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findDuplicate(int[] nums) { 3 | int length = nums.length; 4 | int low = 1, high = length - 1; 5 | while (low < high) { 6 | int mid = (high - low) / 2 + low; 7 | int count = 0; 8 | for (int num : nums) { 9 | if (num <= mid) 10 | count++; 11 | } 12 | if (count <= mid) 13 | low = mid + 1; 14 | else 15 | high = mid; 16 | } 17 | return low; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0389. Find the Difference/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public char findTheDifference(String s, String t) { 3 | int[] sCount = new int[26]; 4 | int[] tCount = new int[26]; 5 | char[] sArray = s.toCharArray(); 6 | char[] tArray = t.toCharArray(); 7 | for (char c : sArray) 8 | sCount[c - 'a']++; 9 | for (char c : tArray) 10 | tCount[c - 'a']++; 11 | for (int i = 0; i < 26; i++) { 12 | if (tCount[i] > sCount[i]) 13 | return (char) (i + 'a'); 14 | } 15 | return '0'; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1230. Toss Strange Coins/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double probabilityOfHeads(double[] prob, int target) { 3 | int length = prob.length; 4 | double[][] dp = new double[length + 1][target + 1]; 5 | dp[0][0] = 1; 6 | for (int i = 1; i <= length; i++) { 7 | double curProb = prob[i - 1]; 8 | dp[i][0] = dp[i - 1][0] * (1 - curProb); 9 | for (int j = 1; j <= target; j++) 10 | dp[i][j] = dp[i - 1][j - 1] * curProb + dp[i - 1][j] * (1 - curProb); 11 | } 12 | return dp[length][target]; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1259. Handshakes That Don't Cross/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfWays(int num_people) { 3 | int numPairs = num_people / 2; 4 | if (numPairs == 0 || numPairs == 1) 5 | return 1; 6 | final int MODULO = 1000000007; 7 | long[] dp = new long[numPairs + 1]; 8 | dp[0] = 1; 9 | dp[1] = 1; 10 | for (int i = 2; i <= numPairs; i++) { 11 | for (int j = 0; j < i; j++) 12 | dp[i] = (dp[i] + dp[j] * dp[i - j - 1]) % MODULO; 13 | } 14 | return (int) (dp[numPairs] % MODULO); 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1605. Find Valid Matrix Given Row and Column Sums/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] restoreMatrix(int[] rowSum, int[] colSum) { 3 | int rows = rowSum.length, columns = colSum.length; 4 | int[][] matrix = new int[rows][columns]; 5 | for (int i = 0; i < rows; i++) { 6 | for (int j = 0; j < columns; j++) { 7 | int num = Math.min(rowSum[i], colSum[j]); 8 | matrix[i][j] = num; 9 | rowSum[i] -= num; 10 | colSum[j] -= num; 11 | } 12 | } 13 | return matrix; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1679. Max Number of K-Sum Pairs/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxOperations(int[] nums, int k) { 3 | int operations = 0; 4 | Arrays.sort(nums); 5 | int low = 0, high = nums.length - 1; 6 | while (low < high) { 7 | int sum = nums[low] + nums[high]; 8 | if (sum == k) { 9 | operations++; 10 | low++; 11 | high--; 12 | } else if (sum < k) 13 | low++; 14 | else 15 | high--; 16 | } 17 | return operations; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1725. Number Of Rectangles That Can Form The Largest Square/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countGoodRectangles(int[][] rectangles) { 3 | int width = 0; 4 | for (int[] rectangle : rectangles) { 5 | int shorter = Math.min(rectangle[0], rectangle[1]); 6 | width = Math.max(width, shorter); 7 | } 8 | int count = 0; 9 | for (int[] rectangle : rectangles) { 10 | int shorter = Math.min(rectangle[0], rectangle[1]); 11 | count += shorter == width ? 1 : 0; 12 | } 13 | return count; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1785. Minimum Elements to Add to Form a Given Sum/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minElements(int[] nums, int limit, int goal) { 3 | long sum = 0; 4 | for (int num : nums) 5 | sum += (long) num; 6 | long difference = (long) goal - sum; 7 | if (difference == 0) 8 | return 0; 9 | else { 10 | difference = Math.abs(difference); 11 | long minAdd = difference / limit; 12 | if (difference % limit != 0) 13 | minAdd++; 14 | return (int) minAdd; 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0088. Merge Sorted Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public void merge(int[] nums1, int m, int[] nums2, int n) { 3 | int index1 = m - 1, index2 = n - 1, index = m + n - 1; 4 | while (index1 >= 0 && index2 >= 0) { 5 | if (nums2[index2] >= nums1[index1]) 6 | nums1[index--] = nums2[index2--]; 7 | else 8 | nums1[index--] = nums1[index1--]; 9 | } 10 | while (index1 >= 0) 11 | nums1[index--] = nums1[index1--]; 12 | while (index2 >= 0) 13 | nums1[index--] = nums2[index2--]; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0112. Path Sum/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 | class Solution { 11 | public boolean hasPathSum(TreeNode root, int sum) { 12 | if (root == null) 13 | return false; 14 | if (root.left == null && root.right == null) 15 | return root.val == sum; 16 | int remain = sum - root.val; 17 | return hasPathSum(root.left, remain) || hasPathSum(root.right, remain); 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0371. Sum of Two Integers/0371. Sum of Two Integers.md: -------------------------------------------------------------------------------- 1 | # 371. Sum of Two Integers 2 | ## Level 3 | Easy 4 | 5 | ## Description 6 | Calculate the sum of two integers *a* and *b*, but you are **not allowed** to use the operator `+` and `-`. 7 | 8 | **Example 1:** 9 | 10 | **Input:** a = 1, b = 2 11 | 12 | **Output:** 3 13 | 14 | **Example 2:** 15 | 16 | **Input:** a = -2, b = 3 17 | 18 | **Output:** 1 19 | 20 | ## Solution 21 | Use bitwise operations. The operation `a + b` is equivalent to `(a ^ b) + ((a & b) << 1)`. Set `a` to `(a & b) << 1` and set `b` to `a ^ b` until `a` becomes 0, and return `b`. 22 | -------------------------------------------------------------------------------- /Algorithms/0001-1000/0396. Rotate Function/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public int maxRotateFunction(int[] A) { 3 | int length = A.length; 4 | if (length <= 1) 5 | return 0; 6 | int sum = 0, function = 0; 7 | for (int i = 0; i < length; i++) { 8 | sum += A[i]; 9 | function += i * A[i]; 10 | } 11 | int max = function; 12 | for (int i = 1; i < length; i++) { 13 | function = function + sum - length * A[length - i]; 14 | max = Math.max(max, function); 15 | } 16 | return max; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0876. Middle of the Linked List/Solution.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Definition for singly-linked list. 3 | * public class ListNode { 4 | * int val; 5 | * ListNode next; 6 | * ListNode(int x) { val = x; } 7 | * } 8 | */ 9 | class Solution { 10 | public ListNode middleNode(ListNode head) { 11 | if (head == null || head.next == null) 12 | return head; 13 | ListNode slow = head, fast = head; 14 | while (fast != null && fast.next != null) { 15 | slow = slow.next; 16 | fast = fast.next.next; 17 | } 18 | return slow; 19 | } 20 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0877. Stone Game/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean stoneGame(int[] piles) { 3 | if (piles == null || piles.length == 0) 4 | return false; 5 | int length = piles.length; 6 | int[][] dp = new int[length][length]; 7 | for (int i = 0; i < length; i++) 8 | dp[i][i] = piles[i]; 9 | for (int i = length - 2; i >= 0; i--) { 10 | for (int j = i + 1; j < length; j++) 11 | dp[i][j] = Math.max(piles[i] - dp[i + 1][j], piles[j] - dp[i][j - 1]); 12 | } 13 | return dp[0][length - 1] > 0; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0922. Sort Array By Parity II/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] sortArrayByParityII(int[] A) { 3 | int length = A.length; 4 | int[] sorted = new int[length]; 5 | int evenIndex = 0, oddIndex = 1; 6 | for (int i = 0; i < length; i++) { 7 | int num = A[i]; 8 | if (num % 2 == 0) { 9 | sorted[evenIndex] = num; 10 | evenIndex += 2; 11 | } else { 12 | sorted[oddIndex] = num; 13 | oddIndex += 2; 14 | } 15 | } 16 | return sorted; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1189. Maximum Number of Balloons/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxNumberOfBalloons(String text) { 3 | int[] count = new int[26]; 4 | char[] array = text.toCharArray(); 5 | for (char c : array) 6 | count[c - 'a']++; 7 | int max = Integer.MAX_VALUE; 8 | max = Math.min(max, count['b' - 'a']); 9 | max = Math.min(max, count['a' - 'a']); 10 | max = Math.min(max, count['l' - 'a'] / 2); 11 | max = Math.min(max, count['o' - 'a'] / 2); 12 | max = Math.min(max, count['n' - 'a']); 13 | return max; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1196. How Many Apples Can You Put into the Basket/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxNumberOfApples(int[] arr) { 3 | Arrays.sort(arr); 4 | int length = arr.length; 5 | final int CAPACITY = 5000; 6 | int totalWeight = 0; 7 | int count = 0; 8 | for (int i = 0; i < length; i++) { 9 | int weight = arr[i]; 10 | if (totalWeight + weight <= CAPACITY) { 11 | totalWeight += weight; 12 | count++; 13 | } else 14 | break; 15 | } 16 | return count; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0093. Restore IP Addresses/0093. Restore IP Addresses.md: -------------------------------------------------------------------------------- 1 | # 93. Restore IP Addresses 2 | ## Level 3 | Medium 4 | 5 | ## Description 6 | Given a string containing only digits, restore it by returning all possible valid IP address combinations. 7 | 8 | **Example:** 9 | 10 | **Input:** "25525511135" 11 | 12 | **Output:** ["255.255.11.135", "255.255.111.35"] 13 | 14 | ## Solution 15 | Since an IP address consists of four numbers, try all possible ways to split `s` into four parts. If all four parts are integers in the range `[0, 255]`, then it is a valid IP address combination, and add the IP to the result list. 16 | -------------------------------------------------------------------------------- /Algorithms/0001-1000/0255. Verify Preorder Sequence in Binary Search Tree/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean verifyPreorder(int[] preorder) { 3 | int index = -1; 4 | int min = Integer.MIN_VALUE; 5 | int length = preorder.length; 6 | for (int i = 0; i < length; i++) { 7 | int element = preorder[i]; 8 | if (element < min) 9 | return false; 10 | while (index >= 0 && element > preorder[index]) 11 | min = preorder[index--]; 12 | preorder[++index] = element; 13 | } 14 | return true; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0494. Target Sum/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findTargetSumWays(int[] nums, int S) { 3 | int sum = 0; 4 | for (int num : nums) 5 | sum += num; 6 | int difference = sum - S; 7 | if (difference < 0 || difference % 2 != 0) 8 | return 0; 9 | int maxValue = difference / 2; 10 | int[] dp = new int[maxValue + 1]; 11 | dp[0] = 1; 12 | for (int num : nums) { 13 | for (int i = maxValue; i >= num; i--) 14 | dp[i] += dp[i - num]; 15 | } 16 | return dp[maxValue]; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1167. Minimum Cost to Connect Sticks/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int connectSticks(int[] sticks) { 3 | PriorityQueue priorityQueue = new PriorityQueue(); 4 | for (int stick : sticks) 5 | priorityQueue.offer(stick); 6 | int cost = 0; 7 | while (priorityQueue.size() > 1) { 8 | int stick1 = priorityQueue.poll(); 9 | int stick2 = priorityQueue.poll(); 10 | int sum = stick1 + stick2; 11 | cost += sum; 12 | priorityQueue.offer(sum); 13 | } 14 | return cost; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1256. Encode Number/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String encode(int num) { 3 | num++; 4 | int bits = (int) (Math.log(num) / Math.log(2)); 5 | int difference = num - (int) Math.pow(2, bits); 6 | String encodeStr = binary(difference, bits); 7 | return encodeStr; 8 | } 9 | 10 | public String binary(int num, int bits) { 11 | String str = ""; 12 | for (int i = 0; i < bits; i++) { 13 | int remainder = num % 2; 14 | str = remainder + str; 15 | num /= 2; 16 | } 17 | return str; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1328. Break a Palindrome/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String breakPalindrome(String palindrome) { 3 | char[] array = palindrome.toCharArray(); 4 | int length = array.length; 5 | if (length == 1) 6 | return ""; 7 | int halfMax = length / 2; 8 | for (int i = 0; i < halfMax; i++) { 9 | char c = array[i]; 10 | if (c != 'a') { 11 | array[i] = 'a'; 12 | return new String(array); 13 | } 14 | } 15 | array[length - 1] = 'b'; 16 | return new String(array); 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1899. Merge Triplets to Form Target Triplet/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean mergeTriplets(int[][] triplets, int[] target) { 3 | int[] merged = new int[3]; 4 | for (int[] triplet : triplets) { 5 | if (triplet[0] <= target[0] && triplet[1] <= target[1] && triplet[2] <= target[2]) { 6 | merged[0] = Math.max(merged[0], triplet[0]); 7 | merged[1] = Math.max(merged[1], triplet[1]); 8 | merged[2] = Math.max(merged[2], triplet[2]); 9 | } 10 | } 11 | return Arrays.equals(merged, target); 12 | } 13 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1228. Missing Number In Arithmetic Progression/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int missingNumber(int[] arr) { 3 | int length = arr.length; 4 | int prevDif = arr[1] - arr[0]; 5 | for (int i = 2; i < length; i++) { 6 | int curDif = arr[i] - arr[i - 1]; 7 | if (Math.abs(curDif) < Math.abs(prevDif)) 8 | return arr[i - 1] - curDif; 9 | else if (Math.abs(curDif) > Math.abs(prevDif)) 10 | return arr[i - 1] + prevDif; 11 | prevDif = curDif; 12 | } 13 | return arr[length - 1] + prevDif; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1662. Check If Two String Arrays are Equivalent/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean arrayStringsAreEqual(String[] word1, String[] word2) { 3 | StringBuffer sb1 = new StringBuffer(); 4 | StringBuffer sb2 = new StringBuffer(); 5 | int length1 = word1.length, length2 = word2.length; 6 | for (int i = 0; i < length1; i++) 7 | sb1.append(word1[i]); 8 | for (int i = 0; i < length2; i++) 9 | sb2.append(word2[i]); 10 | String str1 = sb1.toString(); 11 | String str2 = sb2.toString(); 12 | return str1.equals(str2); 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1855. Maximum Distance Between a Pair of Values/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxDistance(int[] nums1, int[] nums2) { 3 | int maxDistance = 0; 4 | int length1 = nums1.length, length2 = nums2.length; 5 | int index1 = length1 - 1, index2 = length2 - 1; 6 | while (index1 >= 0 && index2 >= 0) { 7 | while (index1 >= 0 && nums1[index1] <= nums2[index2]) { 8 | maxDistance = Math.max(maxDistance, index2 - index1); 9 | index1--; 10 | } 11 | index2--; 12 | } 13 | return maxDistance; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1866. Number of Ways to Rearrange Sticks With K Sticks Visible/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int rearrangeSticks(int n, int k) { 3 | final int MODULO = 1000000007; 4 | int[][] dp = new int[n + 1][n + 1]; 5 | for (int i = 1; i <= n; i++) 6 | dp[i][i] = 1; 7 | for (int i = 2; i <= n; i++) { 8 | dp[i][1] = (int) ((long) dp[i - 1][1] * (i - 1) % MODULO); 9 | for (int j = 2; j <= i; j++) 10 | dp[i][j] = (int) ((dp[i - 1][j - 1] + (long) dp[i - 1][j] * (i - 1)) % MODULO); 11 | } 12 | return dp[n][k]; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0266. Palindrome Permutation/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canPermutePalindrome(String s) { 3 | int[] count = new int[128]; 4 | char[] array = s.toCharArray(); 5 | for (char c : array) 6 | count[c]++; 7 | boolean hasOdd = false; 8 | for (int i = 0; i < 128; i++) { 9 | int curCount = count[i]; 10 | if (curCount % 2 != 0) { 11 | if (hasOdd) 12 | return false; 13 | else 14 | hasOdd = true; 15 | } 16 | } 17 | return true; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0365. Water and Jug Problem/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canMeasureWater(int x, int y, int z) { 3 | if (x + y < z) 4 | return false; 5 | int gcd = gcd(x, y); 6 | return z % gcd == 0; 7 | } 8 | 9 | public int gcd(int x, int y) { 10 | if (x == 0 && y == 0) 11 | return 1; 12 | while (x != 0 && y != 0) { 13 | if (x > y) { 14 | int temp = x; 15 | x = y; 16 | y = temp; 17 | } 18 | y %= x; 19 | } 20 | return x == 0 ? y : x; 21 | } 22 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0486. Predict the Winner/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | public boolean PredictTheWinner(int[] nums) { 3 | if (nums == null || nums.length == 0) 4 | return false; 5 | int length = nums.length; 6 | int[][] dp = new int[length][length]; 7 | for (int i = 0; i < length; i++) 8 | dp[i][i] = nums[i]; 9 | for (int i = length - 2; i >= 0; i--) { 10 | for (int j = i + 1; j < length; j++) 11 | dp[i][j] = Math.max(nums[i] - dp[i + 1][j], nums[j] - dp[i][j - 1]); 12 | } 13 | return dp[0][length - 1] >= 0; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1060. Missing Element in Sorted Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int missingElement(int[] nums, int k) { 3 | if (nums == null || nums.length == 0) 4 | return k; 5 | int length = nums.length; 6 | if (k >= nums[length - 1]) 7 | return k + nums[0] + length - 1; 8 | int num = nums[0]; 9 | int index = 1; 10 | while (index < length && k > 0) { 11 | num++; 12 | if (num == nums[index]) 13 | index++; 14 | else 15 | k--; 16 | } 17 | return num + k; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0242. Valid Anagram/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isAnagram(String s, String t) { 3 | if (s == null || t == null || s.length() != t.length()) 4 | return false; 5 | int[] count = new int[26]; 6 | char[] sArray = s.toCharArray(); 7 | char[] tArray = t.toCharArray(); 8 | for (char c : sArray) 9 | count[c - 'a']++; 10 | for (char c : tArray) 11 | count[c - 'a']--; 12 | for (int i = 0; i < 26; i++) { 13 | if (count[i] != 0) 14 | return false; 15 | } 16 | return true; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0537. Complex Number Multiplication/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String complexNumberMultiply(String a, String b) { 3 | int aReal = Integer.parseInt(a.substring(0, a.indexOf('+'))); 4 | int aImag = Integer.parseInt(a.substring(a.indexOf('+') + 1, a.indexOf('i'))); 5 | int bReal = Integer.parseInt(b.substring(0, b.indexOf('+'))); 6 | int bImag = Integer.parseInt(b.substring(b.indexOf('+') + 1, b.indexOf('i'))); 7 | int mulReal = aReal * bReal - aImag * bImag; 8 | int mulImag = aReal * bImag + bReal * aImag; 9 | return mulReal + "+" + mulImag + "i"; 10 | } 11 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0560. Subarray Sum Equals K/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subarraySum(int[] nums, int k) { 3 | Map map = new HashMap(); 4 | map.put(0, 1); 5 | int count = 0; 6 | int sum = 0; 7 | int length = nums.length; 8 | for (int i = 0; i < length; i++) { 9 | sum += nums[i]; 10 | int curCount = map.getOrDefault(sum - k, 0); 11 | count += curCount; 12 | int sumCount = map.getOrDefault(sum, 0); 13 | map.put(sum, sumCount + 1); 14 | } 15 | return count; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0789. Escape The Ghosts/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean escapeGhosts(int[][] ghosts, int[] target) { 3 | int[] source = {0, 0}; 4 | int distance = manhattanDistance(source, target); 5 | for (int[] ghost : ghosts) { 6 | int ghostDistance = manhattanDistance(ghost, target); 7 | if (ghostDistance <= distance) 8 | return false; 9 | } 10 | return true; 11 | } 12 | 13 | public int manhattanDistance(int[] source, int[] target) { 14 | return Math.abs(source[0] - target[0]) + Math.abs(source[1] - target[1]); 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1013. Partition Array Into Three Parts With Equal Sum/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canThreePartsEqualSum(int[] A) { 3 | int sum = 0; 4 | for (int num : A) 5 | sum += num; 6 | if (sum % 3 != 0) 7 | return false; 8 | int partSum = sum / 3; 9 | int count = 0; 10 | int curSum = 0; 11 | int length = A.length; 12 | for (int i = 0; i < length; i++) { 13 | curSum += A[i]; 14 | if (curSum == partSum * (count + 1)) 15 | count++; 16 | } 17 | return count >= 3; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1455. Check If a Word Occurs As a Prefix of Any Word in a Sentence/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int isPrefixOfWord(String sentence, String searchWord) { 3 | if (sentence == null || searchWord == null || searchWord.length() == 0 || sentence.length() < searchWord.length()) 4 | return -1; 5 | String[] array = sentence.split(" "); 6 | int length = array.length; 7 | for (int i = 0; i < length; i++) { 8 | String word = array[i]; 9 | if (word.indexOf(searchWord) == 0) 10 | return i + 1; 11 | } 12 | return -1; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1539. Kth Missing Positive Number/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findKthPositive(int[] arr, int k) { 3 | List list = new ArrayList(); 4 | int length = arr.length; 5 | int max = arr[length - 1]; 6 | int index = 0; 7 | for (int i = 1; i < max; i++) { 8 | if (arr[index] == i) 9 | index++; 10 | else 11 | list.add(i); 12 | } 13 | int size = list.size(); 14 | if (size >= k) 15 | return list.get(k - 1); 16 | else 17 | return max + k - size; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1991. Find the Middle Index in Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findMiddleIndex(int[] nums) { 3 | int length = nums.length; 4 | int[] leftSum = new int[length]; 5 | for (int i = 1; i < length; i++) 6 | leftSum[i] = leftSum[i - 1] + nums[i - 1]; 7 | int[] rightSum = new int[length]; 8 | for (int i = length - 2; i >= 0; i--) 9 | rightSum[i] = rightSum[i + 1] + nums[i + 1]; 10 | for (int i = 0; i < length; i++) { 11 | if (leftSum[i] == rightSum[i]) 12 | return i; 13 | } 14 | return -1; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0921. Minimum Add to Make Parentheses Valid/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minAddToMakeValid(String S) { 3 | int addCount = 0; 4 | int curCount = 0; 5 | int length = S.length(); 6 | for (int i = 0; i < length; i++) { 7 | char c = S.charAt(i); 8 | if (c == '(') 9 | curCount++; 10 | else { 11 | if (curCount > 0) 12 | curCount--; 13 | else 14 | addCount++; 15 | } 16 | } 17 | addCount += curCount; 18 | return addCount; 19 | } 20 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1968. Array With Elements Not Equal to Average of Neighbors/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] rearrangeArray(int[] nums) { 3 | Arrays.sort(nums); 4 | int length = nums.length; 5 | int[] rearrange = new int[length]; 6 | int left = 0, right = length - 1; 7 | for (int i = 0; i < length; i++) { 8 | if (i % 2 == 0) { 9 | rearrange[i] = nums[left]; 10 | left++; 11 | } else { 12 | rearrange[i] = nums[right]; 13 | right--; 14 | } 15 | } 16 | return rearrange; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0151. Reverse Words in a String/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String reverseWords(String s) { 3 | s = s.trim(); 4 | while (s.indexOf(" ") >= 0) 5 | s = s.replaceAll(" ", " "); 6 | String[] array = s.split(" "); 7 | int length = array.length; 8 | StringBuffer reverseSB = new StringBuffer(); 9 | for (int i = length - 1; i >= 0; i--) { 10 | reverseSB.append(" "); 11 | reverseSB.append(array[i]); 12 | } 13 | reverseSB.deleteCharAt(0); 14 | String reverse = reverseSB.toString(); 15 | return reverse; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0386. Lexicographical Numbers/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List lexicalOrder(int n) { 3 | List lexicalOrderList = new ArrayList(); 4 | int num = 1; 5 | for (int i = 1; i <= n; i++) { 6 | lexicalOrderList.add(num); 7 | if (num * 10 <= n) 8 | num *= 10; 9 | else { 10 | if (num >= n) 11 | num /= 10; 12 | num++; 13 | while (num % 10 == 0) 14 | num /= 10; 15 | } 16 | } 17 | return lexicalOrderList; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0398. Random Pick Index/Solution.java: -------------------------------------------------------------------------------- 1 | public class Solution { 2 | private int[] nums; 3 | private int length; 4 | 5 | public Solution(int[] nums) { 6 | this.nums = nums; 7 | length = nums.length; 8 | } 9 | 10 | public int pick(int target) { 11 | int index = 0; 12 | do { 13 | index = (int) (Math.random() * length); 14 | } while (nums[index] != target); 15 | return index; 16 | } 17 | } 18 | 19 | /** 20 | * Your Solution object will be instantiated and called as such: 21 | * Solution obj = new Solution(nums); 22 | * int param_1 = obj.pick(target); 23 | */ -------------------------------------------------------------------------------- /Algorithms/0001-1000/0718. Maximum Length of Repeated Subarray/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findLength(int[] A, int[] B) { 3 | int maxLength = 0; 4 | int length1 = A.length, length2 = B.length; 5 | int[][] dp = new int[length1 + 1][length2 + 1]; 6 | for (int i = length1 - 1; i >= 0; i--) { 7 | for (int j = length2 - 1; j >= 0; j--) { 8 | if (A[i] == B[j]) { 9 | dp[i][j] = dp[i + 1][j + 1] + 1; 10 | maxLength = Math.max(maxLength, dp[i][j]); 11 | } 12 | } 13 | } 14 | return maxLength; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0946. Validate Stack Sequences/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean validateStackSequences(int[] pushed, int[] popped) { 3 | Stack stack = new Stack(); 4 | int length = pushed.length; 5 | int pushIndex = 0, popIndex = 0; 6 | while (pushIndex < length && popIndex < length) { 7 | stack.push(pushed[pushIndex]); 8 | pushIndex++; 9 | while (!stack.isEmpty() && stack.peek() == popped[popIndex]) { 10 | stack.pop(); 11 | popIndex++; 12 | } 13 | } 14 | return stack.isEmpty(); 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1004. Max Consecutive Ones III/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestOnes(int[] A, int K) { 3 | int max = 0; 4 | int start = 0; 5 | int zerosCount = 0; 6 | int length = A.length; 7 | for (int i = 0; i < length; i++) { 8 | int num = A[i]; 9 | if (num == 0) 10 | zerosCount++; 11 | while (zerosCount > K) { 12 | if (A[start] == 0) 13 | zerosCount--; 14 | start++; 15 | } 16 | max = Math.max(max, i - start + 1); 17 | } 18 | return max; 19 | } 20 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1104. Path In Zigzag Labelled Binary Tree/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List pathInZigZagTree(int label) { 3 | Stack stack = new Stack(); 4 | while (label >= 1) { 5 | stack.push(label); 6 | int log = (int) (Math.log(label) / Math.log(2)); 7 | int minValue = (int) Math.pow(2, log); 8 | label = minValue * 3 - 1 - label; 9 | label /= 2; 10 | } 11 | List path = new ArrayList(); 12 | while (!stack.isEmpty()) 13 | path.add(stack.pop()); 14 | return path; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1111. Maximum Nesting Depth of Two Valid Parentheses Strings/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] maxDepthAfterSplit(String seq) { 3 | int length = seq.length(); 4 | int[] depths = new int[length]; 5 | int curDepth = -1; 6 | for (int i = 0; i < length; i++) { 7 | if (seq.charAt(i) == '(') 8 | depths[i] = ++curDepth; 9 | else 10 | depths[i] = curDepth--; 11 | } 12 | int[] split = new int[length]; 13 | for (int i = 0; i < length; i++) 14 | split[i] = depths[i] % 2; 15 | return split; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1394. Find Lucky Integer in an Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findLucky(int[] arr) { 3 | Map map = new HashMap(); 4 | for (int num : arr) { 5 | int count = map.getOrDefault(num, 0) + 1; 6 | map.put(num, count); 7 | } 8 | int luckyNumber = -1; 9 | Set keySet = map.keySet(); 10 | for (int num : keySet) { 11 | int count = map.get(num); 12 | if (count == num) 13 | luckyNumber = Math.max(luckyNumber, num); 14 | } 15 | return luckyNumber; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1962. Remove Stones to Minimize the Total/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minStoneSum(int[] piles, int k) { 3 | PriorityQueue priorityQueue = new PriorityQueue((a, b) -> (b - a)); 4 | for (int pile : piles) 5 | priorityQueue.offer(pile); 6 | for (int i = 0; i < k; i++) { 7 | int pile = priorityQueue.poll(); 8 | pile = pile - pile / 2; 9 | priorityQueue.offer(pile); 10 | } 11 | int sum = 0; 12 | while (!priorityQueue.isEmpty()) 13 | sum += priorityQueue.poll(); 14 | return sum; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0515. Find Largest Value in Each Tree Row/0515. Find Largest Value in Each Tree Row.md: -------------------------------------------------------------------------------- 1 | # 515. Find Largest Value in Each Tree Row 2 | ## Level 3 | Medium 4 | 5 | ## Description 6 | You need to find the largest value in each row of a binary tree. 7 | 8 | **Example:** 9 | ``` 10 | Input: 11 | 12 | 1 13 | / \ 14 | 3 2 15 | / \ \ 16 | 5 3 9 17 | 18 | Output: [1, 3, 9] 19 | ``` 20 | 21 | ## Solution 22 | Use breadth first search. Each time all nodes in the same row are visited, and the largest value in the row can be obtained, which is added to the result list. Finally, return the result list. 23 | -------------------------------------------------------------------------------- /Algorithms/0001-1000/0780. Reaching Points/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean reachingPoints(int sx, int sy, int tx, int ty) { 3 | while (tx >= sx && ty >= sy) { 4 | if (tx > ty) { 5 | if (ty > sy) 6 | tx %= ty; 7 | else 8 | return (tx - sx) % ty == 0; 9 | } else if (tx < ty) { 10 | if (tx > sx) 11 | ty %= tx; 12 | else 13 | return (ty - sy) % tx == 0; 14 | } else 15 | break; 16 | } 17 | return tx == sx && ty == sy; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1475. Final Prices With a Special Discount in a Shop/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] finalPrices(int[] prices) { 3 | int length = prices.length; 4 | int[] finalPrices = new int[length]; 5 | for (int i = 0; i < length; i++) 6 | finalPrices[i] = prices[i]; 7 | for (int i = 0; i < length; i++) { 8 | for (int j = i + 1; j < length; j++) { 9 | if (prices[j] <= prices[i]) { 10 | finalPrices[i] -= prices[j]; 11 | break; 12 | } 13 | } 14 | } 15 | return finalPrices; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1749. Maximum Absolute Sum of Any Subarray/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxAbsoluteSum(int[] nums) { 3 | int sumMax = nums[0], sumMin = nums[0]; 4 | int max = Math.max(sumMax, 0), min = Math.min(sumMin, 0); 5 | int length = nums.length; 6 | for (int i = 1; i < length; i++) { 7 | int num = nums[i]; 8 | sumMax = Math.max(sumMax + num, num); 9 | sumMin = Math.min(sumMin + num, num); 10 | max = Math.max(max, sumMax); 11 | min = Math.min(min, sumMin); 12 | } 13 | return Math.max(Math.abs(max), Math.abs(min)); 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0257. Binary Tree Paths/0257. Binary Tree Paths.md: -------------------------------------------------------------------------------- 1 | # 257. Binary Tree Paths 2 | ## Level 3 | Easy 4 | 5 | ## Description 6 | Given a binary tree, return all root-to-leaf paths. 7 | 8 | **Note:** A leaf is a node with no children. 9 | 10 | **Example:** 11 | ``` 12 | Input: 13 | 14 | 1 15 | / \ 16 | 2 3 17 | \ 18 | 5 19 | 20 | Output: ["1->2->5", "1->3"] 21 | 22 | Explanation: All root-to-leaf paths are: 1->2->5, 1->3 23 | ``` 24 | 25 | ## Solution 26 | Use breadth first search. When each node is visited, store the corresponding path from the root to the current node. If a leaf is met, add the root-to-leaf path to the result list. 27 | -------------------------------------------------------------------------------- /Algorithms/0001-1000/0383. Ransom Note/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canConstruct(String ransomNote, String magazine) { 3 | int[] count1 = new int[26]; 4 | int[] count2 = new int[26]; 5 | char[] ransomNoteArray = ransomNote.toCharArray(); 6 | char[] magazineArray = magazine.toCharArray(); 7 | for (char c : ransomNoteArray) 8 | count1[c - 'a']++; 9 | for (char c : magazineArray) 10 | count2[c - 'a']++; 11 | for (int i = 0; i < 26; i++) { 12 | if (count1[i] > count2[i]) 13 | return false; 14 | } 15 | return true; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0775. Global and Local Inversions/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isIdealPermutation(int[] A) { 3 | if (A == null) 4 | return false; 5 | if (A.length <= 2) 6 | return true; 7 | int length = A.length; 8 | if (A[0] > 1) 9 | return false; 10 | for (int i = 1; i < length; i++) { 11 | int prev = A[i - 1], cur = A[i]; 12 | if (cur != i && cur != i - 1 && cur != i + 1) 13 | return false; 14 | if (cur == i - 1 && prev != i) 15 | return false; 16 | } 17 | return true; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1047. Remove All Adjacent Duplicates In String/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String removeDuplicates(String S) { 3 | Stack stack = new Stack(); 4 | int length = S.length(); 5 | for (int i = length - 1; i >= 0; i--) { 6 | char c = S.charAt(i); 7 | if (stack.isEmpty() || stack.peek() != c) 8 | stack.push(c); 9 | else 10 | stack.pop(); 11 | } 12 | StringBuffer sb = new StringBuffer(); 13 | while (!stack.isEmpty()) 14 | sb.append(stack.pop()); 15 | return sb.toString(); 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1403. Minimum Subsequence in Non-Increasing Order/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List minSubsequence(int[] nums) { 3 | int sum = 0; 4 | for (int num : nums) 5 | sum += num; 6 | int halfSum = sum / 2; 7 | Arrays.sort(nums); 8 | List list = new ArrayList(); 9 | int subsum = 0; 10 | for (int i = nums.length - 1; i >= 0; i--) { 11 | int num = nums[i]; 12 | list.add(num); 13 | subsum += num; 14 | if (subsum > halfSum) 15 | break; 16 | } 17 | return list; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1427. Perform String Shifts/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String stringShift(String s, int[][] shift) { 3 | int totalShift = 0; 4 | for (int[] curShift : shift) { 5 | int direction = curShift[0], amount = curShift[1]; 6 | if (direction == 1) 7 | amount = -amount; 8 | totalShift += amount; 9 | } 10 | int length = s.length(); 11 | totalShift %= length; 12 | if (totalShift < 0) 13 | totalShift += length; 14 | String shifted = s.substring(totalShift) + s.substring(0, totalShift); 15 | return shifted; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1441. Build an Array With Stack Operations/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List buildArray(int[] target, int n) { 3 | List operations = new ArrayList(); 4 | int length = target.length; 5 | int curNum = 1; 6 | for (int i = 0; i < length; i++) { 7 | int num = target[i]; 8 | while (num != curNum) { 9 | operations.add("Push"); 10 | operations.add("Pop"); 11 | curNum++; 12 | } 13 | operations.add("Push"); 14 | curNum++; 15 | } 16 | return operations; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1624. Largest Substring Between Two Equal Characters/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxLengthBetweenEqualCharacters(String s) { 3 | int maxLength = -1; 4 | Map map = new HashMap(); 5 | int length = s.length(); 6 | for (int i = 0; i < length; i++) { 7 | char c = s.charAt(i); 8 | if (map.containsKey(c)) { 9 | int prevIndex = map.get(c); 10 | maxLength = Math.max(maxLength, i - prevIndex - 1); 11 | } else 12 | map.put(c, i); 13 | } 14 | return maxLength; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1663. Smallest String With A Given Numeric Value/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String getSmallestString(int n, int k) { 3 | int[] array = new int[n]; 4 | for (int i = 0; i < n; i++) { 5 | int remainLength = n - i - 1; 6 | int remainSum = 26 * remainLength; 7 | array[i] = Math.max(1, k - remainSum); 8 | k -= array[i]; 9 | } 10 | StringBuffer sb = new StringBuffer(); 11 | for (int i = 0; i < n; i++) { 12 | char c = (char) ('a' + array[i] - 1); 13 | sb.append(c); 14 | } 15 | return sb.toString(); 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1891. Cutting Ribbons/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxLength(int[] ribbons, int k) { 3 | int maxRibbon = 0; 4 | for (int ribbon : ribbons) 5 | maxRibbon = Math.max(maxRibbon, ribbon); 6 | int low = 0, high = maxRibbon; 7 | while (low < high) { 8 | int mid = (high - low + 1) / 2 + low; 9 | int count = 0; 10 | for (int ribbon : ribbons) 11 | count += ribbon / mid; 12 | if (count >= k) 13 | low = mid; 14 | else 15 | high = mid - 1; 16 | } 17 | return low; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1897. Redistribute Characters to Make All Strings Equal/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean makeEqual(String[] words) { 3 | int[] counts = new int[26]; 4 | for (String word : words) { 5 | int wordLength = word.length(); 6 | for (int i = 0; i < wordLength; i++) { 7 | char c = word.charAt(i); 8 | counts[c - 'a']++; 9 | } 10 | } 11 | int length = words.length; 12 | for (int i = 0; i < 26; i++) { 13 | if (counts[i] % length != 0) 14 | return false; 15 | } 16 | return true; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0169. Majority Element/0169. Majority Element.md: -------------------------------------------------------------------------------- 1 | # 169. Majority Element 2 | ## Level 3 | Easy 4 | 5 | ## Description 6 | Given an array of size *n*, find the majority element. The majority element is the element that appears **more than** `⌊ n/2 ⌋` times. 7 | 8 | You may assume that the array is non-empty and the majority element always exist in the array. 9 | 10 | **Example 1:** 11 | 12 | **Input:** [3,2,3] 13 | 14 | **Output:** 3 15 | 16 | **Example 2:** 17 | 18 | **Input:** [2,2,1,1,1,2,2] 19 | 20 | **Output:** 2 21 | 22 | ## Solution 23 | Sort the array. The element at the middle index must be the majority element, so return the element. 24 | -------------------------------------------------------------------------------- /Algorithms/0001-1000/0334. Increasing Triplet Subsequence/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean increasingTriplet(int[] nums) { 3 | if (nums == null || nums.length < 3) 4 | return false; 5 | int firstNum = Integer.MAX_VALUE, secondNum = Integer.MAX_VALUE; 6 | int length = nums.length; 7 | for (int i = 0; i < length; i++) { 8 | int num = nums[i]; 9 | if (num > secondNum) 10 | return true; 11 | else if (num > firstNum) 12 | secondNum = num; 13 | else 14 | firstNum = num; 15 | } 16 | return false; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0392. Is Subsequence/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isSubsequence(String s, String t) { 3 | if (s == null || t == null) 4 | return false; 5 | int sLength = s.length(), tLength = t.length(); 6 | if (sLength == 0) 7 | return true; 8 | int sIndex = 0; 9 | for (int i = 0; i < tLength; i++) { 10 | char sChar = s.charAt(sIndex); 11 | char tChar = t.charAt(i); 12 | if (sChar == tChar) 13 | sIndex++; 14 | if (sIndex == sLength) 15 | return true; 16 | } 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0667. Beautiful Arrangement II/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] constructArray(int n, int k) { 3 | int[] sorted = new int[n]; 4 | for (int i = 0; i < n; i++) 5 | sorted[i] = i + 1; 6 | int[] arrangement = new int[n]; 7 | int low = 0, high = k; 8 | for (int i = 0; i <= k; i++) { 9 | if (i % 2 == 0) 10 | arrangement[i] = sorted[low++]; 11 | else 12 | arrangement[i] = sorted[high--]; 13 | } 14 | for (int i = k + 1; i < n; i++) 15 | arrangement[i] = sorted[i]; 16 | return arrangement; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0915. Partition Array into Disjoint Intervals/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int partitionDisjoint(int[] A) { 3 | int length = A.length; 4 | int[] minRight = new int[length]; 5 | minRight[length - 1] = A[length - 1]; 6 | for (int i = length - 2; i >= 0; i--) 7 | minRight[i] = Math.min(minRight[i + 1], A[i]); 8 | int maxLeft = Integer.MIN_VALUE; 9 | for (int i = 0; i < length - 1; i++) { 10 | maxLeft = Math.max(maxLeft, A[i]); 11 | if (maxLeft <= minRight[i + 1]) 12 | return i + 1; 13 | } 14 | return length - 1; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0973. K Closest Points to Origin/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[][] kClosest(int[][] points, int K) { 3 | Arrays.sort(points, new Comparator() { 4 | public int compare(int[] point1, int[] point2) { 5 | return (point1[0] * point1[0] + point1[1] * point1[1]) - (point2[0] * point2[0] + point2[1] * point2[1]); 6 | } 7 | }); 8 | int[][] closestPoints = new int[K][2]; 9 | for (int i = 0; i < K; i++) { 10 | for (int j = 0; j < 2; j++) 11 | closestPoints[i][j] = points[i][j]; 12 | } 13 | return closestPoints; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1043. Partition Array for Maximum Sum/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSumAfterPartitioning(int[] A, int K) { 3 | if (A == null || A.length == 0) 4 | return 0; 5 | int length = A.length; 6 | int[] dp = new int[length + 1]; 7 | for (int i = 1; i <= length; i++) { 8 | int max = 0; 9 | int leftBound = Math.max(i - K, 0); 10 | for (int j = i - 1; j >= leftBound; j--) { 11 | max = Math.max(max, A[j]); 12 | dp[i] = Math.max(dp[i], dp[j] + max * (i - j)); 13 | } 14 | } 15 | return dp[length]; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1071. Greatest Common Divisor of Strings/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String gcdOfStrings(String str1, String str2) { 3 | if (str1 == null || str2 == null) 4 | return null; 5 | while (str1.length() > 0 && str2.length() > 0) { 6 | if (str1.length() > str2.length()) { 7 | String temp = str1; 8 | str1 = str2; 9 | str2 = temp; 10 | } 11 | if (str2.indexOf(str1) < 0) 12 | return ""; 13 | str2 = str2.substring(str1.length()); 14 | } 15 | return str1.length() == 0 ? str2 : str1; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1400. Construct K Palindrome Strings/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canConstruct(String s, int k) { 3 | int length = s.length(); 4 | if (length < k) 5 | return false; 6 | if (length == k) 7 | return true; 8 | int[] counts = new int[26]; 9 | for (int i = 0; i < length; i++) { 10 | char c = s.charAt(i); 11 | counts[c - 'a']++; 12 | } 13 | int oddCount = 0; 14 | for (int i = 0; i < 26; i++) { 15 | if (counts[i] % 2 == 1) 16 | oddCount++; 17 | } 18 | return oddCount <= k; 19 | } 20 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1829. Maximum XOR for Each Query/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] getMaximumXor(int[] nums, int maximumBit) { 3 | int maximum = (1 << maximumBit) - 1; 4 | int n = nums.length; 5 | int[] prefixXors = new int[n]; 6 | prefixXors[0] = nums[0]; 7 | for (int i = 1; i < n; i++) 8 | prefixXors[i] = prefixXors[i - 1] ^ nums[i]; 9 | int[] maximumXors = new int[n]; 10 | for (int i = 0; i < n; i++) { 11 | int prefixXor = prefixXors[n - 1 - i]; 12 | maximumXors[i] = maximum ^ (maximum & prefixXor); 13 | } 14 | return maximumXors; 15 | } 16 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1957. Delete Characters to Make Fancy String/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String makeFancyString(String s) { 3 | StringBuffer sb = new StringBuffer(); 4 | int length = s.length(); 5 | char prev = ' '; 6 | int count = 1; 7 | for (int i = 0; i < length; i++) { 8 | char curr = s.charAt(i); 9 | if (curr == prev) 10 | count++; 11 | else { 12 | count = 1; 13 | prev = curr; 14 | } 15 | if (count < 3) 16 | sb.append(curr); 17 | } 18 | return sb.toString(); 19 | } 20 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0011. Container With Most Water/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxArea(int[] height) { 3 | int maxArea = 0; 4 | int left = 0, right = height.length - 1; 5 | while (left < right) { 6 | int containerWidth = right - left; 7 | int containerHeight = Math.min(height[left], height[right]); 8 | int containerArea = containerWidth * containerHeight; 9 | maxArea = Math.max(maxArea, containerArea); 10 | if (height[left] < height[right]) 11 | left++; 12 | else 13 | right--; 14 | } 15 | return maxArea; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0434. Number of Segments in a String/0434. Number of Segments in a String.md: -------------------------------------------------------------------------------- 1 | # 434. Number of Segments in a String 2 | ## Level 3 | Easy 4 | 5 | ## Description 6 | Count the number of segments in a string, where a segment is defined to be a contiguous sequence of non-space characters. 7 | 8 | Please note that the string does not contain any **non-printable** characters. 9 | 10 | **Example:** 11 | 12 | **Input:** "Hello, my name is John" 13 | 14 | **Output:** 5 15 | 16 | ## Solution 17 | If there are continuous spaces in the string, replace them with a single space. Then split the string using space to form an array, and return the length of the array. 18 | -------------------------------------------------------------------------------- /Algorithms/0001-1000/0022. Generate Parentheses/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List generateParenthesis(int n) { 3 | List generated = new ArrayList(); 4 | if (n == 0) { 5 | generated.add(""); 6 | return generated; 7 | } 8 | for (int i = 0; i < n; i++) { 9 | int j = n - 1 - i; 10 | for (String left: generateParenthesis(i)) { 11 | for (String right: generateParenthesis(j)) 12 | generated.add("(" + left + ")" + right); 13 | } 14 | } 15 | Collections.sort(generated); 16 | return generated; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0026. Remove Duplicates from Sorted Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeDuplicates(int[] nums) { 3 | if (nums == null || nums.length == 0) 4 | return 0; 5 | int length = nums.length; 6 | int index = 1; 7 | int num = nums[0]; 8 | for (int i = 1; i < length; i++) { 9 | int curNum = nums[i]; 10 | if (curNum == num) 11 | continue; 12 | int temp = nums[i]; 13 | nums[i] = nums[index]; 14 | nums[index] = temp; 15 | index++; 16 | num = curNum; 17 | } 18 | return index; 19 | } 20 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0119. Pascal's Triangle II/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List getRow(int rowIndex) { 3 | List row = new ArrayList(); 4 | int midPoint = rowIndex / 2; 5 | for (int i = 0; i <= midPoint; i++) 6 | row.add(combination(rowIndex, i)); 7 | for (int i = midPoint + 1; i <= rowIndex; i++) 8 | row.add(row.get(rowIndex - i)); 9 | return row; 10 | } 11 | 12 | public int combination(int n, int k) { 13 | long result = 1; 14 | for (int i = 1; i <= k; i++) 15 | result = result * (n - k + i) / i; 16 | return (int) result; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0367. Valid Perfect Square/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isPerfectSquare(int num) { 3 | if (num == 1 || num == 4 || num == 9) 4 | return true; 5 | long numLong = (long) num; 6 | int low = 1, high = num / 2; 7 | while (low <= high) { 8 | int mid = (high - low) / 2 + low; 9 | long square = (long) mid * (long) mid; 10 | if (square == numLong) 11 | return true; 12 | else if (square > numLong) 13 | high = mid - 1; 14 | else 15 | low = mid + 1; 16 | } 17 | return false; 18 | } 19 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0724. Find Pivot Index/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int pivotIndex(int[] nums) { 3 | if (nums == null || nums.length == 0) 4 | return -1; 5 | int sum = 0; 6 | for (int num : nums) 7 | sum += num; 8 | int length = nums.length; 9 | int leftSum = 0, rightSum = sum - nums[0]; 10 | if (leftSum == rightSum) 11 | return 0; 12 | for (int i = 1; i < length; i++) { 13 | leftSum += nums[i - 1]; 14 | rightSum -= nums[i]; 15 | if (leftSum == rightSum) 16 | return i; 17 | } 18 | return -1; 19 | } 20 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0806. Number of Lines To Write String/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] numberOfLines(int[] widths, String S) { 3 | final int MAX_WIDTH = 100; 4 | int linesCount = 1, curWidth = 0; 5 | int length = S.length(); 6 | for (int i = 0; i < length; i++) { 7 | char c = S.charAt(i); 8 | int width = widths[c - 'a']; 9 | if (curWidth + width <= MAX_WIDTH) 10 | curWidth += width; 11 | else { 12 | linesCount++; 13 | curWidth = width; 14 | } 15 | } 16 | return new int[]{linesCount, curWidth}; 17 | } 18 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0888. Fair Candy Swap/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] fairCandySwap(int[] A, int[] B) { 3 | int sumA = 0, sumB = 0; 4 | for (int num : A) 5 | sumA += num; 6 | for (int num : B) 7 | sumB += num; 8 | int dif = (sumA - sumB) / 2; 9 | int[] swap = new int[2]; 10 | for (int numA : A) { 11 | for (int numB : B) { 12 | if (numA - numB == dif) { 13 | swap[0] = numA; 14 | swap[1] = numB; 15 | return swap; 16 | } 17 | } 18 | } 19 | return swap; 20 | } 21 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1157. Online Majority Element In Subarray/MajorityChecker.java: -------------------------------------------------------------------------------- 1 | class MajorityChecker { 2 | int[] arr; 3 | 4 | public MajorityChecker(int[] arr) { 5 | this.arr = arr; 6 | } 7 | 8 | public int query(int left, int right, int threshold) { 9 | int[] count = new int[20001]; 10 | for (int i = left; i <= right; i++) { 11 | int num = arr[i]; 12 | if (++count[num] == threshold) 13 | return num; 14 | } 15 | return -1; 16 | } 17 | } 18 | 19 | /** 20 | * Your MajorityChecker object will be instantiated and called as such: 21 | * MajorityChecker obj = new MajorityChecker(arr); 22 | * int param_1 = obj.query(left,right,threshold); 23 | */ -------------------------------------------------------------------------------- /Algorithms/1001-2000/1232. Check If It Is a Straight Line/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean checkStraightLine(int[][] coordinates) { 3 | int length = coordinates.length; 4 | if (length == 2) 5 | return true; 6 | int deltaX = coordinates[1][0] - coordinates[0][0], deltaY = coordinates[1][1] - coordinates[0][1]; 7 | for (int i = 2; i < length; i++) { 8 | int curDeltaX = coordinates[i][0] - coordinates[i - 1][0], curDeltaY = coordinates[i][1] - coordinates[i - 1][1]; 9 | if (deltaX * curDeltaY != deltaY * curDeltaX) 10 | return false; 11 | } 12 | return true; 13 | } 14 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1276. Number of Burgers with No Waste of Ingredients/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List numOfBurgers(int tomatoSlices, int cheeseSlices) { 3 | List numbers = new ArrayList(); 4 | if (tomatoSlices % 2 != 0) 5 | return numbers; 6 | int halfTomatoSlices = tomatoSlices / 2; 7 | int totalJumbo = halfTomatoSlices - cheeseSlices; 8 | int totalSmall = cheeseSlices * 2 - halfTomatoSlices; 9 | if (totalJumbo >= 0 && totalSmall >= 0) { 10 | numbers.add(totalJumbo); 11 | numbers.add(totalSmall); 12 | } 13 | return numbers; 14 | } 15 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1299. Replace Elements with Greatest Element on Right Side/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] replaceElements(int[] arr) { 3 | if (arr == null || arr.length == 0) 4 | return arr; 5 | int length = arr.length; 6 | int[] newArray = new int[length]; 7 | newArray[length - 1] = -1; 8 | if (length == 1) 9 | return newArray; 10 | newArray[length - 2] = arr[length - 1]; 11 | for (int i = length - 3; i >= 0; i--) { 12 | int nextNum = arr[i + 1]; 13 | newArray[i] = Math.max(newArray[i + 1], nextNum); 14 | } 15 | return newArray; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1313. Decompress Run-Length Encoded List/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] decompressRLElist(int[] nums) { 3 | List list = new ArrayList(); 4 | int length = nums.length; 5 | for (int i = 0; i < length; i += 2) { 6 | int count = nums[i]; 7 | int value = nums[i + 1]; 8 | for (int j = 0; j < count; j++) 9 | list.add(value); 10 | } 11 | int size = list.size(); 12 | int[] decompressed = new int[size]; 13 | for (int i = 0; i < size; i++) 14 | decompressed[i] = list.get(i); 15 | return decompressed; 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1446. Consecutive Characters/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxPower(String s) { 3 | if (s == null || s.length() == 0) 4 | return 0; 5 | int length = s.length(); 6 | int maxPower = 1; 7 | int power = 0; 8 | char prevC = 'A'; 9 | for (int i = 0; i < length; i++) { 10 | char c = s.charAt(i); 11 | if (c == prevC) { 12 | power++; 13 | maxPower = Math.max(maxPower, power); 14 | } else { 15 | power = 1; 16 | prevC = c; 17 | } 18 | } 19 | return maxPower; 20 | } 21 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0215. Kth Largest Element in an Array/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findKthLargest(int[] nums, int k) { 3 | PriorityQueue priorityQueue = new PriorityQueue(); 4 | int length = nums.length; 5 | for (int i = 0; i < k; i++) 6 | priorityQueue.offer(nums[i]); 7 | for (int i = k; i < length; i++) { 8 | int kthNum = priorityQueue.peek(); 9 | int num = nums[i]; 10 | if (num > kthNum) { 11 | priorityQueue.offer(num); 12 | priorityQueue.poll(); 13 | } 14 | } 15 | return priorityQueue.poll(); 16 | } 17 | } -------------------------------------------------------------------------------- /Algorithms/0001-1000/0709. To Lower Case/0709. To Lower Case.md: -------------------------------------------------------------------------------- 1 | # 709. To Lower Case 2 | ## Level 3 | Easy 4 | 5 | ## Description 6 | Implement function ToLowerCase() that has a string parameter str, and returns the same string in lowercase. 7 | 8 | **Example 1:** 9 | 10 | **Input:** "Hello" 11 | 12 | **Output:** "hello" 13 | 14 | **Example 2:** 15 | 16 | **Input:** "here" 17 | 18 | **Output:** "here" 19 | 20 | **Example 3:** 21 | 22 | **Input:** "LOVELY" 23 | 24 | **Output:** "lovely" 25 | 26 | ## Solution 27 | Loop over the string and check each character. If a character is an uppercase letter, change it to the corresponding lowercase letter. Finally, return the changed string. 28 | -------------------------------------------------------------------------------- /Algorithms/0001-1000/0713. Subarray Product Less Than K/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numSubarrayProductLessThanK(int[] nums, int k) { 3 | if (nums == null || nums.length == 0 || k <= 1) 4 | return 0; 5 | int product = 1; 6 | int count = 0; 7 | int start = 0, end = 0; 8 | int length = nums.length; 9 | while (end < length) { 10 | product *= nums[end]; 11 | while (product >= k) { 12 | product /= nums[start]; 13 | start++; 14 | } 15 | count += end - start + 1; 16 | end++; 17 | } 18 | return count; 19 | } 20 | } -------------------------------------------------------------------------------- /Algorithms/1001-2000/1099. Two Sum Less Than K/Solution.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int twoSumLessThanK(int[] A, int K) { 3 | if (A == null || A.length < 2) 4 | return -1; 5 | Arrays.sort(A); 6 | if (A[0] + A[1] >= K) 7 | return -1; 8 | int maxSum = Integer.MIN_VALUE; 9 | int low = 0, high = A.length - 1; 10 | while (low < high) { 11 | int sum = A[low] + A[high]; 12 | if (sum >= K) 13 | high--; 14 | else { 15 | maxSum = Math.max(maxSum, sum); 16 | low++; 17 | } 18 | } 19 | return maxSum; 20 | } 21 | } --------------------------------------------------------------------------------