├── .gitignore ├── src ├── 0037-Sudoku-Solver │ └── 0037.cpp ├── 0279-Perfect-Squares │ └── 0279.cpp ├── 0210-Course-Schedule-II │ └── 0210.go ├── 1001-Grid-Illumination │ └── 1001.py ├── 1230-Toss-Strange-Coins │ ├── 1230.js │ └── 1230.py ├── 0678-Valid-Parenthesis-String │ ├── 0678.go │ ├── 0678.java │ ├── 0678.js │ ├── 0678.py │ └── 0678.rs ├── 1202-Smallest-String-With-Swaps │ └── 1202.js ├── 1316-Distinct-Echo-Substrings │ ├── 1316.go │ ├── 1316.java │ └── 1316.js ├── 1167-Minimum-Cost-to-Connect-Sticks │ ├── 1167.js │ └── 1167.py ├── 0583-Delete-Operation-for-Two-Strings │ ├── 0583.go │ ├── 0583.java │ ├── 0583.js │ └── 0583.py ├── 1022-Sum-of-Root-To-Leaf-Binary-Numbers │ ├── 1022.cpp │ └── 1022.py ├── 0236-Lowest-Common-Ancestor-of-a-Binary-Tree │ ├── 0236.cpp │ └── 0236.py ├── 0987-Vertical-Order-Traversal-of-a-Binary-Tree │ └── 0987.py ├── 1011-Capacity-To-Ship-Packages-Within-D-Days │ └── 1011.py ├── 0524-Longest-Word-in-Dictionary-through-Deleting │ ├── 0524.go │ ├── 0524.java │ └── 0524.js ├── 1342-Number-of-Steps-to-Reduce-a-Number-to-Zero │ ├── 1342.go │ ├── 1342.js │ ├── 1342.java │ ├── 1342.py │ └── 1342.cpp ├── 1431-Kids-With-the-Greatest-Number-of-Candies │ ├── 1431.java │ ├── 1431.py │ └── 1431.js ├── 1326-Minimum-Number-of-Taps-to-Open-to-Water-a-Garden │ └── 1326.js ├── 1434-Number-of-Ways-to-Wear-Different-Hats-to-Each-Other │ └── 1434.py ├── 1025-Divisor-Game │ ├── 1025.py │ └── 1025.cpp ├── 1256-Encode-Number │ ├── 1256.py │ ├── 1256.java │ ├── 1256.go │ ├── 1256.js │ └── 1256.cpp ├── 1323-Maximum-69-Number │ ├── 1323.js │ ├── 1323.py │ ├── 1323.go │ └── 1323.java ├── 0319-Bulb-Switcher │ ├── 0319.py │ ├── 0319.go │ └── 0319.cpp ├── 0231-Power-of-Two │ ├── 231.py │ ├── 231.cpp │ └── 0231.go ├── 0459-Repeated-Substring-Pattern │ ├── 0459.js │ ├── 0459.py │ ├── 0459.go │ ├── 0459.java │ └── 0459.cpp ├── 1108-Defanging-an-IP-Address │ ├── 1108.go │ ├── 1108.py │ └── 1108.cpp ├── 1119-Remove-Vowels-from-a-String │ ├── 1119.py │ ├── 1119.go │ └── 1119.cpp ├── 2549-Count-Distinct-Numbers-on-Board │ └── 2549.cpp ├── 0326-Power-of-Three │ ├── 0326.rs │ ├── 0326.cpp │ └── 0326.go ├── 0151-Reverse-Words-in-a-String │ └── 0151.py ├── 1304-Find-N-Unique-Integers-Sum-up-to-Zero │ ├── 1304.py │ ├── 1304.js │ ├── 1304.go │ ├── 1304.java │ └── 1304.cpp ├── 0836-Rectangle-Overlap │ ├── 0836.js │ ├── 0836.go │ ├── 0836.py │ ├── 0836.java │ └── 0836.cpp ├── 1332-Remove-Palindromic-Subsequences │ ├── 1332.py │ ├── 1332.js │ ├── 1332.cpp │ └── 1332.java ├── 1374-Generate-a-String-With-Characters-That-Have-Odd-Counts │ ├── 1374.js │ ├── 1374.py │ ├── 1374.java │ ├── 1374.cpp │ └── 1374.go ├── 1051-Height-Checker │ └── 1051.py ├── 1134-Armstrong-Number │ ├── 1134.py │ ├── 1134.js │ ├── 1134.cpp │ └── 1134.go ├── 1359-Count-All-Valid-Pickup-and-Delivery-Options │ ├── 1359.py │ ├── 1359.js │ ├── 1359.go │ ├── 1359.java │ └── 1359.cpp ├── 1436-Destination-City │ ├── 1436.py │ ├── 1436.js │ ├── 1436.go │ └── 1436.cpp ├── 0058-Length-of-Last-Word │ ├── 0058.rs │ └── 0058.py ├── 0344-Reverse-String │ ├── 0344.go │ └── 0344.py ├── 1217-Play-with-Chips │ ├── 1217.js │ ├── 1217.py │ ├── 1217.cpp │ └── 1217.go ├── 1228-Missing-Number-In-Arithmetic-Progression │ ├── 1228.py │ ├── 1228.go │ ├── 1228.js │ └── 1228.cpp ├── 1276-Number-of-Burgers-with-No-Waste-of-Ingredients │ ├── 1276.js │ ├── 1276.py │ ├── 1276.go │ ├── 1276.java │ └── 1276.cpp ├── 1295-Find-Numbers-with-Even-Number-of-Digits │ ├── 1295.py │ ├── 1295.js │ ├── 1295.go │ ├── 1295.java │ └── 1295.cpp ├── 1344-Angle-Between-Hands-of-a-Clock │ ├── 1344.js │ ├── 1344.py │ ├── 1344.java │ └── 1344.cpp ├── 1356-Sort-Integers-by-The-Number-of-1-Bits │ ├── 1356.py │ ├── 1356.java │ ├── 1356.js │ └── 1356.cpp ├── 2469-Convert-the-Temperature │ └── 2469.cpp ├── 0070-Climbing-Stairs │ ├── 0070.py │ ├── 0070.cpp │ └── 0070.go ├── 1380-Lucky-Numbers-in-a-Matrix │ └── 1380.py ├── 0914-X-of-a-Kind-in-a-Deck-of-Cards │ └── 0914.py ├── 1071-Greatest-Common-Divisor-of-Strings │ ├── 1071.py │ ├── 1071.cpp │ ├── 1071.js │ ├── 1071.go │ └── 1071.java ├── 1394-Find-Lucky-Integer-in-an-Array │ ├── 1394.py │ ├── 1394.js │ ├── 1394.go │ ├── 1394.cpp │ └── 1394.java ├── 1400-Construct-K-Palindrome-Strings │ ├── 1400.py │ ├── 1400.cpp │ ├── 1400.js │ ├── 1400.go │ └── 1400.java ├── 1523-Count-Odd-Numbers-in-an-Interval-Range │ └── 1523.cpp ├── 0136-Single-Number │ ├── 0136.go │ └── 0136.py ├── 0191-Number-of-1-Bits │ ├── 0191.py │ ├── 0191.go │ └── 0191.cpp ├── 1037-Valid-Boomerang │ ├── 1037.py │ └── 1037.cpp ├── 1085-Sum-of-Digits-in-the-Minimum-Number │ ├── 1085.py │ └── 1085.cpp ├── 1238-Circular-Permutation-in-Binary-Representation │ ├── 1238.py │ ├── 1238.js │ ├── 1238.go │ └── 1238.cpp ├── 1309-Decrypt-String-from-Alphabet-to-Integer-Mapping │ └── 1309.py ├── 1313-Decompress-Run-Length-Encoded-List │ ├── 1313.py │ ├── 1313.js │ ├── 1313.go │ └── 1313.cpp ├── 1324-Print-Words-Vertically │ └── 1324.py ├── 1331-Rank-Transform-of-an-Array │ └── 1331.py ├── 1812-Determine-Color-of-a-Chessboard-Square │ └── 1812.cpp ├── 1207-Unique-Number-of-Occurrences │ ├── 1207.py │ ├── 1207.go │ └── 1207.cpp ├── 1250-Check-If-It-Is-a-Good-Array │ ├── 1250.py │ ├── 1250.cpp │ ├── 1250.go │ ├── 1250.js │ └── 1250.java ├── 0009-Palindrome-Number │ ├── 0009.py │ └── 0009.go ├── 0263-Ugly-Number │ ├── 0263.py │ └── 0263.go ├── 0462-Minimum-Moves-to-Equal-Array-Elements-II │ └── 0462.py ├── 0891-Sum-of-Subsequence-Widths │ ├── 0891.py │ ├── 0891.go │ └── 0891.js ├── 1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings │ ├── 1111.py │ ├── 1111.go │ └── 1111.cpp ├── 1317-Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers │ ├── 1317.py │ ├── 1317.js │ ├── 1317.go │ └── 1317.java ├── 1009-Complement-of-Base-10-Integer │ ├── 1009.py │ └── 1009.cpp ├── 1121-Divide-Array-Into-Increasing-Sequences │ └── 1121.py ├── 0977-Squares-of-a-Sorted-Array │ └── 0977.py ├── 0991-Broken-Calculator │ ├── 0991.py │ └── 0991.cpp ├── 1150-Check-If-a-Number-Is-Majority-Element-in-a-Sorted-Array │ ├── 1150.py │ ├── 1150.js │ ├── 1150.go │ └── 1150.cpp ├── 1154-Day-of-the-Year │ ├── 1154.py │ └── 1154.js ├── 1213-Intersection-of-Three-Sorted-Arrays │ ├── 1213.py │ └── 1213.js ├── 1232-Check-If-It-Is-a-Straight-Line │ ├── 1232.py │ ├── 1232.go │ └── 1232.js ├── 0268-Missing-Number │ ├── 0268.go │ └── 0268.py ├── 1072-Flip-Columns-For-Maximum-Number-of-Equal-Rows │ ├── 1072.py │ └── 1072.js ├── 1189-Maximum-Number-of-Balloons │ ├── 1189.py │ └── 1189.js ├── 1347-Minimum-Number-of-Steps-to-Make-Two-Strings-Anagram │ └── 1347.py ├── 0172-Factorial-Trailing-Zeroes │ ├── 0172.py │ └── 0172.cpp ├── 1018-Binary-Prefix-Divisible-By-5 │ └── 1018.py ├── 1266-Minimum-Time-Visiting-All-Points │ ├── 1266.py │ ├── 1266.js │ ├── 1266.java │ └── 1266.cpp ├── 0371-Sum-of-Two-Integers │ ├── 0371.py │ ├── 0371.rs │ └── 0371.cpp ├── 0409-Longest-Palindrome │ ├── 0409.py │ ├── 0409.js │ ├── 0409.cpp │ ├── 0409.java │ └── 0409.go ├── 1002-Find-Common-Characters │ └── 1002.py ├── 1118-Number-of-Days-in-a-Month │ ├── 1118.go │ ├── 1118.py │ └── 1118.cpp ├── 1128-Number-of-Equivalent-Domino-Pairs │ ├── 1128.py │ ├── 1128.js │ └── 1128.cpp ├── 1832-Check-if-the-Sentence-is-Pangram │ ├── 1832.py │ └── 1832.cpp ├── 0215-Kth-Largest-Element-in-an-Array │ └── 0215.py ├── 0492-Construct-the-Rectangle │ ├── 0492.cpp │ └── 0492.rs ├── 1290-Convert-Binary-Number-in-a-Linked-List-to-Integer │ ├── 1290.go │ ├── 1290.js │ ├── 1290.py │ ├── 1290.java │ └── 1290.cpp ├── 1389-Create-Target-Array-in-the-Given-Order │ ├── 1389.js │ ├── 1389.go │ ├── 1389.py │ └── 1389.cpp ├── 0453-Minimum-Moves-to-Equal-Array-Elements │ ├── 0453.py │ ├── 0453.cpp │ └── 0453.rs ├── 1122-Relative-Sort-Array │ └── 1122.py ├── 1221-Split-a-String-in-Balanced-Strings │ ├── 1221.js │ ├── 1221.go │ ├── 1221.py │ └── 1221.cpp ├── 1160-Find-Words-That-Can-Be-Formed-by-Characters │ └── 1160.py ├── 1184-Distance-Between-Bus-Stops │ ├── 1184.py │ └── 1184.js ├── 1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer │ ├── 1281.py │ ├── 1281.go │ ├── 1281.java │ ├── 1281.js │ └── 1281.cpp ├── 1375-Bulb-Switcher-III │ ├── 1375.js │ ├── 1375.py │ ├── 1375.java │ ├── 1375.cpp │ └── 1375.go ├── 0201-Bitwise-AND-of-Numbers-Range │ └── 0201.py ├── 0650-2-Keys-Keyboard │ └── 0650.py ├── 1198-Find-Smallest-Common-Element-in-All-Rows │ └── 1198.py ├── 1328-Break-a-Palindrome │ ├── 1328.py │ ├── 1328.js │ └── 1328.go ├── 0190-Reverse-Bits │ ├── 0190.go │ └── 0190.py ├── 0365-Water-and-Jug-Problem │ ├── 0365.cpp │ ├── 0365.py │ ├── 0365.js │ ├── 0365.java │ └── 0365.go ├── 1185-Day-of-the-Week │ ├── 1185.py │ ├── 1185.go │ └── 1185.js ├── 1259-Handshakes-That-Don't-Cross │ └── 1259.py ├── 1267-Count-Servers-that-Communicate │ └── 1267.py ├── 1310-XOR-Queries-of-a-Subarray │ └── 1310.py ├── 1413-Minimum-Value-to-Get-Positive-Step-by-Step-Sum │ ├── 1413.js │ ├── 1413.py │ ├── 1413.java │ ├── 1413.cpp │ └── 1413.go ├── 0171-Excel-Sheet-Column-Number │ └── 0171.py ├── 1402-Reducing-Dishes │ ├── 1402.go │ ├── 1402.py │ ├── 1402.js │ ├── 1402.java │ └── 1402.cpp ├── 1411-Number-of-Ways-to-Paint-N×3-Grid │ ├── 1411.py │ ├── 1411.go │ └── 1411.js ├── 1732-Find-the-Highest-Altitude │ └── 1732.cpp ├── 0069-Sqrt(x) │ ├── 0069.py │ ├── 0069.go │ └── 0069.cpp ├── 0458-Poor-Pigs │ ├── 0458.rs │ ├── 0458.py │ └── 0458.cpp ├── 1047-Remove-All-Adjacent-Duplicates-In-String │ ├── 1047.js │ ├── 1047.py │ └── 1047.go ├── 1302-Deepest-Leaves-Sum │ └── 1302.py ├── 1780-Check-if-Number-is-a-Sum-of-Powers-of-Three │ └── 1780.cpp ├── 2185-Counting-Words-With-a-Given-Prefix │ └── 2185.cpp ├── 0989-Add-to-Array-Form-of-Integer │ └── 0989.py ├── 1262-Greatest-Sum-Divisible-by-Three │ ├── 1262.py │ ├── 1262.js │ └── 1262.java ├── 1288-Remove-Covered-Intervals │ ├── 1288.py │ ├── 1288.java │ └── 1288.js ├── 0027-Remove-Element │ └── 0027.go ├── 0223-Rectangle-Area │ └── 0223.py ├── 1197-Minimum-Knight-Moves │ ├── 1197.js │ ├── 1197.py │ └── 1197.cpp ├── 1346-Check-If-N-and-Its-Double-Exist │ ├── 1346.js │ ├── 1346.py │ ├── 1346.java │ └── 1346.cpp ├── 1220-Count-Vowels-Permutation │ ├── 1220.py │ └── 1220.go ├── 2351-First-Letter-to-Appear-Twice │ └── 2351.cpp ├── 0217-Contains-Duplicate │ └── 0217.go ├── 1014-Best-Sightseeing-Pair │ └── 1014.py ├── 1133-Largest-Unique-Number │ ├── 1133.py │ └── 1133.go ├── 0169-Majority-Element │ ├── 0169.go │ └── 0169.rs ├── 0793-Preimage-Size-of-Factorial-Zeroes-Function │ ├── 0793.js │ ├── 0793.go │ └── 0793.py ├── 0961-N-Repeated-Element-in-Size-2N-Array │ └── 0961.py ├── 1299-Replace-Elements-with-Greatest-Element-on-Right-Side │ ├── 1299.py │ ├── 1299.java │ ├── 1299.js │ ├── 1299.go │ └── 1299.cpp ├── 1318-Minimum-Flips-to-Make-a-OR-b-Equal-to-c │ └── 1318.js ├── addProg.py ├── 0001-Two-Sum │ └── two_sum.js ├── 1170-Compare-Strings-by-Frequency-of-the-Smallest-Character │ └── 1170.py ├── 1196-How-Many-Apples-Can-You-Put-into-the-Basket │ ├── 1196.go │ ├── 1196.py │ └── 1196.js ├── 1362-Closest-Divisors │ ├── 1362.js │ └── 1362.go ├── 0973-K-Closest-Points-to-Origin │ └── 0973.py ├── 1437-Check-If-All-1's-Are-at-Least-Length-K-Places-Away │ ├── 1437.js │ ├── 1437.py │ ├── 1437.go │ ├── 1437.java │ └── 1437.cpp ├── 1470-Shuffle-the-Array │ └── 1470.cpp ├── 0559-Maximum-Depth-of-N-ary-Tree │ └── 0559.cpp ├── 0559-maximum-depth-of-n-ary-tree │ └── 0559.cpp ├── 0946-Validate-Stack-Sequences │ ├── 0946.js │ ├── 0946.go │ ├── 0946.py │ └── 0946.cpp ├── 1287-Element-Appearing-More-Than-25%-In-Sorted-Array │ └── 1287.py ├── 1325-Delete-Leaves-With-a-Given-Value │ ├── 1325.js │ ├── 1325.java │ └── 1325.go ├── 1005-Maximize-Sum-Of-Array-After-K-Negations │ └── 1005.py ├── 1048-Longest-String-Chain │ └── 1048.py ├── 1218-Longest-Arithmetic-Subsequence-of-Given-Difference │ ├── 1218.js │ └── 1218.py ├── 1300-Sum-of-Mutated-Array-Closest-to-Target │ ├── 1300.py │ └── 1300.js ├── 1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number │ └── 1365.py ├── 1147-Longest-Chunked-Palindrome-Decomposition │ ├── 1147.js │ ├── 1147.py │ └── 1147.go ├── 0026-Remove-Duplicates-from-Sorted-Array │ ├── 0026.go │ └── 0026.js ├── 1007-Minimum-Domino-Rotations-For-Equal-Row │ └── 1007.py ├── 1269-Number-of-Ways-to-Stay-in-the-Same-Place-After-Some-Steps │ └── 1269.py ├── 2180-Count-Integers-With-Even-Digit-Sum │ └── 2180.cpp ├── 2427-Number-of-Common-Factors │ └── 2427.cpp ├── 0035-Search-Insert-Position │ └── 0035.go ├── 0043-Multiply-Strings │ └── 0043.py ├── 0219-Contains-Duplicate-II │ └── 0219.go ├── 1033-Moving-Stones-Until-Consecutive │ └── 1033.py ├── 1366-Rank-Teams-by-Votes │ └── 1366.py ├── 1798-Maximum-Number-of-Consecutive-Values-You-Can-Make │ └── 1798.cpp ├── 0089-Gray-Code │ └── 0089.py ├── 0137-Single-Number-II │ ├── 0137.py │ └── 0137.go ├── 1010-Pairs-of-Songs-With-Total-Durations-Divisible-by-60 │ └── 1010.py ├── 1282-Group-the-People-Given-the-Group-Size-They-Belong-To │ └── 1282.py ├── 1422-Maximum-Score-After-Splitting-a-String │ ├── 1422.js │ └── 1422.py ├── 1806-Minimum-Number-of-Operations-to-Reinitialize-a-Permutation │ └── 1806.cpp ├── 1015-Smallest-Integer-Divisible-by-K │ └── 1015.py ├── 1199-Minimum-Time-to-Build-Blocks │ └── 1199.py ├── 1423-Maximum-Points-You-Can-Obtain-from-Cards │ ├── 1423.js │ └── 1423.py ├── 1758-Minimum-Changes-To-Make-Alternating-Binary-String │ └── 1758.cpp ├── 2011-Final-Value-of-Variable-After-Performing-Operations │ └── 2011.cpp ├── 2357-Make-Array-Zero-by-Subtracting-Equal-Amounts │ └── 2357.cpp ├── 2439-Minimize-Maximum-of-Array │ └── 2439.cpp ├── 0168-Excel-Sheet-Column-Title │ ├── 0168.py │ └── 0168.cpp ├── 0278-First-Bad-Version │ ├── 0278.cpp │ └── 0278.py ├── 0700-Search-in-a-Binary-Search-Tree │ └── 0700.cpp ├── 0824-Goat-Latin │ └── 0824.py ├── 0876-Middle-of-the-Linked-List │ ├── 0876.js │ ├── 0876.go │ └── 0876.py ├── 1271-Hexspeak │ └── 1271.js ├── 1333-Filter-Restaurants-by-Vegan-Friendly,-Price-and-Distance │ ├── 1333.js │ └── 1333.py ├── 2027-Minimum-Moves-to-Convert-String │ └── 2027.cpp ├── 2395-Find-Subarrays-With-Equal-Sum │ └── 2395.cpp ├── 0153-Find-Minimum-in-Rotated-Sorted-Array │ └── 0153.go ├── 0179-Largest-Number │ └── 0179.py ├── 0253-Meeting-Rooms-II │ └── 0253.py ├── 1144-Decrease-Elements-To-Make-Array-Zigzag │ ├── 1144.py │ └── 1144.js ├── 1401-Circle-and-Rectangle-Overlapping │ ├── 1401.js │ └── 1401.py ├── 1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One │ ├── 1404.go │ ├── 1404.js │ └── 1404.py ├── 1752-Check-if-Array-Is-Sorted-and-Rotated │ └── 1752.cpp ├── 0451-Sort-Characters-By-Frequency │ ├── 0451.py │ └── 0451.cpp ├── 1109-Corporate-Flight-Bookings │ ├── 1109.go │ └── 1109.py ├── 1165-Single-Row-Keyboard │ ├── 1165.py │ └── 1165.js ├── 1289-Minimum-Falling-Path-Sum-II │ └── 1289.py ├── 1315-Sum-of-Nodes-with-Even-Valued-Grandparent │ └── 1315.js ├── 1392-Longest-Happy-Prefix │ ├── 1392.js │ ├── 1392.go │ └── 1392.py ├── 0242-Valid-Anagram │ └── 0242.go ├── 0944-Delete-Columns-to-Make-Sorted │ └── 0944.py ├── 0997-Find-the-Town-Judge │ └── 0997.py ├── 1026-Maximum-Difference-Between-Node-and-Ancestor │ └── 1026.py ├── 1233-Remove-Sub-Folders-from-the-Filesystem │ ├── 1233.py │ └── 1233.js ├── 1351-Count-Negative-Numbers-in-a-Sorted-Matrix │ ├── 1351.js │ └── 1351.go ├── 0965-Univalued-Binary-Tree │ └── 0965.cpp ├── 1006-Clumsy-Factorial │ ├── 1006.py │ └── 1006.cpp ├── 1038-Binary-Search-Tree-to-Greater-Sum-Tree │ └── 1038.py ├── 1399-Count-Largest-Group │ └── 1399.py ├── 0274-H-Index │ ├── 0274.py │ └── 0274.cpp ├── 0790-Domino-and-Tromino-Tiling │ └── 0790.cpp ├── 1099-Two-Sum-Less-Than-K │ └── 1099.js ├── 1237-Find-Positive-Integer-Solution-for-a-Given-Equation │ └── 1237.js ├── 1260-Shift-2D-Grid │ └── 1260.py ├── 1283-Find-the-Smallest-Divisor-Given-a-Threshold │ └── 1283.py ├── 1403-Minimum-Subsequence-in-Non-Increasing-Order │ └── 1403.py ├── 1414-Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K │ ├── 1414.go │ └── 1414.py ├── 1739-Building-Boxes │ └── 1739.cpp ├── 2244-Minimum-Rounds-to-Complete-All-Tasks │ └── 2244.cpp ├── 2341-Maximum-Number-of-Pairs-in-Array │ └── 2341.cpp ├── 0828-Unique-Letter-String │ ├── 0828.go │ └── 0828.py ├── 0976-Largest-Perimeter-Triangle │ └── 0976.py ├── 1004-Max-Consecutive-Ones-III │ └── 1004.py ├── 1046-Last-Stone-Weight │ └── 1046.py ├── 1297-Maximum-Number-of-Occurrences-of-a-Substring │ └── 1297.py ├── 1827-Minimum-Operations-to-Make-the-Array-Increasing │ └── 1827.cpp ├── 2315-Count-Asterisks │ └── 2315.cpp ├── 2331-Evaluate-Boolean-Binary-Tree │ └── 2331.cpp ├── 0121-Best-Time-to-Buy-and-Sell-Stock │ └── 0121.go ├── 0594-Longest-Harmonious-Subsequence │ └── 0594.cpp ├── 0594-longest-harmonious-subsequence │ └── 0594.cpp ├── 1021-Remove-Outermost-Parentheses │ └── 1021.py ├── 1089-Duplicate-Zeros │ └── 1089.cpp ├── 1104-Path-In-Zigzag-Labelled-Binary-Tree │ └── 1104.py ├── 1176-Diet-Plan-Performance │ └── 1176.py ├── 0969-Pancake-Sorting │ └── 0969.py ├── 1424-Diagonal-Traverse-II │ └── 1424.py ├── 0075-Sort-Colors │ └── 0075.go ├── 0125-Valid-Palindrome │ └── 0125.py ├── 0198-House-Robber │ └── 0198.py ├── 1064-Fixed-Point │ └── 1064.py ├── 1100-Find-K-Length-Substrings-With-No-Repeated-Characters │ └── 1100.js ├── 1247-Minimum-Swaps-to-Make-Strings-Equal │ └── 1247.js ├── 0048-Rotate-Image │ └── 0048.go ├── 0055-Jump-Game │ └── 0055.go ├── 0066-Plus-One │ └── 0066.rs ├── 0238-Product-of-Array-Except-Self │ └── 0238.go ├── 0331-Verify-Preorder-Serialization-of-a-Binary-Tree │ └── 0331.py ├── 0653-Two-Sum-IV-Input-is-a-BST │ └── 0653.cpp ├── 0795-Number-of-Subarrays-with-Bounded-Maximum │ └── 0795.cpp ├── 1145-Binary-Tree-Coloring-Game │ └── 1145.js ├── 1153-String-Transforms-Into-Another-String │ └── 1153.js └── 1277-Count-Square-Submatrices-with-All-Ones │ └── 1277.py └── test ├── 0062-test.cpp ├── 0050-test.cpp ├── 0067-test.cpp ├── 0096-test.cpp └── 0058-test.cpp /.gitignore: -------------------------------------------------------------------------------- 1 | *vscode* -------------------------------------------------------------------------------- /src/0037-Sudoku-Solver/0037.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0279-Perfect-Squares/0279.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0210-Course-Schedule-II/0210.go: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1001-Grid-Illumination/1001.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1230-Toss-Strange-Coins/1230.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0678-Valid-Parenthesis-String/0678.go: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0678-Valid-Parenthesis-String/0678.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0678-Valid-Parenthesis-String/0678.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0678-Valid-Parenthesis-String/0678.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1202-Smallest-String-With-Swaps/1202.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1316-Distinct-Echo-Substrings/1316.go: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1316-Distinct-Echo-Substrings/1316.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1316-Distinct-Echo-Substrings/1316.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1167-Minimum-Cost-to-Connect-Sticks/1167.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0583-Delete-Operation-for-Two-Strings/0583.go: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0583-Delete-Operation-for-Two-Strings/0583.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0583-Delete-Operation-for-Two-Strings/0583.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0583-Delete-Operation-for-Two-Strings/0583.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1022-Sum-of-Root-To-Leaf-Binary-Numbers/1022.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1022-Sum-of-Root-To-Leaf-Binary-Numbers/1022.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0236-Lowest-Common-Ancestor-of-a-Binary-Tree/0236.cpp: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0236-Lowest-Common-Ancestor-of-a-Binary-Tree/0236.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0987-Vertical-Order-Traversal-of-a-Binary-Tree/0987.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1011-Capacity-To-Ship-Packages-Within-D-Days/1011.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0524-Longest-Word-in-Dictionary-through-Deleting/0524.go: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0524-Longest-Word-in-Dictionary-through-Deleting/0524.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/0524-Longest-Word-in-Dictionary-through-Deleting/0524.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1342-Number-of-Steps-to-Reduce-a-Number-to-Zero/1342.go: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1431-Kids-With-the-Greatest-Number-of-Candies/1431.java: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1326-Minimum-Number-of-Taps-to-Open-to-Water-a-Garden/1326.js: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1434-Number-of-Ways-to-Wear-Different-Hats-to-Each-Other/1434.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /src/1025-Divisor-Game/1025.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def divisorGame(self, N: int) -> bool: 3 | return N % 2 == 0 -------------------------------------------------------------------------------- /src/1256-Encode-Number/1256.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def encode(self, num: int) -> str: 3 | return bin(num + 1)[3:] -------------------------------------------------------------------------------- /src/1323-Maximum-69-Number/1323.js: -------------------------------------------------------------------------------- 1 | var maximum69Number = function(num) { 2 | return Number((num + "").replace(/6/, "9")); 3 | }; -------------------------------------------------------------------------------- /src/0319-Bulb-Switcher/0319.py: -------------------------------------------------------------------------------- 1 | import math 2 | class Solution: 3 | def bulbSwitch(self, n: int) -> int: 4 | return int(math.sqrt(n)) -------------------------------------------------------------------------------- /src/0231-Power-of-Two/231.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPowerOfTwo(self, n: int) -> bool: 3 | return False if n < 0 else bin(n).count('1') == 1 -------------------------------------------------------------------------------- /src/0459-Repeated-Substring-Pattern/0459.js: -------------------------------------------------------------------------------- 1 | var repeatedSubstringPattern = function(s) { 2 | return (s + s).slice(1, -1).indexOf(s) != -1; 3 | }; -------------------------------------------------------------------------------- /src/1108-Defanging-an-IP-Address/1108.go: -------------------------------------------------------------------------------- 1 | func defangIPaddr(address string) string { 2 | return strings.Replace(address, ".", "[.]", -1) 3 | } -------------------------------------------------------------------------------- /src/1119-Remove-Vowels-from-a-String/1119.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeVowels(self, S: str) -> str: 3 | return re.sub(r'[aeiou]', '', S) -------------------------------------------------------------------------------- /src/2549-Count-Distinct-Numbers-on-Board/2549.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int distinctIntegers(int n) { return n == 1 ? 1 : n - 1; } 4 | }; -------------------------------------------------------------------------------- /src/0326-Power-of-Three/0326.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn is_power_of_three(n: i32) -> bool { 3 | n > 0 && 1162261467 % n == 0 4 | } 5 | } -------------------------------------------------------------------------------- /src/0459-Repeated-Substring-Pattern/0459.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def repeatedSubstringPattern(self, s: str) -> bool: 3 | return s in (2 * s)[1:-1] -------------------------------------------------------------------------------- /src/1108-Defanging-an-IP-Address/1108.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def defangIPaddr(self, address: str) -> str: 3 | return address.replace('.', '[.]') -------------------------------------------------------------------------------- /src/1323-Maximum-69-Number/1323.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maximum69Number (self, num: int) -> int: 3 | return int(str(num).replace('6', '9', 1)) -------------------------------------------------------------------------------- /src/0151-Reverse-Words-in-a-String/0151.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseWords(self, s: str) -> str: 3 | return ' '.join([i for i in s.split()][::-1]) -------------------------------------------------------------------------------- /src/0319-Bulb-Switcher/0319.go: -------------------------------------------------------------------------------- 1 | package bulbswitcher 2 | 3 | import "math" 4 | 5 | func bulbSwitch(n int) int { 6 | return int(math.Sqrt(float64(n))) 7 | } 8 | -------------------------------------------------------------------------------- /src/0326-Power-of-Three/0326.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfThree(int n) { 4 | return n > 0 && 1162261467 % n == 0; 5 | } 6 | }; -------------------------------------------------------------------------------- /src/1304-Find-N-Unique-Integers-Sum-up-to-Zero/1304.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumZero(self, n: int) -> List[int]: 3 | return list(range(1 - n, n, 2)) -------------------------------------------------------------------------------- /src/0231-Power-of-Two/231.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isPowerOfTwo(int n) { 4 | return n < 0 ? false : __builtin_popcountll(n) == 1; 5 | } 6 | }; -------------------------------------------------------------------------------- /src/0459-Repeated-Substring-Pattern/0459.go: -------------------------------------------------------------------------------- 1 | func repeatedSubstringPattern(s string) bool { 2 | t := s + s 3 | return strings.Contains(t[1:len(t)-1], s) 4 | } -------------------------------------------------------------------------------- /src/0836-Rectangle-Overlap/0836.js: -------------------------------------------------------------------------------- 1 | var isRectangleOverlap = function(r1, r2) { 2 | return r1[0] < r2[2] && r1[1] < r2[3] && r1[2] > r2[0] && r1[3] > r2[1]; 3 | }; -------------------------------------------------------------------------------- /src/1332-Remove-Palindromic-Subsequences/1332.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removePalindromeSub(self, s: str) -> int: 3 | return 2 - (s == s[::-1]) - (s == "") -------------------------------------------------------------------------------- /src/0836-Rectangle-Overlap/0836.go: -------------------------------------------------------------------------------- 1 | func isRectangleOverlap(r1 []int, r2 []int) bool { 2 | return r1[0] < r2[2] && r1[1] < r2[3] && r1[2] > r2[0] && r1[3] > r2[1] 3 | } -------------------------------------------------------------------------------- /src/1256-Encode-Number/1256.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String encode(int num) { 3 | return Integer.toBinaryString(num + 1).substring(1); 4 | } 5 | } -------------------------------------------------------------------------------- /src/1374-Generate-a-String-With-Characters-That-Have-Odd-Counts/1374.js: -------------------------------------------------------------------------------- 1 | var generateTheString = function(n) { 2 | return 'a'.repeat(n - 1) + (n % 2 ? 'a' : 'b'); 3 | }; -------------------------------------------------------------------------------- /src/1119-Remove-Vowels-from-a-String/1119.go: -------------------------------------------------------------------------------- 1 | func removeVowels(S string) string { 2 | reg := regexp.MustCompile(`(a|e|i|o|u)`) 3 | return reg.ReplaceAllString(S, "") 4 | } -------------------------------------------------------------------------------- /src/1323-Maximum-69-Number/1323.go: -------------------------------------------------------------------------------- 1 | func maximum69Number (num int) int { 2 | res, _ := strconv.Atoi(strings.Replace(strconv.Itoa(num), "6", "9", 1)) 3 | return res 4 | } -------------------------------------------------------------------------------- /src/1332-Remove-Palindromic-Subsequences/1332.js: -------------------------------------------------------------------------------- 1 | var removePalindromeSub = function(s) { 2 | return s.length == 0 ? 0 : s == s.split("").reverse().join("") ? 1 : 2; 3 | }; -------------------------------------------------------------------------------- /src/1051-Height-Checker/1051.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def heightChecker(self, heights: List[int]) -> int: 3 | return sum(h1 != h2 for h1, h2 in zip(heights, sorted(heights))) -------------------------------------------------------------------------------- /src/1134-Armstrong-Number/1134.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isArmstrong(self, N: int) -> bool: 3 | n = len(str(N)) 4 | return sum(pow(int(d), n) for d in str(N)) == N -------------------------------------------------------------------------------- /src/1342-Number-of-Steps-to-Reduce-a-Number-to-Zero/1342.js: -------------------------------------------------------------------------------- 1 | var numberOfSteps = function(num) { 2 | return num == 0 ? 0 : 1 + numberOfSteps(num & 1 ? num - 1 : num >> 1); 3 | }; -------------------------------------------------------------------------------- /src/1359-Count-All-Valid-Pickup-and-Delivery-Options/1359.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countOrders(self, n: int) -> int: 3 | return (math.factorial(n * 2) >> n) % (10**9 + 7) -------------------------------------------------------------------------------- /src/1436-Destination-City/1436.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def destCity(self, paths: List[List[str]]) -> str: 3 | A, B = map(set, zip(*paths)) 4 | return (B - A).pop() -------------------------------------------------------------------------------- /src/0058-Length-of-Last-Word/0058.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn length_of_last_word(s: String) -> i32 { 3 | s.split_whitespace().last().unwrap().len() as i32 4 | } 5 | } -------------------------------------------------------------------------------- /src/0344-Reverse-String/0344.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func reverseString(s []byte) { 4 | for i, j := 0, len(s)-1; i < len(s)/2; i, j = i+1, j-1 { 5 | s[i], s[j] = s[j], s[i] 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /src/1217-Play-with-Chips/1217.js: -------------------------------------------------------------------------------- 1 | var minCostToMoveChips = function(chips) { 2 | let res = [0, 0]; 3 | for (let c of chips) res[c%2]++; 4 | return Math.min(res[0], res[1]); 5 | }; -------------------------------------------------------------------------------- /src/1228-Missing-Number-In-Arithmetic-Progression/1228.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def missingNumber(self, A: List[int]) -> int: 3 | return (A[0] + A[-1]) * (len(A) + 1) // 2 - sum(A) -------------------------------------------------------------------------------- /src/1276-Number-of-Burgers-with-No-Waste-of-Ingredients/1276.js: -------------------------------------------------------------------------------- 1 | var numOfBurgers = function(t, c) { 2 | return t % 2 == 0 && c * 2 <= t && t <= c * 4 ? [t / 2 - c, c * 2 - t / 2] : []; 3 | }; -------------------------------------------------------------------------------- /src/1295-Find-Numbers-with-Even-Number-of-Digits/1295.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findNumbers(self, nums: List[int]) -> int: 3 | return sum(map(lambda n: len(str(n)) % 2 == 0, nums)) -------------------------------------------------------------------------------- /src/1323-Maximum-69-Number/1323.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maximum69Number (int num) { 3 | return Integer.valueOf(String.valueOf(num).replaceFirst("6", "9")); 4 | } 5 | } -------------------------------------------------------------------------------- /src/1344-Angle-Between-Hands-of-a-Clock/1344.js: -------------------------------------------------------------------------------- 1 | var angleClock = function(hour, minutes) { 2 | let t = Math.abs((hour*30 + minutes/2.0) - minutes*6); 3 | return Math.min(t, 360 - t); 4 | }; -------------------------------------------------------------------------------- /src/1356-Sort-Integers-by-The-Number-of-1-Bits/1356.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortByBits(self, arr: List[int]) -> List[int]: 3 | return sorted(arr, key=lambda v: [bin(v).count('1'), v]) -------------------------------------------------------------------------------- /src/1374-Generate-a-String-With-Characters-That-Have-Odd-Counts/1374.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def generateTheString(self, n: int) -> str: 3 | return 'a'*(n - 1) + ('a' if n % 2 else 'b') -------------------------------------------------------------------------------- /src/2469-Convert-the-Temperature/2469.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector convertTemperature(double celsius) { 4 | return {celsius + 273.15, celsius * 1.8 + 32}; 5 | } 6 | }; -------------------------------------------------------------------------------- /src/0070-Climbing-Stairs/0070.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def climbStairs(self, n: int) -> int: 3 | a = b = 1 4 | for _ in range(n): 5 | a, b = b, a + b 6 | return a -------------------------------------------------------------------------------- /src/1380-Lucky-Numbers-in-a-Matrix/1380.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def luckyNumbers(self, A: List[List[int]]) -> List[int]: 3 | return list({min(row) for row in A} & {max(col) for col in zip(*A)}) -------------------------------------------------------------------------------- /src/0914-X-of-a-Kind-in-a-Deck-of-Cards/0914.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hasGroupsSizeX(self, deck: List[int]) -> bool: 3 | return functools.reduce(math.gcd, collections.Counter(deck).values()) >= 2 -------------------------------------------------------------------------------- /src/1071-Greatest-Common-Divisor-of-Strings/1071.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def gcdOfStrings(self, s1: str, s2: str) -> str: 3 | return s1[:math.gcd(len(s1), len(s2))] if s1 + s2 == s2 + s1 else "" -------------------------------------------------------------------------------- /src/1119-Remove-Vowels-from-a-String/1119.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string removeVowels(string S) 5 | { 6 | return regex_replace(S, regex("a|e|i|o|u"), ""); 7 | } 8 | }; -------------------------------------------------------------------------------- /src/1304-Find-N-Unique-Integers-Sum-up-to-Zero/1304.js: -------------------------------------------------------------------------------- 1 | var sumZero = function(n) { 2 | let res = new Array(n); 3 | for (let i = 1 - n, j = 0; i < n; i += 2, j++) res[j] = i; 4 | return res; 5 | }; -------------------------------------------------------------------------------- /src/1394-Find-Lucky-Integer-in-an-Array/1394.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findLucky(self, arr: List[int]) -> int: 3 | return max([k for k, v in collections.Counter(arr).items() if k == v] + [-1]) -------------------------------------------------------------------------------- /src/1400-Construct-K-Palindrome-Strings/1400.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canConstruct(self, s: str, k: int) -> bool: 3 | return sum([v % 2 for v in collections.Counter(s).values()]) <= k <= len(s) -------------------------------------------------------------------------------- /src/1523-Count-Odd-Numbers-in-an-Interval-Range/1523.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countOdds(int low, int high) { 4 | return (high - low) / 2 + (low & 1 || high & 1 ? 1 : 0); 5 | } 6 | }; -------------------------------------------------------------------------------- /src/0136-Single-Number/0136.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func singleNumber(nums []int) int { 4 | ans := 0 5 | for _, val := range nums { 6 | ans ^= val 7 | } 8 | return ans 9 | } 10 | -------------------------------------------------------------------------------- /src/0191-Number-of-1-Bits/0191.py: -------------------------------------------------------------------------------- 1 | class Solution(object): 2 | def hammingWeight(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | return bin(n).count("1") -------------------------------------------------------------------------------- /src/0459-Repeated-Substring-Pattern/0459.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean repeatedSubstringPattern(String s) { 3 | return (s + s).substring(1, 2 * s.length() - 1).contains(s); 4 | } 5 | } -------------------------------------------------------------------------------- /src/0836-Rectangle-Overlap/0836.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isRectangleOverlap(self, r1: List[int], r2: List[int]) -> bool: 3 | return r1[0] < r2[2] and r1[1] < r2[3] and r1[2] > r2[0] and r1[3] > r2[1] -------------------------------------------------------------------------------- /src/1037-Valid-Boomerang/1037.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isBoomerang(self, p: List[List[int]]) -> bool: 3 | return (p[0][0] - p[1][0])*(p[0][1] - p[2][1]) != (p[0][0] - p[2][0]) * (p[0][1] - p[1][1]) -------------------------------------------------------------------------------- /src/1085-Sum-of-Digits-in-the-Minimum-Number/1085.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumOfDigits(self, A: List[int]) -> int: 3 | ret = sum([int(i) for i in str(min(A))]) 4 | return 0 if ret & 1 else 1 -------------------------------------------------------------------------------- /src/1108-Defanging-an-IP-Address/1108.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string defangIPaddr(string address) 5 | { 6 | return regex_replace(address, regex("[.]"), "[.]"); 7 | } 8 | }; -------------------------------------------------------------------------------- /src/1238-Circular-Permutation-in-Binary-Representation/1238.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def circularPermutation(self, n: int, start: int) -> List[int]: 3 | return [start^i^(i >> 1) for i in range(1 << n)] -------------------------------------------------------------------------------- /src/1304-Find-N-Unique-Integers-Sum-up-to-Zero/1304.go: -------------------------------------------------------------------------------- 1 | func sumZero(n int) []int { 2 | res := []int{} 3 | for i := 1 - n; i < n; i += 2 { 4 | res = append(res, i) 5 | } 6 | return res 7 | } -------------------------------------------------------------------------------- /src/1309-Decrypt-String-from-Alphabet-to-Integer-Mapping/1309.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def freqAlphabets(self, s: str) -> str: 3 | return ''.join(chr(int(i[:2]) + 96) for i in re.findall(r'\d\d#|\d', s)) -------------------------------------------------------------------------------- /src/1313-Decompress-Run-Length-Encoded-List/1313.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def decompressRLElist(self, nums: List[int]) -> List[int]: 3 | return [x for a, b in zip(nums[0::2], nums[1::2]) for x in [b] * a] -------------------------------------------------------------------------------- /src/1324-Print-Words-Vertically/1324.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def printVertically(self, s: str) -> List[str]: 3 | return [''.join(a).rstrip() for a in itertools.zip_longest(*s.split(), fillvalue=' ')] -------------------------------------------------------------------------------- /src/1331-Rank-Transform-of-an-Array/1331.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arrayRankTransform(self, arr: List[int]) -> List[int]: 3 | return map({a: i + 1 for i, a in enumerate(sorted(set(arr)))}.get, arr) -------------------------------------------------------------------------------- /src/1812-Determine-Color-of-a-Chessboard-Square/1812.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool squareIsWhite(string coordinates) { 4 | return (coordinates[0] - 'a' + coordinates[1] - '1') & 1; 5 | } 6 | }; -------------------------------------------------------------------------------- /src/0191-Number-of-1-Bits/0191.go: -------------------------------------------------------------------------------- 1 | package numberof1bits 2 | 3 | func hammingWeight(num uint32) int { 4 | ans := 0 5 | for num != 0 { 6 | ans++ 7 | num &= (num - 1) 8 | } 9 | return ans 10 | } 11 | -------------------------------------------------------------------------------- /src/0326-Power-of-Three/0326.go: -------------------------------------------------------------------------------- 1 | package powerofthree 2 | 3 | func isPowerOfThree(n int) bool { 4 | if n < 1 { 5 | return false 6 | } 7 | for n%3 == 0 { 8 | n /= 3 9 | } 10 | return n == 1 11 | } 12 | -------------------------------------------------------------------------------- /src/0836-Rectangle-Overlap/0836.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean isRectangleOverlap(int[] r1, int[] r2) { 3 | return r1[0] < r2[2] && r1[1] < r2[3] && r1[2] > r2[0] && r1[3] > r2[1]; 4 | } 5 | } -------------------------------------------------------------------------------- /src/1207-Unique-Number-of-Occurrences/1207.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def uniqueOccurrences(self, arr: List[int]) -> bool: 3 | cnt = collections.Counter(arr) 4 | return len(cnt) == len(set(cnt.values())) -------------------------------------------------------------------------------- /src/1250-Check-If-It-Is-a-Good-Array/1250.py: -------------------------------------------------------------------------------- 1 | from math import gcd 2 | from functools import reduce 3 | class Solution: 4 | def isGoodArray(self, nums: List[int]) -> bool: 5 | return reduce(gcd, nums) == 1 -------------------------------------------------------------------------------- /src/1344-Angle-Between-Hands-of-a-Clock/1344.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def angleClock(self, hour: int, minutes: int) -> float: 3 | t = abs((hour*30 + minutes/2) - minutes*6) 4 | return min(t, 360 - t) -------------------------------------------------------------------------------- /src/0009-Palindrome-Number/0009.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, x): 3 | """ 4 | :type x: int 5 | :rtype: bool 6 | """ 7 | x = str(x) 8 | return x == x[::-1] -------------------------------------------------------------------------------- /src/0058-Length-of-Last-Word/0058.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def lengthOfLastWord(self, s): 3 | """ 4 | :type s: str 5 | :rtype: int 6 | """ 7 | return len(s.strip().split(' ')[-1]) -------------------------------------------------------------------------------- /src/0263-Ugly-Number/0263.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isUgly(self, num: int) -> bool: 3 | for p in [2, 3, 5]: 4 | while num and num%p == 0: 5 | num //= p 6 | return num == 1 -------------------------------------------------------------------------------- /src/0459-Repeated-Substring-Pattern/0459.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool repeatedSubstringPattern(string s) 5 | { 6 | return (s + s).substr(1, s.size() * 2 - 2).find(s) != -1; 7 | } 8 | }; -------------------------------------------------------------------------------- /src/0462-Minimum-Moves-to-Equal-Array-Elements-II/0462.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minMoves2(self, nums: List[int]) -> int: 3 | nums.sort() 4 | return sum(nums[~i] - nums[i] for i in range(len(nums) // 2)) -------------------------------------------------------------------------------- /src/0891-Sum-of-Subsequence-Widths/0891.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sumSubseqWidths(self, A: List[int]) -> int: 3 | return sum(((1 << i) - (1 << len(A) - i - 1)) * a for i, a in enumerate(sorted(A))) % (10**9 + 7) -------------------------------------------------------------------------------- /src/1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/1111.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxDepthAfterSplit(self, seq: str) -> List[int]: 3 | return [i & 1 ^ (seq[i] == ')') for i, c in enumerate(seq)] -------------------------------------------------------------------------------- /src/1217-Play-with-Chips/1217.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minCostToMoveChips(self, chips: List[int]) -> int: 3 | res = [0, 0] 4 | for c in chips: 5 | res[c % 2] += 1 6 | return min(res) -------------------------------------------------------------------------------- /src/1256-Encode-Number/1256.go: -------------------------------------------------------------------------------- 1 | func encode(num int) string { 2 | res := "" 3 | num++ 4 | for num > 0 { 5 | res = strconv.Itoa(num & 1) + res 6 | num >>= 1 7 | } 8 | return res[1:] 9 | } -------------------------------------------------------------------------------- /src/1317-Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/1317.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getNoZeroIntegers(self, n: int) -> List[int]: 3 | return next([a, n-a] for a in range(1, n) if '0' not in f'{a}{n-a}') -------------------------------------------------------------------------------- /src/1374-Generate-a-String-With-Characters-That-Have-Odd-Counts/1374.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public String generateTheString(int n) { 3 | return "a".repeat(n - 1) + (n % 2 == 1 ? "a" : "b"); 4 | } 5 | } -------------------------------------------------------------------------------- /src/1009-Complement-of-Base-10-Integer/1009.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def bitwiseComplement(self, N: int) -> int: 3 | X = 1 4 | while X < N: 5 | X = (X << 1) + 1 6 | 7 | return N ^ X -------------------------------------------------------------------------------- /src/1121-Divide-Array-Into-Increasing-Sequences/1121.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canDivideIntoSubsequences(self, nums: List[int], K: int) -> bool: 3 | return len(nums) >= K * max(collections.Counter(nums).values()) -------------------------------------------------------------------------------- /src/1238-Circular-Permutation-in-Binary-Representation/1238.js: -------------------------------------------------------------------------------- 1 | var circularPermutation = function(n, start) { 2 | let res = []; 3 | for (let i = 0; i < (1 << n); i++) res.push(start^i^(i >> 1)); 4 | return res; 5 | }; -------------------------------------------------------------------------------- /src/1332-Remove-Palindromic-Subsequences/1332.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int removePalindromeSub(string s) 5 | { 6 | return 2 - (s == string(s.rbegin(), s.rend())) - s.empty(); 7 | } 8 | }; -------------------------------------------------------------------------------- /src/1342-Number-of-Steps-to-Reduce-a-Number-to-Zero/1342.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numberOfSteps (int num) { 3 | return num == 0 ? 0 : 1 + numberOfSteps(num % 2 == 1 ? num - 1 : num >> 1); 4 | } 5 | } -------------------------------------------------------------------------------- /src/1342-Number-of-Steps-to-Reduce-a-Number-to-Zero/1342.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfSteps (self, num: int) -> int: 3 | return 0 if not num else 1 + self.numberOfSteps(num >> 1 if num % 2 == 0 else num - 1) -------------------------------------------------------------------------------- /src/0836-Rectangle-Overlap/0836.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool isRectangleOverlap(vector& r1, vector& r2) { 4 | return r1[0] < r2[2] && r1[1] < r2[3] && r1[2] > r2[0] && r1[3] > r2[1]; 5 | } 6 | }; -------------------------------------------------------------------------------- /src/0977-Squares-of-a-Sorted-Array/0977.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def sortedSquares(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: List[int] 6 | """ 7 | return sorted([i**2 for i in A]) -------------------------------------------------------------------------------- /src/0991-Broken-Calculator/0991.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def brokenCalc(self, X: 'int', Y: 'int') -> 'int': 3 | if X >= Y: 4 | return X - Y 5 | return 1 + self.brokenCalc(X, Y + 1 if Y % 2 else Y // 2) -------------------------------------------------------------------------------- /src/1150-Check-If-a-Number-Is-Majority-Element-in-a-Sorted-Array/1150.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isMajorityElement(self, nums: List[int], target: int) -> bool: 3 | return collections.Counter(nums)[target] > len(nums)/2 -------------------------------------------------------------------------------- /src/1154-Day-of-the-Year/1154.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | class Solution: 3 | def dayOfYear(self, date: str) -> int: 4 | Y, M, D = list(map(int, date.split("-"))) 5 | return int(datetime.date(Y, M, D).strftime("%j")) -------------------------------------------------------------------------------- /src/1213-Intersection-of-Three-Sorted-Arrays/1213.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def arraysIntersection(self, arr1: List[int], arr2: List[int], arr3: List[int]) -> List[int]: 3 | return sorted(set(arr1) & set(arr2) & set(arr3)) -------------------------------------------------------------------------------- /src/1232-Check-If-It-Is-a-Straight-Line/1232.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkStraightLine(self, c: List[List[int]]) -> bool: 3 | return all((x - c[0][0]) * (y - c[1][1]) == (x - c[1][0]) * (y - c[0][1]) for x, y in c[2:]) -------------------------------------------------------------------------------- /src/1276-Number-of-Burgers-with-No-Waste-of-Ingredients/1276.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numOfBurgers(self, t: int, c: int) -> List[int]: 3 | return [t // 2 - c, c * 2 - t // 2] if t % 2 == 0 and c * 2 <= t <= c * 4 else [] -------------------------------------------------------------------------------- /src/1332-Remove-Palindromic-Subsequences/1332.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removePalindromeSub(String s) { 3 | return s.isEmpty() ? 0 : (s.equals(new StringBuilder(s).reverse().toString()) ? 1 : 2); 4 | } 5 | } -------------------------------------------------------------------------------- /src/0268-Missing-Number/0268.go: -------------------------------------------------------------------------------- 1 | package missingnumber 2 | 3 | func missingNumber(nums []int) int { 4 | ans := 0 5 | for idx, val := range nums { 6 | ans ^= idx 7 | ans ^= val 8 | } 9 | return ans ^ len(nums) 10 | } 11 | -------------------------------------------------------------------------------- /src/0319-Bulb-Switcher/0319.cpp: -------------------------------------------------------------------------------- 1 | static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }(); 2 | class Solution 3 | { 4 | public: 5 | int bulbSwitch(int n) 6 | { 7 | return sqrt(n); 8 | } 9 | }; -------------------------------------------------------------------------------- /src/1072-Flip-Columns-For-Maximum-Number-of-Equal-Rows/1072.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxEqualRowsAfterFlips(self, A: List[List[int]]) -> int: 3 | return max(collections.Counter(tuple(x ^ r[0] for x in r) for r in A).values()) -------------------------------------------------------------------------------- /src/1150-Check-If-a-Number-Is-Majority-Element-in-a-Sorted-Array/1150.js: -------------------------------------------------------------------------------- 1 | var isMajorityElement = function(nums, target) { 2 | var cnt = 0; 3 | for (let i of nums) if (target == i) cnt++; 4 | return cnt > nums.length/2; 5 | }; -------------------------------------------------------------------------------- /src/1189-Maximum-Number-of-Balloons/1189.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxNumberOfBalloons(self, text: str) -> int: 3 | cnt = collections.Counter(text) 4 | return min(cnt['b'], cnt['a'], cnt['l']//2, cnt['o']//2, cnt['n']) -------------------------------------------------------------------------------- /src/1228-Missing-Number-In-Arithmetic-Progression/1228.go: -------------------------------------------------------------------------------- 1 | func missingNumber(arr []int) int { 2 | A := 0 3 | for _, v := range arr{ 4 | A += v 5 | } 6 | return (arr[0] + arr[len(arr)-1])*(len(arr) + 1)/2 - A 7 | } -------------------------------------------------------------------------------- /src/1228-Missing-Number-In-Arithmetic-Progression/1228.js: -------------------------------------------------------------------------------- 1 | var missingNumber = function(arr) { 2 | let A = 0; 3 | for (let a of arr) A += a; 4 | return Math.floor((arr[0] + arr[arr.length - 1]) * (arr.length + 1)/2) - A; 5 | }; -------------------------------------------------------------------------------- /src/1250-Check-If-It-Is-a-Good-Array/1250.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool isGoodArray(vector& nums) 5 | { 6 | return accumulate(nums.begin(), nums.end(), nums[0], gcd) == 1; 7 | } 8 | }; -------------------------------------------------------------------------------- /src/1256-Encode-Number/1256.js: -------------------------------------------------------------------------------- 1 | var encode = function(num) { 2 | let res = ""; 3 | num++; 4 | while (num) { 5 | res = (num & 1 ? "1" : "0") + res; 6 | num >>= 1; 7 | } 8 | return res.substring(1); 9 | }; -------------------------------------------------------------------------------- /src/1374-Generate-a-String-With-Characters-That-Have-Odd-Counts/1374.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string generateTheString(int n) 5 | { 6 | return string(n - 1, 'a') + (n % 2 ? 'a' : 'b'); 7 | } 8 | }; -------------------------------------------------------------------------------- /src/0231-Power-of-Two/0231.go: -------------------------------------------------------------------------------- 1 | package poweroftwo 2 | 3 | func isPowerOfTwo(n int) bool { 4 | if n <= 0 { 5 | return false 6 | } 7 | val := n & (n - 1) 8 | if val == 0 { 9 | return true 10 | } 11 | return false 12 | } 13 | -------------------------------------------------------------------------------- /src/1025-Divisor-Game/1025.cpp: -------------------------------------------------------------------------------- 1 | static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }(); 2 | class Solution 3 | { 4 | public: 5 | bool divisorGame(int N) 6 | { 7 | return (N & 1) == 0; 8 | } 9 | }; -------------------------------------------------------------------------------- /src/1342-Number-of-Steps-to-Reduce-a-Number-to-Zero/1342.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int numberOfSteps (int num) 5 | { 6 | return num == 0 ? 0 : 1 + numberOfSteps(num & 1 ? num - 1 : num >> 1); 7 | } 8 | }; -------------------------------------------------------------------------------- /src/1347-Minimum-Number-of-Steps-to-Make-Two-Strings-Anagram/1347.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSteps(self, s: str, t: str) -> int: 3 | cnt1, cnt2 = map(collections.Counter, (s, t)) 4 | return sum((cnt1 - cnt2).values()) -------------------------------------------------------------------------------- /src/0172-Factorial-Trailing-Zeroes/0172.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def trailingZeroes(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | return 0 if n == 0 else n // 5 + self.trailingZeroes(n // 5) -------------------------------------------------------------------------------- /src/1018-Binary-Prefix-Divisible-By-5/1018.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def prefixesDivBy5(self, A: List[int]) -> List[bool]: 3 | for i in range(1, len(A)): 4 | A[i] += A[i - 1] * 2 % 5 5 | return [a % 5 == 0 for a in A] -------------------------------------------------------------------------------- /src/1266-Minimum-Time-Visiting-All-Points/1266.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minTimeToVisitAllPoints(self, p: List[List[int]]) -> int: 3 | return sum(max(abs(p[i][0] - p[i - 1][0]), abs(p[i][1] - p[i - 1][1])) for i in range(1, len(p))) -------------------------------------------------------------------------------- /src/1276-Number-of-Burgers-with-No-Waste-of-Ingredients/1276.go: -------------------------------------------------------------------------------- 1 | func numOfBurgers(t int, c int) []int { 2 | if t % 2 == 0 && c * 2 <= t && t <= c * 4 { 3 | return []int{t / 2 - c, c * 2 - t / 2} 4 | } 5 | return []int{} 6 | } -------------------------------------------------------------------------------- /src/0371-Sum-of-Two-Integers/0371.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getSum(self, a: int, b: int) -> int: 3 | mask = 0xffffffff 4 | while b & mask: 5 | a, b = a ^ b, (a & b) << 1 6 | 7 | return a & mask if b > mask else a -------------------------------------------------------------------------------- /src/0409-Longest-Palindrome/0409.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestPalindrome(self, s: str) -> int: 3 | cnt = collections.Counter(s) 4 | res = sum(i // 2 * 2 for i in cnt.values()) 5 | return res + 1 if res < len(s) else res -------------------------------------------------------------------------------- /src/1002-Find-Common-Characters/1002.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | class Solution: 3 | def commonChars(self, A: List[str]) -> List[str]: 4 | return list(reduce(collections.Counter.__and__, map(collections.Counter, A)).elements()) -------------------------------------------------------------------------------- /src/1071-Greatest-Common-Divisor-of-Strings/1071.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string gcdOfStrings(string s1, string s2) 5 | { 6 | return (s1 + s2 == s2 + s1) ? s1.substr(0, gcd(size(s1), size(s2))) : ""; 7 | } 8 | }; -------------------------------------------------------------------------------- /src/1118-Number-of-Days-in-a-Month/1118.go: -------------------------------------------------------------------------------- 1 | func numberOfDays(Y int, M int) int { 2 | D := []int{31,28,31,30,31,30,31,31,30,31,30,31} 3 | if Y % 400 == 0 || Y % 4 == 0 && Y % 100 != 0 { 4 | D[1]++; 5 | } 6 | return D[M - 1]; 7 | } -------------------------------------------------------------------------------- /src/1128-Number-of-Equivalent-Domino-Pairs/1128.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numEquivDominoPairs(self, dominoes: List[List[int]]) -> int: 3 | return sum(v*(v - 1)//2 for v in collections.Counter(tuple(sorted(d)) for d in dominoes).values()) -------------------------------------------------------------------------------- /src/1134-Armstrong-Number/1134.js: -------------------------------------------------------------------------------- 1 | var isArmstrong = function(N) { 2 | var str = N.toString(); 3 | var res = 0, n = str.length; 4 | for (let c of str) { 5 | res += Math.pow(parseInt(c), n); 6 | } 7 | return res == N; 8 | }; -------------------------------------------------------------------------------- /src/1344-Angle-Between-Hands-of-a-Clock/1344.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public double angleClock(int hour, int minutes) { 3 | double t = Math.abs((hour*30 + minutes/2.0) - minutes*6); 4 | return Math.min(t, 360 - t); 5 | } 6 | } -------------------------------------------------------------------------------- /src/1431-Kids-With-the-Greatest-Number-of-Candies/1431.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kidsWithCandies(self, candies: List[int], extraCandies: int) -> List[bool]: 3 | m = max(candies) 4 | return [i + extraCandies >= m for i in candies] -------------------------------------------------------------------------------- /src/1436-Destination-City/1436.js: -------------------------------------------------------------------------------- 1 | var destCity = function(paths) { 2 | let s = {}; 3 | for (let path of paths) s[path[0]] = 1; 4 | for (let path of paths) { 5 | if (!(path[1] in s)) return path[1] 6 | } 7 | return ""; 8 | }; -------------------------------------------------------------------------------- /src/1832-Check-if-the-Sentence-is-Pangram/1832.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkIfPangram(self, sentence: str) -> bool: 3 | if set(sentence) == set('abcdefghijklmnopqrstuvwxyz'): return True 4 | else: return False 5 | 6 | -------------------------------------------------------------------------------- /src/0215-Kth-Largest-Element-in-an-Array/0215.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findKthLargest(self, nums, k): 3 | """ 4 | :type nums: List[int] 5 | :type k: int 6 | :rtype: int 7 | """ 8 | return sorted(nums)[-k] -------------------------------------------------------------------------------- /src/0492-Construct-the-Rectangle/0492.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector constructRectangle(int area) { 4 | int w = sqrt(area); 5 | while (area % w) { 6 | --w; 7 | } 8 | return {area / w, w}; 9 | } 10 | }; -------------------------------------------------------------------------------- /src/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/1290.go: -------------------------------------------------------------------------------- 1 | func getDecimalValue(head *ListNode) int { 2 | res := 0 3 | for head != nil { 4 | res = (res << 1) | head.Val 5 | head = head.Next 6 | } 7 | return res 8 | } -------------------------------------------------------------------------------- /src/1304-Find-N-Unique-Integers-Sum-up-to-Zero/1304.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] sumZero(int n) { 3 | int[] res = new int[n]; 4 | for (int i = 1 - n, j = 0; i < n; i += 2, j++) res[j] = i; 5 | return res; 6 | } 7 | } -------------------------------------------------------------------------------- /src/1389-Create-Target-Array-in-the-Given-Order/1389.js: -------------------------------------------------------------------------------- 1 | var createTargetArray = function(nums, index) { 2 | let res = []; 3 | for (let i = 0; i < nums.length; i++) { 4 | res.splice(index[i], 0, nums[i]); 5 | } 6 | return res; 7 | }; -------------------------------------------------------------------------------- /src/0453-Minimum-Moves-to-Equal-Array-Elements/0453.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minMoves(self, nums: List[int]) -> int: 3 | min_num = min(nums) 4 | res = 0 5 | for num in nums: 6 | res += num - min_num 7 | return res -------------------------------------------------------------------------------- /src/1122-Relative-Sort-Array/1122.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def relativeSortArray(self, arr1: List[int], arr2: List[int]) -> List[int]: 3 | order = {v: i for i, v in enumerate(arr2)} 4 | return sorted(arr1 , key=lambda a: order.get(a, 1000 + a)) -------------------------------------------------------------------------------- /src/1221-Split-a-String-in-Balanced-Strings/1221.js: -------------------------------------------------------------------------------- 1 | var balancedStringSplit = function(s) { 2 | var res = L = 0; 3 | for (let c of s) { 4 | if (c == 'L') L++; 5 | else L--; 6 | if (L == 0) res++; 7 | } 8 | return res; 9 | }; -------------------------------------------------------------------------------- /src/1344-Angle-Between-Hands-of-a-Clock/1344.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | double angleClock(int hour, int minutes) 5 | { 6 | double t = abs((hour*30 + minutes/2.0) - minutes*6); 7 | return min(t, 360 - t); 8 | } 9 | }; -------------------------------------------------------------------------------- /src/1359-Count-All-Valid-Pickup-and-Delivery-Options/1359.js: -------------------------------------------------------------------------------- 1 | var countOrders = function(n) { 2 | let res = 1, mod = 1000000007; 3 | 4 | for (let i = 1; i <= n; i++) { 5 | res = res * (2 * i - 1) * i % mod; 6 | } 7 | return res; 8 | }; -------------------------------------------------------------------------------- /src/1071-Greatest-Common-Divisor-of-Strings/1071.js: -------------------------------------------------------------------------------- 1 | var gcdOfStrings = function(s1, s2) { 2 | let gcd = function(a, b) { 3 | return b ? gcd(b, a % b) : a; 4 | } 5 | return s1 + s2 == s2 + s1 ? s1.substring(0, gcd(s1.length, s2.length)) : ""; 6 | }; -------------------------------------------------------------------------------- /src/1160-Find-Words-That-Can-Be-Formed-by-Characters/1160.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countCharacters(self, words: List[str], chars: str) -> int: 3 | return len(''.join(filter(lambda x : len(collections.Counter(x) - collections.Counter(chars)) == 0, words))) -------------------------------------------------------------------------------- /src/1184-Distance-Between-Bus-Stops/1184.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def distanceBetweenBusStops(self, dis: List[int], l: int, r: int) -> int: 3 | if l > r: 4 | l, r = r, l 5 | k, v = sum(dis[l:r]), sum(dis) 6 | return min(k, v - k) -------------------------------------------------------------------------------- /src/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281.py: -------------------------------------------------------------------------------- 1 | from functools import reduce 2 | class Solution: 3 | def subtractProductAndSum(self, n: int) -> int: 4 | A = list(map(int, str(n))) 5 | return reduce(operator.mul, A) - sum(A) -------------------------------------------------------------------------------- /src/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/1290.js: -------------------------------------------------------------------------------- 1 | var getDecimalValue = function(head) { 2 | let res = 0; 3 | while (head != null) { 4 | res = (res << 1) | head.val; 5 | head = head.next; 6 | } 7 | return res; 8 | }; -------------------------------------------------------------------------------- /src/0070-Climbing-Stairs/0070.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int climbStairs(int n) { 4 | long long a = 1, b = 1, c; 5 | for (int i = 0; i < n; i++) { 6 | c = a; 7 | a = b; 8 | b += c; 9 | } 10 | return a; 11 | } 12 | }; -------------------------------------------------------------------------------- /src/1217-Play-with-Chips/1217.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int minCostToMoveChips(vector& chips) 5 | { 6 | int res[2] = {0}; 7 | for (int c : chips) res[c%2]++; 8 | return min(res[0], res[1]); 9 | } 10 | }; -------------------------------------------------------------------------------- /src/1375-Bulb-Switcher-III/1375.js: -------------------------------------------------------------------------------- 1 | var numTimesAllBlue = function(light) { 2 | let res = 0, maxv = 0; 3 | for (let i = 0; i < light.length; i++) { 4 | maxv = Math.max(maxv, light[i]); 5 | if (maxv == i + 1) res++; 6 | } 7 | return res; 8 | }; -------------------------------------------------------------------------------- /src/1389-Create-Target-Array-in-the-Given-Order/1389.go: -------------------------------------------------------------------------------- 1 | func createTargetArray(nums []int, index []int) []int { 2 | res := make([]int, len(nums)) 3 | for i, v := range index { 4 | copy(res[v + 1:i + 1], res[v:i]) 5 | res[v] = nums[i] 6 | } 7 | return res 8 | } -------------------------------------------------------------------------------- /src/0070-Climbing-Stairs/0070.go: -------------------------------------------------------------------------------- 1 | package climbingstairs 2 | 3 | func climbStairs(n int) int { 4 | if n < 4 { 5 | return n 6 | } 7 | a, b := 2, 3 8 | for i := 4; i < n+1; i++ { 9 | temp := b 10 | b = a + b 11 | a = temp 12 | } 13 | return b 14 | } 15 | -------------------------------------------------------------------------------- /src/0201-Bitwise-AND-of-Numbers-Range/0201.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rangeBitwiseAnd(self, m, n): 3 | """ 4 | :type m: int 5 | :type n: int 6 | :rtype: int 7 | """ 8 | b = (m ^ n).bit_length() 9 | return m >> b << b -------------------------------------------------------------------------------- /src/0371-Sum-of-Two-Integers/0371.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn get_sum(mut a: i32, mut b: i32) -> i32 { 3 | while b != 0 { 4 | let c = (a & b) << 1; 5 | a = a ^ b; 6 | b = c; 7 | } 8 | a 9 | } 10 | } -------------------------------------------------------------------------------- /src/0650-2-Keys-Keyboard/0650.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSteps(self, n: int) -> int: 3 | i, res = 2, 0 4 | while n > 1: 5 | while n % i == 0: 6 | res += i 7 | n //= i 8 | i += 1 9 | return res -------------------------------------------------------------------------------- /src/1189-Maximum-Number-of-Balloons/1189.js: -------------------------------------------------------------------------------- 1 | var maxNumberOfBalloons = function(text) { 2 | var cnt = {}; 3 | for (let c of text) { 4 | cnt[c] = cnt[c] || 0; 5 | cnt[c]++; 6 | } 7 | return Math.min(cnt.b, cnt.a, cnt.l/2, cnt.o/2, cnt.n) | 0; 8 | }; -------------------------------------------------------------------------------- /src/1198-Find-Smallest-Common-Element-in-All-Rows/1198.py: -------------------------------------------------------------------------------- 1 | from functools import reduce 2 | class Solution: 3 | def smallestCommonElement(self, mat: List[List[int]]) -> int: 4 | res = reduce(operator. __and__, map(set, mat)) 5 | return min(res) if res else -1 -------------------------------------------------------------------------------- /src/1276-Number-of-Burgers-with-No-Waste-of-Ingredients/1276.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public List numOfBurgers(int t, int c) { 3 | return t % 2 == 0 && c * 2 <= t && t <= c * 4 ? Arrays.asList(t / 2 - c, c * 2 - t / 2) : new ArrayList(); 4 | } 5 | } -------------------------------------------------------------------------------- /src/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281.go: -------------------------------------------------------------------------------- 1 | func subtractProductAndSum(n int) int { 2 | sum, product := 0, 1 3 | for n > 0 { 4 | sum += n % 10 5 | product *= n % 10 6 | n /= 10 7 | } 8 | return product - sum 9 | } -------------------------------------------------------------------------------- /src/1328-Break-a-Palindrome/1328.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def breakPalindrome(self, p: str) -> str: 3 | for i in range(len(p) // 2): 4 | if p[i] != 'a': 5 | return p[:i] + 'a' + p[i + 1:] 6 | return p[:-1] + 'b' if p[:-1] else '' -------------------------------------------------------------------------------- /src/1356-Sort-Integers-by-The-Number-of-1-Bits/1356.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] sortByBits(int[] arr) { 3 | return Arrays.stream(arr).boxed().sorted(Comparator.comparingInt(i -> Integer.bitCount(i) * 10000 + i)).mapToInt(i -> i).toArray(); 4 | } 5 | } -------------------------------------------------------------------------------- /src/1394-Find-Lucky-Integer-in-an-Array/1394.js: -------------------------------------------------------------------------------- 1 | var findLucky = function(arr) { 2 | let cnt = new Array(501).fill(0); 3 | for (let i of arr) cnt[i]++; 4 | 5 | for (let i = 500; i > 0; i--) { 6 | if (cnt[i] == i) return i; 7 | } 8 | return -1; 9 | }; -------------------------------------------------------------------------------- /src/1400-Construct-K-Palindrome-Strings/1400.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canConstruct(string s, int k) { 4 | bitset<26> odds; 5 | for (char& c : s) odds.flip(c - 'a'); 6 | return odds.count() <= k && k <= s.length(); 7 | } 8 | }; -------------------------------------------------------------------------------- /src/0190-Reverse-Bits/0190.go: -------------------------------------------------------------------------------- 1 | package reversebits 2 | 3 | func reverseBits(num uint32) uint32 { 4 | var ans uint32 = 0 5 | for i := 0; i < 32; i++ { 6 | val := num & (1 << i) 7 | if val != 0 { 8 | ans |= (1 << (31 - i)) 9 | } 10 | } 11 | return ans 12 | } 13 | -------------------------------------------------------------------------------- /src/0190-Reverse-Bits/0190.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | # @param n, an integer 3 | # @return an integer 4 | def reverseBits(self, n): 5 | res = 0 6 | for _ in range(32): 7 | res = (res << 1) + (n & 1) 8 | n >>= 1 9 | return res -------------------------------------------------------------------------------- /src/0365-Water-and-Jug-Problem/0365.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool canMeasureWater(int x, int y, int z) { 4 | if (x + y < z) return false; 5 | if (x == 0 || y == 0) return z == 0 || x + y == z; 6 | return z % gcd(x, y) == 0; 7 | } 8 | }; -------------------------------------------------------------------------------- /src/0371-Sum-of-Two-Integers/0371.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int getSum(int a, int b) 5 | { 6 | if (!b) return a; 7 | long long sum = a ^ b, carry = (a & b & 0xffffffff) << 1; 8 | return getSum(sum, carry); 9 | } 10 | }; -------------------------------------------------------------------------------- /src/1118-Number-of-Days-in-a-Month/1118.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfDays(self, Y: int, M: int) -> int: 3 | D = [31,28,31,30,31,30,31,31,30,31,30,31] 4 | if Y % 400 == 0 or Y % 4 == 0 and Y % 100 != 0: 5 | D[1] += 1 6 | return D[M - 1] -------------------------------------------------------------------------------- /src/1185-Day-of-the-Week/1185.py: -------------------------------------------------------------------------------- 1 | import datetime 2 | class Solution: 3 | def dayOfTheWeek(self, day: int, month: int, year: int) -> str: 4 | return ("Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday")[datetime.date(year, month, day).weekday()] -------------------------------------------------------------------------------- /src/1238-Circular-Permutation-in-Binary-Representation/1238.go: -------------------------------------------------------------------------------- 1 | func circularPermutation(n int, start int) []int { 2 | res, e := make([]int, 0), int(1 << uint(n)) 3 | for i := 0; i < e; i++ { 4 | res = append(res, start^i^(i >> 1)) 5 | } 6 | return res 7 | } -------------------------------------------------------------------------------- /src/1250-Check-If-It-Is-a-Good-Array/1250.go: -------------------------------------------------------------------------------- 1 | func isGoodArray(nums []int) bool { 2 | x, y := nums[0], 0 3 | for _, a := range nums { 4 | for a > 0 { 5 | y, x = x % a, a 6 | a = y 7 | } 8 | } 9 | return x == 1 10 | } -------------------------------------------------------------------------------- /src/1259-Handshakes-That-Don't-Cross/1259.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numberOfWays(self, n: int) -> int: 3 | res = 1 4 | for i in range(1, n // 2 + 1): 5 | res *= n - i + 1 6 | res //= i 7 | return res // (n // 2 + 1) % (10**9 + 7) -------------------------------------------------------------------------------- /src/1267-Count-Servers-that-Communicate/1267.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countServers(self, A: List[List[int]]) -> int: 3 | r, c = list(map(sum, A)), list(map(sum, zip(*A))) 4 | return sum(r[i] + c[j] > 2 for i in range(len(A)) for j in range(len(A[0])) if A[i][j]) -------------------------------------------------------------------------------- /src/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/1290.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def getDecimalValue(self, head: ListNode) -> int: 3 | res = 0 4 | while head: 5 | res = (res << 1) | head.val 6 | head = head.next 7 | return res -------------------------------------------------------------------------------- /src/1304-Find-N-Unique-Integers-Sum-up-to-Zero/1304.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector sumZero(int n) 5 | { 6 | vector res; 7 | for (int i = 1 - n; i < n; i += 2) res.push_back(i); 8 | return res; 9 | } 10 | }; -------------------------------------------------------------------------------- /src/1310-XOR-Queries-of-a-Subarray/1310.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def xorQueries(self, arr: List[int], queries: List[List[int]]) -> List[int]: 3 | A = [0] 4 | for a in arr: 5 | A.append(A[-1] ^ a) 6 | return [A[l] ^ A[r + 1] for l, r in queries] -------------------------------------------------------------------------------- /src/1375-Bulb-Switcher-III/1375.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numTimesAllBlue(self, light: List[int]) -> int: 3 | res, maxv = 0, 0 4 | for i, v in enumerate(light): 5 | maxv = max(v, maxv) 6 | if maxv == i + 1: res += 1 7 | return res -------------------------------------------------------------------------------- /src/1413-Minimum-Value-to-Get-Positive-Step-by-Step-Sum/1413.js: -------------------------------------------------------------------------------- 1 | var minStartValue = function(nums) { 2 | let pref = 0, min_pref = 0; 3 | for (let i of nums) { 4 | pref += i; 5 | min_pref = Math.min(min_pref, pref); 6 | } 7 | return 1 - min_pref; 8 | }; -------------------------------------------------------------------------------- /src/0171-Excel-Sheet-Column-Number/0171.py: -------------------------------------------------------------------------------- 1 | from functools import reduce 2 | class Solution: 3 | def titleToNumber(self, s): 4 | """ 5 | :type s: str 6 | :rtype: int 7 | """ 8 | return reduce(lambda x, y : x * 26 + y, [ord(c) - 64 for c in s]) -------------------------------------------------------------------------------- /src/0492-Construct-the-Rectangle/0492.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn construct_rectangle(area: i32) -> Vec { 3 | let mut w = (area as f32).sqrt() as i32; 4 | while area % w != 0 { 5 | w -= 1; 6 | } 7 | vec![area / w, w] 8 | } 9 | } -------------------------------------------------------------------------------- /src/1228-Missing-Number-In-Arithmetic-Progression/1228.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int missingNumber(vector& arr) 5 | { 6 | return (arr[0] + arr[arr.size()-1])*(arr.size() + 1)/2 - 7 | accumulate(arr.begin(), arr.end(), 0); 8 | } 9 | }; -------------------------------------------------------------------------------- /src/1266-Minimum-Time-Visiting-All-Points/1266.js: -------------------------------------------------------------------------------- 1 | var minTimeToVisitAllPoints = function(p) { 2 | let res = 0; 3 | for (let i = 1; i < p.length; i++) { 4 | res += Math.max(Math.abs(p[i][0] - p[i-1][0]), Math.abs(p[i][1] - p[i-1][1])); 5 | } 6 | return res; 7 | }; -------------------------------------------------------------------------------- /src/1359-Count-All-Valid-Pickup-and-Delivery-Options/1359.go: -------------------------------------------------------------------------------- 1 | func countOrders(n int) int { 2 | var res, mod int64 3 | res, mod = 1, 1000000007 4 | 5 | for i := 1; i <= n; i++ { 6 | res = res * int64((i * 2 - 1) * i) % mod 7 | } 8 | return int(res) 9 | } -------------------------------------------------------------------------------- /src/1389-Create-Target-Array-in-the-Given-Order/1389.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def createTargetArray(self, nums: List[int], index: List[int]) -> List[int]: 3 | res = [] 4 | for i in range(len(nums)): 5 | res.insert(index[i], nums[i]) 6 | return res -------------------------------------------------------------------------------- /src/1402-Reducing-Dishes/1402.go: -------------------------------------------------------------------------------- 1 | func maxSatisfaction(s []int) int { 2 | sort.Ints(s) 3 | res, total, n := 0, 0, len(s) 4 | 5 | for i := n - 1; i >= 0 && s[i] > -total; i-- { 6 | total += s[i] 7 | res += total 8 | } 9 | return res 10 | } -------------------------------------------------------------------------------- /src/1411-Number-of-Ways-to-Paint-N×3-Grid/1411.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numOfWays(self, n: int) -> int: 3 | aba, abc, mod = 6, 6, 10**9 + 7 4 | for i in range(n - 1): 5 | aba, abc = aba * 3 + abc * 2, aba * 2 + abc * 2 6 | return (aba + abc) % mod -------------------------------------------------------------------------------- /src/1732-Find-the-Highest-Altitude/1732.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int largestAltitude(vector& gain) { 4 | int res = 0, cur = 0; 5 | for (auto& x : gain) { 6 | cur += x; 7 | res = max(res, cur); 8 | } 9 | return res; 10 | } 11 | }; -------------------------------------------------------------------------------- /src/0069-Sqrt(x)/0069.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def mySqrt(self, x): 3 | """ 4 | :type x: int 5 | :rtype: int 6 | """ 7 | res = x 8 | while res**2 > x: 9 | res = (res + x//res)//2 10 | 11 | return res -------------------------------------------------------------------------------- /src/0458-Poor-Pigs/0458.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn poor_pigs(buckets: i32, minutes_to_die: i32, minutes_to_test: i32) -> i32 { 3 | let states = minutes_to_test / minutes_to_die + 1; 4 | ((buckets as f32).log2() / (states as f32).log2()).ceil() as i32 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /src/1047-Remove-All-Adjacent-Duplicates-In-String/1047.js: -------------------------------------------------------------------------------- 1 | var removeDuplicates = function(S) { 2 | let res = []; 3 | for (let c of S) { 4 | if (res.length > 0 && c == res[res.length - 1]) res.pop(); 5 | else res.push(c); 6 | } 7 | return res.join(''); 8 | }; -------------------------------------------------------------------------------- /src/1150-Check-If-a-Number-Is-Majority-Element-in-a-Sorted-Array/1150.go: -------------------------------------------------------------------------------- 1 | func isMajorityElement(nums []int, target int) bool { 2 | cnt := 0 3 | for _, v := range nums { 4 | if target == v { 5 | cnt++ 6 | } 7 | } 8 | return cnt > len(nums)/2 9 | } -------------------------------------------------------------------------------- /src/1276-Number-of-Burgers-with-No-Waste-of-Ingredients/1276.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector numOfBurgers(int t, int c) 5 | { 6 | return t % 2 == 0 && c * 2 <= t && t <= c * 4 ? vector({t / 2 - c, c * 2 - t / 2}) : vector(); 7 | } 8 | }; -------------------------------------------------------------------------------- /src/1302-Deepest-Leaves-Sum/1302.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def deepestLeavesSum(self, root: TreeNode) -> int: 3 | l = [root] 4 | while l: 5 | pre, l = l, [child for p in l for child in [p.left, p.right] if child] 6 | return sum(node.val for node in pre) -------------------------------------------------------------------------------- /src/1402-Reducing-Dishes/1402.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSatisfaction(self, s: List[int]) -> int: 3 | res = total = 0 4 | s.sort() 5 | 6 | while s and s[-1] + total > 0: 7 | total += s.pop() 8 | res += total 9 | return res -------------------------------------------------------------------------------- /src/1780-Check-if-Number-is-a-Sum-of-Powers-of-Three/1780.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkPowersOfThree(int n) { 4 | while (n) { 5 | if (n % 3 == 2) { 6 | return false; 7 | } 8 | n /= 3; 9 | } 10 | return true; 11 | } 12 | }; -------------------------------------------------------------------------------- /src/2185-Counting-Words-With-a-Given-Prefix/2185.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int prefixCount(vector& words, string pref) { 4 | return count_if(words.begin(), words.end(), [&](auto& s) { 5 | return s.compare(0, pref.size(), pref) == 0; 6 | }); 7 | } 8 | }; -------------------------------------------------------------------------------- /src/0365-Water-and-Jug-Problem/0365.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def canMeasureWater(self, x: int, y: int, z: int) -> bool: 3 | if x + y < z: 4 | return False 5 | if x == 0 or y == 0: 6 | return z == 0 or x + y == z 7 | return z % math.gcd(x, y) == 0 -------------------------------------------------------------------------------- /src/0989-Add-to-Array-Form-of-Integer/0989.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def addToArrayForm(self, A: 'List[int]', K: 'int') -> 'List[int]': 3 | for i in range(len(A))[::-1]: 4 | A[i], K = (A[i] + K) % 10, (A[i] + K) // 10 5 | return [int(i) for i in str(K)] + A if K else A -------------------------------------------------------------------------------- /src/1262-Greatest-Sum-Divisible-by-Three/1262.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxSumDivThree(self, nums: List[int]) -> int: 3 | dp = [0, 0, 0] 4 | for a in nums: 5 | for i in dp[:]: 6 | dp[(i + a) % 3] = max(dp[(i + a) % 3], i + a) 7 | return dp[0] -------------------------------------------------------------------------------- /src/1288-Remove-Covered-Intervals/1288.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeCoveredIntervals(self, A: List[List[int]]) -> int: 3 | res = r = 0 4 | for _, j in sorted(A, key=lambda a: [a[0], -a[1]]): 5 | res += j > r 6 | r = max(r, j) 7 | return res -------------------------------------------------------------------------------- /src/0027-Remove-Element/0027.go: -------------------------------------------------------------------------------- 1 | package removeelement 2 | 3 | func removeElement(nums []int, val int) int { 4 | for i := 0; i < len(nums); { 5 | if nums[i] == val { 6 | nums = append(nums[:i], nums[i+1:]...) 7 | } else { 8 | i++ 9 | } 10 | } 11 | return len(nums) 12 | } 13 | -------------------------------------------------------------------------------- /src/0223-Rectangle-Area/0223.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def computeArea(self, A: int, B: int, C: int, D: int, E: int, F: int, G: int, H: int) -> int: 3 | x = min(C, G) - max(A, E) 4 | y = min(D, H) - max(B, F) 5 | return (C - A) * (D - B) + (G - E) * (H - F) - max(x, 0)*max(y, 0) -------------------------------------------------------------------------------- /src/0344-Reverse-String/0344.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def reverseString(self, s): 3 | """ 4 | :type s: str 5 | :rtype: str 6 | """ 7 | return s[::-1] 8 | 9 | if __name__ == "__main__": 10 | s = "hello" 11 | print(Solution().reverseString(s)) -------------------------------------------------------------------------------- /src/0458-Poor-Pigs/0458.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def poorPigs(self, buckets: int, minutesToDie: int, minutesToTest: int) -> int: 3 | b = minutesToTest // minutesToDie + 1 4 | k, n = 0, 1 5 | while n < buckets: 6 | k += 1 7 | n *= b 8 | return k -------------------------------------------------------------------------------- /src/1128-Number-of-Equivalent-Domino-Pairs/1128.js: -------------------------------------------------------------------------------- 1 | var numEquivDominoPairs = function(dominoes) { 2 | var cnt = Array(100), res = 0; 3 | cnt.fill(0); 4 | for (let d of dominoes) { 5 | res += cnt[Math.min(d[0], d[1])*10 + Math.max(d[0], d[1])]++; 6 | } 7 | return res; 8 | }; -------------------------------------------------------------------------------- /src/1197-Minimum-Knight-Moves/1197.js: -------------------------------------------------------------------------------- 1 | var minKnightMoves = function(x, y) { 2 | x = Math.abs(x), y = Math.abs(y); 3 | if (x + y == 1) return 3; 4 | var res = Math.max(Math.max(Math.ceil(x/2), Math.ceil(y/2)), Math.ceil((x + y)/3)); 5 | res += (res ^ x ^ y) & 1; 6 | return res; 7 | }; -------------------------------------------------------------------------------- /src/1250-Check-If-It-Is-a-Good-Array/1250.js: -------------------------------------------------------------------------------- 1 | var isGoodArray = function(nums) { 2 | let x = nums[0], y; 3 | for (let a of nums) { 4 | while (a > 0) { 5 | y = x % a; 6 | x = a; 7 | a = y; 8 | } 9 | } 10 | return x == 1; 11 | }; -------------------------------------------------------------------------------- /src/1313-Decompress-Run-Length-Encoded-List/1313.js: -------------------------------------------------------------------------------- 1 | var decompressRLElist = function(nums) { 2 | let res = []; 3 | for (let i = 0; i < nums.length; i += 2) { 4 | for (let j = 0; j < nums[i]; j++) { 5 | res.push(nums[i + 1]); 6 | } 7 | } 8 | return res; 9 | }; -------------------------------------------------------------------------------- /src/1346-Check-If-N-and-Its-Double-Exist/1346.js: -------------------------------------------------------------------------------- 1 | var checkIfExist = function(arr) { 2 | let seen = new Map(); 3 | for (let i of arr) { 4 | if (seen.has(i * 2) || (i % 2 == 0 && seen.has(Math.floor(i / 2)))) return true; 5 | seen.set(i, 1); 6 | } 7 | return false; 8 | }; -------------------------------------------------------------------------------- /src/1359-Count-All-Valid-Pickup-and-Delivery-Options/1359.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int countOrders(int n) { 3 | long res = 1, mod = (long)1e9 + 7; 4 | for (int i = 1; i <= n; ++i) 5 | res = res * (i * 2 - 1) * i % mod; 6 | return (int)res; 7 | } 8 | } -------------------------------------------------------------------------------- /src/1413-Minimum-Value-to-Get-Positive-Step-by-Step-Sum/1413.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minStartValue(self, nums: List[int]) -> int: 3 | pref, min_pref = 0, 0 4 | for i in nums: 5 | pref += i 6 | min_pref = min(min_pref, pref) 7 | return 1 - min_pref -------------------------------------------------------------------------------- /src/0409-Longest-Palindrome/0409.js: -------------------------------------------------------------------------------- 1 | var longestPalindrome = function(s) { 2 | let cnt = new Array(128).fill(0); 3 | for (let c of s) cnt[c.charCodeAt()]++; 4 | 5 | let res = 0; 6 | for (let i of cnt) res += Math.floor(i / 2) * 2; 7 | return res == s.length ? res : res + 1; 8 | }; -------------------------------------------------------------------------------- /src/1134-Armstrong-Number/1134.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool isArmstrong(int N) 5 | { 6 | string Nstr = to_string(N); 7 | int n = Nstr.size(), res = 0; 8 | for (auto c : Nstr) res += pow(c - '0', n); 9 | return res == N; 10 | } 11 | }; -------------------------------------------------------------------------------- /src/1220-Count-Vowels-Permutation/1220.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countVowelPermutation(self, n: int) -> int: 3 | a, e, i, o, u = 1, 1, 1, 1, 1 4 | for _ in range(n - 1): 5 | a, e, i, o, u = e + i + u, a + i, e + o, i, i + o 6 | return (a + e + i + o + u) % (10**9 + 7) -------------------------------------------------------------------------------- /src/1232-Check-If-It-Is-a-Straight-Line/1232.go: -------------------------------------------------------------------------------- 1 | func checkStraightLine(c [][]int) bool { 2 | for i := 2; i < len(c); i++ { 3 | if (c[i][0] - c[0][0]) * (c[i][1] - c[1][1]) != (c[i][0] - c[1][0]) * (c[i][1] - c[0][1]) { 4 | return false 5 | } 6 | } 7 | return true 8 | } -------------------------------------------------------------------------------- /src/2351-First-Letter-to-Appear-Twice/2351.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | char repeatedCharacter(string s) { 4 | int cnt[26] = {}; 5 | for (auto c : s) { 6 | auto x = c - 'a'; 7 | if (cnt[x]) return c; 8 | cnt[x]++; 9 | } 10 | return ' '; 11 | } 12 | }; -------------------------------------------------------------------------------- /src/0217-Contains-Duplicate/0217.go: -------------------------------------------------------------------------------- 1 | package containsduplicate 2 | 3 | func containsDuplicate(nums []int) bool { 4 | mp := make(map[int]bool) 5 | for _, val := range nums { 6 | if _, ok := mp[val]; ok == true { 7 | return true 8 | } 9 | mp[val] = true 10 | } 11 | return false 12 | } 13 | -------------------------------------------------------------------------------- /src/1014-Best-Sightseeing-Pair/1014.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxScoreSightseeingPair(self, A: List[int]) -> int: 3 | sell, buy = 0, float('-inf') 4 | for i, val in enumerate(A): 5 | sell = max(sell, val - i + buy) 6 | buy = max(buy, val + i) 7 | return sell -------------------------------------------------------------------------------- /src/1118-Number-of-Days-in-a-Month/1118.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int numberOfDays(int Y, int M) 5 | { 6 | int D[] = {31,28,31,30,31,30,31,31,30,31,30,31}; 7 | if (Y % 400 == 0 or Y % 4 == 0 and Y % 100 != 0) D[1]++; 8 | return D[M - 1]; 9 | } 10 | }; -------------------------------------------------------------------------------- /src/1133-Largest-Unique-Number/1133.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestUniqueNumber(self, A: List[int]) -> int: 3 | cnt = collections.Counter(A) 4 | sCnt = sorted(cnt.items(), key=lambda a:(a[1], -a[0])) 5 | if sCnt[0][1] != 1: 6 | return -1 7 | return sCnt[0][0] -------------------------------------------------------------------------------- /src/1402-Reducing-Dishes/1402.js: -------------------------------------------------------------------------------- 1 | var maxSatisfaction = function(s) { 2 | s.sort((a, b) => a - b); 3 | let res = 0, total = 0, n = s.length; 4 | 5 | for (let i = n - 1; i >= 0 && s[i] > -total; --i) { 6 | total += s[i]; 7 | res += total; 8 | } 9 | return res; 10 | }; -------------------------------------------------------------------------------- /src/0169-Majority-Element/0169.go: -------------------------------------------------------------------------------- 1 | package majorityelement 2 | 3 | func majorityElement(nums []int) int { 4 | mp := make(map[int]int, 0) 5 | ans, mx := -1, -1 6 | for _, v := range nums { 7 | mp[v]++ 8 | if mp[v] > mx { 9 | mx = mp[v] 10 | ans = v 11 | } 12 | } 13 | return ans 14 | } 15 | -------------------------------------------------------------------------------- /src/0365-Water-and-Jug-Problem/0365.js: -------------------------------------------------------------------------------- 1 | var canMeasureWater = function(x, y, z) { 2 | let gcd = function(a, b) { 3 | return b == 0 ? a : gcd(b, a % b); 4 | } 5 | 6 | if (x + y < z) return false; 7 | if (x == 0 || y == 0) return z == 0 || x + y == z; 8 | return z % gcd(x, y) == 0; 9 | }; -------------------------------------------------------------------------------- /src/0793-Preimage-Size-of-Factorial-Zeroes-Function/0793.js: -------------------------------------------------------------------------------- 1 | var preimageSizeFZF = function(K) { 2 | let x = 1; 3 | while (x < K) x = x * 5 + 1; 4 | while (x > 1) { 5 | K %= x; 6 | if (x - 1 == K) return 0; 7 | x = Math.floor((x - 1) / 5); 8 | } 9 | return 5; 10 | }; -------------------------------------------------------------------------------- /src/0961-N-Repeated-Element-in-Size-2N-Array/0961.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def repeatedNTimes(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: int 6 | """ 7 | for i in range(len(A)): 8 | if A[i - 1] == A[i] or A[i - 2] == A[i]: 9 | return A[i] -------------------------------------------------------------------------------- /src/1185-Day-of-the-Week/1185.go: -------------------------------------------------------------------------------- 1 | func dayOfTheWeek(d int, m int, y int) string { 2 | weeks := []string{"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} 3 | if (m < 3) { 4 | m += 12 5 | y-- 6 | } 7 | return weeks[(d+2*m+3*(m+1)/5+y+y/4-y/100+y/400+1)%7] 8 | } -------------------------------------------------------------------------------- /src/1207-Unique-Number-of-Occurrences/1207.go: -------------------------------------------------------------------------------- 1 | func uniqueOccurrences(arr []int) bool { 2 | m := make(map[int]int) 3 | for _, i := range arr { 4 | m[i]++ 5 | } 6 | s := make(map[int]int) 7 | for _, v := range m { 8 | s[v] = 1 9 | } 10 | return len(s) == len(m) 11 | } -------------------------------------------------------------------------------- /src/1299-Replace-Elements-with-Greatest-Element-on-Right-Side/1299.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def replaceElements(self, arr: List[int]) -> List[int]: 3 | max_right = -1 4 | for i in range(len(arr) - 1, -1, -1): 5 | arr[i], max_right = max_right, max(max_right, arr[i]) 6 | return arr -------------------------------------------------------------------------------- /src/1318-Minimum-Flips-to-Make-a-OR-b-Equal-to-c/1318.js: -------------------------------------------------------------------------------- 1 | var minFlips = function(a, b, c) { 2 | let res = 0; 3 | while (a || b || c) { 4 | if (c & 1) res += (a & 1) || (b & 1) ? 0 : 1; 5 | else res += (a & 1) + (b & 1); 6 | a >>= 1, b >>= 1, c >>= 1; 7 | } 8 | return res; 9 | }; -------------------------------------------------------------------------------- /src/1411-Number-of-Ways-to-Paint-N×3-Grid/1411.go: -------------------------------------------------------------------------------- 1 | func numOfWays(n int) int { 2 | var aba, abc, mod int64 3 | aba, abc, mod = 6, 6, 1000000007 4 | for i := 0; i < n - 1; i++ { 5 | aba, abc = (aba * 3 + abc * 2) % mod, (aba * 2 + abc * 2) % mod 6 | } 7 | return int((aba + abc) % mod) 8 | } -------------------------------------------------------------------------------- /src/1431-Kids-With-the-Greatest-Number-of-Candies/1431.js: -------------------------------------------------------------------------------- 1 | var kidsWithCandies = function(candies, extraCandies) { 2 | let m = 0, res = []; 3 | for (let i of candies) m = Math.max(m, i); 4 | 5 | for (let i of candies) { 6 | res.push(extraCandies + i >= m); 7 | } 8 | return res; 9 | }; -------------------------------------------------------------------------------- /src/addProg.py: -------------------------------------------------------------------------------- 1 | with open("README.md") as f: 2 | data = f.readlines() 3 | 4 | with open("new.md", 'w') as f: 5 | for i, l in enumerate(data): 6 | if i < 6: 7 | continue 8 | sl = l.split('|') 9 | sl.insert(len(sl) - 2, " ") 10 | f.write("|".join(sl)) 11 | -------------------------------------------------------------------------------- /src/0001-Two-Sum/two_sum.js: -------------------------------------------------------------------------------- 1 | var twoSum = function (nums, target) { 2 | for (let i = 0; i < nums.length; ++i) { 3 | for (let j = i + 1; j < nums.length; ++j) { 4 | if (nums[i] + nums[j] == target) { 5 | return [i, j]; 6 | } 7 | } 8 | } 9 | }; 10 | -------------------------------------------------------------------------------- /src/0172-Factorial-Trailing-Zeroes/0172.cpp: -------------------------------------------------------------------------------- 1 | static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }(); 2 | class Solution 3 | { 4 | public: 5 | int trailingZeroes(int n) 6 | { 7 | int res = 0; 8 | while (n) res += (n /= 5); 9 | return res; 10 | } 11 | }; -------------------------------------------------------------------------------- /src/0991-Broken-Calculator/0991.cpp: -------------------------------------------------------------------------------- 1 | static int x = [](){std::ios::sync_with_stdio(false);cin.tie(0);return 0;}(); 2 | class Solution 3 | { 4 | public: 5 | int brokenCalc(int X, int Y) 6 | { 7 | if (X >= Y) return X - Y; 8 | return 1 + brokenCalc(X, Y % 2 ? Y + 1: Y / 2); 9 | } 10 | }; -------------------------------------------------------------------------------- /src/1170-Compare-Strings-by-Frequency-of-the-Smallest-Character/1170.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numSmallerByFrequency(self, queries: List[str], words: List[str]) -> List[int]: 3 | w = sorted([i.count(min(i)) for i in words]) 4 | return [len(w) - bisect.bisect_right(w, i.count(min(i))) for i in queries] -------------------------------------------------------------------------------- /src/1196-How-Many-Apples-Can-You-Put-into-the-Basket/1196.go: -------------------------------------------------------------------------------- 1 | func maxNumberOfApples(arr []int) int { 2 | sort.Ints(arr) 3 | res, t := 0, 0 4 | for _, v := range arr { 5 | t += v 6 | if t > 5000 { 7 | break 8 | } 9 | res++ 10 | } 11 | return res 12 | } -------------------------------------------------------------------------------- /src/1197-Minimum-Knight-Moves/1197.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minKnightMoves(self, x: int, y: int) -> int: 3 | x, y = abs(x), abs(y) 4 | if x + y == 1: 5 | return 3 6 | res = max(max((x + 1)//2, (y + 1)//2), (x + y + 2)//3) 7 | res += (res ^ x ^ y) & 1 8 | return res -------------------------------------------------------------------------------- /src/1232-Check-If-It-Is-a-Straight-Line/1232.js: -------------------------------------------------------------------------------- 1 | var checkStraightLine = function(c) { 2 | for (let i = 2; i < c.length; i++) { 3 | if ((c[i][0] - c[0][0]) * (c[i][1] - c[1][1]) != 4 | (c[i][0] - c[1][0]) * (c[i][1] - c[0][1])) 5 | return false; 6 | } 7 | return true; 8 | }; -------------------------------------------------------------------------------- /src/1313-Decompress-Run-Length-Encoded-List/1313.go: -------------------------------------------------------------------------------- 1 | func decompressRLElist(nums []int) []int { 2 | res := []int{} 3 | 4 | for i := 0; i < len(nums); i += 2 { 5 | for j := 0; j < nums[i]; j++ { 6 | res = append(res, nums[i + 1]) 7 | } 8 | } 9 | return res; 10 | } -------------------------------------------------------------------------------- /src/1346-Check-If-N-and-Its-Double-Exist/1346.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkIfExist(self, arr: List[int]) -> bool: 3 | seen = set() 4 | for i in arr: 5 | if 2 * i in seen or i % 2 == 0 and i // 2 in seen: 6 | return True 7 | seen.add(i) 8 | return False -------------------------------------------------------------------------------- /src/1362-Closest-Divisors/1362.js: -------------------------------------------------------------------------------- 1 | var closestDivisors = function(num) { 2 | for (let a = Math.floor(Math.sqrt(num + 2)); a > 0; a--) { 3 | if ((num + 1) % a == 0) return [a, Math.floor((num + 1) / a)]; 4 | if ((num + 2) % a == 0) return [a, Math.floor((num + 2) / a)]; 5 | } 6 | return []; 7 | }; -------------------------------------------------------------------------------- /src/0453-Minimum-Moves-to-Equal-Array-Elements/0453.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minMoves(vector& nums) { 4 | int minNum = *min_element(nums.begin(), nums.end()); 5 | int res = 0; 6 | for (int num : nums) { 7 | res += num - minNum; 8 | } 9 | return res; 10 | } 11 | }; -------------------------------------------------------------------------------- /src/0973-K-Closest-Points-to-Origin/0973.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | class Solution: 3 | def kClosest(self, points, K): 4 | """ 5 | :type points: List[List[int]] 6 | :type K: int 7 | :rtype: List[List[int]] 8 | """ 9 | return heapq.nsmallest(K, points, lambda p: p[0]**2 + p[1]**2) -------------------------------------------------------------------------------- /src/1238-Circular-Permutation-in-Binary-Representation/1238.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector circularPermutation(int n, int start) 5 | { 6 | vector res; 7 | for (int i = 0; i < (1 << n); i++) res.push_back(start^i^(i >> 1)); 8 | return res; 9 | } 10 | }; -------------------------------------------------------------------------------- /src/1317-Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/1317.js: -------------------------------------------------------------------------------- 1 | var getNoZeroIntegers = function(n) { 2 | for (let a = 1; a < n; a++) { 3 | let b = n - a; 4 | if ((b+"").indexOf("0") == -1 && (a+"").indexOf("0") == -1) { 5 | return [a, b]; 6 | } 7 | } 8 | return []; 9 | }; -------------------------------------------------------------------------------- /src/1359-Count-All-Valid-Pickup-and-Delivery-Options/1359.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int countOrders(int n) 5 | { 6 | long long res = 1, mod = 1e9 + 7; 7 | for (int i = 1; i <= n; ++i) 8 | res = res * (i * 2 - 1) * i % mod; 9 | return res; 10 | } 11 | }; -------------------------------------------------------------------------------- /src/1437-Check-If-All-1's-Are-at-Least-Length-K-Places-Away/1437.js: -------------------------------------------------------------------------------- 1 | var kLengthApart = function(nums, k) { 2 | for (let pre = -k - 1, i = 0; i < nums.length; i++) { 3 | if (nums[i] == 1) { 4 | if (i - pre <= k) return false; 5 | pre = i; 6 | } 7 | } 8 | return true; 9 | }; -------------------------------------------------------------------------------- /src/1470-Shuffle-the-Array/1470.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector shuffle(vector& nums, int n) { 4 | vector res(n * 2); 5 | for (int i = 0, j = n; i < n; i++, j++) { 6 | res[i << 1] = nums[i]; 7 | res[(i << 1) + 1] = nums[j]; 8 | } 9 | return res; 10 | } 11 | }; -------------------------------------------------------------------------------- /src/0453-Minimum-Moves-to-Equal-Array-Elements/0453.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn min_moves(nums: Vec) -> i32 { 3 | let min_num = nums.iter().min().unwrap(); 4 | let mut res = 0; 5 | for num in nums.iter() { 6 | res += num - min_num; 7 | } 8 | res 9 | } 10 | } -------------------------------------------------------------------------------- /src/0559-Maximum-Depth-of-N-ary-Tree/0559.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDepth(Node* root) { 4 | if (root == nullptr) { 5 | return 0; 6 | } 7 | int res = 0; 8 | for (auto c : root->children) { 9 | res = max(res, maxDepth(c)); 10 | } 11 | return res + 1; 12 | } 13 | }; -------------------------------------------------------------------------------- /src/0559-maximum-depth-of-n-ary-tree/0559.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxDepth(Node* root) { 4 | if (root == nullptr) { 5 | return 0; 6 | } 7 | int res = 0; 8 | for (auto c : root->children) { 9 | res = max(res, maxDepth(c)); 10 | } 11 | return res + 1; 12 | } 13 | }; -------------------------------------------------------------------------------- /src/0946-Validate-Stack-Sequences/0946.js: -------------------------------------------------------------------------------- 1 | var validateStackSequences = function(pushed, popped) { 2 | let i = 0, j = 0; 3 | 4 | for (let num of pushed) { 5 | pushed[i] = num; 6 | i++; 7 | while (i > 0 && pushed[i - 1] == popped[j]) i--, j++; 8 | } 9 | 10 | return i == 0; 11 | }; -------------------------------------------------------------------------------- /src/1150-Check-If-a-Number-Is-Majority-Element-in-a-Sorted-Array/1150.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool isMajorityElement(vector& nums, int target) 5 | { 6 | int cnt = 0; 7 | for (auto i : nums) if (i == target) cnt++; 8 | return cnt > nums.size() / 2; 9 | } 10 | }; -------------------------------------------------------------------------------- /src/1217-Play-with-Chips/1217.go: -------------------------------------------------------------------------------- 1 | func minCostToMoveChips(chips []int) int { 2 | res := make([]int, 2) 3 | for _, v := range chips { 4 | res[v%2]++ 5 | } 6 | return min(res[0], res[1]) 7 | } 8 | 9 | func min(a, b int) int { 10 | if a < b { 11 | return a 12 | } 13 | return b 14 | } -------------------------------------------------------------------------------- /src/1287-Element-Appearing-More-Than-25%-In-Sorted-Array/1287.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findSpecialInteger(self, arr: List[int]) -> int: 3 | n = len(arr) 4 | for i in range(0, n, n//4 + 1): 5 | if bisect.bisect(arr, arr[i]) - bisect.bisect_left(arr, arr[i]) > n//4 : 6 | return arr[i] -------------------------------------------------------------------------------- /src/1325-Delete-Leaves-With-a-Given-Value/1325.js: -------------------------------------------------------------------------------- 1 | var removeLeafNodes = function(root, target) { 2 | if (root == null) return null; 3 | root.left = removeLeafNodes(root.left, target); 4 | root.right = removeLeafNodes(root.right, target); 5 | return root.val == target && root.left == root.right ? null : root; 6 | }; -------------------------------------------------------------------------------- /src/1394-Find-Lucky-Integer-in-an-Array/1394.go: -------------------------------------------------------------------------------- 1 | func findLucky(arr []int) int { 2 | cnt := make([]int, 501) 3 | for _, i := range arr { 4 | cnt[i]++ 5 | } 6 | 7 | for i := 500; i > 0; i-- { 8 | if cnt[i] == i { 9 | return i 10 | } 11 | } 12 | return -1 13 | } -------------------------------------------------------------------------------- /src/0891-Sum-of-Subsequence-Widths/0891.go: -------------------------------------------------------------------------------- 1 | func sumSubseqWidths(A []int) int { 2 | sort.Ints(A) 3 | res, n, mod := 0, len(A), 1000000007 4 | for i := 0; i < n; i++ { 5 | res *= 2 6 | res -= A[i] 7 | res += A[n - i - 1] 8 | res %= mod 9 | } 10 | return (res + mod) % mod 11 | } -------------------------------------------------------------------------------- /src/1005-Maximize-Sum-Of-Array-After-K-Negations/1005.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestSumAfterKNegations(self, A: List[int], K: int) -> int: 3 | A.sort() 4 | i = 0 5 | while i < len(A) and i < K and A[i] < 0: 6 | A[i] = -A[i] 7 | i += 1 8 | return sum(A) - (K - i)%2*min(A)*2 -------------------------------------------------------------------------------- /src/1037-Valid-Boomerang/1037.cpp: -------------------------------------------------------------------------------- 1 | static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }(); 2 | class Solution 3 | { 4 | public: 5 | bool isBoomerang(vector>& p) 6 | { 7 | return (p[0][0] - p[1][0]) * (p[0][1] - p[2][1]) != (p[0][0] - p[2][0]) * (p[0][1] - p[1][1]); 8 | } 9 | }; -------------------------------------------------------------------------------- /src/1047-Remove-All-Adjacent-Duplicates-In-String/1047.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeDuplicates(self, S: str) -> str: 3 | res = list() 4 | for c in S: 5 | if len(res) and res[-1] == c: 6 | res.pop() 7 | else: 8 | res.append(c) 9 | return "".join(res) -------------------------------------------------------------------------------- /src/1048-Longest-String-Chain/1048.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestStrChain(self, words: List[str]) -> int: 3 | words.sort(key=len) 4 | mem = collections.defaultdict(int) 5 | for w in words: 6 | mem[w] = max(mem[w[:i] + w[i + 1:]] + 1 for i in range(len(w))) 7 | return max(mem.values()) -------------------------------------------------------------------------------- /src/1134-Armstrong-Number/1134.go: -------------------------------------------------------------------------------- 1 | func isArmstrong(N int) bool { 2 | str := strconv.Itoa(N) 3 | n := len(str) 4 | var res float64 = 0 5 | for _, v := range []byte(str) { 6 | t, _ := strconv.ParseFloat(string(v), 64) 7 | res += math.Pow(t, float64(n)) 8 | } 9 | return int(res) == N 10 | } -------------------------------------------------------------------------------- /src/1218-Longest-Arithmetic-Subsequence-of-Given-Difference/1218.js: -------------------------------------------------------------------------------- 1 | var longestSubsequence = function(arr, dif) { 2 | let mem = {}, res = 1; 3 | for (let i of arr) { 4 | if (mem[i - dif]) mem[i] = mem[i - dif] + 1; 5 | else mem[i] = 1; 6 | res = Math.max(res, mem[i]); 7 | } 8 | return res; 9 | }; -------------------------------------------------------------------------------- /src/1262-Greatest-Sum-Divisible-by-Three/1262.js: -------------------------------------------------------------------------------- 1 | var maxSumDivThree = function(nums) { 2 | let dp = [0, 0, 0]; 3 | for (let a of nums) { 4 | let dp2 = dp.slice(); 5 | for (let i of dp2) { 6 | dp[(i + a)%3] = Math.max(dp[(i + a)%3], i + a); 7 | } 8 | } 9 | return dp[0]; 10 | }; -------------------------------------------------------------------------------- /src/1300-Sum-of-Mutated-Array-Closest-to-Target/1300.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findBestValue(self, A: List[int], target: int) -> int: 3 | A.sort(reverse=True) 4 | maxA = A[0] 5 | while A and target >= A[-1] * len(A): 6 | target -= A.pop() 7 | return int(target/len(A) + 0.49) if A else maxA -------------------------------------------------------------------------------- /src/1328-Break-a-Palindrome/1328.js: -------------------------------------------------------------------------------- 1 | var breakPalindrome = function(p) { 2 | let n = Math.floor(p.length/2); 3 | for (let i = 0; i < n; i++) { 4 | if (p[i] != 'a') { 5 | return p.slice(0, i) + 'a' + p.slice(i + 1); 6 | } 7 | } 8 | return p.length > 1 ? p.slice(0, p.length - 1) + 'b' : ""; 9 | }; -------------------------------------------------------------------------------- /src/1365-How-Many-Numbers-Are-Smaller-Than-the-Current-Number/1365.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]: 3 | cnt = collections.Counter(nums) 4 | for i in range(1, 101): 5 | cnt[i] += cnt[i - 1] 6 | 7 | return [cnt[x - 1] for x in nums] -------------------------------------------------------------------------------- /src/0263-Ugly-Number/0263.go: -------------------------------------------------------------------------------- 1 | package uglynumber 2 | 3 | func isUgly(n int) bool { 4 | if n == 0 { 5 | return false 6 | } 7 | fact := [3]int{2, 3, 5} 8 | for i := 0; i < len(fact); i++ { 9 | for n%fact[i] == 0 { 10 | n /= fact[i] 11 | } 12 | } 13 | if n == 1 { 14 | return true 15 | } 16 | return false 17 | } 18 | -------------------------------------------------------------------------------- /src/0409-Longest-Palindrome/0409.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int longestPalindrome(string s) { 4 | int cnt[128] = {}; 5 | for (char c : s) cnt[c]++; 6 | 7 | int res = 0; 8 | for (int v : cnt) res += v / 2 * 2; 9 | return res == s.size() ? res : res + 1; 10 | } 11 | }; -------------------------------------------------------------------------------- /src/1071-Greatest-Common-Divisor-of-Strings/1071.go: -------------------------------------------------------------------------------- 1 | func gcdOfStrings(s1 string, s2 string) string { 2 | if s1 + s2 == s2 + s1 { 3 | return s1[:gcd(len(s1), len(s2))] 4 | } 5 | return "" 6 | } 7 | 8 | func gcd(a, b int) int { 9 | if b > 0 { 10 | return gcd(b, a % b) 11 | } 12 | return a 13 | } -------------------------------------------------------------------------------- /src/1147-Longest-Chunked-Palindrome-Decomposition/1147.js: -------------------------------------------------------------------------------- 1 | var longestDecomposition = function(text) { 2 | var n = text.length; 3 | for (var i = 1; i <= n/2; ++i) { 4 | if (text.substr(0,i) == text.substr(n-i)) 5 | return 2 + longestDecomposition(text.substr(i,n-2*i)); 6 | } 7 | return n == 0 ? 0 : 1; 8 | }; -------------------------------------------------------------------------------- /src/1230-Toss-Strange-Coins/1230.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def probabilityOfHeads(self, prob: List[float], target: int) -> float: 3 | dp = [1] + [0] * target 4 | for p in prob: 5 | for k in range(target, -1, -1): 6 | dp[k] = (dp[k - 1] if k else 0) * p + dp[k] * (1 - p) 7 | return dp[-1] -------------------------------------------------------------------------------- /src/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/1290.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int getDecimalValue(ListNode head) { 3 | int res = 0; 4 | while (head != null) { 5 | res = (res << 1) | head.val; 6 | head = head.next; 7 | } 8 | return res; 9 | } 10 | } -------------------------------------------------------------------------------- /src/1437-Check-If-All-1's-Are-at-Least-Length-K-Places-Away/1437.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def kLengthApart(self, nums: List[int], k: int) -> bool: 3 | pre = -k - 1 4 | for i, v in enumerate(nums): 5 | if v == 1: 6 | if i - pre <= k: return False 7 | pre = i 8 | return True -------------------------------------------------------------------------------- /src/0026-Remove-Duplicates-from-Sorted-Array/0026.go: -------------------------------------------------------------------------------- 1 | package removeduplicatesfromsortedarray 2 | 3 | func removeDuplicates(nums []int) int { 4 | for i := 0; i < len(nums); { 5 | if i > 0 && nums[i] == nums[i-1] { 6 | nums = append(nums[:i], nums[i+1:]...) 7 | } else { 8 | i++ 9 | } 10 | } 11 | return len(nums) 12 | } 13 | -------------------------------------------------------------------------------- /src/0069-Sqrt(x)/0069.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func mySqrt(x int) int { 4 | if x < 2 { 5 | return x 6 | } 7 | lo, hi, mid, ans := 0, x, 0, 0 8 | for lo <= hi { 9 | mid = lo + (hi-lo)/2 10 | if mid*mid <= x { 11 | lo = mid + 1 12 | ans = mid 13 | } else { 14 | hi = mid - 1 15 | } 16 | } 17 | return ans 18 | } 19 | -------------------------------------------------------------------------------- /src/0191-Number-of-1-Bits/0191.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | 4 | static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }(); 5 | class Solution 6 | { 7 | public: 8 | int hammingWeight(uint32_t n) 9 | { 10 | bitset<32> bin(n); 11 | return bin.count(); 12 | } 13 | }; -------------------------------------------------------------------------------- /src/1007-Minimum-Domino-Rotations-For-Equal-Row/1007.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minDominoRotations(self, A: List[int], B: List[int]) -> int: 3 | for i in range(1,7): 4 | if all(i == a or i == b for a,b in zip(A,B)): 5 | return min(len(A)-A.count(i),len(B)-B.count(i)) 6 | 7 | return -1 -------------------------------------------------------------------------------- /src/1185-Day-of-the-Week/1185.js: -------------------------------------------------------------------------------- 1 | var dayOfTheWeek = function(d, m, y) { 2 | var weeks = ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]; 3 | if (m < 3) { 4 | m += 12; y--; 5 | } 6 | return weeks[(d+2*m+Math.floor(3*(m+1)/5)+y+Math.floor(y/4)-Math.floor(y/100)+Math.floor(y/400)+1)%7]; 7 | }; -------------------------------------------------------------------------------- /src/1196-How-Many-Apples-Can-You-Put-into-the-Basket/1196.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxNumberOfApples(self, arr: List[int]) -> int: 3 | arr.sort() 4 | res, t = 0, 0 5 | for v in arr: 6 | t += v 7 | if t > 5000: 8 | break 9 | res += 1 10 | return res -------------------------------------------------------------------------------- /src/1266-Minimum-Time-Visiting-All-Points/1266.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minTimeToVisitAllPoints(int[][] p) { 3 | int res = 0; 4 | for (int i = 1; i < p.length; i++) { 5 | res += Math.max(Math.abs(p[i][0] - p[i-1][0]), Math.abs(p[i][1] - p[i-1][1])); 6 | } 7 | return res; 8 | } 9 | } -------------------------------------------------------------------------------- /src/1269-Number-of-Ways-to-Stay-in-the-Same-Place-After-Some-Steps/1269.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numWays(self, steps: int, arrLen: int) -> int: 3 | A, mod = [0, 1], 10**9 + 7 4 | for t in range(steps): 5 | A[1:] = [sum(A[i - 1:i + 2]) % mod for i in range(1, min(arrLen + 1, t + 3))] 6 | return A[1] % mod -------------------------------------------------------------------------------- /src/1295-Find-Numbers-with-Even-Number-of-Digits/1295.js: -------------------------------------------------------------------------------- 1 | var findNumbers = function(nums) { 2 | let res = 0; 3 | for (let n of nums) { 4 | let x = 0; 5 | while (n) { 6 | n = Math.floor(n / 10); 7 | x++; 8 | } 9 | if (x % 2 == 0) res++; 10 | } 11 | return res; 12 | }; -------------------------------------------------------------------------------- /src/1299-Replace-Elements-with-Greatest-Element-on-Right-Side/1299.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] replaceElements(int[] arr) { 3 | int max_right = -1; 4 | for (int i = arr.length - 1; i >= 0; i--) { 5 | max_right = Math.max(arr[i], arr[i] = max_right); 6 | } 7 | return arr; 8 | } 9 | } -------------------------------------------------------------------------------- /src/1317-Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/1317.go: -------------------------------------------------------------------------------- 1 | func getNoZeroIntegers(n int) []int { 2 | for a := 1; a < n; a++ { 3 | b := n - a 4 | if !strings.Contains(strconv.Itoa(a), "0") && !strings.Contains(strconv.Itoa(b), "0") { 5 | return []int{a, b} 6 | } 7 | } 8 | return []int{} 9 | } -------------------------------------------------------------------------------- /src/1375-Bulb-Switcher-III/1375.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int numTimesAllBlue(int[] light) { 3 | int res = 0, maxv = 0; 4 | for (int i = 0; i < light.length; i++) { 5 | maxv = Math.max(maxv, light[i]); 6 | if (maxv == i + 1) res++; 7 | } 8 | return res; 9 | } 10 | } -------------------------------------------------------------------------------- /src/1389-Create-Target-Array-in-the-Given-Order/1389.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector createTargetArray(vector& nums, vector& index) { 4 | vector res; 5 | for (int i = 0; i < nums.size(); i++) 6 | res.insert(res.begin() + index[i], nums[i]); 7 | return res; 8 | } 9 | }; -------------------------------------------------------------------------------- /src/1400-Construct-K-Palindrome-Strings/1400.js: -------------------------------------------------------------------------------- 1 | var canConstruct = function(s, k) { 2 | let odds = 0, n = s.length, count = new Array(26).fill(0); 3 | for (let i = 0; i < n; ++i) { 4 | count[s.charCodeAt(i) - 97] ^= 1; 5 | odds += count[s.charCodeAt(i) - 97] > 0 ? 1 : -1; 6 | } 7 | return odds <= k && k <= n; 8 | }; -------------------------------------------------------------------------------- /src/2180-Count-Integers-With-Even-Digit-Sum/2180.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countEven(int num) { 4 | int res = num / 10 * 5 - 1; 5 | int s = 0; 6 | for (int x = num / 10; x > 0; x /= 10) { 7 | s += x % 10; 8 | } 9 | res += (num % 10 + 2 - (s & 1)) >> 1; 10 | return res; 11 | } 12 | }; 13 | -------------------------------------------------------------------------------- /src/2427-Number-of-Common-Factors/2427.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int commonFactors(int a, int b) { 4 | int c = gcd(a, b), res = 0, i = 1; 5 | for (; i * i < c; i++) { 6 | if (c % i == 0) { 7 | res += 2; 8 | } 9 | } 10 | if (i * i == c) res++; 11 | return res; 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /src/0035-Search-Insert-Position/0035.go: -------------------------------------------------------------------------------- 1 | package searchinsertposition 2 | 3 | func searchInsert(nums []int, target int) int { 4 | lo, hi, mid := 0, len(nums)-1, 0 5 | for lo <= hi { 6 | mid = lo + (hi-lo)/2 7 | if target <= nums[mid] { 8 | hi = mid - 1 9 | } else { 10 | lo = mid + 1 11 | } 12 | } 13 | return lo 14 | } 15 | -------------------------------------------------------------------------------- /src/0043-Multiply-Strings/0043.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def multiply(self, num1: str, num2: str) -> str: 3 | res, m, n = 0, len(num1), len(num2) 4 | 5 | for i in range(1, m + 1): 6 | for j in range(1, n + 1): 7 | res += int(num1[-i]) * int(num2[-j]) * 10 ** (i + j - 2) 8 | return str(res) -------------------------------------------------------------------------------- /src/0219-Contains-Duplicate-II/0219.go: -------------------------------------------------------------------------------- 1 | package containsduplicateii 2 | 3 | func containsNearbyDuplicate(nums []int, k int) bool { 4 | mp := make(map[int]int, 0) 5 | for i, v := range nums { 6 | if val, ok := mp[v]; ok { 7 | if i-val <= k { 8 | return true 9 | } 10 | } 11 | mp[v] = i 12 | } 13 | return false 14 | } 15 | -------------------------------------------------------------------------------- /src/1033-Moving-Stones-Until-Consecutive/1033.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numMovesStones(self, a: int, b: int, c: int) -> List[int]: 3 | nums = sorted([a, b, c]) 4 | d1, d2 = nums[1] - nums[0], nums[2] - nums[1] 5 | if d1 + d2 == 2: 6 | return [0, 0] 7 | return [1 if min(d1, d2) <= 2 else 2, d1 + d2 - 2] -------------------------------------------------------------------------------- /src/1147-Longest-Chunked-Palindrome-Decomposition/1147.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestDecomposition(self, text: str) -> int: 3 | n = len(text) 4 | for i in range(1, n//2 + 1): 5 | if text[:i] == text[n-i:]: 6 | return 2 + self.longestDecomposition(text[i:n-i]) 7 | return 0 if n == 0 else 1 -------------------------------------------------------------------------------- /src/1196-How-Many-Apples-Can-You-Put-into-the-Basket/1196.js: -------------------------------------------------------------------------------- 1 | var maxNumberOfApples = function(arr) { 2 | arr.sort(function sortNumber(a,b) { 3 | return a - b; 4 | }); 5 | var res = 0, t = 0; 6 | for (let i of arr) { 7 | t += i; 8 | if (t > 5000) break; 9 | res++; 10 | } 11 | return res; 12 | }; -------------------------------------------------------------------------------- /src/1256-Encode-Number/1256.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | string encode(int num) 5 | { 6 | string res; 7 | num++; 8 | while (num) 9 | { 10 | res = string(num&1 ? "1" : "0") + res; 11 | num >>= 1; 12 | } 13 | return res.substr(1); 14 | } 15 | }; -------------------------------------------------------------------------------- /src/1366-Rank-Teams-by-Votes/1366.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rankTeams(self, votes: List[str]) -> str: 3 | cnt = {v: [0] * len(votes[0]) + [v] for v in votes[0]} 4 | for vote in votes: 5 | for i, v in enumerate(vote): 6 | cnt[v][i] -= 1 7 | return "".join(sorted(votes[0], key=lambda x: cnt[x])) -------------------------------------------------------------------------------- /src/1394-Find-Lucky-Integer-in-an-Array/1394.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLucky(vector& arr) { 4 | int cnt[501] = {}; 5 | for (int i : arr) cnt[i]++; 6 | 7 | for (int i = 500; i; i--) { 8 | if (cnt[i] == i) return i; 9 | } 10 | return -1; 11 | } 12 | }; -------------------------------------------------------------------------------- /src/1394-Find-Lucky-Integer-in-an-Array/1394.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findLucky(int[] arr) { 3 | int[] cnt = new int[501]; 4 | for (int i : arr) cnt[i]++; 5 | 6 | for (int i = 500; i > 0; i--) { 7 | if (cnt[i] == i) return i; 8 | } 9 | return -1; 10 | } 11 | } -------------------------------------------------------------------------------- /src/1436-Destination-City/1436.go: -------------------------------------------------------------------------------- 1 | func destCity(paths [][]string) string { 2 | s := make(map[string]int) 3 | for _, path := range paths { 4 | s[path[0]] = 1 5 | } 6 | for _, path := range paths { 7 | if _, ok := s[path[1]]; !ok { 8 | return path[1] 9 | } 10 | } 11 | return "" 12 | } -------------------------------------------------------------------------------- /src/1798-Maximum-Number-of-Consecutive-Values-You-Can-Make/1798.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int getMaximumConsecutive(vector& coins) { 4 | sort(coins.begin(), coins.end()); 5 | int res = 1; 6 | for (auto& c : coins) { 7 | if (c > res) break; 8 | res += c; 9 | } 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /src/0089-Gray-Code/0089.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def grayCode(self, n): 3 | """ 4 | :type n: int 5 | :rtype: List[int] 6 | """ 7 | if n == 0: 8 | return [0] 9 | 10 | res = list() 11 | for i in range(1 << n): 12 | res.append(i ^ (i>>1)) 13 | return res -------------------------------------------------------------------------------- /src/0137-Single-Number-II/0137.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def singleNumber(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | return (3*sum(set(nums)) - sum(nums))//2 8 | 9 | if __name__ == "__main__": 10 | nums = [2,2,3,2] 11 | print(Solution().singleNumber(nums)) -------------------------------------------------------------------------------- /src/0793-Preimage-Size-of-Factorial-Zeroes-Function/0793.go: -------------------------------------------------------------------------------- 1 | func preimageSizeFZF(K int) int { 2 | x := 1 3 | for x < K { 4 | x = x * 5 + 1 5 | } 6 | 7 | for x > 1 { 8 | K %= x 9 | if x - 1 == K { 10 | return 0 11 | } 12 | x = (x - 1) / 5 13 | } 14 | return 5 15 | } -------------------------------------------------------------------------------- /src/1010-Pairs-of-Songs-With-Total-Durations-Divisible-by-60/1010.py: -------------------------------------------------------------------------------- 1 | from collections import Counter 2 | class Solution: 3 | def numPairsDivisibleBy60(self, time: List[int]) -> int: 4 | cnt = Counter() 5 | res = 0 6 | for t in time: 7 | res += cnt[-t % 60]#trick 8 | cnt[t % 60] += 1 9 | return res -------------------------------------------------------------------------------- /src/1197-Minimum-Knight-Moves/1197.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int minKnightMoves(int x, int y) 5 | { 6 | x = abs(x), y = abs(y); 7 | if (x + y == 1) return 3; 8 | int res = max({(x + 1)/2, (y + 1)/2, (x + y + 2)/3}); 9 | res += (res ^ x ^ y) & 1; 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /src/1220-Count-Vowels-Permutation/1220.go: -------------------------------------------------------------------------------- 1 | func countVowelPermutation(n int) int { 2 | var a, e, i, o, u, mod uint32 3 | a, e, i, o, u, mod = 1, 1, 1, 1, 1, 1000000007 4 | for k := 1; k < n; k++ { 5 | a, e, i, o, u = (e + i + u)%mod, (a + i)%mod, (e + o)%mod, i, (i + o)%mod 6 | } 7 | return int((a + e + i + o + u) % mod) 8 | } -------------------------------------------------------------------------------- /src/1221-Split-a-String-in-Balanced-Strings/1221.go: -------------------------------------------------------------------------------- 1 | func balancedStringSplit(s string) int { 2 | L, res := 0, 0 3 | for _, v := range s { 4 | if v == 'L' { 5 | L++ 6 | } else { 7 | L-- 8 | } 9 | if L == 0 { 10 | res++ 11 | } 12 | } 13 | return res 14 | } -------------------------------------------------------------------------------- /src/1282-Group-the-People-Given-the-Group-Size-They-Belong-To/1282.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def groupThePeople(self, a: List[int]) -> List[List[int]]: 3 | data = collections.defaultdict(list) 4 | for i, v in enumerate(a): 5 | data[v].append(i) 6 | return [v[i:i+k] for k, v in data.items() for i in range(0, len(v), k)] -------------------------------------------------------------------------------- /src/1295-Find-Numbers-with-Even-Number-of-Digits/1295.go: -------------------------------------------------------------------------------- 1 | func findNumbers(nums []int) int { 2 | res := 0 3 | for _, v := range nums { 4 | x := 0 5 | for v != 0 { 6 | v /= 10 7 | x++ 8 | } 9 | if x % 2 == 0 { 10 | res++ 11 | } 12 | } 13 | return res 14 | } -------------------------------------------------------------------------------- /src/1300-Sum-of-Mutated-Array-Closest-to-Target/1300.js: -------------------------------------------------------------------------------- 1 | var findBestValue = function(arr, target) { 2 | arr.sort((a, b) => b - a); 3 | let maxv = arr[0]; 4 | while (arr != false && target >= arr[arr.length-1]*arr.length) { 5 | target -= arr.pop(); 6 | } 7 | return arr != false ? Math.floor(target/arr.length + 0.49) : maxv; 8 | }; -------------------------------------------------------------------------------- /src/1413-Minimum-Value-to-Get-Positive-Step-by-Step-Sum/1413.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int minStartValue(int[] nums) { 3 | int pref = 0, min_pref = 0; 4 | for (int i : nums) { 5 | pref += i; 6 | min_pref = Math.min(min_pref, pref); 7 | } 8 | return 1 - min_pref; 9 | } 10 | } -------------------------------------------------------------------------------- /src/1422-Maximum-Score-After-Splitting-a-String/1422.js: -------------------------------------------------------------------------------- 1 | var maxScore = function(s) { 2 | let zeros = 0, ones = 0, res = -1000, n = s.length; 3 | for (let i = 0; i < n; i++) { 4 | if (s.charAt(i) == '1') ones++; 5 | else zeros++; 6 | if (i != n - 1) res = Math.max(res, zeros - ones); 7 | } 8 | return res + ones; 9 | }; -------------------------------------------------------------------------------- /src/1806-Minimum-Number-of-Operations-to-Reinitialize-a-Permutation/1806.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int reinitializePermutation(int n) { 4 | if (n == 2) { 5 | return 1; 6 | } 7 | int s = 1, p = 2; 8 | while (p != 1) { 9 | s++; 10 | p = p * 2 % (n - 1); 11 | } 12 | return s; 13 | } 14 | }; -------------------------------------------------------------------------------- /src/0009-Palindrome-Number/0009.go: -------------------------------------------------------------------------------- 1 | import "strconv" 2 | func isPalindrome(x int) bool { 3 | if x < 0 { 4 | return false 5 | } 6 | 7 | s := strconv.Itoa(x) 8 | for i, j := 0, len(s)-1; i < j; i, j = i+1, j-1 { 9 | if s[i] != s[j] { 10 | return false 11 | } 12 | } 13 | return true 14 | } -------------------------------------------------------------------------------- /src/0268-Missing-Number/0268.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def missingNumber(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | n = len(nums) 8 | return n*(n+1)// 2 - sum(nums) 9 | 10 | if __name__ == "__main__": 11 | nums = [3,0,1] 12 | print(Solution().missingNumber(nums)) -------------------------------------------------------------------------------- /src/0409-Longest-Palindrome/0409.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int longestPalindrome(String s) { 3 | int[] cnt = new int[128]; 4 | for (char c : s.toCharArray()) cnt[c]++; 5 | 6 | int res = 0; 7 | for (int i : cnt) res += i / 2 * 2; 8 | return res == s.length() ? res : res + 1; 9 | } 10 | } -------------------------------------------------------------------------------- /src/0891-Sum-of-Subsequence-Widths/0891.js: -------------------------------------------------------------------------------- 1 | var sumSubseqWidths = function(A) { 2 | A.sort((a, b) => a - b); 3 | let res = 0, mod = 1e9 + 7, n = A.length; 4 | for (let i = 0; i < n; i++) { 5 | res <<= 1; 6 | res -= A[i]; 7 | res += A[n - i - 1]; 8 | res %= mod; 9 | } 10 | return (res + mod)%mod; 11 | }; -------------------------------------------------------------------------------- /src/1015-Smallest-Integer-Divisible-by-K/1015.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestRepunitDivByK(self, K: int) -> int: 3 | if K % 2 == 0 or K % 5 == 0: 4 | return -1 5 | 6 | r = 0 7 | for N in range(1, K + 1): 8 | r = (r * 10 + 1) % K 9 | if r == 0: 10 | return N -------------------------------------------------------------------------------- /src/1199-Minimum-Time-to-Build-Blocks/1199.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | class Solution: 3 | def minBuildTime(self, blocks: List[int], split: int) -> int: 4 | heapq.heapify(blocks) 5 | while len(blocks) > 1: 6 | _, y = heapq.heappop(blocks), heapq.heappop(blocks) 7 | heapq.heappush(blocks, y + split) 8 | return blocks[0] -------------------------------------------------------------------------------- /src/1413-Minimum-Value-to-Get-Positive-Step-by-Step-Sum/1413.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minStartValue(vector& nums) { 4 | int pref = 0, min_pref = 0; 5 | for (int i : nums) { 6 | pref += i; 7 | min_pref = min(min_pref, pref); 8 | } 9 | return 1 - min_pref; 10 | } 11 | }; -------------------------------------------------------------------------------- /src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.js: -------------------------------------------------------------------------------- 1 | var maxScore = function(cardPoints, k) { 2 | let res = 0, cur = 0, n = cardPoints.length; 3 | for (let i = n - k; i < n + k; i++) { 4 | cur += cardPoints[i % n]; 5 | if (i >= n) cur -= cardPoints[i - k]; 6 | res = Math.max(res, cur); 7 | } 8 | return res; 9 | }; -------------------------------------------------------------------------------- /src/1758-Minimum-Changes-To-Make-Alternating-Binary-String/1758.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(string s) { 4 | int n = s.size(); 5 | int res = 0; 6 | for (int i = 0; i < n; i++) { 7 | if (s[i] != ('0' + i % 2)) { 8 | res++; 9 | } 10 | } 11 | return min(res, n - res); 12 | } 13 | }; -------------------------------------------------------------------------------- /src/2011-Final-Value-of-Variable-After-Performing-Operations/2011.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int finalValueAfterOperations(vector& operations) { 4 | int res = 0; 5 | for (auto& op : operations) { 6 | if (op[1] == '+') 7 | res++; 8 | else 9 | res--; 10 | } 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /src/2357-Make-Array-Zero-by-Subtracting-Equal-Amounts/2357.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumOperations(vector& nums) { 4 | int cnt[101] = {}; 5 | for (auto& num : nums) { 6 | cnt[num]++; 7 | } 8 | int res = 0; 9 | for (int i = 1; i <= 100; i++) res += cnt[i] ? 1 : 0; 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /src/2439-Minimize-Maximum-of-Array/2439.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimizeArrayValue(vector& nums) { 4 | long long sum = 0; 5 | int n = nums.size(), res = 0; 6 | for (int i = 0; i < n; i++) { 7 | sum += nums[i]; 8 | res = max(res, int((sum + i) / (i + 1))); 9 | } 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /src/0069-Sqrt(x)/0069.cpp: -------------------------------------------------------------------------------- 1 | static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }(); 2 | class Solution 3 | { 4 | public: 5 | int mySqrt(int x) 6 | { 7 | long res = x; 8 | while (res * res > x) 9 | { 10 | res = (res + x / res) / 2; 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /src/0137-Single-Number-II/0137.go: -------------------------------------------------------------------------------- 1 | package singlenumberii 2 | 3 | func singleNumber(nums []int) int { 4 | ans := 0 5 | for bit := 1; bit != 0; bit <<= 1 { 6 | count := 0 7 | for _, num := range nums { 8 | if num&bit != 0 { 9 | count++ 10 | } 11 | } 12 | if count%3 == 1 { 13 | ans |= bit 14 | } 15 | } 16 | return ans 17 | } 18 | -------------------------------------------------------------------------------- /src/0168-Excel-Sheet-Column-Title/0168.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def convertToTitle(self, n): 3 | """ 4 | :type n: int 5 | :rtype: str 6 | """ 7 | res = '' 8 | while n: 9 | n -= 1 10 | res += chr(ord('A')+n%26) 11 | n //= 26 12 | 13 | return res[::-1] -------------------------------------------------------------------------------- /src/0278-First-Bad-Version/0278.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int firstBadVersion(int n) { 5 | int l = 0, r = n; 6 | while (l < r) 7 | { 8 | int mid = (r - l)/2 + l; 9 | if (isBadVersion(mid)) r = mid; 10 | else l = mid + 1; 11 | } 12 | return l; 13 | } 14 | }; -------------------------------------------------------------------------------- /src/0409-Longest-Palindrome/0409.go: -------------------------------------------------------------------------------- 1 | func longestPalindrome(s string) int { 2 | cnt := make([]int, 128) 3 | for _, c := range s { 4 | cnt[c]++ 5 | } 6 | 7 | res := 0 8 | for _, i := range cnt { 9 | res += i / 2 * 2 10 | } 11 | if res == len(s) { 12 | return res 13 | } 14 | return res + 1 15 | } -------------------------------------------------------------------------------- /src/0700-Search-in-a-Binary-Search-Tree/0700.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | TreeNode *searchBST(TreeNode *root, int val) { 4 | if (root == nullptr) { 5 | return nullptr; 6 | } 7 | if (val == root->val) { 8 | return root; 9 | } 10 | return searchBST(val < root->val ? root->left : root->right, val); 11 | } 12 | }; -------------------------------------------------------------------------------- /src/0824-Goat-Latin/0824.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def toGoatLatin(self, S: str) -> str: 3 | res, vowels = [], set("aeiouAEIOU") 4 | S = S.split(" ") 5 | for i, w in enumerate(S): 6 | if w[0] not in vowels: 7 | w = w[1:] + w[0] 8 | res.append(w + "ma" + (i + 1) * "a") 9 | return " ".join(res) -------------------------------------------------------------------------------- /src/0876-Middle-of-the-Linked-List/0876.js: -------------------------------------------------------------------------------- 1 | var middleNode = function(head) { 2 | let fast = head, slow = head; 3 | while (fast.next != null && fast.next.next != null) { 4 | fast = fast.next.next; 5 | slow = slow.next; 6 | } 7 | 8 | if (fast.next != null && fast.next.next == null) return slow.next; 9 | return slow; 10 | }; -------------------------------------------------------------------------------- /src/1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/1111.go: -------------------------------------------------------------------------------- 1 | func maxDepthAfterSplit(seq string) []int { 2 | res := make([]int, len(seq)) 3 | for i, v := range []byte(seq) { 4 | if v == '(' { 5 | res[i] = i & 1 6 | } else { 7 | res[i] = 1 - i & 1 8 | } 9 | } 10 | return res 11 | } -------------------------------------------------------------------------------- /src/1271-Hexspeak/1271.js: -------------------------------------------------------------------------------- 1 | var toHexspeak = function(num) { 2 | let data = Number(num).toString(16).toUpperCase(), res = ""; 3 | for (let i of data) { 4 | if (i > '1' && i <= '9') return "ERROR"; 5 | if (i == '0') res += 'O'; 6 | else if (i == '1') res += 'I'; 7 | else res += i; 8 | } 9 | return res; 10 | }; -------------------------------------------------------------------------------- /src/1299-Replace-Elements-with-Greatest-Element-on-Right-Side/1299.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} arr 3 | * @return {number[]} 4 | */ 5 | var replaceElements = function(arr) { 6 | let max_right = -1; 7 | for (let i = arr.length - 1; i >= 0; i--) { 8 | max_right = Math.max(arr[i], arr[i] = max_right); 9 | } 10 | return arr; 11 | }; -------------------------------------------------------------------------------- /src/1333-Filter-Restaurants-by-Vegan-Friendly,-Price-and-Distance/1333.js: -------------------------------------------------------------------------------- 1 | var filterRestaurants = function(restaurants, veganFriendly, maxPrice, maxDistance) { 2 | return restaurants 3 | .filter(s => s[2] >= veganFriendly && s[3] <= maxPrice && s[4] <= maxDistance) 4 | .sort((a, b) => b[1] - a[1] || b[0] - a[0]) 5 | .map(s => s[0]); 6 | }; -------------------------------------------------------------------------------- /src/1437-Check-If-All-1's-Are-at-Least-Length-K-Places-Away/1437.go: -------------------------------------------------------------------------------- 1 | func kLengthApart(nums []int, k int) bool { 2 | for pre, i := -k - 1, 0; i < len(nums); i++ { 3 | if nums[i] == 1 { 4 | if i - pre <= k { 5 | return false 6 | } 7 | pre = i 8 | } 9 | } 10 | return true 11 | } -------------------------------------------------------------------------------- /src/2027-Minimum-Moves-to-Convert-String/2027.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumMoves(string s) { 4 | int n = s.size(); 5 | int res = 0, i = 0; 6 | while (i < n) { 7 | if (s[i] == 'X') { 8 | res++; 9 | i += 3; 10 | } else { 11 | i++; 12 | } 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /src/2395-Find-Subarrays-With-Equal-Sum/2395.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool findSubarrays(vector& nums) { 4 | unordered_set s; 5 | for (int i = 0; i < nums.size() - 1; i++) { 6 | auto x = nums[i] + nums[i + 1]; 7 | if (s.count(x)) return true; 8 | s.insert(x); 9 | } 10 | return false; 11 | } 12 | }; -------------------------------------------------------------------------------- /src/0153-Find-Minimum-in-Rotated-Sorted-Array/0153.go: -------------------------------------------------------------------------------- 1 | package findminimuminrotatedsortedarray 2 | 3 | func findMin(nums []int) int { 4 | var mid int 5 | lo, hi := 0, len(nums)-1 6 | for lo < hi { 7 | mid = lo + (hi-lo)/2 8 | if nums[mid] > nums[hi] { 9 | lo = mid + 1 10 | } else { 11 | hi = mid 12 | } 13 | } 14 | return nums[lo] 15 | } 16 | -------------------------------------------------------------------------------- /src/0179-Largest-Number/0179.py: -------------------------------------------------------------------------------- 1 | class compare(str): 2 | def __lt__(x, y): 3 | return x+y > y+x 4 | 5 | class Solution: 6 | def largestNumber(self, nums: List[int]) -> str: 7 | if not nums: 8 | return "" 9 | nums = sorted(list(map(str, nums)), key=compare) 10 | return '0' if nums[0] == '0' else "".join(nums) -------------------------------------------------------------------------------- /src/0253-Meeting-Rooms-II/0253.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minMeetingRooms(self, intervals): 3 | if not intervals: 4 | return 0 5 | tmp = sorted(x for i, j in intervals for x in [[i, 1], [j, -1]]) 6 | res, n = 0, 0 7 | for _, v in tmp: 8 | n += v 9 | res = max(res, n) 10 | return res -------------------------------------------------------------------------------- /src/1144-Decrease-Elements-To-Make-Array-Zigzag/1144.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def movesToMakeZigzag(self, nums: List[int]) -> int: 3 | nums = [float("inf")] + nums + [float("inf")] 4 | res = [0, 0] 5 | for i in range(1, len(nums) - 1): 6 | res[i % 2] += max(0, nums[i] - min(nums[i - 1], nums[i + 1]) + 1) 7 | return min(res) -------------------------------------------------------------------------------- /src/1207-Unique-Number-of-Occurrences/1207.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool uniqueOccurrences(vector& arr) 5 | { 6 | unordered_map m; 7 | unordered_set s; 8 | for (int n : arr) ++m[n]; 9 | for (auto& it : m) s.insert(it.second); 10 | return m.size() == s.size(); 11 | } 12 | }; -------------------------------------------------------------------------------- /src/1221-Split-a-String-in-Balanced-Strings/1221.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def balancedStringSplit(self, s: str) -> int: 3 | res = L = 0 4 | for i in s: 5 | if i == 'L': 6 | L += 1 7 | else: 8 | L -= 1 9 | if L == 0: 10 | res += 1 11 | return res -------------------------------------------------------------------------------- /src/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int subtractProductAndSum(int n) { 3 | int sum = 0, product = 1; 4 | while (n > 0) { 5 | sum += n % 10; 6 | product *= n % 10; 7 | n /= 10; 8 | } 9 | return product - sum; 10 | } 11 | } -------------------------------------------------------------------------------- /src/1401-Circle-and-Rectangle-Overlapping/1401.js: -------------------------------------------------------------------------------- 1 | var checkOverlap = function(r, xc, yc, x1, y1, x2, y2) { 2 | let x = (x1 + x2) / 2, y = (y1 + y2) / 2; 3 | let h = [x2 - x, y2 - y]; 4 | let v = [Math.abs(x - xc), Math.abs(y - yc)]; 5 | let u = [Math.max(v[0] - h[0], 0), Math.max(v[1] - h[1], 0)]; 6 | return r * r >= u[0] * u[0] + u[1] * u[1]; 7 | }; -------------------------------------------------------------------------------- /src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.go: -------------------------------------------------------------------------------- 1 | func numSteps(s string) int { 2 | res, add, n := 0, 0, len(s) 3 | 4 | for i := n - 1; i > 0; i-- { 5 | res++ 6 | if int(s[i] - '0') + add == 1 { 7 | add = 1 8 | res++ 9 | } 10 | } 11 | return res + add 12 | } -------------------------------------------------------------------------------- /src/1422-Maximum-Score-After-Splitting-a-String/1422.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxScore(self, s: str) -> int: 3 | zeros, ones, res, n = 0, 0, float("-inf"), len(s) 4 | for i in range(n): 5 | if s[i] == '1': ones += 1 6 | else: zeros += 1 7 | if i != n - 1: res = max(res, zeros - ones) 8 | return res + ones -------------------------------------------------------------------------------- /src/1423-Maximum-Points-You-Can-Obtain-from-Cards/1423.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxScore(self, cardPoints: List[int], k: int) -> int: 3 | res = cur = 0 4 | for i in range(-k, k): 5 | cur += cardPoints[i] 6 | if i >= 0: 7 | cur -= cardPoints[i - k] 8 | res = max(cur, res) 9 | return res -------------------------------------------------------------------------------- /src/1436-Destination-City/1436.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string destCity(vector>& paths) { 4 | unordered_set s; 5 | for (auto& path : paths) s.insert(path[0]); 6 | for (auto& path : paths) { 7 | if (!s.count(path[1])) return path[1]; 8 | } 9 | return ""; 10 | } 11 | }; -------------------------------------------------------------------------------- /src/1752-Check-if-Array-Is-Sorted-and-Rotated/1752.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool check(vector& nums) { 4 | int n = nums.size(); 5 | for (int i = 0, ring = 0; i < n; i++) { 6 | if (nums[i] > nums[(i + 1) % n]) { 7 | ring++; 8 | } 9 | if (ring > 1) return false; 10 | } 11 | return true; 12 | } 13 | }; -------------------------------------------------------------------------------- /src/0026-Remove-Duplicates-from-Sorted-Array/0026.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[]} nums 3 | * @return {number} 4 | */ 5 | var removeDuplicates = function (nums) { 6 | nums.forEach((element, i) => { 7 | if (element == nums[i + 1]) { 8 | nums.splice(i + 1, 1) 9 | removeDuplicates(nums); 10 | } 11 | }); 12 | return nums.length; 13 | }; 14 | -------------------------------------------------------------------------------- /src/0451-Sort-Characters-By-Frequency/0451.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def frequencySort(self, s): 3 | """ 4 | :type s: str 5 | :rtype: str 6 | """ 7 | return ''.join(c*-n for n,c in sorted((-s.count(c),c)for c in set(s))) 8 | 9 | 10 | if __name__ == "__main__": 11 | s = "abcc" 12 | print(Solution().frequencySort(s)) -------------------------------------------------------------------------------- /src/1109-Corporate-Flight-Bookings/1109.go: -------------------------------------------------------------------------------- 1 | func corpFlightBookings(bookings [][]int, n int) []int { 2 | res := make([]int, n + 1) 3 | for _, book := range bookings { 4 | res[book[0]-1] += book[2] 5 | res[book[1]] -= book[2] 6 | } 7 | for i := 1; i <= n; i++ { 8 | res[i] += res[i-1] 9 | } 10 | return res[:n] 11 | } -------------------------------------------------------------------------------- /src/1109-Corporate-Flight-Bookings/1109.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def corpFlightBookings(self, bookings: List[List[int]], n: int) -> List[int]: 3 | res = [0] * (n + 1) 4 | for i, j, k in bookings: 5 | res[i-1] += k 6 | res[j] -= k 7 | for i in range(1, len(res)): 8 | res[i] += res[i-1] 9 | return res[:-1] -------------------------------------------------------------------------------- /src/1147-Longest-Chunked-Palindrome-Decomposition/1147.go: -------------------------------------------------------------------------------- 1 | func longestDecomposition(text string) int { 2 | var n = len(text); 3 | for i := 1; i <= n/2; i++ { 4 | if text[:i] == text[n-i:] { 5 | return 2 + longestDecomposition(text[i:n-i]) 6 | } 7 | } 8 | if n == 0 { 9 | return 0 10 | } 11 | return 1 12 | } -------------------------------------------------------------------------------- /src/1165-Single-Row-Keyboard/1165.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def calculateTime(self, keyboard: str, word: str) -> int: 3 | d = dict() 4 | for i, v in enumerate(keyboard): 5 | d[v] = i 6 | 7 | res, pre = 0, 0 8 | for c in word: 9 | res += abs(d[c] - pre) 10 | pre = d[c] 11 | return res -------------------------------------------------------------------------------- /src/1184-Distance-Between-Bus-Stops/1184.js: -------------------------------------------------------------------------------- 1 | var distanceBetweenBusStops = function(d, l, r) { 2 | if (l > r) { 3 | var t = l; 4 | l = r, r = t; 5 | } 6 | var k = 0, v = 0; 7 | for (var i = 0; i < d.length; ++i) { 8 | if (i < l || i >= r) k += d[i]; 9 | else v += d[i]; 10 | } 11 | return Math.min(k, v); 12 | }; -------------------------------------------------------------------------------- /src/1289-Minimum-Falling-Path-Sum-II/1289.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minFallingPathSum(self, A: List[List[int]]) -> int: 3 | r, c = len(A), len(A[0]) 4 | for i in range(1, r): 5 | r = heapq.nsmallest(2, A[i - 1]) 6 | for j in range(c): 7 | A[i][j] += r[1] if A[i - 1][j] == r[0] else r[0] 8 | return min(A[-1]) -------------------------------------------------------------------------------- /src/1295-Find-Numbers-with-Even-Number-of-Digits/1295.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int findNumbers(int[] nums) { 3 | int res = 0; 4 | for (int i = 0; i < nums.length; i++) { 5 | double value = (double) nums[i]; 6 | double x = Math.log(value) / Math.log(10); 7 | if (((int)(x+1)) % 2 == 0) res++; 8 | } 9 | return res; 10 | } 11 | } -------------------------------------------------------------------------------- /src/1313-Decompress-Run-Length-Encoded-List/1313.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector decompressRLElist(vector& nums) 5 | { 6 | vector res; 7 | for (int i = 0; i < nums.size(); i += 2) 8 | { 9 | res.insert(res.end(), nums[i], nums[i + 1]); 10 | } 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /src/1315-Sum-of-Nodes-with-Even-Valued-Grandparent/1315.js: -------------------------------------------------------------------------------- 1 | var sumEvenGrandparent = function(root) { 2 | let dfs = function(node, p, gp) { 3 | if (node == null) return 0; 4 | return dfs(node.left, node.val, p) + 5 | dfs(node.right, node.val, p) + 6 | (gp % 2 == 0 ? node.val : 0); 7 | } 8 | return dfs(root, 1, 1); 9 | }; -------------------------------------------------------------------------------- /src/1362-Closest-Divisors/1362.go: -------------------------------------------------------------------------------- 1 | func closestDivisors(num int) []int { 2 | for i := int(math.Sqrt(float64(num + 2))); i > 0; i-- { 3 | if (num + 1) % i == 0 { 4 | return []int{i, (num + 1) / i} 5 | } 6 | if (num + 2) % i == 0 { 7 | return []int{i, (num + 2) / i} 8 | } 9 | } 10 | return []int{} 11 | } -------------------------------------------------------------------------------- /src/1392-Longest-Happy-Prefix/1392.js: -------------------------------------------------------------------------------- 1 | var longestPrefix = function(s) { 2 | let n = s.length, i = 0, j; 3 | let f = new Array(n + 1).fill(0); 4 | f[0] = j = -1; 5 | 6 | while (i < n) { 7 | while (j != -1 && s.charAt(i) != s.charAt(j)) j = f[j]; 8 | ++i; ++j; 9 | f[i] = j; 10 | } 11 | return s.substring(0, f[n]); 12 | }; -------------------------------------------------------------------------------- /src/1402-Reducing-Dishes/1402.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSatisfaction(int[] s) { 3 | Arrays.sort(s); 4 | int res = 0, total = 0, n = s.length; 5 | 6 | for (int i = n - 1; i >= 0 && s[i] > -total; --i) { 7 | total += s[i]; 8 | res += total; 9 | } 10 | return res; 11 | } 12 | } -------------------------------------------------------------------------------- /src/0242-Valid-Anagram/0242.go: -------------------------------------------------------------------------------- 1 | package validanagram 2 | 3 | func isAnagram(s string, t string) bool { 4 | if len(s) != len(t) { 5 | return false 6 | } 7 | mp := make(map[rune]int, 0) 8 | for _, v := range s { 9 | mp[v]++ 10 | } 11 | for _, v := range t { 12 | mp[v]-- 13 | if mp[v] < 0 { 14 | return false 15 | } 16 | } 17 | return true 18 | } 19 | -------------------------------------------------------------------------------- /src/0944-Delete-Columns-to-Make-Sorted/0944.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minDeletionSize(self, A): 3 | """ 4 | :type A: List[str] 5 | :rtype: int 6 | """ 7 | return sum(list(a) != sorted(a) for a in zip(*A)) 8 | 9 | if __name__ == "__main__": 10 | A = ["cba","daf","ghi"] 11 | print(Solution().minDeletionSize(A)) -------------------------------------------------------------------------------- /src/0946-Validate-Stack-Sequences/0946.go: -------------------------------------------------------------------------------- 1 | func validateStackSequences(pushed []int, popped []int) bool { 2 | i, j := 0, 0 3 | 4 | for _, v := range pushed { 5 | pushed[i] = v 6 | i++ 7 | for i > 0 && pushed[i - 1] == popped[j] { 8 | i-- 9 | j++ 10 | } 11 | } 12 | 13 | return i == 0 14 | } -------------------------------------------------------------------------------- /src/0997-Find-the-Town-Judge/0997.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findJudge(self, N: int, trust: List[List[int]]) -> int: 3 | count = [0] * (N + 1) 4 | for i, j in trust: 5 | count[i] -= 1 6 | count[j] += 1 7 | for i in range(1, N + 1): 8 | if count[i] == N - 1: 9 | return i 10 | return -1 -------------------------------------------------------------------------------- /src/1009-Complement-of-Base-10-Integer/1009.cpp: -------------------------------------------------------------------------------- 1 | static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }(); 2 | class Solution 3 | { 4 | public: 5 | int bitwiseComplement(int N) 6 | { 7 | int X = 1; 8 | while (X < N) 9 | { 10 | X = (X << 1) + 1; 11 | } 12 | return X ^ N; 13 | } 14 | }; -------------------------------------------------------------------------------- /src/1026-Maximum-Difference-Between-Node-and-Ancestor/1026.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxAncestorDiff(self, root: TreeNode, mn=float('inf'), mx=0) -> int: 3 | return max(self.maxAncestorDiff(root.left, min(mn, root.val), max(mx, root.val)), \ 4 | self.maxAncestorDiff(root.right, min(mn, root.val), max(mx, root.val))) \ 5 | if root else mx - mn -------------------------------------------------------------------------------- /src/1047-Remove-All-Adjacent-Duplicates-In-String/1047.go: -------------------------------------------------------------------------------- 1 | func removeDuplicates(S string) string { 2 | res := make([]rune, 0) 3 | for _, c := range S { 4 | if (len(res) > 0 && res[len(res)-1] == c) { 5 | res = res[:len(res)-1] 6 | } else { 7 | res = append(res, c) 8 | } 9 | } 10 | return string(res) 11 | } -------------------------------------------------------------------------------- /src/1071-Greatest-Common-Divisor-of-Strings/1071.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public String gcdOfStrings(String s1, String s2) 4 | { 5 | return (s1 + s2).compareTo(s2 + s1) == 0 ? s1.substring(0, gcd(s1.length(), s2.length())) : ""; 6 | } 7 | 8 | private int gcd(int a, int b) { 9 | return b > 0 ? gcd(b, a % b) : a; 10 | } 11 | } -------------------------------------------------------------------------------- /src/1165-Single-Row-Keyboard/1165.js: -------------------------------------------------------------------------------- 1 | var calculateTime = function(keyboard, word) { 2 | var d = {}; 3 | for (var i = 0; i < keyboard.length; ++i) { 4 | d[keyboard[i]] = i; 5 | } 6 | 7 | var res = 0, pre = 0; 8 | for (let c of word) { 9 | res += Math.abs(d[c] - pre); 10 | pre = d[c]; 11 | } 12 | return res; 13 | }; -------------------------------------------------------------------------------- /src/1213-Intersection-of-Three-Sorted-Arrays/1213.js: -------------------------------------------------------------------------------- 1 | var arraysIntersection = function(arr1, arr2, arr3) { 2 | let d = new Array(2001); 3 | d.fill(0); 4 | for (let x of arr1) d[x]++; 5 | for (let x of arr2) d[x]++; 6 | 7 | let res = []; 8 | for (let x of arr3) { 9 | if (d[x] == 2) res.push(x); 10 | } 11 | return res; 12 | }; -------------------------------------------------------------------------------- /src/1221-Split-a-String-in-Balanced-Strings/1221.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int balancedStringSplit(string s) 5 | { 6 | int L = 0, res = 0; 7 | for (char c : s) 8 | { 9 | if (c == 'L') L++; 10 | else L--; 11 | if (L == 0) res++; 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /src/1233-Remove-Sub-Folders-from-the-Filesystem/1233.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeSubfolders(self, folder: List[str]) -> List[str]: 3 | folder.sort() 4 | res, l, parent = [], 2, '' 5 | for f in folder: 6 | if f[:l] != parent: 7 | l, parent = len(f) + 1, f + '/' 8 | res.append(f) 9 | return res -------------------------------------------------------------------------------- /src/1290-Convert-Binary-Number-in-a-Linked-List-to-Integer/1290.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int getDecimalValue(ListNode* head) 5 | { 6 | int res = 0; 7 | while (head != nullptr) 8 | { 9 | res = (res << 1) | head->val; 10 | head = head->next; 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /src/1351-Count-Negative-Numbers-in-a-Sorted-Matrix/1351.js: -------------------------------------------------------------------------------- 1 | var countNegatives = function(grid) { 2 | let r = grid.length, c = grid[0].length; 3 | let res = 0, m = r - 1, n = 0; 4 | 5 | while (m >= 0 && n < c) { 6 | if (grid[m][n] < 0) { 7 | res += c - n; 8 | m--; 9 | } else n++; 10 | } 11 | return res; 12 | }; -------------------------------------------------------------------------------- /src/1356-Sort-Integers-by-The-Number-of-1-Bits/1356.js: -------------------------------------------------------------------------------- 1 | var sortByBits = function(arr) { 2 | let bin = function(x) { 3 | let res = 0; 4 | while (x) { 5 | x = x & (x - 1); 6 | res++; 7 | } 8 | return res; 9 | } 10 | arr.sort((a, b) => (bin(a) * 10000 + a) - (bin(b) * 10000 + b)); 11 | return arr; 12 | }; -------------------------------------------------------------------------------- /src/1375-Bulb-Switcher-III/1375.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int numTimesAllBlue(vector& light) 5 | { 6 | int res = 0, maxv = 0; 7 | for (int i = 0; i < light.size(); i++) 8 | { 9 | maxv = max(maxv, light[i]); 10 | if (maxv == i + 1) res++; 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /src/1411-Number-of-Ways-to-Paint-N×3-Grid/1411.js: -------------------------------------------------------------------------------- 1 | var numOfWays = function(n) { 2 | let aba = 6, abc = 6, t_aba, t_abc, mod = 1000000007; 3 | for (let i = 0; i < n - 1; i++) { 4 | t_aba = aba * 3 + abc * 2; 5 | t_abc = aba * 2 + abc * 2; 6 | aba = t_aba % mod; 7 | abc = t_abc % mod; 8 | } 9 | return (aba + abc) % mod; 10 | }; -------------------------------------------------------------------------------- /src/1832-Check-if-the-Sentence-is-Pangram/1832.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool checkIfPangram(string sentence) { 4 | int cnt[26] = {}; 5 | for (auto c : sentence) { 6 | cnt[c - 'a']++; 7 | } 8 | bool res = true; 9 | for (int i = 0; i < 26; i++) { 10 | res &= (cnt[i] >= 1); 11 | } 12 | return res; 13 | } 14 | }; 15 | -------------------------------------------------------------------------------- /src/0793-Preimage-Size-of-Factorial-Zeroes-Function/0793.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def preimageSizeFZF(self, K: 'int') -> 'int': 3 | x = 1 4 | while x < K: 5 | x = x * 5 + 1 6 | 7 | while x > 1: 8 | K %= x 9 | if x - 1 == K: 10 | return 0 11 | x = (x - 1) // 5 12 | return 5 -------------------------------------------------------------------------------- /src/0876-Middle-of-the-Linked-List/0876.go: -------------------------------------------------------------------------------- 1 | func middleNode(head *ListNode) *ListNode { 2 | fast, slow := head, head 3 | for fast.Next != nil && fast.Next.Next != nil { 4 | fast = fast.Next.Next; 5 | slow = slow.Next; 6 | } 7 | 8 | if fast.Next != nil && fast.Next.Next == nil { 9 | return slow.Next 10 | } 11 | return slow 12 | } -------------------------------------------------------------------------------- /src/0965-Univalued-Binary-Tree/0965.cpp: -------------------------------------------------------------------------------- 1 | static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }(); 2 | class Solution 3 | { 4 | public: 5 | bool isUnivalTree(TreeNode* r, int v = -1) 6 | { 7 | return r == nullptr || ((v == -1 || r->val == v) && 8 | isUnivalTree(r->left, r->val) && isUnivalTree(r->right, r->val)); 9 | } 10 | }; -------------------------------------------------------------------------------- /src/1006-Clumsy-Factorial/1006.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def clumsy(self, N: int) -> int: 3 | if N <= 2: 4 | return N 5 | if N <= 4: 6 | return N + 3 7 | 8 | if (N - 4) % 4 == 0: 9 | return N + 1 10 | elif (N - 4) % 4 <= 2: 11 | return N + 2 12 | else: 13 | return N - 1 -------------------------------------------------------------------------------- /src/1038-Binary-Search-Tree-to-Greater-Sum-Tree/1038.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | pre = 0 3 | def bstToGst(self, root: TreeNode) -> TreeNode: 4 | if root.right: 5 | self.bstToGst(root.right) 6 | 7 | root.val = self.pre = self.pre + root.val 8 | if root.left: 9 | self.bstToGst(root.left) 10 | return root -------------------------------------------------------------------------------- /src/1154-Day-of-the-Year/1154.js: -------------------------------------------------------------------------------- 1 | var dayOfYear = function(date) { 2 | var date = date.split("-"); 3 | var Y = Number(date[0]), M = Number(date[1]) - 1; 4 | var days = [31,28,31,30,31,30,31,31,30,31,30,31], res = Number(date[2]); 5 | if (Y % 400 == 0 || Y % 4 == 0 && Y % 100 != 0) days[1]++; 6 | for (var i = 0; i < M; ++i) res += days[i]; 7 | return res; 8 | }; -------------------------------------------------------------------------------- /src/1266-Minimum-Time-Visiting-All-Points/1266.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int minTimeToVisitAllPoints(vector>& p) 5 | { 6 | int res = 0; 7 | for (int i = 1; i < p.size(); i++) 8 | { 9 | res += max(abs(p[i][0] - p[i-1][0]), abs(p[i][1] - p[i-1][1])); 10 | } 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /src/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number} n 3 | * @return {number} 4 | */ 5 | var subtractProductAndSum = function(n) { 6 | let sum = 0, product = 1; 7 | while (n > 0) { 8 | sum += n % 10; 9 | product *= n % 10; 10 | n = Math.floor(n / 10); 11 | } 12 | return product - sum; 13 | }; -------------------------------------------------------------------------------- /src/1346-Check-If-N-and-Its-Double-Exist/1346.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean checkIfExist(int[] arr) { 3 | Set seen = new HashSet(); 4 | for (int i : arr) { 5 | if (seen.contains(i * 2) || (i % 2 == 0 && seen.contains(i / 2))) return true; 6 | seen.add(i); 7 | } 8 | return false; 9 | } 10 | } -------------------------------------------------------------------------------- /src/1392-Longest-Happy-Prefix/1392.go: -------------------------------------------------------------------------------- 1 | func longestPrefix(s string) string { 2 | n := len(s) 3 | i, j := 0, -1 4 | f := make([]int, n + 1) 5 | f[0] = -1 6 | 7 | for i < n { 8 | for j != -1 && s[i] != s[j] { 9 | j = f[j] 10 | } 11 | i++ 12 | j++ 13 | f[i] = j 14 | } 15 | return s[:f[n]] 16 | } -------------------------------------------------------------------------------- /src/1399-Count-Largest-Group/1399.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countLargestGroup(self, n: int) -> int: 3 | cnt, mx, res = collections.defaultdict(int), 0, 0 4 | for i in range(1, n + 1): 5 | t = sum([int(j) for j in str(i)]) 6 | cnt[t] += 1 7 | mx = max(mx, cnt[t]) 8 | 9 | return sum([i == mx for i in cnt.values()]) -------------------------------------------------------------------------------- /src/0136-Single-Number/0136.py: -------------------------------------------------------------------------------- 1 | from functools import reduce 2 | from operator import xor 3 | class Solution: 4 | def singleNumber(self, nums): 5 | """ 6 | :type nums: List[int] 7 | :rtype: int 8 | """ 9 | return reduce(xor, nums) 10 | 11 | if __name__ == "__main__": 12 | nums = [2,2,1] 13 | print(Solution().singleNumber(nums)) -------------------------------------------------------------------------------- /src/0274-H-Index/0274.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def hIndex(self, citations: List[int]) -> int: 3 | n = len(citations) 4 | cnt = [0] * (n + 1) 5 | for c in citations: 6 | cnt[min(n, c)] += 1 7 | 8 | i, total = n, cnt[-1] 9 | while total < i: 10 | i -= 1 11 | total += cnt[i] 12 | return i -------------------------------------------------------------------------------- /src/0790-Domino-and-Tromino-Tiling/0790.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numTilings(int n) { 4 | if (n == 1) return 1; 5 | int mod = 1e9 + 7; 6 | long long a = 1, b = 1, c = 2; 7 | for (int i = 3; i <= n; ++i) { 8 | long long d = (c * 2 + a) % mod; 9 | a = b; 10 | b = c; 11 | c = d; 12 | } 13 | return c; 14 | } 15 | }; -------------------------------------------------------------------------------- /src/1099-Two-Sum-Less-Than-K/1099.js: -------------------------------------------------------------------------------- 1 | var twoSumLessThanK = function(A, K) { 2 | var res = -1, n = A.length; 3 | A.sort(); 4 | var l = 0, r = n - 1; 5 | while (l < r) { 6 | var t = (A[l] + A[r]); 7 | if (t >= K) r--; 8 | else { 9 | res = Math.max(res, t); 10 | l++; 11 | } 12 | } 13 | return res; 14 | } -------------------------------------------------------------------------------- /src/1111-Maximum-Nesting-Depth-of-Two-Valid-Parentheses-Strings/1111.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector maxDepthAfterSplit(string seq) 5 | { 6 | vector res(seq.size(), 0); 7 | for (int i = 0; i < seq.size(); ++i) 8 | { 9 | res[i] = i & 1 ^ (seq[i] == ')'); 10 | } 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /src/1133-Largest-Unique-Number/1133.go: -------------------------------------------------------------------------------- 1 | func largestUniqueNumber(A []int) int { 2 | sort.Ints(A) 3 | for len(A) > 1 && A[len(A)-1] == A[len(A)-2] { 4 | bk := A[len(A)-1] 5 | for len(A) > 0 && A[len(A)-1] == bk { 6 | A = A[:len(A)-1] 7 | } 8 | } 9 | if len(A) > 0 { 10 | return A[len(A)-1] 11 | } 12 | return -1 13 | } -------------------------------------------------------------------------------- /src/1144-Decrease-Elements-To-Make-Array-Zigzag/1144.js: -------------------------------------------------------------------------------- 1 | var movesToMakeZigzag = function(nums) { 2 | var res = [0, 0]; 3 | nums.unshift(Number.MAX_VALUE); 4 | nums.push(Number.MAX_VALUE); 5 | for (var i = 1; i < nums.length - 1; ++i) { 6 | res[i % 2] += Math.max(0, nums[i] - Math.min(nums[i-1], nums[i+1]) + 1); 7 | } 8 | return Math.min(res[0], res[1]); 9 | }; -------------------------------------------------------------------------------- /src/1237-Find-Positive-Integer-Solution-for-a-Given-Equation/1237.js: -------------------------------------------------------------------------------- 1 | var findSolution = function(customfunction, z) { 2 | let res = [], y = 1000; 3 | for (let x = 1; x <= 1000; x++) { 4 | while (y > 1 && customfunction.f(x, y) > z) { 5 | y--; 6 | } 7 | if (customfunction.f(x, y) == z) res.push([x, y]); 8 | } 9 | return res; 10 | }; -------------------------------------------------------------------------------- /src/1260-Shift-2D-Grid/1260.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def shiftGrid(self, grid: List[List[int]], k: int) -> List[List[int]]: 3 | n, m = len(grid), len(grid[0]) 4 | res = [[0] * m for _ in range(n)] 5 | for i in range(n): 6 | for j in range(m): 7 | t = j + k 8 | res[(i + t//m)%n][t % m] = grid[i][j] 9 | return res -------------------------------------------------------------------------------- /src/1283-Find-the-Smallest-Divisor-Given-a-Threshold/1283.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def smallestDivisor(self, A: List[int], t: int) -> int: 3 | l, r = 1, 10**6 4 | while l < r: 5 | m = l + r >> 1 6 | if sum((i + m - 1) // m for i in A) > t: 7 | l = m + 1 8 | else: 9 | r = m 10 | return l -------------------------------------------------------------------------------- /src/1295-Find-Numbers-with-Even-Number-of-Digits/1295.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int findNumbers(vector& nums) 5 | { 6 | int res = 0; 7 | for (int n : nums) 8 | { 9 | int x = 0; 10 | while (n) n /= 10, x++; 11 | if (x % 2 == 0) ++res; 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /src/1374-Generate-a-String-With-Characters-That-Have-Odd-Counts/1374.go: -------------------------------------------------------------------------------- 1 | func generateTheString(n int) string { 2 | res := []byte{} 3 | for i := 0; i < n - 1; i++ { 4 | res = append(res, 'a') 5 | } 6 | 7 | if n % 2 == 1 { 8 | res = append(res, 'a') 9 | } else { 10 | res = append(res, 'b') 11 | } 12 | return string(res) 13 | } -------------------------------------------------------------------------------- /src/1403-Minimum-Subsequence-in-Non-Increasing-Order/1403.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def minSubsequence(self, nums: List[int]) -> List[int]: 3 | nums.sort(reverse=True) 4 | res, cur, t = [], 0, sum(nums) / 2 5 | 6 | for i in nums: 7 | cur += i 8 | res.append(i) 9 | if cur > t: return res 10 | return res -------------------------------------------------------------------------------- /src/1413-Minimum-Value-to-Get-Positive-Step-by-Step-Sum/1413.go: -------------------------------------------------------------------------------- 1 | func minStartValue(nums []int) int { 2 | pref, min_pref := 0, 0 3 | for _, i := range nums { 4 | pref += i 5 | min_pref = min(min_pref, i) 6 | } 7 | return 1 - min_pref 8 | } 9 | 10 | func min(a, b int) int { 11 | if a < b { 12 | return a 13 | } 14 | return b 15 | } -------------------------------------------------------------------------------- /src/1414-Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/1414.go: -------------------------------------------------------------------------------- 1 | func findMinFibonacciNumbers(k int) int { 2 | res, a, b := 0, 1, 1 3 | for b <= k { 4 | a, b = b, a + b 5 | } 6 | 7 | for a > 0 { 8 | if a <= k { 9 | k -= a 10 | res++ 11 | } 12 | a, b = b - a, a 13 | } 14 | return res 15 | } -------------------------------------------------------------------------------- /src/1739-Building-Boxes/1739.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumBoxes(int n) { 4 | int x = cbrt(6L * n); 5 | int res = x * (x + 1) / 2; 6 | int max_n = x * (x + 1ll) * (x + 2ll) / 6ll; 7 | if (max_n > n) { 8 | max_n -= res; 9 | res -= x; 10 | } 11 | return res + ceil((-1 + sqrt(1 + 8 * (n - max_n))) / 2); 12 | } 13 | }; 14 | -------------------------------------------------------------------------------- /src/2244-Minimum-Rounds-to-Complete-All-Tasks/2244.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minimumRounds(vector& tasks) { 4 | unordered_map cnt; 5 | for (auto& task : tasks) cnt[task]++; 6 | int res = 0; 7 | for (auto& [k, v] : cnt) { 8 | if (v == 1) return -1; 9 | res += (v + 2) / 3; 10 | } 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /src/2341-Maximum-Number-of-Pairs-in-Array/2341.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | vector numberOfPairs(vector& nums) { 4 | int a[101] = {}; 5 | for (auto x : nums) { 6 | a[x]++; 7 | } 8 | int res = 0; 9 | for (int i = 0; i <= 100; i++) { 10 | res += a[i] / 2; 11 | } 12 | return {res, (int)nums.size() - res * 2}; 13 | } 14 | }; -------------------------------------------------------------------------------- /test/0062-test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../src/0062-Unique-Paths/0062.cpp" 5 | 6 | TEST(test, test1) 7 | { 8 | ASSERT_EQ(1, Solution().uniquePaths(1, 2)); 9 | } 10 | 11 | GTEST_API_ int main(int argc, char **argv) 12 | { 13 | testing::InitGoogleTest(&argc, argv); 14 | return RUN_ALL_TESTS(); 15 | } 16 | -------------------------------------------------------------------------------- /src/0169-Majority-Element/0169.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn majority_element(nums: Vec) -> i32 { 3 | let mut cnt = 0; 4 | let mut res = 0; 5 | for num in nums.into_iter() { 6 | if cnt == 0 { 7 | res = num; 8 | } 9 | cnt += if res == num {1} else {-1} 10 | } 11 | res 12 | } 13 | } -------------------------------------------------------------------------------- /src/0365-Water-and-Jug-Problem/0365.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canMeasureWater(int x, int y, int z) { 3 | if (x + y < z) return false; 4 | if (x == 0 || y == 0) return z == 0 || x + y == z; 5 | return z % gcd(x, y) == 0; 6 | } 7 | 8 | private int gcd(int a, int b) { 9 | return b == 0 ? a : gcd(b, a % b); 10 | } 11 | } -------------------------------------------------------------------------------- /src/0828-Unique-Letter-String/0828.go: -------------------------------------------------------------------------------- 1 | func uniqueLetterString(S string) int { 2 | res, f, mod := 0, 0, 1000000007 3 | m := make([]int, 26) 4 | n := make([]int, 26) 5 | for i, v := range S { 6 | t := int(v) - 65 7 | f = f + 1 + i - 2 * m[t] + n[t] 8 | res = (res + f) % mod 9 | n[t], m[t] = m[t], i + 1 10 | } 11 | return res 12 | } -------------------------------------------------------------------------------- /src/0976-Largest-Perimeter-Triangle/0976.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def largestPerimeter(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: int 6 | """ 7 | A.sort(reverse=True) 8 | for i in range(len(A)-2): 9 | if A[i+2] + A[i+1] > A[i]: 10 | return A[i] + A[i+1] + A[i+2] 11 | 12 | return 0 -------------------------------------------------------------------------------- /src/1004-Max-Consecutive-Ones-III/1004.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestOnes(self, A: List[int], K: int) -> int: 3 | n = len(A) 4 | zeros = res = l = 0 5 | for r in range(n): 6 | zeros += 1 - A[r] 7 | if zeros > K: 8 | zeros -= 1 - A[l] 9 | l += 1 10 | res = r - l + 1 11 | return res -------------------------------------------------------------------------------- /src/1046-Last-Stone-Weight/1046.py: -------------------------------------------------------------------------------- 1 | import bisect 2 | class Solution: 3 | def lastStoneWeight(self, stones: List[int]) -> int: 4 | stones.sort() 5 | while len(stones) > 1: 6 | t1 = stones.pop() 7 | t2 = stones.pop() 8 | if t1 != t2: 9 | bisect.insort_left(stones, t1 - t2) 10 | return stones[0] if stones else 0 -------------------------------------------------------------------------------- /src/1072-Flip-Columns-For-Maximum-Number-of-Equal-Rows/1072.js: -------------------------------------------------------------------------------- 1 | var maxEqualRowsAfterFlips = function(matrix) { 2 | let m = new Map(), res = 0; 3 | 4 | for (let row of matrix) { 5 | let s = ""; 6 | for (let r of row) s += (r ^ row[0]); 7 | m.set(s, (m.get(s) || 0) + 1); 8 | res = Math.max(res, m.get(s)); 9 | } 10 | return res; 11 | }; -------------------------------------------------------------------------------- /src/1233-Remove-Sub-Folders-from-the-Filesystem/1233.js: -------------------------------------------------------------------------------- 1 | var removeSubfolders = function(folder) { 2 | folder.sort(); 3 | let res = [], l = 2, parent = ''; 4 | for (let f of folder) { 5 | if (f.substr(0, l) != parent) { 6 | l = f.length + 1; 7 | parent = f + '/'; 8 | res.push(f); 9 | } 10 | } 11 | return res; 12 | }; -------------------------------------------------------------------------------- /src/1250-Check-If-It-Is-a-Good-Array/1250.java: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public boolean isGoodArray(int[] nums) 4 | { 5 | int x = nums[0], y; 6 | for (int a : nums) { 7 | while (a > 0) { 8 | y = x % a; 9 | x = a; 10 | a = y; 11 | } 12 | } 13 | return x == 1; 14 | } 15 | } -------------------------------------------------------------------------------- /src/1297-Maximum-Number-of-Occurrences-of-a-Substring/1297.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def maxFreq(self, s: str, maxLetters: int, minSize: int, maxSize: int) -> int: 3 | cnt = collections.Counter([s[i:i + minSize] for i in range(len(s) - minSize + 1)]) 4 | for k, v in cnt.most_common(): 5 | if len(set(k)) <= maxLetters: 6 | return v 7 | return 0 -------------------------------------------------------------------------------- /src/1325-Delete-Leaves-With-a-Given-Value/1325.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public TreeNode removeLeafNodes(TreeNode root, int target) { 3 | if (root == null) return null; 4 | root.left = removeLeafNodes(root.left, target); 5 | root.right = removeLeafNodes(root.right, target); 6 | return root.val == target && root.left == root.right ? null : root; 7 | } 8 | } -------------------------------------------------------------------------------- /src/1328-Break-a-Palindrome/1328.go: -------------------------------------------------------------------------------- 1 | func breakPalindrome(palindrome string) string { 2 | p := []byte(palindrome) 3 | for i := 0; i < len(p)/2; i++ { 4 | if p[i] != 'a' { 5 | p[i] = 'a' 6 | return string(p) 7 | } 8 | } 9 | if len(p) < 2 { 10 | return "" 11 | } 12 | p[len(p) - 1] = 'b' 13 | return string(p) 14 | } -------------------------------------------------------------------------------- /src/1333-Filter-Restaurants-by-Vegan-Friendly,-Price-and-Distance/1333.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def filterRestaurants(self, restaurants: List[List[int]], veganFriendly: int, maxPrice: int, maxDistance: int) -> List[int]: 3 | restaurants.sort(key=lambda x: (-x[1], -x[0])) 4 | return [i for i, _, v, p, d in restaurants if v >= veganFriendly and p <= maxPrice and d <= maxDistance] -------------------------------------------------------------------------------- /src/1351-Count-Negative-Numbers-in-a-Sorted-Matrix/1351.go: -------------------------------------------------------------------------------- 1 | func countNegatives(grid [][]int) int { 2 | r, c := len(grid), len(grid[0]) 3 | res, m, n := 0, r - 1, 0 4 | 5 | for m >= 0 && n < c { 6 | if grid[m][n] < 0 { 7 | res += c - n 8 | m-- 9 | } else { 10 | n++ 11 | } 12 | } 13 | return res 14 | } -------------------------------------------------------------------------------- /src/1400-Construct-K-Palindrome-Strings/1400.go: -------------------------------------------------------------------------------- 1 | func canConstruct(s string, k int) bool { 2 | odds, n := 0, len(s) 3 | cnt := make([]int, 26) 4 | for i := 0; i < n; i++ { 5 | cnt[s[i] - 'a'] ^= 1 6 | if cnt[s[i] - 'a'] > 0 { 7 | odds++ 8 | } else { 9 | odds-- 10 | } 11 | } 12 | return odds <= k && k <= n 13 | } -------------------------------------------------------------------------------- /src/1401-Circle-and-Rectangle-Overlapping/1401.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def checkOverlap(self, r: int, xc: int, yc: int, x1: int, y1: int, x2: int, y2: int) -> bool: 3 | x, y = (x1 + x2) / 2, (y1 + y2) / 2 4 | h = [x2 - x, y2 - y] 5 | v = [abs(x - xc), abs(y - yc)] 6 | u = [max(v[0] - h[0], 0), max(v[1] - h[1], 0)] 7 | return r**2 >= (u[0]**2 + u[1]**2) -------------------------------------------------------------------------------- /src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.js: -------------------------------------------------------------------------------- 1 | var numSteps = function(s) { 2 | let res = 0, add = 0, n = s.length; 3 | 4 | for (let i = n - 1; i > 0; i--) { 5 | res++; 6 | if (s.charCodeAt(i) - 48 + add == 1) { 7 | add = 1; 8 | res++; 9 | } 10 | } 11 | return res + add; 12 | }; -------------------------------------------------------------------------------- /src/1827-Minimum-Operations-to-Make-the-Array-Increasing/1827.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int minOperations(vector& nums) { 4 | int res = 0; 5 | int p = 0; 6 | for (auto& num : nums) { 7 | if (num <= p) { 8 | p++; 9 | res += p - num; 10 | } else { 11 | p = num; 12 | } 13 | } 14 | return res; 15 | } 16 | }; -------------------------------------------------------------------------------- /src/2315-Count-Asterisks/2315.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int countAsterisks(string s) { 4 | int res = 0, n = s.size(); 5 | for (int i = 0; i < n; i++) { 6 | if (s[i] == '|') { 7 | do { 8 | i++; 9 | } while (i < n && s[i] != '|'); 10 | } else if (s[i] == '*') 11 | res++; 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /src/2331-Evaluate-Boolean-Binary-Tree/2331.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool evaluateTree(TreeNode* root) { 4 | if (root->val == 0) return false; 5 | if (root->val == 1) return true; 6 | if (root->val == 2) 7 | return evaluateTree(root->left) || evaluateTree(root->right); 8 | return evaluateTree(root->left) && evaluateTree(root->right); 9 | } 10 | }; -------------------------------------------------------------------------------- /test/0050-test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../src/0050-Pow(x,n)/0050.cpp" 5 | 6 | TEST(test, test1) 7 | { 8 | ASSERT_EQ(1024.00000, Solution().myPow(2.00000, 10)); 9 | } 10 | 11 | GTEST_API_ int main(int argc, char **argv) 12 | { 13 | testing::InitGoogleTest(&argc, argv); 14 | return RUN_ALL_TESTS(); 15 | } 16 | -------------------------------------------------------------------------------- /test/0067-test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../src/0067-Add-Binary/0067.cpp" 5 | 6 | TEST(test, test1) 7 | { 8 | ASSERT_EQ("100", Solution().addBinary("11", "1")); 9 | } 10 | 11 | GTEST_API_ int main(int argc, char **argv) 12 | { 13 | testing::InitGoogleTest(&argc, argv); 14 | return RUN_ALL_TESTS(); 15 | } 16 | -------------------------------------------------------------------------------- /src/0121-Best-Time-to-Buy-and-Sell-Stock/0121.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func maxProfit(prices []int) int { 4 | ans, mx := 0, prices[len(prices)-1] 5 | for i := len(prices) - 2; i >= 0; i-- { 6 | ans = max(ans, mx-prices[i]) 7 | mx = max(mx, prices[i]) 8 | } 9 | return ans 10 | } 11 | 12 | func max(a, b int) int { 13 | if a > b { 14 | return a 15 | } 16 | return b 17 | } 18 | -------------------------------------------------------------------------------- /src/0274-H-Index/0274.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int hIndex(vector& citations) 5 | { 6 | int n = citations.size(); 7 | vector cnt(n + 1, 0); 8 | for (int c : citations) cnt[min(c, n)]++; 9 | 10 | int i = n; 11 | for (int total = cnt[n]; i > total; total += cnt[i]) --i; 12 | return i; 13 | } 14 | }; -------------------------------------------------------------------------------- /src/0594-Longest-Harmonious-Subsequence/0594.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLHS(vector& nums) { 4 | unordered_map cnt; 5 | int res = 0; 6 | for (int num : nums) cnt[num]++; 7 | for (auto& [k, v] : cnt) { 8 | if (cnt.count(k + 1)) { 9 | res = max(res, v + cnt[k + 1]); 10 | } 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /src/0594-longest-harmonious-subsequence/0594.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int findLHS(vector& nums) { 4 | unordered_map cnt; 5 | int res = 0; 6 | for (int num : nums) cnt[num]++; 7 | for (auto& [k, v] : cnt) { 8 | if (cnt.count(k + 1)) { 9 | res = max(res, v + cnt[k + 1]); 10 | } 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /src/0876-Middle-of-the-Linked-List/0876.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def middleNode(self, head: ListNode) -> ListNode: 3 | fast, slow = head, head 4 | while fast.next and fast.next.next: 5 | fast = fast.next.next 6 | slow = slow.next 7 | 8 | if fast.next and not fast.next.next: 9 | return slow.next 10 | return slow -------------------------------------------------------------------------------- /src/1021-Remove-Outermost-Parentheses/1021.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def removeOuterParentheses(self, S: str) -> str: 3 | res, opened = "", 0 4 | for c in S: 5 | if c == '(' and opened > 0: 6 | res += c 7 | if c == ')' and opened > 1: 8 | res += c 9 | opened += 1 if c == '(' else -1 10 | return res -------------------------------------------------------------------------------- /src/1085-Sum-of-Digits-in-the-Minimum-Number/1085.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int sumOfDigits(vector& A) 5 | { 6 | int min_A = *min_element(A.begin(), A.end()); 7 | int res = 0; 8 | while (min_A) 9 | { 10 | res += min_A%10; 11 | min_A /= 10; 12 | } 13 | return !(res & 1); 14 | } 15 | }; -------------------------------------------------------------------------------- /src/1089-Duplicate-Zeros/1089.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | void duplicateZeros(vector& arr) 5 | { 6 | int n = arr.size(), j = n + count(arr.begin(), arr.end(), 0); 7 | for (int i = n - 1; i >= 0; --i) 8 | { 9 | if (--j < n) arr[j] = arr[i]; 10 | if (arr[i] == 0 and --j < n) arr[j] = 0; 11 | } 12 | } 13 | }; -------------------------------------------------------------------------------- /src/1104-Path-In-Zigzag-Labelled-Binary-Tree/1104.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def pathInZigZagTree(self, label: int) -> List[int]: 3 | res = list() 4 | while 1: 5 | res.append(label) 6 | if label == 1: 7 | break 8 | label >>= 1 9 | label = label ^ (1 << (label.bit_length() - 1)) - 1 10 | return res[::-1] -------------------------------------------------------------------------------- /src/1167-Minimum-Cost-to-Connect-Sticks/1167.py: -------------------------------------------------------------------------------- 1 | import heapq 2 | class Solution: 3 | def connectSticks(self, sticks: List[int]) -> int: 4 | heapq.heapify(sticks) 5 | res = 0 6 | while len(sticks) > 1: 7 | a, b = heapq.heappop(sticks), heapq.heappop(sticks) 8 | res += a + b 9 | heapq.heappush(sticks, a + b) 10 | return res -------------------------------------------------------------------------------- /src/1176-Diet-Plan-Performance/1176.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def dietPlanPerformance(self, calories: List[int], k: int, lower: int, upper: int) -> int: 3 | T = sum(calories[:k]) 4 | res = (T > upper) - (T < lower) 5 | for i in range(k, len(calories)): 6 | T += calories[i] - calories[i - k] 7 | res += (T > upper) - (T < lower) 8 | return res -------------------------------------------------------------------------------- /src/1281-Subtract-the-Product-and-Sum-of-Digits-of-an-Integer/1281.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int subtractProductAndSum(int n) 5 | { 6 | int sum = 0, product = 1; 7 | while (n) 8 | { 9 | sum += n % 10; 10 | product *= n % 10; 11 | n /= 10; 12 | } 13 | return product - sum; 14 | } 15 | }; -------------------------------------------------------------------------------- /src/1288-Remove-Covered-Intervals/1288.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int removeCoveredIntervals(int[][] A) { 3 | int res = 0, r = 0; 4 | Arrays.sort(A, (a, b) -> a[0] != b[0] ? a[0] - b[0] : b[1] - a[1]); 5 | for (int[] v : A) { 6 | if (v[1] > r) ++res; 7 | r = Math.max(r, v[1]); 8 | } 9 | return res; 10 | } 11 | } -------------------------------------------------------------------------------- /src/1288-Remove-Covered-Intervals/1288.js: -------------------------------------------------------------------------------- 1 | /** 2 | * @param {number[][]} intervals 3 | * @return {number} 4 | */ 5 | var removeCoveredIntervals = function(A) { 6 | let res = 0, r = 0; 7 | A.sort((a, b) => a[0] != b[0] ? a[0] - b[0] : b[1] - a[1]); 8 | for (let v of A) { 9 | if (v[1] > r) res++; 10 | r = Math.max(r, v[1]); 11 | } 12 | return res; 13 | }; -------------------------------------------------------------------------------- /src/1299-Replace-Elements-with-Greatest-Element-on-Right-Side/1299.go: -------------------------------------------------------------------------------- 1 | func replaceElements(arr []int) []int { 2 | max_right := -1 3 | for i := len(arr) - 1; i >= 0; i-- { 4 | max_right, arr[i] = max(arr[i], max_right), max_right 5 | } 6 | return arr 7 | } 8 | 9 | func max(a, b int) int { 10 | if a > b { 11 | return a 12 | } 13 | return b 14 | } -------------------------------------------------------------------------------- /src/1402-Reducing-Dishes/1402.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int maxSatisfaction(vector& s) { 4 | sort(s.begin(), s.end()); 5 | int res = 0, total = 0, n = s.size(); 6 | 7 | for (int i = n - 1; i >= 0 && s[i] > -total; --i) { 8 | total += s[i]; 9 | res += total; 10 | } 11 | return res; 12 | } 13 | }; -------------------------------------------------------------------------------- /test/0096-test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../src/0096-Unique-Binary-Search-Trees/0096.cpp" 5 | 6 | TEST(test, test1) 7 | { 8 | ASSERT_EQ(5, Solution().numTrees(3)); 9 | } 10 | 11 | GTEST_API_ int main(int argc, char **argv) 12 | { 13 | testing::InitGoogleTest(&argc, argv); 14 | return RUN_ALL_TESTS(); 15 | } 16 | -------------------------------------------------------------------------------- /src/0278-First-Bad-Version/0278.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def firstBadVersion(self, n): 3 | """ 4 | :type n: int 5 | :rtype: int 6 | """ 7 | l, r = 0, n 8 | while l < r: 9 | mid = (l + r) >> 1 10 | if isBadVersion(mid): 11 | r = mid 12 | else: 13 | l = mid + 1 14 | return l -------------------------------------------------------------------------------- /src/0969-Pancake-Sorting/0969.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def pancakeSort(self, A): 3 | """ 4 | :type A: List[int] 5 | :rtype: List[int] 6 | """ 7 | res = list() 8 | for i in range(len(A), 1, -1): 9 | idx = A.index(i) 10 | A = A[:idx:-1] + A[:idx] 11 | res += [idx + 1, i] 12 | 13 | return res -------------------------------------------------------------------------------- /src/1299-Replace-Elements-with-Greatest-Element-on-Right-Side/1299.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector replaceElements(vector& arr) 5 | { 6 | int max_right = -1; 7 | for (int i = arr.size() - 1; i >= 0; i--) 8 | { 9 | max_right = max(arr[i], exchange(arr[i], max_right)); 10 | } 11 | return arr; 12 | } 13 | }; -------------------------------------------------------------------------------- /src/1346-Check-If-N-and-Its-Double-Exist/1346.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool checkIfExist(vector& arr) 5 | { 6 | unordered_set seen; 7 | for (int i : arr) 8 | { 9 | if (seen.count(2*i) || (i % 2 == 0 && seen.count(i / 2))) return true; 10 | seen.insert(i); 11 | } 12 | return false; 13 | } 14 | }; -------------------------------------------------------------------------------- /src/1404-Number-of-Steps-to-Reduce-a-Number-in-Binary-Representation-to-One/1404.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def numSteps(self, s: str) -> int: 3 | res, add, n = 0, 0, len(s) 4 | for i in range(n - 1, 0, -1): 5 | res += 1 6 | if (1 if s[i] == '1' else 0) + add == 1: # 当前为奇数,需要加1和移位 7 | add = 1 8 | res += 1 9 | return res + add -------------------------------------------------------------------------------- /src/1424-Diagonal-Traverse-II/1424.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findDiagonalOrder(self, nums: List[List[int]]) -> List[int]: 3 | res = [] 4 | for i, r in enumerate(nums): 5 | for j, a in enumerate(r): 6 | if len(res) <= i + j: 7 | res.append([]) 8 | res[i + j].append(a) 9 | return [a for r in res for a in r[::-1]] -------------------------------------------------------------------------------- /src/1437-Check-If-All-1's-Are-at-Least-Length-K-Places-Away/1437.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean kLengthApart(int[] nums, int k) { 3 | for(int pre = -k - 1, i = 0; i < nums.length; i++) { 4 | if (nums[i] == 1) { 5 | if (i - pre <= k) return false; 6 | pre = i; 7 | } 8 | } 9 | return true; 10 | } 11 | } -------------------------------------------------------------------------------- /src/0075-Sort-Colors/0075.go: -------------------------------------------------------------------------------- 1 | package sortcolors 2 | 3 | func sortColors(nums []int) { 4 | lo, mid, hi := 0, 0, len(nums)-1 5 | for mid <= hi { 6 | if nums[mid] == 2 { 7 | nums[mid], nums[hi] = nums[hi], nums[mid] 8 | hi-- 9 | } else if nums[mid] == 0 { 10 | nums[lo], nums[mid] = nums[mid], nums[lo] 11 | lo++ 12 | mid++ 13 | } else { 14 | mid++ 15 | } 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /src/0125-Valid-Palindrome/0125.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isPalindrome(self, s): 3 | """ 4 | :type s: str 5 | :rtype: bool 6 | """ 7 | s_filter = ''.join(filter(str.isalnum, s)).lower() 8 | return s_filter[::-1] == s_filter 9 | 10 | if __name__ == "__main__": 11 | s = "A man, a plan, a canal: Panama" 12 | print(Solution().isPalindrome(s)) -------------------------------------------------------------------------------- /src/0198-House-Robber/0198.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def rob(self, nums): 3 | """ 4 | :type nums: List[int] 5 | :rtype: int 6 | """ 7 | pre, cur = 0, 0 8 | for i in nums: 9 | pre, cur = cur, max(pre + i, cur) 10 | 11 | return cur 12 | 13 | if __name__ == '__main__': 14 | nums = [2,7,9,3,1] 15 | print(Solution().rob(nums)) -------------------------------------------------------------------------------- /src/0365-Water-and-Jug-Problem/0365.go: -------------------------------------------------------------------------------- 1 | func canMeasureWater(x int, y int, z int) bool { 2 | if x + y < z { 3 | return false 4 | } 5 | 6 | if x == 0 || y == 0 { 7 | return z == 0 || x + y == z 8 | } 9 | return z % gcd(x, y) == 0 10 | } 11 | 12 | func gcd(a, b int) int { 13 | if b == 0 { 14 | return a 15 | } 16 | return gcd(b, a % b) 17 | } -------------------------------------------------------------------------------- /src/0451-Sort-Characters-By-Frequency/0451.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | string frequencySort(string s) { 4 | vector v(127); 5 | for (auto i : s) v[i] += i; 6 | sort(v.begin(), v.end(), 7 | [](const auto& s1, const auto& s2) { return s1.size() > s2.size(); }); 8 | stringstream res; 9 | for (auto i : v) res << i; 10 | return res.str(); 11 | } 12 | }; -------------------------------------------------------------------------------- /src/0458-Poor-Pigs/0458.cpp: -------------------------------------------------------------------------------- 1 | static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }(); 2 | class Solution 3 | { 4 | public: 5 | int poorPigs(int buckets, int minutesToTest, int minutesToTest) 6 | { 7 | int b = minutesToTest / minutesToTest + 1; 8 | int k = 0, n = 1; 9 | while (n < buckets) ++k, n *= b; 10 | return k; 11 | } 12 | }; -------------------------------------------------------------------------------- /src/0828-Unique-Letter-String/0828.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def uniqueLetterString(self, S: str) -> int: 3 | res, mod, f = 0, 10**9 + 7, 0 4 | m, n = [0]*26, [0]*26 5 | for i, v in enumerate(S): 6 | t = ord(v) - 65 7 | f = f + 1 + i - 2 * m[t] + n[t] 8 | res = (res + f) % mod 9 | n[t], m[t] = m[t], i + 1 10 | return res -------------------------------------------------------------------------------- /src/1064-Fixed-Point/1064.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def fixedPoint(self, A: List[int]) -> int: 3 | l, r = 0, len(A)-1 4 | while l <= r: 5 | mid = l + r >> 1 6 | if A[mid] - mid == 0: 7 | return mid 8 | elif A[mid] - mid < 0: 9 | l = mid + 1 10 | else: 11 | r = mid - 1 12 | return -1 -------------------------------------------------------------------------------- /src/1100-Find-K-Length-Substrings-With-No-Repeated-Characters/1100.js: -------------------------------------------------------------------------------- 1 | var numKLenSubstrNoRepeats = function(S, K) { 2 | var res = 0, low = -1; 3 | var usedChar = {}; 4 | for (var i = 0; i < S.length; ++i) { 5 | if (usedChar[S[i]] && usedChar[S[i]] >= low) low = usedChar[S[i]]; 6 | if (i - low >= K) res++; 7 | usedChar[S[i]] = i; 8 | } 9 | return res; 10 | } -------------------------------------------------------------------------------- /src/1218-Longest-Arithmetic-Subsequence-of-Given-Difference/1218.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestSubsequence(self, arr: List[int], dif: int) -> int: 3 | mem, res = {}, 1 4 | for i in arr: 5 | if i - dif in mem: 6 | mem[i] = mem[i-dif] + 1 7 | else: 8 | mem[i] = 1 9 | res = max(res, mem[i]) 10 | return res -------------------------------------------------------------------------------- /src/1247-Minimum-Swaps-to-Make-Strings-Equal/1247.js: -------------------------------------------------------------------------------- 1 | var minimumSwap = function(s1, s2) { 2 | let x = 0, y = 0, n = s1.length; 3 | for (let i = 0; i < n; i++) { 4 | if (s1[i] != s2[i]) { 5 | if (s1[i] == 'x') x++; 6 | else y++; 7 | } 8 | } 9 | if ((x + y) & 1) return -1; 10 | return Math.floor(x / 2) + Math.floor(y / 2) + 2 * (x % 2); 11 | }; -------------------------------------------------------------------------------- /src/1317-Convert-Integer-to-the-Sum-of-Two-No-Zero-Integers/1317.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int[] getNoZeroIntegers(int n) { 3 | for (int a = 1; a < n; a++) { 4 | int b = n - a; 5 | if (!String.valueOf(a).contains("0") && !String.valueOf(b).contains("0")) 6 | return new int[]{a, b}; 7 | } 8 | return new int[]{}; 9 | } 10 | } -------------------------------------------------------------------------------- /test/0058-test.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include "../src/0058-Length-of-Last-Word/0058.cpp" 5 | 6 | TEST(test, test1) 7 | { 8 | ASSERT_EQ(5, Solution().lengthOfLastWord("Hello World")); 9 | } 10 | 11 | GTEST_API_ int main(int argc, char **argv) 12 | { 13 | testing::InitGoogleTest(&argc, argv); 14 | return RUN_ALL_TESTS(); 15 | } 16 | -------------------------------------------------------------------------------- /src/0048-Rotate-Image/0048.go: -------------------------------------------------------------------------------- 1 | package rotateimage 2 | 3 | func rotate(matrix [][]int) { 4 | n, m := len(matrix), len(matrix[0]) 5 | for i := 0; i < n; i++ { 6 | for j := i; j < m; j++ { 7 | matrix[i][j], matrix[j][i] = matrix[j][i], matrix[i][j] 8 | } 9 | for j, k := 0, m-1; j < k; j, k = j+1, k-1 { 10 | matrix[i][j], matrix[i][k] = matrix[i][k], matrix[i][j] 11 | } 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /src/0055-Jump-Game/0055.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func canJump(nums []int) bool { 4 | power := nums[0] - 1 5 | for i := 1; i < len(nums); i++ { 6 | if power < 0 { 7 | return false 8 | } 9 | new_power := nums[i] - 1 10 | power-- 11 | if new_power > power { 12 | power = new_power 13 | } 14 | } 15 | return true 16 | } 17 | -------------------------------------------------------------------------------- /src/0066-Plus-One/0066.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn plus_one(digits: Vec) -> Vec { 3 | let mut digits = digits; 4 | for i in (0..digits.len()).rev() { 5 | digits[i] = (digits[i] + 1) % 10; 6 | if digits[i] != 0 { 7 | return digits; 8 | } 9 | } 10 | digits.insert(0, 1); 11 | digits 12 | } 13 | } -------------------------------------------------------------------------------- /src/0238-Product-of-Array-Except-Self/0238.go: -------------------------------------------------------------------------------- 1 | package productofarrayexceptself 2 | 3 | func productExceptSelf(nums []int) []int { 4 | ans := make([]int, len(nums), len(nums)) 5 | ans[0] = 1 6 | for i := 1; i < len(nums); i++ { 7 | ans[i] = ans[i-1] * nums[i-1] 8 | } 9 | r := 1 10 | for i := len(nums) - 1; i >= 0; i-- { 11 | ans[i] *= r 12 | r *= nums[i] 13 | } 14 | return ans 15 | } 16 | -------------------------------------------------------------------------------- /src/0331-Verify-Preorder-Serialization-of-a-Binary-Tree/0331.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def isValidSerialization(self, preorder: str) -> bool: 3 | nodes = preorder.split(",") 4 | dif = 1 5 | for node in nodes: 6 | dif -= 1 7 | if dif < 0: 8 | return False 9 | if node != "#": 10 | dif += 2 11 | return dif == 0 -------------------------------------------------------------------------------- /src/0653-Two-Sum-IV-Input-is-a-BST/0653.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool findTarget(TreeNode* root, int k) 5 | { 6 | if (!root) return false; 7 | if (seen.count(k - root->val)) return true; 8 | seen.insert(root->val); 9 | return findTarget(root->left, k) or findTarget(root->right, k); 10 | } 11 | private: 12 | unordered_set seen; 13 | }; -------------------------------------------------------------------------------- /src/0795-Number-of-Subarrays-with-Bounded-Maximum/0795.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | int numSubarrayBoundedMax(vector &nums, int left, int right) { 4 | int n = nums.size(), res = 0, i0 = -1, i1 = -1; 5 | for (int i = 0; i < n; ++i) { 6 | if (nums[i] > right) i0 = i; 7 | if (nums[i] >= left) i1 = i; 8 | res += i1 - i0; 9 | } 10 | return res; 11 | } 12 | }; -------------------------------------------------------------------------------- /src/0946-Validate-Stack-Sequences/0946.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def validateStackSequences(self, pushed: List[int], popped: List[int]) -> bool: 3 | i = j = 0 4 | 5 | for x in pushed: 6 | pushed[i] = x 7 | i += 1 8 | while i > 0 and pushed[i - 1] == popped[j]: 9 | i, j = i - 1, j + 1 10 | 11 | return i == 0 -------------------------------------------------------------------------------- /src/1128-Number-of-Equivalent-Domino-Pairs/1128.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | int numEquivDominoPairs(vector>& dominoes) 5 | { 6 | unordered_map cnt; 7 | int res = 0; 8 | for (auto& d : dominoes) 9 | { 10 | res += cnt[min(d[0], d[1]) * 10 + max(d[0], d[1])]++; 11 | } 12 | return res; 13 | } 14 | }; -------------------------------------------------------------------------------- /src/1145-Binary-Tree-Coloring-Game/1145.js: -------------------------------------------------------------------------------- 1 | var btreeGameWinningMove = function(root, n, x) { 2 | var tl, tr; 3 | function dfs(node) { 4 | if (node == null) return 0; 5 | var l = dfs(node.left), r = dfs(node.right); 6 | if (node.val == x) tl = l, tr = r; 7 | return l + r + 1; 8 | } 9 | dfs(root); 10 | return Math.max(tl, tr, n - tl - tr - 1) > n/2; 11 | }; -------------------------------------------------------------------------------- /src/1153-String-Transforms-Into-Another-String/1153.js: -------------------------------------------------------------------------------- 1 | var canConvert = function(str1, str2) { 2 | var s2 = new Set(str2); 3 | if (s2.size == 26) return str1 == str2; 4 | var d = {}; 5 | for (var i = 0; i < str1.length; ++i) { 6 | if (d[str1[i]]) { 7 | if (d[str1[i]] != str2[i]) return false; 8 | } else d[str1[i]] = str2[i]; 9 | } 10 | return true; 11 | }; -------------------------------------------------------------------------------- /src/1262-Greatest-Sum-Divisible-by-Three/1262.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public int maxSumDivThree(int[] nums) { 3 | int[] res = new int[3]; 4 | for (int a : nums) { 5 | int[] tmp = res.clone(); 6 | for (int i : tmp) { 7 | res[(i + a)%3] = Math.max(res[(i + a)%3], i + a); 8 | } 9 | } 10 | return res[0]; 11 | } 12 | } -------------------------------------------------------------------------------- /src/1277-Count-Square-Submatrices-with-All-Ones/1277.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def countSquares(self, A: List[List[int]]) -> int: 3 | res = 0 4 | for i in range(len(A)): 5 | for j in range(len(A[0])): 6 | if i and j and A[i][j]: 7 | A[i][j] = min(A[i - 1][j], A[i][j - 1], A[i - 1][j - 1]) + 1 8 | res += A[i][j] 9 | return res -------------------------------------------------------------------------------- /src/1325-Delete-Leaves-With-a-Given-Value/1325.go: -------------------------------------------------------------------------------- 1 | func removeLeafNodes(root *TreeNode, target int) *TreeNode { 2 | if root == nil { 3 | return nil 4 | } 5 | 6 | root.Left = removeLeafNodes(root.Left, target) 7 | root.Right = removeLeafNodes(root.Right, target) 8 | if root.Val == target && root.Left == root.Right { 9 | return nil 10 | } 11 | return root 12 | } -------------------------------------------------------------------------------- /src/1356-Sort-Integers-by-The-Number-of-1-Bits/1356.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | vector sortByBits(vector& arr) 5 | { 6 | sort(arr.begin(), arr.end(),[](const int& a, const int& b){ 7 | int ca = __builtin_popcount(a), cb = __builtin_popcount(b); 8 | return ca == cb ? a < b : ca < cb; 9 | }); 10 | return arr; 11 | } 12 | }; -------------------------------------------------------------------------------- /src/1375-Bulb-Switcher-III/1375.go: -------------------------------------------------------------------------------- 1 | func numTimesAllBlue(light []int) int { 2 | res, maxv := 0, 0 3 | for i := 0; i < len(light); i++ { 4 | maxv = max(maxv, light[i]) 5 | if maxv == i + 1 { 6 | res++ 7 | } 8 | } 9 | return res 10 | 11 | } 12 | 13 | func max(a, b int) int { 14 | if a > b { 15 | return a 16 | } 17 | return b 18 | } -------------------------------------------------------------------------------- /src/1392-Longest-Happy-Prefix/1392.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def longestPrefix(self, s: str) -> str: 3 | n = len(s) 4 | f = [0] * (n + 1) 5 | f[0], i, j = -1, 0, -1 6 | 7 | while i < n: 8 | while j != -1 and s[i] != s[j]: 9 | j = f[j] 10 | i += 1 11 | j += 1 12 | f[i] = j 13 | return s[:f[-1]] -------------------------------------------------------------------------------- /src/1400-Construct-K-Palindrome-Strings/1400.java: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public boolean canConstruct(String s, int k) { 3 | int odds = 0, n = s.length(), count[] = new int[26]; 4 | for (int i = 0; i < n; ++i) { 5 | count[s.charAt(i) - 'a'] ^= 1; 6 | odds += count[s.charAt(i) - 'a'] > 0 ? 1 : -1; 7 | } 8 | return odds <= k && k <= n; 9 | } 10 | } -------------------------------------------------------------------------------- /src/1414-Find-the-Minimum-Number-of-Fibonacci-Numbers-Whose-Sum-Is-K/1414.py: -------------------------------------------------------------------------------- 1 | class Solution: 2 | def findMinFibonacciNumbers(self, k: int) -> int: 3 | res, a, b = 0, 1, 1 4 | while b <= k: 5 | a, b = b, a + b 6 | 7 | while a: 8 | if a <= k: 9 | k -= a 10 | res += 1 11 | a, b = b - a, a 12 | return res -------------------------------------------------------------------------------- /src/1437-Check-If-All-1's-Are-at-Least-Length-K-Places-Away/1437.cpp: -------------------------------------------------------------------------------- 1 | class Solution { 2 | public: 3 | bool kLengthApart(vector& nums, int k) { 4 | for(int pre = -k - 1, i = 0; i < nums.size(); i++) { 5 | if (nums[i] == 1) { 6 | if (i - pre <= k) return false; 7 | pre = i; 8 | } 9 | } 10 | return true; 11 | } 12 | }; -------------------------------------------------------------------------------- /src/0168-Excel-Sheet-Column-Title/0168.cpp: -------------------------------------------------------------------------------- 1 | static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }(); 2 | class Solution 3 | { 4 | public: 5 | string convertToTitle(int n) 6 | { 7 | string res = ""; 8 | while (n) 9 | { 10 | res = (char)('A' + --n%26) + res; 11 | n /= 26; 12 | } 13 | return res; 14 | } 15 | }; -------------------------------------------------------------------------------- /src/0678-Valid-Parenthesis-String/0678.rs: -------------------------------------------------------------------------------- 1 | impl Solution { 2 | pub fn check_valid_string(s: String) -> bool { 3 | let mut res = 1u128; 4 | for c in s.chars() { 5 | match c { 6 | '(' => res <<= 1, 7 | ')' => res >>= 1, 8 | _ => res |= (res << 1) | (res >> 1) 9 | } 10 | } 11 | res & 1 != 0 12 | } 13 | } -------------------------------------------------------------------------------- /src/0946-Validate-Stack-Sequences/0946.cpp: -------------------------------------------------------------------------------- 1 | class Solution 2 | { 3 | public: 4 | bool validateStackSequences(vector& pushed, vector& popped) 5 | { 6 | int i = 0, j = 0; 7 | for (int num : pushed) 8 | { 9 | pushed[i++] = num; 10 | while (i > 0 && pushed[i - 1] == popped[j]) i--, j++; 11 | } 12 | return i == 0; 13 | } 14 | }; -------------------------------------------------------------------------------- /src/1006-Clumsy-Factorial/1006.cpp: -------------------------------------------------------------------------------- 1 | static int x = []() {std::ios::sync_with_stdio(false); cin.tie(0); return 0; }(); 2 | class Solution 3 | { 4 | public: 5 | int clumsy(int N) 6 | { 7 | if (N <= 2) return N; 8 | if (N <= 4) return N + 3; 9 | if ((N - 4)% 4 == 0) return N + 1; 10 | else if ((N - 4) % 4 <= 2) return N + 2; 11 | else return N - 1; 12 | } 13 | }; --------------------------------------------------------------------------------